swirl/api/user.go

203 lines
4.9 KiB
Go
Raw Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2022-01-06 08:54:14 +00:00
"github.com/cuigh/swirl/misc"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/swirl/security"
)
// UserHandler encapsulates user related handlers.
type UserHandler struct {
SignIn web.HandlerFunc `path:"/sign-in" method:"post" auth:"*" desc:"user sign in"`
Search web.HandlerFunc `path:"/search" auth:"user.view" desc:"search users"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"user.edit" desc:"create or update user"`
Find web.HandlerFunc `path:"/find" auth:"user.view" desc:"find user by id"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"user.delete" desc:"delete user"`
SetStatus web.HandlerFunc `path:"/set-status" method:"post" auth:"user.edit" desc:"set user status"`
ModifyPassword web.HandlerFunc `path:"/modify-password" method:"post" auth:"?" desc:"modify password"`
ModifyProfile web.HandlerFunc `path:"/modify-profile" method:"post" auth:"?" desc:"modify profile"`
}
// NewUser creates an instance of UserHandler
func NewUser(b biz.UserBiz, eb biz.EventBiz, auth *security.Identifier) *UserHandler {
2021-12-06 12:24:22 +00:00
return &UserHandler{
SignIn: userSignIn(auth, eb),
Search: userSearch(b),
Save: userSave(b),
Find: userFind(b),
Delete: userDelete(b),
SetStatus: userSetStatus(b),
ModifyPassword: userModifyPassword(b),
ModifyProfile: userModifyProfile(b),
}
}
func userSignIn(auth *security.Identifier, eb biz.EventBiz) web.HandlerFunc {
2021-12-06 12:24:22 +00:00
type SignInArgs struct {
Name string `json:"name"`
Password string `json:"password"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
var (
args = &SignInArgs{}
user security.Identity
2021-12-06 12:24:22 +00:00
)
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err != nil {
return err
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
if user, err = auth.Identify(ctx, args.Name, args.Password); err != nil {
2021-12-06 12:24:22 +00:00
return err
}
eb.CreateUser(biz.EventActionLogin, user.ID(), user.Name(), user)
2022-01-06 08:54:14 +00:00
return success(c, data.Map{
2021-12-06 12:24:22 +00:00
"name": user.Name(),
"token": user.Token(),
"perms": user.Perms(),
2021-12-06 12:24:22 +00:00
})
}
}
func userSave(b biz.UserBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-23 11:28:31 +00:00
user := &dao.User{}
2022-01-06 08:54:14 +00:00
err := c.Bind(user, true)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
2021-12-06 12:24:22 +00:00
if user.ID == "" {
2022-01-06 08:54:14 +00:00
_, err = b.Create(ctx, user, c.User())
2021-12-06 12:24:22 +00:00
} else {
2022-01-06 08:54:14 +00:00
err = b.Update(ctx, user, c.User())
2021-12-06 12:24:22 +00:00
}
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}
func userSearch(b biz.UserBiz) web.HandlerFunc {
type Args struct {
Filter string `bind:"filter"` // admins, active, blocked
Name string `bind:"name"`
LoginName string `bind:"loginName"`
PageIndex int `bind:"pageIndex"`
PageSize int `bind:"pageSize"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-06 12:24:22 +00:00
args := &Args{}
2022-01-06 08:54:14 +00:00
err := c.Bind(args)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
users, total, err := b.Search(ctx, args.Name, args.LoginName, args.Filter, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{"items": users, "total": total})
2021-12-06 12:24:22 +00:00
}
}
func userFind(b biz.UserBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
id := c.Query("id")
2021-12-06 12:24:22 +00:00
if id == "" {
2022-01-06 08:54:14 +00:00
id = c.User().ID()
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
user, err := b.FindByID(ctx, id)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, user)
2021-12-06 12:24:22 +00:00
}
}
func userDelete(b biz.UserBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Name string `json:"name"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-06 12:24:22 +00:00
args := &Args{}
2022-01-06 08:54:14 +00:00
err := c.Bind(args)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
err = b.Delete(ctx, args.ID, args.Name, c.User())
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}
func userSetStatus(b biz.UserBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Status int32 `json:"status"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-06 12:24:22 +00:00
args := &Args{}
2022-01-06 08:54:14 +00:00
err := c.Bind(args)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
err = b.SetStatus(ctx, args.ID, args.Status, c.User())
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}
func userModifyPassword(b biz.UserBiz) web.HandlerFunc {
type Args struct {
OldPassword string `json:"oldPwd"`
NewPassword string `json:"newPwd"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-06 12:24:22 +00:00
args := &Args{}
2022-01-06 08:54:14 +00:00
err := c.Bind(args)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
err = b.ModifyPassword(ctx, args.OldPassword, args.NewPassword, c.User())
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}
func userModifyProfile(b biz.UserBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-23 11:28:31 +00:00
u := &dao.User{}
2022-01-06 08:54:14 +00:00
err := c.Bind(u, true)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
err = b.ModifyProfile(ctx, u, c.User())
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}