rts-sim-testing-service/service/auth.go
weizhihong d4a2074aab 【调整生成link接口路径】
【调整权限查询逻辑】
2023-09-01 17:26:04 +08:00

285 lines
8.6 KiB
Go

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"
)
// 查询权限角色信息列表
func PageAuthRoleQuery(query *dto.PageQueryDto) (*dto.PageDto, error) {
d := dbquery.AuthRole
records, total, err := d.Debug().Select(d.ID, d.Name).Order(d.CreateTime).FindByPage(query.Offset(), query.Size)
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
return &dto.PageDto{Total: int(total), PageQueryDto: *query, Records: dto.ConvertFromAuthRole(records)}, nil
}
// 获取角色列表
func ListAuthRoleQuery() []*dto.AuthRoleRspDto {
d := dbquery.AuthRole
records, err := d.Debug().Select(d.ID, d.Name).Order(d.CreateTime).Find()
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
return dto.ConvertFromAuthRole(records)
}
// 创建权限角色
func CreateAuthRole(a *dto.AuthRoleReqDto) bool {
d := model.AuthRole{Name: a.Name, CreateTime: time.Now()}
aq := dbquery.AuthRole
err := aq.Save(&d)
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
n := len(a.AddPaths)
if n == 0 {
return false
}
// 查询刚插入的角色
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
}
// 查询角色详情
func QueryAuthRole(rid int32) *dto.AuthRoleDetailRspDto {
// 查询用户角色信息
role, err := dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).First()
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
rsp := &dto.AuthRoleDetailRspDto{
Id: role.ID,
Name: role.Name,
Paths: QueryAuthApiPathByRids([]int32{rid}),
}
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 {
panic(dto.ErrorDto{Code: dto.DataNotExist, Message: err1.Error()})
}
if len(arus) == 0 {
return nil
}
rids := make([]int32, len(arus))
for i, v := range arus {
rids[i] = v.Rid
}
roles, err2 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.In(rids...)).Find()
if err2 != nil {
panic(dto.ErrorDto{Code: dto.DataNotExist, Message: err2.Error()})
}
return roles
}
// 编辑角色信息
func UpdateAuthRole(rid int32, info *dto.AuthRoleReqDto) bool {
// 查询用户角色信息
role, err := dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).First()
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
role.Name = info.Name // 更新名称
dbquery.AuthRole.Updates(role)
dqarap := dbquery.AuthRoleAPIPath
// 删除关联
rn := len(info.DelPaths)
if rn > 0 {
dqarap.Where(dqarap.Rid.Eq(rid), dqarap.Pid.In(info.DelPaths...)).Delete()
}
// 增加关联
an := len(info.AddPaths)
if an == 0 {
return true
}
rolePaths := make([]*model.AuthRoleAPIPath, an)
for i, v := range info.AddPaths {
rolePaths[i] = &model.AuthRoleAPIPath{Rid: rid, Pid: v}
}
dqarap.Save(rolePaths...)
return true
}
// 删除权限角色
func DeleteAuthRole(rid int32) bool {
oldD, err1 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.Eq(rid)).First()
if err1 != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
}
if dto.IsSystemRole(oldD.ID) {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: "系统角色不可删除"})
}
// 如果有用户关联则不删除
count, err2 := dbquery.AuthRoleUser.Where(dbquery.AuthRoleUser.Rid.Eq(rid)).Count()
if err2 != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()})
}
if count > 0 {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: "有用户关联该角色"})
}
// 删除用户关联关系
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
}
// 查询接口路径信息列表
func PageAuthApiPathQuery(query *dto.AuthApiPathPageReqDto) (*dto.PageDto, error) {
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()})
}
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
}
// 查询接口路径信息列表
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
}
// 根据角色ID查询路径信息
func QueryAuthApiPathByRids(rids []int32) []*model.AuthAPIPath {
if len(rids) == 0 {
return nil
}
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
}
// 更新接口路径信息
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)
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()})
}
return true
}
// 查询用户权限信息
func QueryUserAuthApiPath(uid int32) *dto.AuthUserStorageDto {
authUser := &dto.AuthUserStorageDto{UID: uid, IsAdmin: false}
// 查询用户角色
roles := QueryAuthRoleByUid(uid)
rn := len(roles)
rids := make([]int32, rn+1)
if rn > 0 {
for i, r := range roles {
rids[i] = r.ID
authUser.IsAdmin = authUser.IsAdmin || (r.ID == int32(dto.ADMIN))
}
if authUser.IsAdmin { // 管理员直接返回
return authUser
}
}
rids[rn] = int32(dto.USER) // 最后添加一个普通用户角色
apiPaths := QueryAuthApiPathByRids(rids) // 查询角色拥有的权限路径
authUser.AuthPaths = dto.ConvertFromAuthPath(apiPaths) // 赋值路径数组
return authUser
}