swirl/biz/user.go

154 lines
3.1 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package biz
import (
"time"
"github.com/cuigh/auxo/errors"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/auxo/security/passwd"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/dao"
2017-12-01 08:04:20 +00:00
"github.com/cuigh/swirl/misc"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/model"
)
var User = &userBiz{} // nolint: golint
2017-09-26 12:50:09 +00:00
type userBiz struct {
}
func (b *userBiz) GetByID(id string) (user *model.User, err error) {
do(func(d dao.Interface) {
user, err = d.UserGetByID(id)
})
return
}
func (b *userBiz) GetByName(loginName string) (user *model.User, err error) {
do(func(d dao.Interface) {
user, err = d.UserGetByName(loginName)
})
return
}
func (b *userBiz) Create(user *model.User, ctxUser web.User) (err error) {
2017-12-01 08:04:20 +00:00
user.ID = misc.NewID()
2017-09-26 12:50:09 +00:00
user.Status = model.UserStatusActive
user.CreatedAt = time.Now()
user.UpdatedAt = user.CreatedAt
if user.Type == model.UserTypeInternal {
user.Password, user.Salt, err = passwd.Generate(user.Password)
2017-09-26 12:50:09 +00:00
if err != nil {
return
}
}
do(func(d dao.Interface) {
if err = d.UserCreate(user); err == nil && ctxUser != nil {
Event.CreateUser(model.EventActionCreate, user.LoginName, user.Name, ctxUser)
}
})
return
}
func (b *userBiz) Update(user *model.User, ctxUser web.User) (err error) {
do(func(d dao.Interface) {
user.UpdatedAt = time.Now()
if err = d.UserUpdate(user); err == nil {
Event.CreateUser(model.EventActionUpdate, user.LoginName, user.Name, ctxUser)
}
})
return
}
func (b *userBiz) Block(id string) (err error) {
do(func(d dao.Interface) {
err = d.UserBlock(id, true)
})
return
}
func (b *userBiz) Unblock(id string) (err error) {
do(func(d dao.Interface) {
err = d.UserBlock(id, false)
})
return
}
func (b *userBiz) Delete(id string) (err error) {
do(func(d dao.Interface) {
err = d.UserDelete(id)
})
return
}
func (b *userBiz) UpdateInfo(user *model.User) (err error) {
do(func(d dao.Interface) {
err = d.ProfileUpdateInfo(user)
})
return
}
2017-11-09 04:34:45 +00:00
func (b *userBiz) UpdatePassword(id, oldPwd, newPwd string) (err error) {
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
var (
user *model.User
pwd, salt string
)
user, err = d.UserGetByID(id)
if err != nil {
return
}
if !passwd.Validate(oldPwd, user.Password, user.Salt) {
2017-09-26 12:50:09 +00:00
err = errors.New("Current password is incorrect")
return
}
pwd, salt, err = passwd.Generate(newPwd)
2017-09-26 12:50:09 +00:00
if err != nil {
return
}
err = d.ProfileUpdatePassword(id, pwd, salt)
})
return
}
func (b *userBiz) List(args *model.UserListArgs) (users []*model.User, count int, err error) {
do(func(d dao.Interface) {
users, count, err = d.UserList(args)
})
return
}
func (b *userBiz) Count() (count int, err error) {
do(func(d dao.Interface) {
count, err = d.UserCount()
})
return
}
func (b *userBiz) UpdateSession(id string) (token string, err error) {
session := &model.Session{
UserID: id,
Token: misc.NewID(),
UpdatedAt: time.Now(),
}
session.Expires = session.UpdatedAt.Add(time.Hour * 24)
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
err = d.SessionUpdate(session)
2017-11-13 10:16:16 +00:00
if err == nil {
token = session.Token
2017-11-13 10:16:16 +00:00
}
})
2017-11-13 10:16:16 +00:00
return
}
func (b *userBiz) GetSession(token string) (session *model.Session, err error) {
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
session, err = d.SessionGet(token)
2017-09-26 12:50:09 +00:00
})
return
}