123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package user
- import (
- "context"
- "cp-organization-management/errors"
- "cp-organization-management/impl/v1/common"
- "cp-organization-management/impl/v1/rbac"
- "cp-organization-management/impl/v1/zone"
- "cp-organization-management/model"
- pb_v1 "cp-organization-management/pb/v1"
- "cp-organization-management/utils"
- "encoding/json"
- "fmt"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func getUserBaseInfo(user *model.RbacUser, reply *pb_v1.UserInfoReply) error {
- reply.Id = user.Id
- reply.GroupId = user.GroupId
- reply.Name = user.Name
- reply.Username = user.Username
- reply.Email = user.Email
- reply.Phone = user.Phone
- return nil
- }
- func getUserRbacInfo(req *pb_v1.UserInfoRequest, reply *pb_v1.UserInfoReply, dbname string, groupId int64) error {
- if groupId < 1 {
- return nil
- }
- group := model.NewRbacGroup(dbname)
- where := map[string]interface{}{
- "id":groupId,
- }
- err := group.Find(database.DB(), where)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil
- }
- return errors.DataBaseError
- }
- mreq := pb_v1.RbacNodeListByGroupOrUserRequest{OrganizationCode:req.OrganizationCode, GroupId:groupId, Select:true}
- mreply, err := rbac.RbacNodeListByGroupOrUser(context.Background(), &mreq)
- if err != nil {
- return err
- }
- reply.NodeList = mreply.List
- return nil
- }
- func selectUserZone(zones map[string]string, loginUserZones []*pb_v1.ZoneItem) ([]*pb_v1.ZoneItemSelect) {
- list := make([]*pb_v1.ZoneItemSelect, len(loginUserZones))
- for i, v := range loginUserZones {
- list[i] = &pb_v1.ZoneItemSelect{
- ZoneName:v.ZoneName,
- ZoneCode:v.ZoneCode,
- ParentZoneCode:v.ParentZoneCode,
- }
- if _, ok := zones[v.ZoneCode]; ok {
- list[i].Select = true
- }
- if len(v.Childs) > 0 {
- list[i].Childs = selectUserZone(zones, v.Childs)
- }
- }
- return list
- }
- func selectUserZoneAll(loginUserZones []*pb_v1.ZoneItem) ([]*pb_v1.ZoneItemSelect) {
- list := make([]*pb_v1.ZoneItemSelect, len(loginUserZones))
- for i, v := range loginUserZones {
- list[i] = &pb_v1.ZoneItemSelect{
- ZoneName:v.ZoneName,
- ZoneCode:v.ZoneCode,
- ParentZoneCode:v.ParentZoneCode,
- Select:true,
- }
- if len(v.Childs) > 0 {
- list[i].Childs = selectUserZoneAll(v.Childs)
- }
- }
- return list
- }
- func getUserZoneInfo(req *pb_v1.UserInfoRequest, reply *pb_v1.UserInfoReply, dbname string) error {
- // 获取登录用户的区域
- loginUserZoneReq := pb_v1.ZoneListRequest{OrganizationCode:req.OrganizationCode, Uid:req.Uid}
- loginUserZoneReply, err := zone.ZoneList(context.Background(), &loginUserZoneReq)
- if err != nil {
- return err
- }
- if len(loginUserZoneReply.List) == 0 {
- return nil
- }
- // 获取指定用户的区域
- super, zones, err := common.GetUserZone(req.Id, dbname)
- if err != nil {
- return err
- }
- // 不能对超级用户进行操作
- if super {
- reply.ZoneList = selectUserZoneAll(loginUserZoneReply.List)
- return nil
- }
- // 在登录用户的区域中,勾选指定用户的区域
- reply.ZoneList = selectUserZone(zones, loginUserZoneReply.List)
- return nil
- }
- func UserInfo(ctx context.Context, req *pb_v1.UserInfoRequest)(reply *pb_v1.UserInfoReply, err error) {
- reply = &pb_v1.UserInfoReply{}
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- dbname := utils.GetDbName(req.OrganizationCode)
- loginUserInfo, err := common.GetUserBaseInfo(req.Uid, dbname)
- if err != nil {
- return nil, err
- }
- targetUserInfo, err := common.GetUserBaseInfo(req.Id, dbname)
- if err != nil {
- return nil, err
- }
- err = UserPermissionCheck(loginUserInfo, targetUserInfo, dbname)
- if err != nil {
- return nil, err
- }
- if err = getUserBaseInfo(targetUserInfo, reply); err != nil {
- return nil, err
- }
- if err = getUserRbacInfo(req, reply, dbname, reply.GroupId); err != nil {
- return nil, err
- }
- if err = getUserZoneInfo(req, reply, dbname); err != nil {
- return nil, err
- }
- return reply, nil
- }
|