2017-09-26 12:50:09 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2018-02-24 04:26:38 +00:00
|
|
|
"github.com/cuigh/auxo/data"
|
2017-09-26 12:50:09 +00:00
|
|
|
"github.com/cuigh/auxo/log"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/biz"
|
|
|
|
"github.com/cuigh/swirl/model"
|
|
|
|
)
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
// UserController is a controller of user
|
2017-09-26 12:50:09 +00:00
|
|
|
type UserController struct {
|
|
|
|
Index web.HandlerFunc `path:"/" name:"user.list" authorize:"!" desc:"user list page"`
|
|
|
|
New web.HandlerFunc `path:"/new" name:"user.new" authorize:"!" desc:"new user page"`
|
|
|
|
Create web.HandlerFunc `path:"/new" method:"post" name:"user.create" authorize:"!" desc:"create user"`
|
|
|
|
Detail web.HandlerFunc `path:"/:name/detail" name:"user.detail" authorize:"!" desc:"user detail page"`
|
|
|
|
Edit web.HandlerFunc `path:"/:name/edit" name:"user.edit" authorize:"!" desc:"edit user page"`
|
|
|
|
Update web.HandlerFunc `path:"/:name/update" method:"post" name:"user.update" authorize:"!" desc:"update user"`
|
|
|
|
Block web.HandlerFunc `path:"/block" method:"post" name:"user.block" authorize:"!" desc:"block user"`
|
|
|
|
Unblock web.HandlerFunc `path:"/unblock" method:"post" name:"user.unblock" authorize:"!" desc:"unblock user"`
|
|
|
|
Delete web.HandlerFunc `path:"/delete" method:"post" name:"user.delete" authorize:"!" desc:"delete user"`
|
2017-11-24 05:03:51 +00:00
|
|
|
Search web.HandlerFunc `path:"/search" method:"post" name:"user.search" authorize:"?" desc:"search users"`
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
// User creates an instance of UserController
|
2017-09-26 12:50:09 +00:00
|
|
|
func User() (c *UserController) {
|
2017-10-11 03:26:03 +00:00
|
|
|
return &UserController{
|
|
|
|
Index: userIndex,
|
|
|
|
New: userNew,
|
|
|
|
Create: userCreate,
|
|
|
|
Detail: userDetail,
|
|
|
|
Edit: userEdit,
|
|
|
|
Update: userUpdate,
|
|
|
|
Block: userBlock,
|
|
|
|
Unblock: userUnblock,
|
|
|
|
Delete: userDelete,
|
2017-11-24 05:03:51 +00:00
|
|
|
Search: userSearch,
|
2017-10-11 03:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
func userIndex(ctx web.Context) error {
|
|
|
|
args := &model.UserListArgs{}
|
|
|
|
err := ctx.Bind(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args.PageSize = model.PageSize
|
|
|
|
if args.PageIndex == 0 {
|
|
|
|
args.PageIndex = 1
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
users, totalCount, err := biz.User.List(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
|
2017-11-08 10:36:13 +00:00
|
|
|
Set("Query", args.Query).
|
|
|
|
Set("Filter", args.Filter).
|
|
|
|
Set("Users", users)
|
2017-10-11 03:26:03 +00:00
|
|
|
return ctx.Render("system/user/list", m)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
func userNew(ctx web.Context) error {
|
|
|
|
roles, err := biz.Role.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 10:36:13 +00:00
|
|
|
m := newModel(ctx).Set("Roles", roles)
|
2017-10-11 03:26:03 +00:00
|
|
|
return ctx.Render("system/user/new", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func userCreate(ctx web.Context) error {
|
|
|
|
user := &model.User{}
|
2017-11-17 04:31:19 +00:00
|
|
|
err := ctx.Bind(user, true)
|
2017-10-11 03:26:03 +00:00
|
|
|
if err == nil {
|
|
|
|
user.Type = model.UserTypeInternal
|
|
|
|
err = biz.User.Create(user, ctx.User())
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2017-10-11 03:26:03 +00:00
|
|
|
return ajaxResult(ctx, err)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
func userDetail(ctx web.Context) error {
|
|
|
|
name := ctx.P("name")
|
|
|
|
user, err := biz.User.GetByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return web.ErrNotFound
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
var (
|
|
|
|
roles map[string]string
|
|
|
|
role *model.Role
|
|
|
|
)
|
|
|
|
if len(user.Roles) > 0 {
|
|
|
|
roles = map[string]string{}
|
|
|
|
for _, id := range user.Roles {
|
|
|
|
role, err = biz.Role.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if role != nil {
|
|
|
|
roles[id] = role.Name
|
|
|
|
} else {
|
|
|
|
log.Get("user").Warnf("Role %v is invalid", id)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 10:36:13 +00:00
|
|
|
m := newModel(ctx).Set("User", user).Set("Roles", roles)
|
2017-10-11 03:26:03 +00:00
|
|
|
return ctx.Render("system/user/detail", m)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
func userEdit(ctx web.Context) error {
|
|
|
|
name := ctx.P("name")
|
|
|
|
user, err := biz.User.GetByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2017-10-11 03:26:03 +00:00
|
|
|
if user == nil {
|
|
|
|
return web.ErrNotFound
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
roles, err := biz.Role.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
userRoles := make(map[string]struct{})
|
|
|
|
for _, id := range user.Roles {
|
2018-02-24 04:26:38 +00:00
|
|
|
userRoles[id] = data.Empty
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2017-11-08 10:36:13 +00:00
|
|
|
m := newModel(ctx).Set("User", user).Set("Roles", roles).Set("UserRoles", userRoles)
|
2017-10-11 03:26:03 +00:00
|
|
|
return ctx.Render("system/user/edit", m)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
func userUpdate(ctx web.Context) error {
|
|
|
|
user := &model.User{}
|
2017-11-20 03:33:38 +00:00
|
|
|
err := ctx.Bind(user)
|
2017-10-11 03:26:03 +00:00
|
|
|
if err == nil {
|
|
|
|
err = biz.User.Update(user, ctx.User())
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2017-10-11 03:26:03 +00:00
|
|
|
return ajaxResult(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func userBlock(ctx web.Context) error {
|
|
|
|
id := ctx.F("id")
|
|
|
|
err := biz.User.Block(id)
|
|
|
|
return ajaxResult(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func userUnblock(ctx web.Context) error {
|
|
|
|
id := ctx.F("id")
|
|
|
|
err := biz.User.Unblock(id)
|
|
|
|
return ajaxResult(ctx, err)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
func userDelete(ctx web.Context) error {
|
|
|
|
id := ctx.F("id")
|
|
|
|
err := biz.User.Delete(id)
|
|
|
|
return ajaxResult(ctx, err)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2017-11-24 05:03:51 +00:00
|
|
|
|
|
|
|
func userSearch(ctx web.Context) error {
|
|
|
|
query := ctx.F("query")
|
|
|
|
args := &model.UserListArgs{
|
|
|
|
Query: query,
|
|
|
|
PageIndex: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
}
|
|
|
|
users, _, err := biz.User.List(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
list := make([]User, len(users))
|
|
|
|
for i, user := range users {
|
|
|
|
list[i] = User{
|
|
|
|
ID: user.ID,
|
|
|
|
Name: user.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.JSON(list)
|
|
|
|
}
|