123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "time"
- "xingjia-management-gateway/apis"
- "xingjia-management-gateway/consts"
- "xingjia-management-gateway/errors"
- "xingjia-management-gateway/impl/v1/operation_log"
- dbmodel "xingjia-management-gateway/model"
- )
- func checkUserUpdateParam(req *apis.UserUpdateRequest) error {
- switch {
- case req.UpdateId == 0:
- return status.Error(10003, "未输入目标")
- case req.SelfId == 0:
- return status.Error(10003, "未知操作者")
- case req.UserType > 0 && req.UserType != consts.UserTypeNormal && req.UserType != consts.UserTypeTemp:
- return status.Error(10003, "不支持的账号类型")
- case req.UserType > 0 && req.UserType == consts.UserTypeTemp && (req.EffectiveStart < 1 || req.EffectiveEnd < 1 || req.EffectiveStart > req.EffectiveEnd):
- return status.Error(10003, "临时账号请输入有效时间")
- }
- return nil
- }
- //
- func UserUpdate(ctx context.Context, req *apis.UserUpdateRequest) (reply *apis.UserUpdateReply, err error) {
- reply = &apis.UserUpdateReply{}
- // 捕获各个task中的异常并返回给调用者
- 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"))
- }
- }
- }()
- if err = checkUserUpdateParam(req); err != nil {
- return nil, err
- }
- if err = checkAdmin(req.SelfId); err != nil {
- return nil, err
- }
- old := &dbmodel.TUser{}
- where := map[string]interface{}{
- "id": req.UpdateId,
- }
- err = old.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if old.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if old.UserType == consts.UserTypeSuper {
- return nil, status.Error(10003, "超级管理员类型不能更改")
- }
- values := map[string]interface{}{}
- now := time.Now()
- if req.UserType == consts.UserTypeNormal && old.UserType != req.UserType {
- values["effective_end"] = 0
- values["user_type"] = req.UserType
- }
- if req.UserType == consts.UserTypeTemp && (req.EffectiveStart != old.EffectiveStart || req.EffectiveEnd != old.EffectiveEnd) {
- values["effective_start"] = req.EffectiveStart
- values["effective_end"] = req.EffectiveEnd
- }
- if req.User != "" && req.User != old.User {
- values["user"] = req.User
- }
- if req.RealName != "" && req.RealName != old.RealName {
- values["real_name"] = req.RealName
- }
- if req.Password != "" {
- passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
- values["password"] = passwd
- }
- if len(values) == 0 {
- return reply, nil
- }
- values["updated_at"] = now
- db := database.DB().Begin()
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- p := dbmodel.TUser{}
- err = p.Update(db, where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- err = p.Find(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- // 操作日志
- content := getUserContent(*old)
- tcontent := getUserContent(p)
- logReq := &apis.OperationLogAddRequest{
- OriginContent: content,
- TargetContent: tcontent,
- Uid: req.SelfId,
- User: req.SelfName,
- Module: consts.OperationModuleAccount,
- OperationTime: time.Now().Unix(),
- Operation: consts.OperationActionUserResetPassword,
- }
- err = operation_log.OperationLogAddHandle(logReq, db)
- if err != nil {
- return nil, err
- }
- db.Commit()
- return reply, nil
- }
|