2023-08-30 09:28:21 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/db/dbquery"
|
|
|
|
"joylink.club/bj-rtsts-server/db/model"
|
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-10-23 13:41:58 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-08-30 09:28:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 查询权限角色信息列表
|
2024-02-23 09:22:07 +08:00
|
|
|
func PageAuthRoleQuery(query *dto.PageQueryDto) *dto.PageDto[*dto.AuthRoleRspDto] {
|
2023-08-30 09:28:21 +08:00
|
|
|
d := dbquery.AuthRole
|
|
|
|
records, total, err := d.Debug().Select(d.ID, d.Name).Order(d.CreateTime).FindByPage(query.Offset(), query.Size)
|
|
|
|
if err != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("查询权限数据库出错,请联系维护人员", err))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2024-02-23 09:22:07 +08:00
|
|
|
return &dto.PageDto[*dto.AuthRoleRspDto]{Total: int(total), PageQueryDto: *query, Records: dto.ConvertFromAuthRole(records)}
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 获取角色列表
|
|
|
|
func ListAuthRoleQuery() []*dto.AuthRoleRspDto {
|
|
|
|
d := dbquery.AuthRole
|
|
|
|
records, err := d.Debug().Select(d.ID, d.Name).Order(d.CreateTime).Find()
|
|
|
|
if err != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("查询权限数据库出错,请联系维护人员", err))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
return dto.ConvertFromAuthRole(records)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建权限角色
|
|
|
|
func CreateAuthRole(a *dto.AuthRoleReqDto) bool {
|
2023-08-31 16:16:18 +08:00
|
|
|
d := model.AuthRole{Name: a.Name, CreateTime: time.Now()}
|
2023-08-30 09:28:21 +08:00
|
|
|
aq := dbquery.AuthRole
|
|
|
|
err := aq.Save(&d)
|
|
|
|
if err != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("保存权限数据库出错,请联系维护人员", err))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
n := len(a.AddPaths)
|
2023-08-31 16:16:18 +08:00
|
|
|
if n == 0 {
|
2023-10-23 13:41:58 +08:00
|
|
|
return true
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
// 查询刚插入的角色
|
|
|
|
newAuthRole, _ := aq.Where(aq.Name.Eq(a.Name)).Order(aq.CreateTime).Last()
|
|
|
|
rolePaths := make([]*model.AuthRoleAPIPath, n)
|
|
|
|
for i, v := range a.AddPaths {
|
|
|
|
rolePaths[i] = &model.AuthRoleAPIPath{Rid: newAuthRole.ID, Pid: v}
|
|
|
|
}
|
|
|
|
dbquery.AuthRoleAPIPath.Save(rolePaths...)
|
|
|
|
return true
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询角色详情
|
|
|
|
func QueryAuthRole(rid int32) *dto.AuthRoleDetailRspDto {
|
|
|
|
// 查询用户角色信息
|
|
|
|
role, err := dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).First()
|
|
|
|
if err != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("查询角色数据库出错,请联系维护人员", err))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
rsp := &dto.AuthRoleDetailRspDto{
|
|
|
|
Id: role.ID,
|
|
|
|
Name: role.Name,
|
|
|
|
Paths: QueryAuthApiPathByRids([]int32{rid}),
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询用户的角色
|
|
|
|
func QueryAuthRoleByUid(uid int32) []*model.AuthRole {
|
|
|
|
aru := dbquery.AuthRoleUser
|
|
|
|
arus, err1 := aru.Distinct(aru.Rid).Select(aru.Rid).Where(aru.UID.Eq(uid)).Find()
|
|
|
|
if err1 != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("查询角色数据库出错,请联系维护人员", err1))
|
2023-08-31 16:16:18 +08:00
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
if len(arus) == 0 {
|
|
|
|
return nil
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
rids := make([]int32, len(arus))
|
|
|
|
for i, v := range arus {
|
|
|
|
rids[i] = v.Rid
|
2023-08-31 16:16:18 +08:00
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
roles, err2 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.In(rids...)).Find()
|
|
|
|
if err2 != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("查询角色数据库发生错误,请联系维护人员", err2))
|
2023-09-01 17:26:04 +08:00
|
|
|
}
|
|
|
|
return roles
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 编辑角色信息
|
|
|
|
func UpdateAuthRole(rid int32, info *dto.AuthRoleReqDto) bool {
|
|
|
|
// 查询用户角色信息
|
|
|
|
role, err := dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).First()
|
|
|
|
if err != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("更新角色数据库发生错误,不存在此用户", err))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
role.Name = info.Name // 更新名称
|
2023-08-30 09:28:21 +08:00
|
|
|
dbquery.AuthRole.Updates(role)
|
|
|
|
dqarap := dbquery.AuthRoleAPIPath
|
2023-08-31 16:16:18 +08:00
|
|
|
// 删除关联
|
2023-08-30 09:28:21 +08:00
|
|
|
rn := len(info.DelPaths)
|
|
|
|
if rn > 0 {
|
|
|
|
dqarap.Where(dqarap.Rid.Eq(rid), dqarap.Pid.In(info.DelPaths...)).Delete()
|
|
|
|
}
|
|
|
|
// 增加关联
|
|
|
|
an := len(info.AddPaths)
|
2023-08-31 16:16:18 +08:00
|
|
|
if an == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
rolePaths := make([]*model.AuthRoleAPIPath, an)
|
|
|
|
for i, v := range info.AddPaths {
|
|
|
|
rolePaths[i] = &model.AuthRoleAPIPath{Rid: rid, Pid: v}
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
dqarap.Save(rolePaths...)
|
2023-08-30 09:28:21 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除权限角色
|
|
|
|
func DeleteAuthRole(rid int32) bool {
|
|
|
|
oldD, err1 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).First()
|
|
|
|
if err1 != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("删除权限角色数据库发生错误", err1))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
if dto.IsSystemRole(oldD.ID) {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("系统角色不可删除", err1))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
// 如果有用户关联则不删除
|
|
|
|
count, err2 := dbquery.AuthRoleUser.Where(dbquery.AuthRoleUser.Rid.Eq(rid)).Count()
|
|
|
|
if err2 != nil {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("删除失败,还存在用户关联", err2))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
if count > 0 {
|
2023-10-23 13:41:58 +08:00
|
|
|
panic(sys_error.New("删除失败,有用户关联该角色"))
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
// 删除用户关联关系
|
|
|
|
dbquery.AuthRoleUser.Where(dbquery.AuthRoleUser.Rid.Eq(rid)).Delete()
|
|
|
|
// 删除路径关联关系
|
|
|
|
dbquery.AuthRoleAPIPath.Where(dbquery.AuthRoleAPIPath.Rid.Eq(rid)).Delete()
|
|
|
|
// 删除角色
|
|
|
|
dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).Delete()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询接口路径信息列表
|
2024-02-23 09:22:07 +08:00
|
|
|
func PageAuthApiPathQuery(query *dto.AuthApiPathPageReqDto) *dto.PageDto[*model.AuthAPIPath] {
|
2023-08-30 09:28:21 +08:00
|
|
|
d := dbquery.AuthAPIPath
|
|
|
|
dq := d.Where()
|
|
|
|
if query.Name != "" {
|
|
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
|
|
}
|
|
|
|
records, total, err := dq.Debug().FindByPage(query.Offset(), query.Size)
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
}
|
2024-02-23 09:22:07 +08:00
|
|
|
return &dto.PageDto[*model.AuthAPIPath]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询接口路径信息列表
|
|
|
|
func ListAuthApiPathQuery() []*model.AuthAPIPath {
|
|
|
|
records, err := dbquery.AuthAPIPath.Find()
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return records
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建接口路径信息
|
|
|
|
func CreateAuthApiPath(ap *dto.AuthApiPathReqDto) bool {
|
|
|
|
d := model.AuthAPIPath{Name: ap.Name, Path: ap.Path, Method: ap.Method}
|
|
|
|
err := dbquery.AuthAPIPath.Save(&d)
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询接口路径信息
|
|
|
|
func QueryAuthApiPath(id int32) *model.AuthAPIPath {
|
|
|
|
data, err := dbquery.AuthAPIPath.Where(dbquery.AuthAPIPath.ID.Eq(id)).Debug().First()
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2023-09-01 17:26:04 +08:00
|
|
|
// 根据角色ID查询路径信息
|
|
|
|
func QueryAuthApiPathByRids(rids []int32) []*model.AuthAPIPath {
|
2023-09-01 17:55:47 +08:00
|
|
|
rn := len(rids)
|
|
|
|
if rn == 0 {
|
2023-09-01 17:26:04 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-09-01 17:55:47 +08:00
|
|
|
// 判断是否是超级管理
|
|
|
|
isAdmin := false
|
|
|
|
for i := 0; i < rn && !isAdmin; i++ {
|
|
|
|
isAdmin = rids[i] == int32(dto.ADMIN)
|
|
|
|
}
|
|
|
|
if isAdmin { // 超级管理员查询所有路径
|
|
|
|
apiPaths, _ := dbquery.AuthAPIPath.Find()
|
|
|
|
return apiPaths
|
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
linkPids, err2 := dbquery.AuthRoleAPIPath.Distinct(dbquery.AuthRoleAPIPath.Pid).Where(dbquery.AuthRoleAPIPath.Rid.In(rids...)).Find()
|
|
|
|
if err2 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()})
|
|
|
|
}
|
|
|
|
pn := len(linkPids)
|
|
|
|
if pn == 0 { // 无关联路径
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pids := make([]int32, pn)
|
|
|
|
for i, r := range linkPids {
|
|
|
|
pids[i] = r.Pid
|
|
|
|
}
|
|
|
|
apiPaths, err4 := dbquery.AuthAPIPath.Where(dbquery.AuthAPIPath.ID.In(pids...)).Find()
|
|
|
|
if err4 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err4.Error()})
|
|
|
|
}
|
|
|
|
return apiPaths
|
|
|
|
}
|
|
|
|
|
2023-08-30 09:28:21 +08:00
|
|
|
// 更新接口路径信息
|
|
|
|
func UpdateAuthApiPath(id int32, a *dto.AuthApiPathReqDto) bool {
|
|
|
|
dbqa := dbquery.AuthAPIPath
|
|
|
|
oldD, err1 := dbqa.Where(dbqa.ID.Eq(id)).Debug().First()
|
|
|
|
if oldD == nil || err1 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
|
|
|
|
}
|
|
|
|
oldD.Name = a.Name
|
|
|
|
oldD.Path = a.Path
|
|
|
|
oldD.Method = a.Method
|
|
|
|
_, err2 := dbqa.Updates(oldD)
|
|
|
|
if err2 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除接口路径信息
|
|
|
|
func DeleteAuthApiPath(id int32) bool {
|
|
|
|
_, err1 := dbquery.AuthAPIPath.Where(dbquery.AuthAPIPath.ID.Eq(id)).First()
|
|
|
|
if err1 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
|
|
|
|
}
|
|
|
|
// 删除角色中的路径信息
|
|
|
|
dbquery.AuthRoleAPIPath.Where(dbquery.AuthRoleAPIPath.Pid.Eq(id)).Delete()
|
|
|
|
// 删除接口路径信息
|
|
|
|
dbquery.AuthAPIPath.Where(dbquery.AuthAPIPath.ID.Eq(id)).Delete()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 用户关联角色信息
|
|
|
|
func UserLinkRole(linkInfo *dto.AuthRoleUserReqDto) bool {
|
|
|
|
dbar := dbquery.AuthRoleUser
|
|
|
|
// 删除角色关联信息
|
|
|
|
_, err1 := dbar.Where(dbar.UID.Eq(linkInfo.Uid), dbar.Rid.In(linkInfo.DelRids...)).Delete()
|
|
|
|
if err1 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
|
|
|
|
}
|
|
|
|
// 插入关联关系
|
|
|
|
n := len(linkInfo.AddRids)
|
2023-08-31 16:16:18 +08:00
|
|
|
if n == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
arul := make([]*model.AuthRoleUser, n)
|
|
|
|
for i, l := range linkInfo.AddRids {
|
|
|
|
arul[i] = &model.AuthRoleUser{UID: linkInfo.Uid, Rid: l}
|
|
|
|
}
|
|
|
|
err2 := dbar.Save(arul...)
|
|
|
|
if err2 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询用户权限信息
|
|
|
|
func QueryUserAuthApiPath(uid int32) *dto.AuthUserStorageDto {
|
2023-08-30 13:25:57 +08:00
|
|
|
authUser := &dto.AuthUserStorageDto{UID: uid, IsAdmin: false}
|
2023-09-01 17:26:04 +08:00
|
|
|
// 查询用户角色
|
|
|
|
roles := QueryAuthRoleByUid(uid)
|
|
|
|
rn := len(roles)
|
|
|
|
rids := make([]int32, rn+1)
|
2023-08-30 09:28:21 +08:00
|
|
|
if rn > 0 {
|
2023-09-01 17:26:04 +08:00
|
|
|
for i, r := range roles {
|
|
|
|
rids[i] = r.ID
|
|
|
|
authUser.IsAdmin = authUser.IsAdmin || (r.ID == int32(dto.ADMIN))
|
|
|
|
}
|
|
|
|
if authUser.IsAdmin { // 管理员直接返回
|
|
|
|
return authUser
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-01 17:26:04 +08:00
|
|
|
rids[rn] = int32(dto.USER) // 最后添加一个普通用户角色
|
|
|
|
apiPaths := QueryAuthApiPathByRids(rids) // 查询角色拥有的权限路径
|
2023-08-31 16:16:18 +08:00
|
|
|
authUser.AuthPaths = dto.ConvertFromAuthPath(apiPaths) // 赋值路径数组
|
2023-08-30 09:28:21 +08:00
|
|
|
return authUser
|
|
|
|
}
|