mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-20 21:11:55 +08:00
[系统用户]实现系统用户的增删改
This commit is contained in:
@@ -2,11 +2,13 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
)
|
||||
|
||||
type AdminService struct {
|
||||
@@ -115,6 +117,8 @@ func (this *AdminService) FindEnabledAdmin(ctx context.Context, req *pb.FindEnab
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
admin, err := models.SharedAdminDAO.FindEnabledAdmin(req.AdminId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -123,11 +127,29 @@ func (this *AdminService) FindEnabledAdmin(ctx context.Context, req *pb.FindEnab
|
||||
return &pb.FindEnabledAdminResponse{Admin: nil}, nil
|
||||
}
|
||||
|
||||
pbModules := []*pb.AdminModule{}
|
||||
modules := []*systemconfigs.AdminModule{}
|
||||
if len(admin.Modules) > 0 && admin.Modules != "null" {
|
||||
err = json.Unmarshal([]byte(admin.Modules), &modules)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, module := range modules {
|
||||
pbModules = append(pbModules, &pb.AdminModule{
|
||||
AllowAll: module.AllowAll,
|
||||
Code: module.Code,
|
||||
Actions: module.Actions,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
result := &pb.Admin{
|
||||
Id: int64(admin.Id),
|
||||
Fullname: admin.Fullname,
|
||||
Username: admin.Username,
|
||||
IsOn: admin.IsOn == 1,
|
||||
IsSuper: admin.IsSuper == 1,
|
||||
Modules: pbModules,
|
||||
}
|
||||
return &pb.FindEnabledAdminResponse{Admin: result}, nil
|
||||
}
|
||||
@@ -151,7 +173,7 @@ func (this *AdminService) CreateOrUpdateAdmin(ctx context.Context, req *pb.Creat
|
||||
}
|
||||
return &pb.CreateOrUpdateAdminResponse{AdminId: adminId}, nil
|
||||
}
|
||||
adminId, err = models.SharedAdminDAO.CreateAdmin(req.Username, req.Password, "管理员")
|
||||
adminId, err = models.SharedAdminDAO.CreateAdmin(req.Username, req.Password, "管理员", true, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -159,14 +181,14 @@ func (this *AdminService) CreateOrUpdateAdmin(ctx context.Context, req *pb.Creat
|
||||
}
|
||||
|
||||
// 修改管理员信息
|
||||
func (this *AdminService) UpdateAdmin(ctx context.Context, req *pb.UpdateAdminRequest) (*pb.RPCSuccess, error) {
|
||||
func (this *AdminService) UpdateAdminInfo(ctx context.Context, req *pb.UpdateAdminInfoRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin, rpcutils.UserTypeAPI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedAdminDAO.UpdateAdmin(req.AdminId, req.Fullname)
|
||||
err = models.SharedAdminDAO.UpdateAdminInfo(req.AdminId, req.Fullname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -195,3 +217,142 @@ func (this *AdminService) UpdateAdminLogin(ctx context.Context, req *pb.UpdateAd
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 获取所有管理员的权限列表
|
||||
func (this *AdminService) FindAllAdminModules(ctx context.Context, req *pb.FindAllAdminModulesRequest) (*pb.FindAllAdminModulesResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
admins, err := models.SharedAdminDAO.FindAllAdminModules()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*pb.AdminModuleList{}
|
||||
for _, admin := range admins {
|
||||
modules := []*systemconfigs.AdminModule{}
|
||||
if len(admin.Modules) > 0 && admin.Modules != "null" {
|
||||
err = json.Unmarshal([]byte(admin.Modules), &modules)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
pbModules := []*pb.AdminModule{}
|
||||
for _, module := range modules {
|
||||
pbModules = append(pbModules, &pb.AdminModule{
|
||||
AllowAll: module.AllowAll,
|
||||
Code: module.Code,
|
||||
Actions: module.Actions,
|
||||
})
|
||||
}
|
||||
|
||||
list := &pb.AdminModuleList{
|
||||
AdminId: int64(admin.Id),
|
||||
IsSuper: admin.IsSuper == 1,
|
||||
Modules: pbModules,
|
||||
}
|
||||
result = append(result, list)
|
||||
}
|
||||
|
||||
return &pb.FindAllAdminModulesResponse{AdminModules: result}, nil
|
||||
}
|
||||
|
||||
// 创建管理员
|
||||
func (this *AdminService) CreateAdmin(ctx context.Context, req *pb.CreateAdminRequest) (*pb.CreateAdminResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
adminId, err := models.SharedAdminDAO.CreateAdmin(req.Username, req.Password, req.Fullname, req.IsSuper, req.ModulesJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateAdminResponse{AdminId: adminId}, nil
|
||||
}
|
||||
|
||||
// 修改管理员
|
||||
func (this *AdminService) UpdateAdmin(ctx context.Context, req *pb.UpdateAdminRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
err = models.SharedAdminDAO.UpdateAdmin(req.AdminId, req.Username, req.Password, req.Fullname, req.IsSuper, req.ModulesJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 计算管理员数量
|
||||
func (this *AdminService) CountAllEnabledAdmins(ctx context.Context, req *pb.CountAllEnabledAdminsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
count, err := models.SharedAdminDAO.CountAllEnabledAdmins()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// 列出单页的管理员
|
||||
func (this *AdminService) ListEnabledAdmins(ctx context.Context, req *pb.ListEnabledAdminsRequest) (*pb.ListEnabledAdminsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
admins, err := models.SharedAdminDAO.ListEnabledAdmins(req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*pb.Admin{}
|
||||
for _, admin := range admins {
|
||||
result = append(result, &pb.Admin{
|
||||
Id: int64(admin.Id),
|
||||
Fullname: admin.Fullname,
|
||||
Username: admin.Username,
|
||||
IsOn: admin.IsOn == 1,
|
||||
IsSuper: admin.IsSuper == 1,
|
||||
CreatedAt: int64(admin.CreatedAt),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.ListEnabledAdminsResponse{Admins: result}, nil
|
||||
}
|
||||
|
||||
// 删除管理员
|
||||
func (this *AdminService) DeleteAdmin(ctx context.Context, req *pb.DeleteAdminRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查权限
|
||||
|
||||
// TODO 超级管理员用户是不能删除的,或者要至少留一个超级管理员用户
|
||||
|
||||
_, err = models.SharedAdminDAO.DisableAdmin(req.AdminId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user