swirl/biz/user.go

193 lines
5.0 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package biz
import (
2021-12-06 12:24:22 +00:00
"context"
2017-09-26 12:50:09 +00:00
2021-12-24 09:24:09 +00:00
"github.com/cuigh/auxo/data"
2017-09-26 12:50:09 +00:00
"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
)
2021-12-06 12:24:22 +00:00
const (
// UserTypeInternal is internal user of swirl
UserTypeInternal = "internal"
// UserTypeLDAP is external user of LDAP
UserTypeLDAP = "ldap"
)
2017-09-26 12:50:09 +00:00
2021-12-06 12:24:22 +00:00
const (
// UserStatusBlocked is the status which user is blocked
UserStatusBlocked = 0
// UserStatusActive is the normal status
UserStatusActive = 1
)
type UserBiz interface {
2021-12-23 11:28:31 +00:00
Search(name, loginName, filter string, pageIndex, pageSize int) (users []*dao.User, total int, err error)
Create(user *dao.User, ctxUser web.User) (id string, err error)
Update(user *dao.User, ctxUser web.User) (err error)
FindByID(id string) (user *dao.User, err error)
FindByName(loginName string) (user *dao.User, err error)
2021-12-24 09:24:09 +00:00
FindByToken(token string) (user *dao.User, err error)
2021-12-06 12:24:22 +00:00
FindPrivacy(loginName string) (privacy *UserPrivacy, err error)
Count() (count int, err error)
Delete(id, name string, user web.User) (err error)
2021-12-16 08:11:16 +00:00
SetStatus(id string, status int32, user web.User) (err error)
ModifyPassword(oldPwd, newPwd string, user web.User) (err error)
2021-12-23 11:28:31 +00:00
ModifyProfile(user *dao.User, ctxUser web.User) (err error)
2021-12-06 12:24:22 +00:00
}
func NewUser(d dao.Interface, eb EventBiz) UserBiz {
return &userBiz{d: d, eb: eb}
}
type userBiz struct {
d dao.Interface
eb EventBiz
}
2021-12-23 11:28:31 +00:00
func (b *userBiz) Search(name, loginName, filter string, pageIndex, pageSize int) (users []*dao.User, total int, err error) {
var args = &dao.UserSearchArgs{
2021-12-16 12:23:08 +00:00
Name: name,
LoginName: loginName,
Status: -1,
PageIndex: pageIndex,
PageSize: pageSize,
}
2021-12-06 12:24:22 +00:00
switch filter {
case "admins":
args.Admin = true
case "active":
args.Status = UserStatusActive
case "blocked":
args.Status = UserStatusBlocked
2017-09-26 12:50:09 +00:00
}
2021-12-16 12:23:08 +00:00
return b.d.UserSearch(context.TODO(), args)
2017-09-26 12:50:09 +00:00
}
2021-12-23 11:28:31 +00:00
func (b *userBiz) FindByID(id string) (user *dao.User, err error) {
2021-12-16 12:23:08 +00:00
return b.d.UserGet(context.TODO(), id)
2017-09-26 12:50:09 +00:00
}
2021-12-23 11:28:31 +00:00
func (b *userBiz) FindByName(loginName string) (user *dao.User, err error) {
2021-12-16 12:23:08 +00:00
return b.d.UserGetByName(context.TODO(), loginName)
2017-09-26 12:50:09 +00:00
}
2021-12-24 09:24:09 +00:00
func (b *userBiz) FindByToken(token string) (user *dao.User, err error) {
return b.d.UserGetByToken(context.TODO(), token)
}
2021-12-06 12:24:22 +00:00
func (b *userBiz) FindPrivacy(loginName string) (privacy *UserPrivacy, err error) {
2021-12-23 11:28:31 +00:00
var u *dao.User
2021-12-06 12:24:22 +00:00
u, err = b.d.UserGetByName(context.TODO(), loginName)
if u != nil {
privacy = &UserPrivacy{
ID: u.ID,
Name: u.Name,
Password: u.Password,
Salt: u.Salt,
Type: u.Type,
Status: u.Status,
}
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (b *userBiz) Create(user *dao.User, ctxUser web.User) (id string, err error) {
2021-12-24 09:24:09 +00:00
user.Tokens = data.Options{data.Option{Name: "test", Value: "abc123"}}
2021-12-16 12:23:08 +00:00
user.ID = createId()
user.Status = UserStatusActive
user.CreatedAt = now()
if ctxUser != nil {
user.CreatedBy = newOperator(ctxUser)
2021-12-06 12:24:22 +00:00
}
user.UpdatedAt = user.CreatedAt
2021-12-16 08:11:16 +00:00
user.UpdatedBy = user.CreatedBy
2021-12-06 12:24:22 +00:00
if user.Type == UserTypeInternal {
2021-12-16 12:23:08 +00:00
user.Password, user.Salt, err = passwd.Generate(user.Password)
2021-12-06 12:24:22 +00:00
if err != nil {
return
}
}
if err = b.d.UserCreate(context.TODO(), user); err == nil && ctxUser != nil {
b.eb.CreateUser(EventActionCreate, user.LoginName, user.Name, ctxUser)
}
id = user.ID
2017-09-26 12:50:09 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (b *userBiz) Update(user *dao.User, ctxUser web.User) (err error) {
2021-12-16 12:23:08 +00:00
user.UpdatedAt = now()
user.UpdatedBy = newOperator(ctxUser)
2021-12-06 12:24:22 +00:00
if err = b.d.UserUpdate(context.TODO(), user); err == nil {
2021-12-16 12:23:08 +00:00
b.eb.CreateUser(EventActionUpdate, user.LoginName, user.Name, ctxUser)
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-16 08:11:16 +00:00
func (b *userBiz) SetStatus(id string, status int32, user web.User) (err error) {
2021-12-23 11:28:31 +00:00
u := &dao.User{
2021-12-16 08:11:16 +00:00
ID: id,
Status: status,
2021-12-16 12:23:08 +00:00
UpdatedAt: now(),
UpdatedBy: newOperator(user),
2021-12-16 08:11:16 +00:00
}
return b.d.UserUpdateStatus(context.TODO(), u)
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
2021-12-06 12:24:22 +00:00
func (b *userBiz) Delete(id, name string, user web.User) (err error) {
err = b.d.UserDelete(context.TODO(), id)
if err == nil {
b.eb.CreateUser(EventActionDelete, id, name, user)
}
return
}
2017-09-26 12:50:09 +00:00
2021-12-16 08:11:16 +00:00
func (b *userBiz) ModifyPassword(oldPwd, newPwd string, user web.User) (err error) {
2021-12-23 11:28:31 +00:00
var u *dao.User
2021-12-16 08:11:16 +00:00
u, err = b.d.UserGet(context.TODO(), user.ID())
2021-12-06 12:24:22 +00:00
if err != nil {
return err
2021-12-16 08:11:16 +00:00
} else if u == nil {
return errors.Format("user not found: %s", user.ID())
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
2021-12-16 08:11:16 +00:00
if !passwd.Validate(oldPwd, u.Password, u.Salt) {
2021-12-06 12:24:22 +00:00
return errors.Coded(misc.ErrOldPasswordIncorrect, "current password is incorrect")
}
2021-12-16 12:23:08 +00:00
if u.Password, u.Salt, err = passwd.Generate(newPwd); err != nil {
2021-12-06 12:24:22 +00:00
return
}
2021-12-16 12:23:08 +00:00
u.UpdatedAt = now()
u.UpdatedBy = newOperator(user)
return b.d.UserUpdatePassword(context.TODO(), u)
2017-09-26 12:50:09 +00:00
}
2021-12-23 11:28:31 +00:00
func (b *userBiz) ModifyProfile(u *dao.User, user web.User) (err error) {
2021-12-16 12:23:08 +00:00
u.ID = user.ID()
u.UpdatedAt = now()
u.UpdatedBy = newOperator(user)
return b.d.UserUpdateProfile(context.TODO(), u)
2017-09-26 12:50:09 +00:00
}
func (b *userBiz) Count() (count int, err error) {
2021-12-06 12:24:22 +00:00
return b.d.UserCount(context.TODO())
2017-09-26 12:50:09 +00:00
}
2021-12-16 08:11:16 +00:00
type UserPrivacy struct {
ID string
Name string
2021-12-16 12:23:08 +00:00
Password string `json:"-"`
Salt string `json:"-"`
2021-12-16 08:11:16 +00:00
Type string
Status int32
}