rts-sim-testing-service/service/auth.go

296 lines
9.0 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"
"joylink.club/bj-rtsts-server/sys_error"
)
// 查询权限角色信息列表
func PageAuthRoleQuery(query *dto.PageQueryDto) *dto.PageDto[*dto.AuthRoleRspDto] {
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(sys_error.New("查询权限数据库出错,请联系维护人员", err))
}
return &dto.PageDto[*dto.AuthRoleRspDto]{Total: int(total), PageQueryDto: *query, Records: dto.ConvertFromAuthRole(records)}
}
// 获取角色列表
func ListAuthRoleQuery() []*dto.AuthRoleRspDto {
d := dbquery.AuthRole
records, err := d.Debug().Select(d.ID, d.Name).Order(d.CreateTime).Find()
if err != nil {
panic(sys_error.New("查询权限数据库出错,请联系维护人员", err))
}
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(sys_error.New("保存权限数据库出错,请联系维护人员", err))
}
n := len(a.AddPaths)
if n == 0 {
return true
}
// 查询刚插入的角色
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(sys_error.New("查询角色数据库出错,请联系维护人员", err))
}
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(sys_error.New("查询角色数据库出错,请联系维护人员", err1))
}
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(sys_error.New("查询角色数据库发生错误,请联系维护人员", err2))
}
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(sys_error.New("更新角色数据库发生错误,不存在此用户", err))
}
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(sys_error.New("删除权限角色数据库发生错误", err1))
}
if dto.IsSystemRole(oldD.ID) {
panic(sys_error.New("系统角色不可删除", err1))
}
// 如果有用户关联则不删除
count, err2 := dbquery.AuthRoleUser.Where(dbquery.AuthRoleUser.Rid.Eq(rid)).Count()
if err2 != nil {
panic(sys_error.New("删除失败,还存在用户关联", err2))
}
if count > 0 {
panic(sys_error.New("删除失败,有用户关联该角色"))
}
// 删除用户关联关系
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[*model.AuthAPIPath] {
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[*model.AuthAPIPath]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
}
// 查询接口路径信息列表
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 {
rn := len(rids)
if rn == 0 {
return nil
}
// 判断是否是超级管理
isAdmin := false
for i := 0; i < rn && !isAdmin; i++ {
isAdmin = rids[i] == int32(dto.ADMIN)
}
if isAdmin { // 超级管理员查询所有路径
apiPaths, _ := dbquery.AuthAPIPath.Find()
return apiPaths
}
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
}