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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 查询权限角色信息列表
|
|
|
|
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 {
|
|
|
|
createTime := time.Now()
|
|
|
|
d := model.AuthRole{Name: a.Name, CreateTime: createTime}
|
|
|
|
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 {
|
|
|
|
// 查询刚插入的角色
|
|
|
|
newAuthRole, _ := aq.Where(aq.Name.Eq(a.Name), aq.CreateTime.Eq(createTime)).First()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询角色详情
|
|
|
|
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}
|
|
|
|
// 查询角色与路径关联信息
|
|
|
|
linkPids, err2 := dbquery.AuthRoleAPIPath.Distinct(dbquery.AuthRoleAPIPath.Pid).Where(dbquery.AuthRoleAPIPath.Rid.Eq(rid)).Find()
|
|
|
|
if err2 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()})
|
|
|
|
}
|
|
|
|
pn := len(linkPids)
|
|
|
|
if pn > 0 {
|
|
|
|
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()})
|
|
|
|
}
|
|
|
|
rsp.Paths = apiPaths
|
|
|
|
}
|
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
|
|
|
// 编辑角色信息
|
|
|
|
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()})
|
|
|
|
}
|
|
|
|
if role.Weight == int32(dto.ADMIN) {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: "超级管理员不可编辑"})
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
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 oldD.Weight == int32(dto.ADMIN) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新接口路径信息
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
linkRids, err1 := dbquery.AuthRoleUser.Where(dbquery.AuthRoleUser.UID.Eq(uid)).Find()
|
|
|
|
if err1 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
|
|
|
|
}
|
2023-08-30 13:25:57 +08:00
|
|
|
authUser := &dto.AuthUserStorageDto{UID: uid, IsAdmin: false}
|
2023-08-30 09:28:21 +08:00
|
|
|
rn := len(linkRids)
|
|
|
|
if rn > 0 {
|
|
|
|
rids := make([]int32, rn)
|
|
|
|
for i, r := range linkRids {
|
|
|
|
rids[i] = r.Rid
|
|
|
|
}
|
|
|
|
authUser.RoleIds = rids // 用户角色ID
|
|
|
|
// 查询用户角色信息
|
|
|
|
roles, err2 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.In(rids...)).Find()
|
|
|
|
if err2 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()})
|
|
|
|
}
|
|
|
|
// 判断是否是管理员
|
|
|
|
for _, r := range roles {
|
|
|
|
authUser.IsAdmin = (authUser.IsAdmin || (r.Weight == int32(dto.ADMIN)))
|
|
|
|
}
|
|
|
|
// 非管理员时,查询角色权限路径
|
|
|
|
if !authUser.IsAdmin {
|
|
|
|
// 查询角色与路径关联信息
|
|
|
|
linkPids, err3 := dbquery.AuthRoleAPIPath.Distinct(dbquery.AuthRoleAPIPath.Pid).Where(dbquery.AuthRoleAPIPath.Rid.In(rids...)).Find()
|
|
|
|
if err3 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err3.Error()})
|
|
|
|
}
|
|
|
|
pn := len(linkPids)
|
|
|
|
if pn > 0 {
|
|
|
|
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()})
|
|
|
|
}
|
|
|
|
// 赋值路径数组
|
|
|
|
authUser.AuthPaths = dto.ConvertFromAuthPath(apiPaths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return authUser
|
|
|
|
}
|