You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.9 KiB
187 lines
5.9 KiB
package repository |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"gofaster/internal/auth/model" |
|
"gofaster/internal/shared/repository" |
|
|
|
"gorm.io/gorm" |
|
) |
|
|
|
type RoleRepository interface { |
|
Create(ctx context.Context, role *model.Role) error |
|
Update(ctx context.Context, role *model.Role) error |
|
Delete(ctx context.Context, id uint) error |
|
GetByID(ctx context.Context, id uint) (*model.Role, error) |
|
GetByCode(ctx context.Context, code string) (*model.Role, error) |
|
List(ctx context.Context, offset, limit int) ([]*model.Role, int64, error) |
|
GetPermissions(ctx context.Context, roleID uint) ([]*model.Permission, error) |
|
AssignPermissions(ctx context.Context, roleID uint, permissionIDs []uint) error |
|
RemovePermissions(ctx context.Context, roleID uint, permissionIDs []uint) error |
|
GetRolesByPermission(ctx context.Context, permissionID uint) ([]*model.Role, error) |
|
GetUserRoles(ctx context.Context, userID uint) ([]*model.Role, error) |
|
AssignRolesToUser(ctx context.Context, userID uint, roleIDs []uint) error |
|
RemoveRolesFromUser(ctx context.Context, userID uint, roleIDs []uint) error |
|
GetUsersByRole(ctx context.Context, roleID uint) ([]*model.User, error) |
|
} |
|
|
|
type roleRepository struct { |
|
*repository.BaseRepo |
|
} |
|
|
|
func NewRoleRepository(db *gorm.DB) RoleRepository { |
|
return &roleRepository{ |
|
BaseRepo: repository.NewBaseRepo(db), |
|
} |
|
} |
|
|
|
func (r *roleRepository) Create(ctx context.Context, role *model.Role) error { |
|
return r.DB().WithContext(ctx).Create(role).Error |
|
} |
|
|
|
func (r *roleRepository) Update(ctx context.Context, role *model.Role) error { |
|
return r.DB().WithContext(ctx).Save(role).Error |
|
} |
|
|
|
func (r *roleRepository) Delete(ctx context.Context, id uint) error { |
|
return r.DB().WithContext(ctx).Delete(&model.Role{}, id).Error |
|
} |
|
|
|
func (r *roleRepository) GetByID(ctx context.Context, id uint) (*model.Role, error) { |
|
var role model.Role |
|
err := r.DB().WithContext(ctx).First(&role, id).Error |
|
if err != nil { |
|
return nil, err |
|
} |
|
return &role, nil |
|
} |
|
|
|
func (r *roleRepository) GetByCode(ctx context.Context, code string) (*model.Role, error) { |
|
var role model.Role |
|
err := r.DB().WithContext(ctx).Where("code = ?", code).First(&role).Error |
|
if err != nil { |
|
return nil, err |
|
} |
|
return &role, nil |
|
} |
|
|
|
func (r *roleRepository) List(ctx context.Context, offset, limit int) ([]*model.Role, int64, error) { |
|
var roles []*model.Role |
|
var total int64 |
|
|
|
// 添加调试日志 |
|
fmt.Printf("🔍 [角色查询] 开始查询角色列表,offset=%d, limit=%d\n", offset, limit) |
|
|
|
err := r.DB().WithContext(ctx).Model(&model.Role{}).Count(&total).Error |
|
if err != nil { |
|
fmt.Printf("🔍 [角色查询] 统计总数失败: %v\n", err) |
|
return nil, 0, err |
|
} |
|
fmt.Printf("🔍 [角色查询] 数据库中共有 %d 条角色记录\n", total) |
|
|
|
err = r.DB().WithContext(ctx).Offset(offset).Limit(limit).Order("id ASC").Find(&roles).Error |
|
if err != nil { |
|
fmt.Printf("🔍 [角色查询] 查询角色列表失败: %v\n", err) |
|
return nil, 0, err |
|
} |
|
fmt.Printf("🔍 [角色查询] 查询到 %d 条角色记录\n", len(roles)) |
|
|
|
return roles, total, nil |
|
} |
|
|
|
func (r *roleRepository) GetPermissions(ctx context.Context, roleID uint) ([]*model.Permission, error) { |
|
var permissions []*model.Permission |
|
|
|
err := r.DB().WithContext(ctx). |
|
Joins("JOIN role_permissions ON permissions.id = role_permissions.permission_id"). |
|
Where("role_permissions.role_id = ?", roleID). |
|
Find(&permissions).Error |
|
|
|
return permissions, err |
|
} |
|
|
|
func (r *roleRepository) AssignPermissions(ctx context.Context, roleID uint, permissionIDs []uint) error { |
|
return r.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
|
// 先删除现有的权限关联 |
|
if err := tx.Where("role_id = ?", roleID).Delete(&model.RolePermission{}).Error; err != nil { |
|
return err |
|
} |
|
|
|
// 添加新的权限关联 |
|
for _, permissionID := range permissionIDs { |
|
rolePermission := &model.RolePermission{ |
|
RoleID: roleID, |
|
PermissionID: permissionID, |
|
} |
|
if err := tx.Create(rolePermission).Error; err != nil { |
|
return err |
|
} |
|
} |
|
|
|
return nil |
|
}) |
|
} |
|
|
|
func (r *roleRepository) RemovePermissions(ctx context.Context, roleID uint, permissionIDs []uint) error { |
|
return r.DB().WithContext(ctx).Where("role_id = ? AND permission_id IN ?", roleID, permissionIDs).Delete(&model.RolePermission{}).Error |
|
} |
|
|
|
func (r *roleRepository) GetRolesByPermission(ctx context.Context, permissionID uint) ([]*model.Role, error) { |
|
var roles []*model.Role |
|
|
|
err := r.DB().WithContext(ctx). |
|
Joins("JOIN role_permissions ON roles.id = role_permissions.role_id"). |
|
Where("role_permissions.permission_id = ?", permissionID). |
|
Find(&roles).Error |
|
|
|
return roles, err |
|
} |
|
|
|
func (r *roleRepository) GetUserRoles(ctx context.Context, userID uint) ([]*model.Role, error) { |
|
var roles []*model.Role |
|
|
|
err := r.DB().WithContext(ctx). |
|
Joins("JOIN user_roles ON roles.id = user_roles.role_id"). |
|
Where("user_roles.user_id = ?", userID). |
|
Find(&roles).Error |
|
|
|
return roles, err |
|
} |
|
|
|
func (r *roleRepository) AssignRolesToUser(ctx context.Context, userID uint, roleIDs []uint) error { |
|
return r.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
|
// 先删除现有的角色关联 |
|
if err := tx.Where("user_id = ?", userID).Delete(&model.UserRole{}).Error; err != nil { |
|
return err |
|
} |
|
|
|
// 添加新的角色关联 |
|
for _, roleID := range roleIDs { |
|
userRole := &model.UserRole{ |
|
UserID: userID, |
|
RoleID: roleID, |
|
} |
|
if err := tx.Create(userRole).Error; err != nil { |
|
return err |
|
} |
|
} |
|
|
|
return nil |
|
}) |
|
} |
|
|
|
func (r *roleRepository) RemoveRolesFromUser(ctx context.Context, userID uint, roleIDs []uint) error { |
|
return r.DB().WithContext(ctx).Where("user_id = ? AND role_id IN ?", userID, roleIDs).Delete(&model.UserRole{}).Error |
|
} |
|
|
|
func (r *roleRepository) GetUsersByRole(ctx context.Context, roleID uint) ([]*model.User, error) { |
|
var users []*model.User |
|
|
|
err := r.DB().WithContext(ctx). |
|
Joins("JOIN user_roles ON users.id = user_roles.user_id"). |
|
Where("user_roles.role_id = ?", roleID). |
|
Find(&users).Error |
|
|
|
return users, err |
|
}
|
|
|