|
@@ -9,6 +9,8 @@ import (
|
|
|
"net/http"
|
|
|
"time"
|
|
|
|
|
|
+ "gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
|
+
|
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
|
|
|
"gitee.com/xuyiping_admin/pkg/jwt"
|
|
@@ -146,36 +148,6 @@ func (s *StoreEntry) IsShowSystemUser(ctx context.Context, userId int64) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// GetCurrentUserName 获取当前用户名
|
|
|
-func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
|
|
|
- userNameInter := ctx.Value(CurrentUserName)
|
|
|
- if userNameInter == nil {
|
|
|
- return "", xerr.Customf("cannot userName")
|
|
|
- }
|
|
|
-
|
|
|
- if userName, ok := userNameInter.(string); ok {
|
|
|
- return userName, nil
|
|
|
- } else {
|
|
|
- return "", xerr.Customf("waring userName")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// GetUserInfo 获取用户信息
|
|
|
-func (s *StoreEntry) GetUserInfo(ctx context.Context, token string) (*operationPb.UserAuth, error) {
|
|
|
- systemUser := &model.SystemUser{}
|
|
|
- userName, err := s.GetCurrentUserName(ctx)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err = s.DB.Where("name = ?", userName).First(systemUser).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- systemRole := make([]*model.SystemRole, 0)
|
|
|
- return systemUser.SystemUserFormat(systemRole, nil), nil
|
|
|
-}
|
|
|
-
|
|
|
// SystemUserCreateOrUpdate 创建或者更新系统用户
|
|
|
func (s *StoreEntry) SystemUserCreateOrUpdate(ctx context.Context, req *pasturePb.SearchUserRequest) error {
|
|
|
if req.Id > 0 {
|
|
@@ -222,6 +194,74 @@ func (s *StoreEntry) ResetPasswordSystemUser(ctx context.Context, req *pasturePb
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (s *StoreEntry) SystemUserRole(ctx context.Context, userId int64) (*pasturePb.SystemUserRoleResponse, error) {
|
|
|
+ systemUser := &model.SystemUser{Id: userId}
|
|
|
+ if err := s.DB.First(systemUser).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return nil, xerr.Custom("该用户不存在")
|
|
|
+ }
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &pasturePb.SystemUserRoleResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: int32(systemUser.RoleId),
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) SystemUserRoleSave(ctx context.Context, req *pasturePb.SystemUserRoleRequest) error {
|
|
|
+ systemUser := &model.SystemUser{Id: int64(req.UserId)}
|
|
|
+ if err := s.DB.First(systemUser).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return xerr.Custom("该用户不存在")
|
|
|
+ }
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ systemRole := &model.SystemRole{Id: int64(req.RoleId)}
|
|
|
+ if err := s.DB.First(systemRole).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return xerr.Custom("该角色不存在")
|
|
|
+ }
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if err := s.DB.Model(systemUser).Update("role_id", systemRole.Id).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetCurrentUserName 获取当前用户名
|
|
|
+func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
|
|
|
+ userNameInter := ctx.Value(CurrentUserName)
|
|
|
+ if userNameInter == nil {
|
|
|
+ return "", xerr.Customf("cannot userName")
|
|
|
+ }
|
|
|
+
|
|
|
+ if userName, ok := userNameInter.(string); ok {
|
|
|
+ return userName, nil
|
|
|
+ } else {
|
|
|
+ return "", xerr.Customf("waring userName")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// GetUserInfo 获取用户信息
|
|
|
+func (s *StoreEntry) GetUserInfo(ctx context.Context, token string) (*operationPb.UserAuth, error) {
|
|
|
+ systemUser := &model.SystemUser{}
|
|
|
+ userName, err := s.GetCurrentUserName(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = s.DB.Where("name = ?", userName).First(systemUser).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ systemRole := make([]*model.SystemRole, 0)
|
|
|
+ return systemUser.SystemUserFormat(systemRole, nil), nil
|
|
|
+}
|
|
|
+
|
|
|
// DetailsSystemUser 系统用户详情
|
|
|
func (s *StoreEntry) DetailsSystemUser(ctx context.Context, userId int64) (*operationPb.UserDetails, error) {
|
|
|
systemUser := &model.SystemUser{
|
|
@@ -265,12 +305,13 @@ func (s *StoreEntry) GetSystemUserPermissions(ctx context.Context, token string)
|
|
|
}
|
|
|
|
|
|
systemAllPermissionsList := &SystemAllPermissionsList{
|
|
|
- MenuList: make([]*model.SystemMenuPermissions, 0),
|
|
|
+ MenuList: make([]*model.SystemRoleMenu, 0),
|
|
|
}
|
|
|
|
|
|
for _, role := range systemRoles {
|
|
|
- item := s.GetSystemAllPermissionsList(ctx, role.Id)
|
|
|
- systemAllPermissionsList.MenuList = append(systemAllPermissionsList.MenuList, item.MenuList...)
|
|
|
+ zaplog.Info(role.Name)
|
|
|
+ /*item := s.GetSystemRoleMenuList(ctx, role.Id)
|
|
|
+ systemAllPermissionsList.MenuList = append(systemAllPermissionsList.MenuList, item.MenuList...)*/
|
|
|
}
|
|
|
|
|
|
systemAllPermissionsList.SystemUserMenuPermissionsUnDuplicate()
|
|
@@ -453,19 +494,6 @@ func (s *StoreEntry) SearchSystemRoleList(ctx context.Context, req *pasturePb.Se
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- /*permissionsMenuMap := make(map[int64][]*model.SystemMenu, 0)
|
|
|
- for _, role := range systemRoleList {
|
|
|
- systemMenuList := make([]*model.SystemMenu, 0)
|
|
|
- if err := s.DB.Table(new(model.SystemMenu).TableName()).Select(fmt.Sprintf("%s.*", new(model.SystemMenu).TableName())).
|
|
|
- Joins(fmt.Sprintf("left join %s on %s = %s and %s", new(model.SystemMenuPermissions).TableName(), fmt.Sprintf("%s.menu_id", new(model.SystemMenuPermissions).TableName()),
|
|
|
- fmt.Sprintf("%s.id", new(model.SystemMenu).TableName()), fmt.Sprintf("%s.is_show = %d", new(model.SystemMenuPermissions).TableName(), operationPb.IsShow_OK))).
|
|
|
- Where(fmt.Sprintf("%s.role_id = ?", new(model.SystemMenuPermissions).TableName()), role.Id).Where(fmt.Sprintf("%s.level = ?", new(model.SystemMenu).TableName()), model.Level2).
|
|
|
- Find(&systemMenuList).Error; err != nil {
|
|
|
- zaplog.Error("SearchSystemRoleList", zap.Any("SystemMenuPermissions", err))
|
|
|
- }
|
|
|
- permissionsMenuMap[role.Id] = systemMenuList
|
|
|
- }*/
|
|
|
-
|
|
|
return &pasturePb.SearchRoleResponse{
|
|
|
Code: http.StatusOK,
|
|
|
Message: "ok",
|
|
@@ -473,7 +501,7 @@ func (s *StoreEntry) SearchSystemRoleList(ctx context.Context, req *pasturePb.Se
|
|
|
Page: req.Pagination.Page,
|
|
|
Total: int32(count),
|
|
|
PageSize: req.Pagination.PageSize,
|
|
|
- List: model.SystemRoleSlice(systemRoleList).ToPB(nil),
|
|
|
+ List: model.SystemRoleSlice(systemRoleList).ToPB(),
|
|
|
},
|
|
|
}, nil
|
|
|
}
|
|
@@ -490,8 +518,8 @@ func (s *StoreEntry) SearchSystemRoleListByIds(ctx context.Context, ids []int64)
|
|
|
return systemRoleList, nil
|
|
|
}
|
|
|
|
|
|
-// GetRolePermissions 查询系统角色权限
|
|
|
-func (s *StoreEntry) GetRolePermissions(ctx context.Context, roleId int64) (*operationPb.RolePermissionsList, error) {
|
|
|
+// GetRoleMenuList 查询系统角色对应的菜单数据
|
|
|
+func (s *StoreEntry) GetRoleMenuList(ctx context.Context, roleId int64) (*pasturePb.SystemRoleMenuResponse, error) {
|
|
|
systemRole := &model.SystemRole{
|
|
|
Id: roleId,
|
|
|
}
|
|
@@ -502,13 +530,67 @@ func (s *StoreEntry) GetRolePermissions(ctx context.Context, roleId int64) (*ope
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- systemAllPermissionsList := &SystemAllPermissionsList{
|
|
|
- MenuList: make([]*model.SystemMenuPermissions, 0),
|
|
|
+ if systemRole.IsDelete == pasturePb.IsShow_No {
|
|
|
+ return nil, xerr.Custom("该数据已被删除")
|
|
|
+ }
|
|
|
+
|
|
|
+ item := s.GetSystemRoleMenuList(ctx, systemRole.Id)
|
|
|
+ return &pasturePb.SystemRoleMenuResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: model.SystemRoleMenuSlice(item).ToPB(),
|
|
|
+ }, nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// RoleMenuSave 保存系统角色对应的菜单数据
|
|
|
+func (s *StoreEntry) RoleMenuSave(ctx context.Context, res *pasturePb.SystemRoleMenuRequest) error {
|
|
|
+ if len(res.MenuIds) <= 0 {
|
|
|
+ return xerr.Custom("请选择菜单")
|
|
|
+ }
|
|
|
+ systemRole := &model.SystemRole{
|
|
|
+ Id: int64(res.RoleId),
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := s.DB.First(systemRole).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return xerr.Custom("该数据不存在")
|
|
|
+ }
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ if err := tx.Model(new(model.SystemRoleMenu)).Where("role_id = ?", systemRole.Id).Delete(&model.SystemRoleMenu{}).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ for _, menuId := range res.MenuIds {
|
|
|
+ if err := tx.Create(&model.SystemRoleMenu{
|
|
|
+ RoleId: systemRole.Id,
|
|
|
+ MenuId: int64(menuId),
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) SystemRoleList(ctx context.Context) (*pasturePb.GetRoleListResponse, error) {
|
|
|
+ systemRoleList := make([]*model.SystemRole, 0)
|
|
|
+ if err := s.DB.Model(new(model.SystemRole)).
|
|
|
+ Where("is_delete = ? and is_show = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
|
|
|
+ Find(&systemRoleList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- item := s.GetSystemAllPermissionsList(ctx, systemRole.Id)
|
|
|
- systemAllPermissionsList.MenuList = append(systemAllPermissionsList.MenuList, item.MenuList...)
|
|
|
- return s.AllPermissionsListToRolePermissions(systemAllPermissionsList), nil
|
|
|
+ return &pasturePb.GetRoleListResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: model.SystemRoleSlice(systemRoleList).ToPB(),
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
// CreateOrUpdateSystemMenu 添加或者更新系统菜单权限
|
|
@@ -548,6 +630,19 @@ func (s *StoreEntry) CreateOrUpdateSystemMenu(ctx context.Context, req *pastureP
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (s *StoreEntry) SystemMenuTree(ctx context.Context) (*pasturePb.SystemMenuTreeResponse, error) {
|
|
|
+ systemMenuList := make([]*model.SystemMenu, 0)
|
|
|
+ if err := s.DB.Model(new(model.SystemMenu)).Where("is_delete = ?", pasturePb.IsShow_Ok).
|
|
|
+ Where("is_show = ?", pasturePb.IsShow_Ok).Find(&systemMenuList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return &pasturePb.SystemMenuTreeResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: model.SystemMenuSlice(systemMenuList).ToPB(),
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
// SearchSystemMenuList 菜单列表查询
|
|
|
func (s *StoreEntry) SearchSystemMenuList(ctx context.Context, req *pasturePb.SearchMenuRequest) (*pasturePb.SearchMenuResponse, error) {
|
|
|
systemMenuList := make([]*model.SystemMenu, 0)
|