feat: rbac完善,file接入完成
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AssignRoleMenusLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAssignRoleMenusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssignRoleMenusLogic {
|
||||
return &AssignRoleMenusLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||||
}
|
||||
|
||||
// AssignRoleMenus 给角色授权菜单(全量替换)
|
||||
func (l *AssignRoleMenusLogic) AssignRoleMenus(in *system.AssignRoleMenusReq) (*system.CommonResp, error) {
|
||||
err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// 先清除旧的菜单关联
|
||||
if err := tx.Where("role_id = ?", in.RoleId).Delete(&sysModel.SundynixRoleMenu{}).Error; err != nil {
|
||||
return fmt.Errorf("清除菜单关联失败: %w", err)
|
||||
}
|
||||
// 重新批量写入
|
||||
for _, mid := range in.MenuIds {
|
||||
if err := tx.Create(&sysModel.SundynixRoleMenu{RoleID: in.RoleId, MenuID: mid}).Error; err != nil {
|
||||
return fmt.Errorf("关联菜单失败: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &system.CommonResp{Code: 200, Msg: "success"}, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AssignUserRolesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAssignUserRolesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssignUserRolesLogic {
|
||||
return &AssignUserRolesLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||||
}
|
||||
|
||||
// AssignUserRoles 给用户授权角色(全量替换)
|
||||
func (l *AssignUserRolesLogic) AssignUserRoles(in *system.AssignUserRolesReq) (*system.CommonResp, error) {
|
||||
err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// 先清除旧的角色关联
|
||||
if err := tx.Where("user_id = ?", in.UserId).Delete(&sysModel.SundynixUserRole{}).Error; err != nil {
|
||||
return fmt.Errorf("清除角色关联失败: %w", err)
|
||||
}
|
||||
// 重新批量写入
|
||||
for _, rid := range in.RoleIds {
|
||||
if err := tx.Create(&sysModel.SundynixUserRole{UserID: in.UserId, RoleID: rid}).Error; err != nil {
|
||||
return fmt.Errorf("关联角色失败: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &system.CommonResp{Code: 200, Msg: "success"}, nil
|
||||
}
|
||||
@@ -28,7 +28,6 @@ func (l *CreateRoleLogic) CreateRole(in *system.RoleReq) (*system.CommonResp, er
|
||||
if err := tx.Create(&role).Error; err != nil {
|
||||
return fmt.Errorf("创建角色失败: %w", err)
|
||||
}
|
||||
// 关联菜单
|
||||
for _, mid := range in.MenuIds {
|
||||
if err := tx.Create(&sysModel.SundynixRoleMenu{RoleID: role.ID, MenuID: mid}).Error; err != nil {
|
||||
return fmt.Errorf("关联菜单失败: %w", err)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetRoleDetailLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetRoleDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleDetailLogic {
|
||||
return &GetRoleDetailLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetRoleDetailLogic) GetRoleDetail(in *system.GetRoleDetailReq) (*system.RoleDetailResp, error) {
|
||||
var role sysModel.SundynixRole
|
||||
if err := l.svcCtx.DB.Preload("Menus").Where("id = ?", in.Id).First(&role).Error; err != nil {
|
||||
return nil, fmt.Errorf("获取角色详情失败: %w", err)
|
||||
}
|
||||
|
||||
var menuIds []string
|
||||
for _, menu := range role.Menus {
|
||||
menuIds = append(menuIds, menu.ID)
|
||||
}
|
||||
|
||||
return &system.RoleDetailResp{
|
||||
Id: role.ID,
|
||||
Name: role.Name,
|
||||
Code: role.Code,
|
||||
Sort: int32(role.Sort),
|
||||
MenuIds: menuIds,
|
||||
}, nil
|
||||
}
|
||||
@@ -39,7 +39,13 @@ func (l *GetRoleListLogic) GetRoleList(in *system.RoleListReq) (*system.RoleList
|
||||
}
|
||||
var items []*system.RoleInfo
|
||||
for _, r := range list {
|
||||
items = append(items, &system.RoleInfo{Id: r.ID, Name: r.Name, Code: r.Code, Sort: int32(r.Sort)})
|
||||
items = append(items, &system.RoleInfo{
|
||||
Id: r.ID,
|
||||
Name: r.Name,
|
||||
Code: r.Code,
|
||||
Sort: int32(r.Sort),
|
||||
CreatedAt: r.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
return &system.RoleListResp{List: items, Total: total}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetUserDetailLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserDetailLogic {
|
||||
return &GetUserDetailLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserDetailLogic) GetUserDetail(in *system.GetUserDetailReq) (*system.UserDetailResp, error) {
|
||||
var user sysModel.SundynixUser
|
||||
if err := l.svcCtx.DB.Preload("Roles").Where("id = ?", in.Id).First(&user).Error; err != nil {
|
||||
return nil, fmt.Errorf("获取用户详情失败: %w", err)
|
||||
}
|
||||
|
||||
var roleIds []string
|
||||
for _, role := range user.Roles {
|
||||
roleIds = append(roleIds, role.ID)
|
||||
}
|
||||
|
||||
return &system.UserDetailResp{
|
||||
Id: user.ID,
|
||||
Name: user.Name,
|
||||
Account: user.Account,
|
||||
NickName: user.NickName,
|
||||
Phone: user.Phone,
|
||||
RoleIds: roleIds,
|
||||
}, nil
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func (l *GetUserListLogic) GetUserList(in *system.GetUserListReq) (*system.GetUs
|
||||
if current <= 0 {
|
||||
current = 1
|
||||
}
|
||||
db.Offset((current - 1) * pageSize).Limit(pageSize).Order("created_at DESC").Find(&list)
|
||||
db.Preload("Roles").Offset((current - 1) * pageSize).Limit(pageSize).Order("created_at DESC").Find(&list)
|
||||
|
||||
var protoList []*system.UserInfo
|
||||
for _, u := range list {
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UpdateRoleLogic struct {
|
||||
@@ -23,26 +22,10 @@ func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Update
|
||||
}
|
||||
|
||||
func (l *UpdateRoleLogic) UpdateRole(in *system.RoleUpdateReq) (*system.CommonResp, error) {
|
||||
err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&sysModel.SundynixRole{}).Where("id = ?", in.Id).Updates(map[string]interface{}{
|
||||
"name": in.Name, "code": in.Code, "sort": in.Sort,
|
||||
}).Error; err != nil {
|
||||
return fmt.Errorf("更新角色失败: %w", err)
|
||||
}
|
||||
// 先删除旧的菜单关联
|
||||
if err := tx.Where("role_id = ?", in.Id).Delete(&sysModel.SundynixRoleMenu{}).Error; err != nil {
|
||||
return fmt.Errorf("清除菜单关联失败: %w", err)
|
||||
}
|
||||
// 重新关联菜单
|
||||
for _, mid := range in.MenuIds {
|
||||
if err := tx.Create(&sysModel.SundynixRoleMenu{RoleID: in.Id, MenuID: mid}).Error; err != nil {
|
||||
return fmt.Errorf("关联菜单失败: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err := l.svcCtx.DB.Model(&sysModel.SundynixRole{}).Where("id = ?", in.Id).Updates(map[string]interface{}{
|
||||
"name": in.Name, "code": in.Code, "sort": in.Sort,
|
||||
}).Error; err != nil {
|
||||
return nil, fmt.Errorf("更新角色失败: %w", err)
|
||||
}
|
||||
return &system.CommonResp{Code: 200, Msg: "success"}, nil
|
||||
}
|
||||
|
||||
@@ -27,5 +27,15 @@ func convertUserToProto(u *sysModel.SundynixUser) *system.UserInfo {
|
||||
if u.LastLoginAt != nil {
|
||||
info.LastLoginAt = u.LastLoginAt.Unix()
|
||||
}
|
||||
|
||||
for _, r := range u.Roles {
|
||||
info.Roles = append(info.Roles, &system.RoleInfo{
|
||||
Id: r.ID,
|
||||
Name: r.Name,
|
||||
Code: r.Code,
|
||||
Sort: int32(r.Sort),
|
||||
})
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
@@ -54,6 +54,11 @@ func (s *SystemServiceServer) GetUserList(ctx context.Context, in *system.GetUse
|
||||
return l.GetUserList(in)
|
||||
}
|
||||
|
||||
func (s *SystemServiceServer) GetUserDetail(ctx context.Context, in *system.GetUserDetailReq) (*system.UserDetailResp, error) {
|
||||
l := logic.NewGetUserDetailLogic(ctx, s.svcCtx)
|
||||
return l.GetUserDetail(in)
|
||||
}
|
||||
|
||||
func (s *SystemServiceServer) DeleteUser(ctx context.Context, in *system.DeleteUserReq) (*system.CommonResp, error) {
|
||||
l := logic.NewDeleteUserLogic(ctx, s.svcCtx)
|
||||
return l.DeleteUser(in)
|
||||
@@ -90,6 +95,22 @@ func (s *SystemServiceServer) GetRoleList(ctx context.Context, in *system.RoleLi
|
||||
return l.GetRoleList(in)
|
||||
}
|
||||
|
||||
func (s *SystemServiceServer) GetRoleDetail(ctx context.Context, in *system.GetRoleDetailReq) (*system.RoleDetailResp, error) {
|
||||
l := logic.NewGetRoleDetailLogic(ctx, s.svcCtx)
|
||||
return l.GetRoleDetail(in)
|
||||
}
|
||||
|
||||
func (s *SystemServiceServer) AssignRoleMenus(ctx context.Context, in *system.AssignRoleMenusReq) (*system.CommonResp, error) {
|
||||
l := logic.NewAssignRoleMenusLogic(ctx, s.svcCtx)
|
||||
return l.AssignRoleMenus(in)
|
||||
}
|
||||
|
||||
// --- RBAC 用户授权 ---
|
||||
func (s *SystemServiceServer) AssignUserRoles(ctx context.Context, in *system.AssignUserRolesReq) (*system.CommonResp, error) {
|
||||
l := logic.NewAssignUserRolesLogic(ctx, s.svcCtx)
|
||||
return l.AssignUserRoles(in)
|
||||
}
|
||||
|
||||
// --- 菜单 ---
|
||||
func (s *SystemServiceServer) GetMenusByRoleId(ctx context.Context, in *system.GetMenusByRoleIdReq) (*system.GetMenusByRoleIdResp, error) {
|
||||
l := logic.NewGetMenusByRoleIdLogic(ctx, s.svcCtx)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
)
|
||||
|
||||
// CleanOperLogJob 定义清理操作日志的定时任务
|
||||
type CleanOperLogJob struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCleanOperLogJob(svcCtx *svc.ServiceContext) *CleanOperLogJob {
|
||||
return &CleanOperLogJob{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (j *CleanOperLogJob) Run() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
logx.WithContext(ctx).Infof("[CRON] 开始执行定时清理操作日志任务")
|
||||
|
||||
// 删除 3 天前的日志
|
||||
threeDaysAgo := time.Now().AddDate(0, 0, -3)
|
||||
|
||||
// 执行硬删除
|
||||
res := j.svcCtx.DB.WithContext(ctx).Exec("DELETE FROM sundynix_operation_record WHERE created_at < ?", threeDaysAgo)
|
||||
if res.Error != nil {
|
||||
logx.WithContext(ctx).Errorf("[CRON] 清理操作日志失败: %v", res.Error)
|
||||
return
|
||||
}
|
||||
|
||||
logx.WithContext(ctx).Infof("[CRON] 定时清理操作日志完成,共删除了 %d 条数据", res.RowsAffected)
|
||||
}
|
||||
Reference in New Issue
Block a user