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
|
|
|
"time"
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/auxo/data/guid"
|
2017-09-26 12:50:09 +00:00
|
|
|
"github.com/cuigh/auxo/errors"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
2017-12-04 07:31:49 +00:00
|
|
|
"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"
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/jinzhu/copier"
|
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 {
|
|
|
|
Search(name, loginName, filter string, pageIndex, pageSize int) (users []*User, total int, err error)
|
|
|
|
Create(user *User, ctxUser web.User) (id string, err error)
|
|
|
|
Update(user *User, ctxUser web.User) (err error)
|
|
|
|
FindByID(id string) (user *User, err error)
|
|
|
|
FindByName(loginName string) (user *User, err error)
|
|
|
|
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)
|
|
|
|
ModifyProfile(user *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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *userBiz) Search(name, loginName, filter string, pageIndex, pageSize int) (users []*User, total int, err error) {
|
|
|
|
var (
|
|
|
|
list []*model.User
|
|
|
|
args = &model.UserSearchArgs{
|
|
|
|
Name: name,
|
|
|
|
LoginName: loginName,
|
|
|
|
Status: -1,
|
|
|
|
PageIndex: pageIndex,
|
|
|
|
PageSize: pageSize,
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
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 08:11:16 +00:00
|
|
|
list, total, err = b.d.UserSearch(context.TODO(), args)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, u := range list {
|
|
|
|
users = append(users, newUser(u))
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
func (b *userBiz) FindByID(id string) (user *User, err error) {
|
|
|
|
var u *model.User
|
2021-12-16 08:11:16 +00:00
|
|
|
u, err = b.d.UserGet(context.TODO(), id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if u != nil {
|
|
|
|
user = newUser(u)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
func (b *userBiz) FindByName(loginName string) (user *User, err error) {
|
|
|
|
var u *model.User
|
|
|
|
u, err = b.d.UserGetByName(context.TODO(), loginName)
|
|
|
|
if u != nil {
|
|
|
|
user = newUser(u)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
func (b *userBiz) FindPrivacy(loginName string) (privacy *UserPrivacy, err error) {
|
|
|
|
var u *model.User
|
|
|
|
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-06 12:24:22 +00:00
|
|
|
func (b *userBiz) Create(u *User, ctxUser web.User) (id string, err error) {
|
|
|
|
user := &model.User{
|
|
|
|
ID: createId(),
|
|
|
|
Name: u.Name,
|
|
|
|
LoginName: u.LoginName,
|
|
|
|
Email: u.Email,
|
|
|
|
Admin: u.Admin,
|
|
|
|
Type: u.Type,
|
|
|
|
Status: UserStatusActive,
|
|
|
|
Roles: u.Roles,
|
|
|
|
CreatedAt: time.Now(),
|
2021-12-16 08:11:16 +00:00
|
|
|
CreatedBy: model.Operator{ID: ctxUser.ID(), Name: ctxUser.Name()},
|
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 {
|
|
|
|
user.Password, user.Salt, err = passwd.Generate(u.Password)
|
|
|
|
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-06 12:24:22 +00:00
|
|
|
func (b *userBiz) Update(u *User, ctxUser web.User) (err error) {
|
|
|
|
user := &model.User{
|
|
|
|
ID: u.ID,
|
|
|
|
Name: u.Name,
|
|
|
|
LoginName: u.LoginName,
|
|
|
|
Email: u.Email,
|
|
|
|
Admin: u.Admin,
|
|
|
|
Type: u.Type,
|
|
|
|
Roles: u.Roles,
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
2021-12-16 08:11:16 +00:00
|
|
|
user.UpdatedBy.ID = ctxUser.ID()
|
|
|
|
user.UpdatedBy.Name = ctxUser.Name()
|
2021-12-06 12:24:22 +00:00
|
|
|
if err = b.d.UserUpdate(context.TODO(), user); err == nil {
|
|
|
|
b.eb.CreateUser(EventActionUpdate, u.LoginName, u.Name, ctxUser)
|
|
|
|
}
|
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) {
|
|
|
|
u := &model.User{
|
|
|
|
ID: id,
|
|
|
|
Status: status,
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
UpdatedBy: model.Operator{ID: user.ID(), Name: user.Name()},
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
var u *model.User
|
|
|
|
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 08:11:16 +00:00
|
|
|
u.Password, u.Salt, err = passwd.Generate(newPwd)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:11:16 +00:00
|
|
|
u.UpdatedAt = time.Now()
|
|
|
|
u.UpdatedBy.ID = user.ID()
|
|
|
|
u.UpdatedBy.Name = user.Name()
|
|
|
|
err = b.d.UserUpdatePassword(context.TODO(), u)
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:11:16 +00:00
|
|
|
func (b *userBiz) ModifyProfile(u *User, user web.User) (err error) {
|
|
|
|
return b.d.UserUpdateProfile(context.TODO(), &model.User{
|
|
|
|
ID: user.ID(),
|
|
|
|
Name: u.Name,
|
|
|
|
LoginName: u.LoginName,
|
|
|
|
Email: u.Email,
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
UpdatedBy: model.Operator{ID: user.ID(), Name: user.Name()},
|
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
|
|
|
}
|
|
|
|
|
2018-02-24 04:26:38 +00:00
|
|
|
func (b *userBiz) UpdateSession(id string) (token string, err error) {
|
|
|
|
session := &model.Session{
|
|
|
|
UserID: id,
|
2021-12-06 12:24:22 +00:00
|
|
|
Token: guid.New().String(),
|
2018-02-24 04:26:38 +00:00
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
session.Expires = session.UpdatedAt.Add(time.Hour * 24)
|
2021-12-06 12:24:22 +00:00
|
|
|
err = b.d.SessionUpdate(context.TODO(), session)
|
|
|
|
if err == nil {
|
|
|
|
token = session.Token
|
|
|
|
}
|
2017-11-13 10:16:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-24 04:26:38 +00:00
|
|
|
func (b *userBiz) GetSession(token string) (session *model.Session, err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
return b.d.SessionGet(context.TODO(), token)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2021-12-16 08:11:16 +00:00
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name" valid:"required"`
|
|
|
|
LoginName string `json:"loginName" valid:"required"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
Email string `json:"email" valid:"required"`
|
|
|
|
Admin bool `json:"admin"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Status int32 `json:"status,omitempty"`
|
|
|
|
Roles []string `json:"roles,omitempty"`
|
|
|
|
CreatedAt string `json:"createdAt,omitempty"`
|
|
|
|
UpdatedAt string `json:"updatedAt,omitempty"`
|
|
|
|
CreatedBy model.Operator `json:"createdBy"`
|
|
|
|
UpdatedBy model.Operator `json:"updatedBy"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserPrivacy struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Password string
|
|
|
|
Salt string
|
|
|
|
Type string
|
|
|
|
Status int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUser(u *model.User) *User {
|
|
|
|
user := &User{
|
|
|
|
CreatedAt: formatTime(u.CreatedAt),
|
|
|
|
UpdatedAt: formatTime(u.UpdatedAt),
|
|
|
|
}
|
|
|
|
_ = copier.CopyWithOption(user, u, copier.Option{IgnoreEmpty: true, DeepCopy: true})
|
|
|
|
return user
|
|
|
|
}
|