swirl/api/user.go

178 lines
4.4 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"
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"`
}
return func(ctx web.Context) (err error) {
var (
args = &SignInArgs{}
user security.Identity
2021-12-06 12:24:22 +00:00
)
if err = ctx.Bind(args); err != nil {
return err
2021-12-06 12:24:22 +00:00
}
if user, err = auth.Identify(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)
2021-12-06 12:24:22 +00:00
return success(ctx, data.Map{
"name": user.Name(),
"token": user.Token(),
"perms": user.Perms(),
2021-12-06 12:24:22 +00:00
})
}
}
func userSave(b biz.UserBiz) web.HandlerFunc {
return func(ctx web.Context) error {
2021-12-23 11:28:31 +00:00
user := &dao.User{}
2021-12-06 12:24:22 +00:00
err := ctx.Bind(user, true)
if err == nil {
if user.ID == "" {
_, err = b.Create(user, ctx.User())
} else {
err = b.Update(user, ctx.User())
}
}
return ajax(ctx, err)
}
}
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"`
}
return func(ctx web.Context) error {
args := &Args{}
err := ctx.Bind(args)
if err != nil {
return err
}
users, total, err := b.Search(args.Name, args.LoginName, args.Filter, args.PageIndex, args.PageSize)
if err != nil {
return err
}
return success(ctx, data.Map{"items": users, "total": total})
}
}
func userFind(b biz.UserBiz) web.HandlerFunc {
return func(ctx web.Context) error {
id := ctx.Query("id")
if id == "" {
id = ctx.User().ID()
}
user, err := b.FindByID(id)
if err != nil {
return err
}
return success(ctx, user)
}
}
func userDelete(b biz.UserBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Name string `json:"name"`
}
return func(ctx web.Context) error {
args := &Args{}
err := ctx.Bind(args)
if err == nil {
err = b.Delete(args.ID, args.Name, ctx.User())
}
return ajax(ctx, err)
}
}
func userSetStatus(b biz.UserBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Status int32 `json:"status"`
}
return func(ctx web.Context) error {
args := &Args{}
err := ctx.Bind(args)
if err == nil {
2021-12-16 08:11:16 +00:00
err = b.SetStatus(args.ID, args.Status, ctx.User())
2021-12-06 12:24:22 +00:00
}
return ajax(ctx, err)
}
}
func userModifyPassword(b biz.UserBiz) web.HandlerFunc {
type Args struct {
OldPassword string `json:"oldPwd"`
NewPassword string `json:"newPwd"`
}
return func(ctx web.Context) error {
args := &Args{}
err := ctx.Bind(args)
if err == nil {
2021-12-16 08:11:16 +00:00
err = b.ModifyPassword(args.OldPassword, args.NewPassword, ctx.User())
2021-12-06 12:24:22 +00:00
}
return ajax(ctx, err)
}
}
func userModifyProfile(b biz.UserBiz) web.HandlerFunc {
return func(ctx web.Context) error {
2021-12-23 11:28:31 +00:00
u := &dao.User{}
2021-12-06 12:24:22 +00:00
err := ctx.Bind(u, true)
if err == nil {
2021-12-16 08:11:16 +00:00
err = b.ModifyProfile(u, ctx.User())
2021-12-06 12:24:22 +00:00
}
return ajax(ctx, err)
}
}