2023-11-20 03:54:30 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-23 03:19:11 +00:00
|
|
|
|
"fusenapi/model/gmodel"
|
2023-11-20 03:54:30 +00:00
|
|
|
|
"fusenapi/utils/basic"
|
|
|
|
|
"fusenapi/utils/email"
|
2023-11-21 10:10:30 +00:00
|
|
|
|
"net/http"
|
2023-11-20 03:54:30 +00:00
|
|
|
|
"strings"
|
2023-11-23 03:19:11 +00:00
|
|
|
|
"time"
|
2023-11-20 03:54:30 +00:00
|
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"fusenapi/server/ldap-admin/internal/svc"
|
|
|
|
|
"fusenapi/server/ldap-admin/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UpdateLdapUserPwdLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUpdateLdapUserPwdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLdapUserPwdLogic {
|
|
|
|
|
return &UpdateLdapUserPwdLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
|
// func (l *UpdateLdapUserPwdLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// }
|
|
|
|
|
|
2023-11-21 10:10:30 +00:00
|
|
|
|
func (l *UpdateLdapUserPwdLogic) UpdateLdapUserPwd(req *types.UpdateLdapUserPwdReq, r *http.Request) (resp *basic.Response) {
|
2023-11-22 02:12:46 +00:00
|
|
|
|
|
2023-11-22 02:19:27 +00:00
|
|
|
|
if !l.svcCtx.Ldap.VerifyAuthority(r) {
|
2023-11-21 10:10:30 +00:00
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeUnAuth, "无权限,请联系管理员开通")
|
|
|
|
|
}
|
2023-11-20 03:54:30 +00:00
|
|
|
|
req.UserDN = strings.Trim(req.UserDN, " ")
|
|
|
|
|
req.NewPassword = strings.Trim(req.NewPassword, " ")
|
|
|
|
|
req.OldPassword = strings.Trim(req.OldPassword, " ")
|
|
|
|
|
if req.NewPassword == "" || req.OldPassword == "" {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "新/旧密码都不允许为空")
|
|
|
|
|
}
|
|
|
|
|
if len(req.UserDN) <= 3 || req.UserDN[:3] != "cn=" {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,无效的用户DN")
|
|
|
|
|
}
|
|
|
|
|
cnEmail := strings.Split(req.UserDN, ",")[0][3:]
|
|
|
|
|
if !email.IsEmailValid(cnEmail) {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "错误的用户cn")
|
|
|
|
|
}
|
|
|
|
|
//查询个人信息
|
2023-11-22 02:12:46 +00:00
|
|
|
|
user, err := l.svcCtx.Ldap.GetLdapUserInfo(req.UserDN)
|
2023-11-20 03:54:30 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
|
|
|
|
}
|
2023-11-22 02:47:19 +00:00
|
|
|
|
if user.Password != req.OldPassword {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "旧密码不对,请重新尝试")
|
2023-11-20 03:54:30 +00:00
|
|
|
|
}
|
2023-11-22 02:12:46 +00:00
|
|
|
|
err = l.svcCtx.Ldap.Update(req.UserDN, map[string][]string{
|
2023-11-22 02:47:19 +00:00
|
|
|
|
"userPassword": {req.NewPassword},
|
2023-11-20 03:54:30 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "修改密码失败")
|
|
|
|
|
}
|
2023-11-23 03:19:11 +00:00
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
err = l.svcCtx.AllModels.LdapUser.Update(l.ctx, req.UserDN, &gmodel.LdapUser{
|
|
|
|
|
Utime: &now,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
}
|
2023-11-20 03:54:30 +00:00
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "修改密码成功")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
|
// func (l *UpdateLdapUserPwdLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
|
// }
|