2022-11-24 23:37:34 +08:00
|
|
|
|
// Package admin
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2023-02-23 17:53:04 +08:00
|
|
|
|
"hotgo/api/admin/role"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/consts"
|
|
|
|
|
"hotgo/internal/dao"
|
|
|
|
|
"hotgo/internal/library/casbin"
|
|
|
|
|
"hotgo/internal/library/contexts"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"hotgo/internal/library/hgorm"
|
|
|
|
|
"hotgo/internal/model/entity"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/model/input/adminin"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"hotgo/internal/model/input/form"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/service"
|
|
|
|
|
"hotgo/utility/auth"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"hotgo/utility/convert"
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"hotgo/utility/tree"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"sort"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type sAdminRole struct{}
|
|
|
|
|
|
|
|
|
|
func NewAdminRole() *sAdminRole {
|
|
|
|
|
return &sAdminRole{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
service.RegisterAdminRole(NewAdminRole())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify 验证权限
|
|
|
|
|
func (s *sAdminRole) Verify(ctx context.Context, path, method string) bool {
|
|
|
|
|
if auth.IsExceptAuth(ctx, path) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
var (
|
2023-01-18 16:23:39 +08:00
|
|
|
|
user = contexts.Get(ctx).User
|
|
|
|
|
superRoleKey = g.Cfg().MustGet(ctx, "hotgo.admin.superRoleKey")
|
|
|
|
|
err error
|
2022-11-24 23:37:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if user == nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Info(ctx, "admin Verify user = nil")
|
2022-11-24 23:37:34 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if service.AdminMember().VerifySuperId(ctx, user.Id) || user.RoleKey == superRoleKey.String() {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
ok, err := casbin.Enforcer.Enforce(user.RoleKey, path, method)
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Infof(ctx, "admin Verify Enforce err:%+v", err)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List 获取列表
|
2023-02-26 14:18:22 +08:00
|
|
|
|
func (s *sAdminRole) List(ctx context.Context, in adminin.RoleListInp) (res *adminin.RoleListModel, totalCount int, err error) {
|
2023-01-25 11:49:21 +08:00
|
|
|
|
var (
|
|
|
|
|
mod = dao.AdminRole.Ctx(ctx)
|
2023-02-26 14:18:22 +08:00
|
|
|
|
models []*entity.AdminRole
|
2023-05-10 23:54:50 +08:00
|
|
|
|
pid int64 = 0
|
2023-01-25 11:49:21 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// 非超管只获取下级角色
|
|
|
|
|
if !service.AdminMember().VerifySuperId(ctx, contexts.GetUserId(ctx)) {
|
|
|
|
|
pid = contexts.GetRoleId(ctx)
|
|
|
|
|
mod = mod.WhereLike(dao.AdminRole.Columns().Tree, "%"+tree.GetIdLabel(pid)+"%")
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
totalCount, err = mod.Count()
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
if err = mod.Page(in.Page, in.PerPage).Order("sort asc,id asc").Scan(&models); err != nil {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2023-02-08 20:29:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
res = new(adminin.RoleListModel)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
res.List = s.treeList(pid, models)
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetName 获取指定角色的名称
|
|
|
|
|
func (s *sAdminRole) GetName(ctx context.Context, RoleId int64) (name string, err error) {
|
|
|
|
|
roleName, err := dao.AdminRole.Ctx(ctx).
|
|
|
|
|
Fields("name").
|
|
|
|
|
Where("id", RoleId).
|
|
|
|
|
Order("id desc").
|
|
|
|
|
Value()
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
|
|
|
|
return name, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return roleName.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
// GetMemberList 获取指定用户的岗位列表
|
2022-11-24 23:37:34 +08:00
|
|
|
|
func (s *sAdminRole) GetMemberList(ctx context.Context, RoleId int64) (list []*adminin.RoleListModel, err error) {
|
|
|
|
|
err = dao.AdminRole.Ctx(ctx).
|
|
|
|
|
Where("id", RoleId).
|
|
|
|
|
Order("id desc").
|
|
|
|
|
Scan(&list)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
|
|
|
|
return list, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPermissions 更改角色菜单权限
|
|
|
|
|
func (s *sAdminRole) GetPermissions(ctx context.Context, reqInfo *role.GetPermissionsReq) (MenuIds []int64, err error) {
|
|
|
|
|
values, err := dao.AdminRoleMenu.Ctx(ctx).
|
|
|
|
|
Fields("menu_id").
|
|
|
|
|
Where("role_id", reqInfo.RoleId).
|
|
|
|
|
Array()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-02-26 14:18:22 +08:00
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
if len(values) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(values); i++ {
|
|
|
|
|
MenuIds = append(MenuIds, values[i].Int64())
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdatePermissions 更改角色菜单权限
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAdminRole) UpdatePermissions(ctx context.Context, reqInfo *role.UpdatePermissionsReq) (err error) {
|
|
|
|
|
err = dao.AdminRoleMenu.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
_, err = dao.AdminRoleMenu.Ctx(ctx).Where("role_id", reqInfo.RoleId).Delete()
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(reqInfo.MenuIds) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-02-26 14:18:22 +08:00
|
|
|
|
|
|
|
|
|
// 去重
|
|
|
|
|
reqInfo.MenuIds = convert.UniqueSliceInt64(reqInfo.MenuIds)
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
addMap := make(g.List, 0, len(reqInfo.MenuIds))
|
|
|
|
|
for _, v := range reqInfo.MenuIds {
|
|
|
|
|
addMap = append(addMap, g.Map{
|
|
|
|
|
"role_id": reqInfo.RoleId,
|
|
|
|
|
"menu_id": v,
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-02-26 14:18:22 +08:00
|
|
|
|
|
|
|
|
|
if _, err = dao.AdminRoleMenu.Ctx(ctx).Data(addMap).Insert(); err != nil {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
})
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return casbin.Refresh(ctx)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *sAdminRole) Edit(ctx context.Context, in *role.EditReq) (err error) {
|
|
|
|
|
if in.Name == "" {
|
|
|
|
|
err = gerror.New("名称不能为空")
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
2023-02-26 14:18:22 +08:00
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
if in.Key == "" {
|
|
|
|
|
err = gerror.New("编码不能为空")
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uniqueName, err := dao.AdminRole.IsUniqueName(ctx, in.Id, in.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
if !uniqueName {
|
|
|
|
|
err = gerror.New("名称已存在")
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uniqueCode, err := dao.AdminRole.IsUniqueCode(ctx, in.Id, in.Key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
if !uniqueCode {
|
|
|
|
|
err = gerror.New("编码已存在")
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
if in.Pid, in.Level, in.Tree, err = hgorm.GenSubTree(ctx, dao.AdminRole, in.Pid); err != nil {
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 修改
|
|
|
|
|
if in.Id > 0 {
|
|
|
|
|
_, err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 新增
|
|
|
|
|
_, err = dao.AdminRole.Ctx(ctx).Data(in).Insert()
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *sAdminRole) Delete(ctx context.Context, in *role.DeleteReq) (err error) {
|
|
|
|
|
if in.Id <= 0 {
|
|
|
|
|
return gerror.New("ID不正确!")
|
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
var models *entity.AdminRole
|
|
|
|
|
if err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Scan(&models); err != nil {
|
|
|
|
|
return
|
2023-01-25 11:49:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if models == nil {
|
|
|
|
|
return gerror.New("数据不存在或已删除!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pidExist, err := dao.AdminRole.Ctx(ctx).Where("pid", models.Id).One()
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrap(err, consts.ErrorORM)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !pidExist.IsEmpty() {
|
|
|
|
|
return gerror.New("请先删除该角色下得所有子级!")
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
_, err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Delete()
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
|
|
|
|
func (s *sAdminRole) DataScopeSelect(ctx context.Context) (res form.Selects) {
|
|
|
|
|
for k, v := range consts.RoleDataNameMap {
|
|
|
|
|
res = append(res, &form.Select{
|
|
|
|
|
Value: k,
|
|
|
|
|
Name: v,
|
|
|
|
|
Label: v,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
sort.Sort(res)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *sAdminRole) DataScopeEdit(ctx context.Context, in *adminin.DataScopeEditInp) (err error) {
|
|
|
|
|
if in.Id <= 0 {
|
|
|
|
|
return gerror.New("角色ID不正确!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
models *entity.AdminRole
|
|
|
|
|
superRoleKey = g.Cfg().MustGet(ctx, "hotgo.admin.superRoleKey")
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
if err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Scan(&models); err != nil {
|
2023-01-18 16:23:39 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if models == nil {
|
|
|
|
|
return gerror.New("角色不存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if models.Key == superRoleKey.String() {
|
|
|
|
|
return gerror.New("超管角色拥有全部权限,无需修改!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if in.DataScope == consts.RoleDataDeptCustom && len(convert.UniqueSliceInt64(in.CustomDept)) == 0 {
|
|
|
|
|
return gerror.New("自定义权限必须配置自定义部门!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
models.DataScope = in.DataScope
|
|
|
|
|
models.CustomDept = gjson.New(convert.UniqueSliceInt64(in.CustomDept))
|
|
|
|
|
|
|
|
|
|
_, err = dao.AdminRole.Ctx(ctx).
|
|
|
|
|
Fields(dao.AdminRole.Columns().DataScope, dao.AdminRole.Columns().CustomDept).
|
|
|
|
|
Where("id", in.Id).
|
|
|
|
|
Data(models).
|
|
|
|
|
Update()
|
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// treeList 树状列表
|
|
|
|
|
func (s *sAdminRole) treeList(pid int64, nodes []*entity.AdminRole) (list []*adminin.RoleTree) {
|
|
|
|
|
list = make([]*adminin.RoleTree, 0)
|
|
|
|
|
for _, v := range nodes {
|
|
|
|
|
if v.Pid == pid {
|
|
|
|
|
item := new(adminin.RoleTree)
|
|
|
|
|
item.AdminRole = *v
|
|
|
|
|
item.Label = v.Name
|
|
|
|
|
item.Value = v.Id
|
|
|
|
|
|
|
|
|
|
child := s.treeList(v.Id, nodes)
|
|
|
|
|
if len(child) > 0 {
|
|
|
|
|
item.Children = child
|
|
|
|
|
}
|
|
|
|
|
list = append(list, item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|