|
|
@@ -31,34 +31,36 @@ func NewUserService(ctx context.Context) (svc *UserService, err error) {
|
|
|
return nil, err
|
|
|
}
|
|
|
svc.Dao = dao.NewSysUserDao(svc.Tenant)
|
|
|
- //svc.userRoleDao = dao.NewSysUserRoleDao(svc.Tenant)
|
|
|
- //svc.userPostDao = dao.NewSysUserPostDao(svc.Tenant)
|
|
|
- //svc.userGroupDao = dao.NewSysUserGroupDao(svc.Tenant)
|
|
|
+ svc.userRoleDao = dao.NewSysUserRoleDao(svc.Tenant)
|
|
|
+ svc.userPostDao = dao.NewSysUserPostDao(svc.Tenant)
|
|
|
+ svc.userGroupDao = dao.NewSysUserGroupDao(svc.Tenant)
|
|
|
return svc, nil
|
|
|
}
|
|
|
|
|
|
// Login 用户登录,成功返回用户UUID,否则返回空字符串;
|
|
|
func (s *UserService) Login(username, password string) (*request.UserInfo, error) {
|
|
|
- record, err := s.Dao.Where("user_name", username).Where("status='10'").FindOne()
|
|
|
+ sysUserInfo, err := s.Dao.Where(s.Dao.C.UserName, username).Where(s.Dao.C.Status, "10").WhereGT(s.Dao.C.AllowErrorNum, 0).FindOne()
|
|
|
if err != nil {
|
|
|
return nil, myerrors.TipsError("系统异常")
|
|
|
}
|
|
|
- if record == nil {
|
|
|
- return nil, myerrors.TipsError("账号或密码错误,或限制登录")
|
|
|
+ if sysUserInfo == nil {
|
|
|
+ return nil, myerrors.TipsError("账号或密码错误,或已限制登录")
|
|
|
}
|
|
|
// 验证密码
|
|
|
- if utils.EncryptPassword(password, record.UserSalt) != record.Password {
|
|
|
+ if utils.EncryptPassword(password, sysUserInfo.UserSalt) != sysUserInfo.Password {
|
|
|
+ s.Dao.WherePri(sysUserInfo.Id).Decrement(s.Dao.C.AllowErrorNum, 1)
|
|
|
return nil, myerrors.TipsError("账号密码错误")
|
|
|
}
|
|
|
//账号状态
|
|
|
- if record.Status == "20" {
|
|
|
+ if sysUserInfo.Status == "20" {
|
|
|
return nil, myerrors.TipsError("账号已被冻结")
|
|
|
}
|
|
|
|
|
|
userInfo := new(request.UserInfo)
|
|
|
- if err = gconv.Struct(record, userInfo); err != nil {
|
|
|
+ if err = gconv.Struct(sysUserInfo, userInfo); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ userInfo.IsFirstLogin = sysUserInfo.IsFirstLogin == "10"
|
|
|
// 权限
|
|
|
userInfo.Roles, userInfo.Posts, userInfo.Groups, err = s.GetUserPermission(userInfo.Id)
|
|
|
if err != nil {
|
|
|
@@ -71,6 +73,15 @@ func (s *UserService) Login(username, password string) (*request.UserInfo, error
|
|
|
g.Log().Error(err)
|
|
|
return nil, myerrors.TipsError("获取用户数据权限失败")
|
|
|
}
|
|
|
+
|
|
|
+ // 更新允许登录错误次数和是否首次登录
|
|
|
+ if sysUserInfo.AllowErrorNum != 5 {
|
|
|
+ data := g.Map{
|
|
|
+ s.Dao.C.AllowErrorNum: 5,
|
|
|
+ }
|
|
|
+ s.Dao.WherePri(sysUserInfo.Id).Data(data).Update()
|
|
|
+ }
|
|
|
+
|
|
|
return userInfo, nil
|
|
|
}
|
|
|
|
|
|
@@ -279,9 +290,10 @@ func (s *UserService) CreateUser(req *model.AddUserReq) (err error) {
|
|
|
tx.Rollback()
|
|
|
return
|
|
|
}
|
|
|
+ userData.IsFirstLogin = "10"
|
|
|
+ userData.AllowErrorNum = 5
|
|
|
userData.UserSalt = grand.S(10)
|
|
|
userData.Password = utils.EncryptPassword(userData.Password, userData.UserSalt)
|
|
|
- g.Log("xxxxpassword", userData.Password)
|
|
|
SetCreatedInfo(userData, s.GetCxtUserId(), s.GetCxtUserName())
|
|
|
res, err := Model.Insert(userData)
|
|
|
if err != nil {
|
|
|
@@ -449,15 +461,17 @@ func (s *UserService) UpdateUser(req *model.EditUserReq) (err error) {
|
|
|
}
|
|
|
|
|
|
// ResetUserPwd 重置用户密码
|
|
|
-func (s *UserService) ResetUserPwd(req *model.SysUserResetPwdReq) error {
|
|
|
+func (s *UserService) ResetUserPwd(req *model.SysResetPwdReq) error {
|
|
|
salt := grand.S(10)
|
|
|
password := utils.EncryptPassword(req.Password, salt)
|
|
|
data := g.Map{
|
|
|
- s.Dao.C.UserSalt: salt,
|
|
|
- s.Dao.C.Password: password,
|
|
|
+ s.Dao.C.UserSalt: salt,
|
|
|
+ s.Dao.C.Password: password,
|
|
|
+ s.Dao.C.IsFirstLogin: "10",
|
|
|
+ s.Dao.C.AllowErrorNum: 5,
|
|
|
}
|
|
|
SetCurrentUpdatedInfo(data, s.CxtUser)
|
|
|
- _, err := s.Dao.WherePri(req.Id).Update()
|
|
|
+ _, err := s.Dao.WherePri(req.Id).Data(data).Update()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
@@ -466,7 +480,7 @@ func (s *UserService) ChangeUserStatus(req *model.SysUserStatusReq) error {
|
|
|
s.Dao.C.Status: req.UserStatus,
|
|
|
}
|
|
|
SetCurrentUpdatedInfo(data, s.CxtUser)
|
|
|
- _, err := s.Dao.WherePri(req.Id).Update()
|
|
|
+ _, err := s.Dao.WherePri(req.Id).Data(data).Update()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
@@ -475,7 +489,7 @@ func (s *UserService) DeleteUserByIds(ctx context.Context, ids []int64) error {
|
|
|
return g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
|
|
_, err := s.Dao.Ctx(ctx).TX(tx).Where(s.Dao.C.Id+" in(?)", ids).Delete()
|
|
|
//删除用户对应的岗位
|
|
|
- _, err = dao.SysUserPost.Ctx(ctx).TX(tx).Delete(dao.SysUserPost.C.UserId+" in (?)", ids)
|
|
|
+ //_, err = dao.SysUserPost.Ctx(ctx).TX(tx).Delete(dao.SysUserPost.C.UserId+" in (?)", ids)
|
|
|
return err
|
|
|
})
|
|
|
}
|
|
|
@@ -502,14 +516,20 @@ func (s *UserService) ProfileUpdatePwd(req *model.ProfileUpdatePwdReq) error {
|
|
|
}
|
|
|
oldPassword := utils.EncryptPassword(req.OldPassword, userInfo.UserSalt)
|
|
|
if oldPassword != userInfo.Password {
|
|
|
- return errors.New("原始密码错误!")
|
|
|
+ return myerrors.TipsError("原始密码错误!")
|
|
|
}
|
|
|
salt := grand.S(10)
|
|
|
newPassword := utils.EncryptPassword(req.NewPassword, salt)
|
|
|
data := g.Map{
|
|
|
- s.Dao.C.UserSalt: salt,
|
|
|
- s.Dao.C.Password: newPassword,
|
|
|
+ s.Dao.C.UserSalt: salt,
|
|
|
+ s.Dao.C.Password: newPassword,
|
|
|
+ s.Dao.C.AllowErrorNum: 5,
|
|
|
+ }
|
|
|
+ // 更新是否首次登录
|
|
|
+ if userInfo.IsFirstLogin == "10" {
|
|
|
+ data[s.Dao.C.IsFirstLogin] = "20"
|
|
|
}
|
|
|
+
|
|
|
SetCurrentUpdatedInfo(data, s.CxtUser)
|
|
|
_, err = s.Dao.WherePri(req.UserId).Unscoped().Update(data)
|
|
|
return err
|