swirl/biz/user.go

194 lines
5.3 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
"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 {
2022-01-06 08:54:14 +00:00
Search(ctx context.Context, name, loginName, filter string, pageIndex, pageSize int) (users []*dao.User, total int, err error)
Create(ctx context.Context, user *dao.User, ctxUser web.User) (id string, err error)
Update(ctx context.Context, user *dao.User, ctxUser web.User) (err error)
FindByID(ctx context.Context, id string) (user *dao.User, err error)
FindByName(ctx context.Context, loginName string) (user *dao.User, err error)
FindByToken(ctx context.Context, token string) (user *dao.User, err error)
FindPrivacy(ctx context.Context, loginName string) (privacy *UserPrivacy, err error)
Count(ctx context.Context) (count int, err error)
Delete(ctx context.Context, id, name string, user web.User) (err error)
SetStatus(ctx context.Context, id string, status int32, user web.User) (err error)
ModifyPassword(ctx context.Context, oldPwd, newPwd string, user web.User) (err error)
ModifyProfile(ctx context.Context, 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
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) Search(ctx context.Context, name, loginName, filter string, pageIndex, pageSize int) (users []*dao.User, total int, err error) {
2021-12-23 11:28:31 +00:00
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
}
2022-01-06 08:54:14 +00:00
return b.d.UserSearch(ctx, args)
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) FindByID(ctx context.Context, id string) (user *dao.User, err error) {
return b.d.UserGet(ctx, id)
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) FindByName(ctx context.Context, loginName string) (user *dao.User, err error) {
return b.d.UserGetByName(ctx, loginName)
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) FindByToken(ctx context.Context, token string) (user *dao.User, err error) {
return b.d.UserGetByToken(ctx, token)
2021-12-24 09:24:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) FindPrivacy(ctx context.Context, loginName string) (privacy *UserPrivacy, err error) {
2021-12-23 11:28:31 +00:00
var u *dao.User
2022-01-06 08:54:14 +00:00
u, err = b.d.UserGetByName(ctx, loginName)
2021-12-06 12:24:22 +00:00
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
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) Create(ctx context.Context, user *dao.User, ctxUser web.User) (id string, err error) {
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
}
}
2022-01-06 08:54:14 +00:00
if err = b.d.UserCreate(ctx, user); err == nil && ctxUser != nil {
2021-12-06 12:24:22 +00:00
b.eb.CreateUser(EventActionCreate, user.LoginName, user.Name, ctxUser)
}
id = user.ID
2017-09-26 12:50:09 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) Update(ctx context.Context, user *dao.User, ctxUser web.User) (err error) {
2021-12-16 12:23:08 +00:00
user.UpdatedAt = now()
user.UpdatedBy = newOperator(ctxUser)
2022-01-06 08:54:14 +00:00
if err = b.d.UserUpdate(ctx, user); err == nil {
go func() {
2022-01-06 08:54:14 +00:00
_ = b.d.SessionUpdateDirty(ctx, user.ID, "")
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
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) SetStatus(ctx context.Context, 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
}
2022-01-06 08:54:14 +00:00
return b.d.UserUpdateStatus(ctx, u)
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
2022-01-06 08:54:14 +00:00
func (b *userBiz) Delete(ctx context.Context, id, name string, user web.User) (err error) {
err = b.d.UserDelete(ctx, id)
2021-12-06 12:24:22 +00:00
if err == nil {
b.eb.CreateUser(EventActionDelete, id, name, user)
}
return
}
2017-09-26 12:50:09 +00:00
2022-01-06 08:54:14 +00:00
func (b *userBiz) ModifyPassword(ctx context.Context, oldPwd, newPwd string, user web.User) (err error) {
2021-12-23 11:28:31 +00:00
var u *dao.User
2022-01-06 08:54:14 +00:00
u, err = b.d.UserGet(ctx, 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)
2022-01-06 08:54:14 +00:00
return b.d.UserUpdatePassword(ctx, u)
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) ModifyProfile(ctx context.Context, 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)
2022-01-06 08:54:14 +00:00
return b.d.UserUpdateProfile(ctx, u)
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *userBiz) Count(ctx context.Context) (count int, err error) {
return b.d.UserCount(ctx)
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
}