|
@@ -7,6 +7,7 @@ import (
|
|
|
"eta/eta_api/models/system"
|
|
|
"eta/eta_api/utils"
|
|
|
"fmt"
|
|
|
+ "github.com/go-ldap/ldap"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -14,6 +15,19 @@ import (
|
|
|
|
|
|
// SendAdminMobileVerifyCode 发送用户手机验证码
|
|
|
func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("SendAdminMobileVerifyCode ErrMsg: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ fmt.Println(tips)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ smsClient, e := NewSmsClient()
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("NewSmsClient err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
verifyCode := utils.GetRandDigit(6)
|
|
|
record := new(system.AdminVerifyCodeRecord)
|
|
|
record.VerifyType = system.AdminVerifyCodeRecordTypeMobile
|
|
@@ -27,49 +41,24 @@ func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, er
|
|
|
err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
|
|
|
return
|
|
|
}
|
|
|
- // 获取配置好的短信模版
|
|
|
- smsCond := ` AND conf_key in (?,?) `
|
|
|
- smsPars := make([]interface{}, 0)
|
|
|
- smsPars = append(smsPars, "LoginSmsTpId", "LoginSmsGjTpId")
|
|
|
- conf := new(models.BusinessConf)
|
|
|
- confList, e := conf.GetItemsByCondition(smsCond, smsPars, []string{"conf_key", "conf_val"}, "")
|
|
|
+
|
|
|
+ var smsReq UserLoginSmsCodeReq
|
|
|
+ smsReq.Mobile = mobile
|
|
|
+ smsReq.TelAreaCode = areaCode
|
|
|
+ smsReq.VerifyCode = verifyCode
|
|
|
+ smsResult, e := smsClient.SendUserLoginCode(smsReq)
|
|
|
if e != nil {
|
|
|
- if e.Error() == utils.ErrNoRow() {
|
|
|
- err = fmt.Errorf("请先配置短信模版")
|
|
|
- return
|
|
|
- }
|
|
|
- err = fmt.Errorf("获取短信模版失败, Err: %s", e.Error())
|
|
|
+ err = fmt.Errorf("SendUserLoginCode err: %s", e.Error())
|
|
|
return
|
|
|
}
|
|
|
+ ok = smsResult.Success
|
|
|
|
|
|
- tplId := ""
|
|
|
- gjTplId := ""
|
|
|
- for _, v := range confList {
|
|
|
- if v.ConfKey == "LoginSmsTpId" {
|
|
|
- tplId = v.ConfVal
|
|
|
- } else if v.ConfKey == "LoginSmsGjTpId" {
|
|
|
- gjTplId = v.ConfVal
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if tplId == "" {
|
|
|
- err = fmt.Errorf("请先配置短信模版")
|
|
|
- return
|
|
|
- }
|
|
|
- if areaCode == "86" {
|
|
|
- ok = SendSmsCode(mobile, verifyCode, tplId)
|
|
|
- } else {
|
|
|
- if gjTplId == "" {
|
|
|
- err = fmt.Errorf("请先配置国际短信模版")
|
|
|
- return
|
|
|
- }
|
|
|
- ok = SendSmsCodeGj(mobile, verifyCode, areaCode, gjTplId)
|
|
|
- }
|
|
|
record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
|
|
|
if !ok {
|
|
|
record.SendStatus = system.AdminVerifyCodeRecordStatusFail
|
|
|
}
|
|
|
- cols := []string{"SendStatus"}
|
|
|
+ record.RequestId = smsResult.RequestId
|
|
|
+ cols := []string{"SendStatus", "RequestId"}
|
|
|
if e := record.Update(cols); e != nil {
|
|
|
err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
|
|
|
}
|
|
@@ -78,7 +67,37 @@ func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, er
|
|
|
|
|
|
// SendAdminEmailVerifyCode 发送用户邮箱验证码
|
|
|
func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("SendAdminEmailVerifyCode ErrMsg: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ fmt.Println(tips)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ // 读取配置
|
|
|
+ confMap, e := models.GetBusinessConf()
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ subjectConf := confMap[models.BusinessConfLoginEmailTemplateSubject]
|
|
|
+ contentConf := confMap[models.BusinessConfLoginEmailTemplateContent]
|
|
|
+ if subjectConf == "" {
|
|
|
+ err = fmt.Errorf("请先配置邮件模版主题")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if contentConf == "" {
|
|
|
+ err = fmt.Errorf("请先配置邮件模版内容")
|
|
|
+ return
|
|
|
+ }
|
|
|
verifyCode := utils.GetRandDigit(6)
|
|
|
+ t := time.Now().Format("2006年01月02日")
|
|
|
+ emailContent := contentConf
|
|
|
+ emailContent = strings.Replace(emailContent, "{{VERIFY_CODE}}", verifyCode, 1)
|
|
|
+ emailContent = strings.Replace(emailContent, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
|
|
|
+ emailContent = strings.Replace(emailContent, "{{DATE_TIME}}", t, 1)
|
|
|
+
|
|
|
+ // 验证码记录
|
|
|
record := new(system.AdminVerifyCodeRecord)
|
|
|
record.VerifyType = system.AdminVerifyCodeRecordTypeEmail
|
|
|
record.Email = email
|
|
@@ -92,84 +111,131 @@ func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 获取邮件配置
|
|
|
- authKey := "english_report_email_conf"
|
|
|
- emailConf, e := company.GetConfigDetailByCode(authKey)
|
|
|
- if e != nil {
|
|
|
- err = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
|
|
|
- return
|
|
|
+ var result string
|
|
|
+ if confMap[models.BusinessConfEmailClient] == models.BusinessConfEmailClientSmtp {
|
|
|
+ // 普通邮箱
|
|
|
+ var emailReq SendEmailReq
|
|
|
+ emailReq.Title = subjectConf
|
|
|
+ emailReq.Content = emailContent
|
|
|
+ emailReq.ToUser = append(emailReq.ToUser, email)
|
|
|
+ ok, e = SendEmail(emailReq)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 默认阿里云邮箱
|
|
|
+ // 读取发信人昵称配置...后面可以优化一下
|
|
|
+ authKey := "english_report_email_conf"
|
|
|
+ emailConf, e := company.GetConfigDetailByCode(authKey)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if emailConf.ConfigValue == "" {
|
|
|
+ err = fmt.Errorf("邮件配置为空, 不可推送")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ conf := new(models.EnglishReportEmailConf)
|
|
|
+ if e = json.Unmarshal([]byte(emailConf.ConfigValue), &conf); e != nil {
|
|
|
+ err = fmt.Errorf("邮件配置有误, 不可推送")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ req := new(EnglishReportSendEmailRequest)
|
|
|
+ req.Subject = subjectConf
|
|
|
+ req.Email = email
|
|
|
+ req.FromAlias = conf.FromAlias // 发信人昵称
|
|
|
+ req.HtmlBody = emailContent
|
|
|
+
|
|
|
+ aliEmail := new(AliyunEmail)
|
|
|
+ o, r, e := aliEmail.SendEmail(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("阿里云邮箱推送失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ok = o
|
|
|
+ result = r
|
|
|
}
|
|
|
- if emailConf.ConfigValue == "" {
|
|
|
- err = fmt.Errorf("邮件配置为空, 不可推送")
|
|
|
- return
|
|
|
+
|
|
|
+ record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
|
|
|
+ if !ok {
|
|
|
+ record.SendStatus = system.AdminVerifyCodeRecordStatusFail
|
|
|
}
|
|
|
- conf := new(models.EnglishReportEmailConf)
|
|
|
- if e = json.Unmarshal([]byte(emailConf.ConfigValue), &conf); e != nil {
|
|
|
- err = fmt.Errorf("邮件配置有误, 不可推送")
|
|
|
- return
|
|
|
+ record.SendResult = result
|
|
|
+ cols := []string{"SendStatus", "SendResult"}
|
|
|
+ if e = record.Update(cols); e != nil {
|
|
|
+ err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
|
|
|
}
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
- // 获取邮箱模板
|
|
|
- // 获取配置好的短信模版
|
|
|
- cond := ` AND (conf_key = ? OR conf_key = ?)`
|
|
|
- pars := make([]interface{}, 0)
|
|
|
- pars = append(pars, "LoginEmailTemplateSubject", "LoginEmailTemplateContent")
|
|
|
- busiConf := new(models.BusinessConf)
|
|
|
- emailConfList, e := busiConf.GetItemsByCondition(cond, pars, []string{"conf_key, conf_val"}, "")
|
|
|
- if e != nil {
|
|
|
- if e.Error() == utils.ErrNoRow() {
|
|
|
- err = fmt.Errorf("请先配置邮件模版")
|
|
|
- return
|
|
|
+// LdapUserCheck AD域用户校验
|
|
|
+func LdapUserCheck(userName, password string) (pass bool, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("LdapUserCheck ErrMsg: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ fmt.Println(tips)
|
|
|
}
|
|
|
- err = fmt.Errorf("获取邮件模版失败, Err: %s", e.Error())
|
|
|
+ }()
|
|
|
+ if userName == "" || password == "" {
|
|
|
+ err = fmt.Errorf("账号密码有误")
|
|
|
return
|
|
|
}
|
|
|
- var emaiContent, emailSubject string
|
|
|
- for _, v := range emailConfList {
|
|
|
- if v.ConfKey == "LoginEmailTemplateContent" {
|
|
|
- emaiContent = v.ConfVal
|
|
|
- } else if v.ConfKey == "LoginEmailTemplateSubject" {
|
|
|
- emailSubject = v.ConfVal
|
|
|
- }
|
|
|
+ confMap, e := models.GetBusinessConf()
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- if emailSubject == "" {
|
|
|
- err = fmt.Errorf("请先配置邮件模版主题")
|
|
|
+ if confMap[models.BusinessConfLdapHost] == "" || confMap[models.BusinessConfLdapBase] == "" {
|
|
|
+ err = fmt.Errorf("AD域配置有误")
|
|
|
return
|
|
|
}
|
|
|
- if emaiContent == "" {
|
|
|
- err = fmt.Errorf("请先配置邮件模版内容")
|
|
|
+ ldapPort, _ := strconv.Atoi(confMap[models.BusinessConfLdapPort])
|
|
|
+ if ldapPort <= 0 {
|
|
|
+ err = fmt.Errorf("AD域端口号有误, Port: %d", ldapPort)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- req := new(EnglishReportSendEmailRequest)
|
|
|
- req.Subject = emailSubject
|
|
|
- req.Email = email
|
|
|
- // todo 发信人昵称
|
|
|
- req.FromAlias = conf.FromAlias
|
|
|
- // 填充模板
|
|
|
- t := time.Now().Format("2006年01月02日")
|
|
|
- ct := emaiContent
|
|
|
- ct = strings.Replace(ct, "{{VERIFY_CODE}}", verifyCode, 1)
|
|
|
- ct = strings.Replace(ct, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
|
|
|
- ct = strings.Replace(ct, "{{DATE_TIME}}", t, 1)
|
|
|
- req.HtmlBody = ct
|
|
|
-
|
|
|
- aliEmail := new(AliyunEmail)
|
|
|
- o, result, e := aliEmail.SendEmail(req)
|
|
|
+ // 连接ldap
|
|
|
+ addr := fmt.Sprintf("%s:%d", confMap[models.BusinessConfLdapHost], ldapPort)
|
|
|
+ conn, e := ldap.Dial("tcp", addr)
|
|
|
if e != nil {
|
|
|
- err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
|
|
|
+ err = fmt.Errorf("ldap Dial err: %s", e.Error())
|
|
|
return
|
|
|
}
|
|
|
- ok = o
|
|
|
+ defer conn.Close()
|
|
|
|
|
|
- record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
|
|
|
- if !ok {
|
|
|
- record.SendStatus = system.AdminVerifyCodeRecordStatusFail
|
|
|
+ // 绑定用户
|
|
|
+ bindUserName := fmt.Sprintf("%s%s", userName, confMap[models.BusinessConfLdapBindUserSuffix])
|
|
|
+ if e = conn.Bind(bindUserName, password); e != nil {
|
|
|
+ err = fmt.Errorf("ldap Bind err: %s", e.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- record.SendResult = result
|
|
|
- cols := []string{"SendStatus", "SendResult"}
|
|
|
- if e = record.Update(cols); e != nil {
|
|
|
- err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
|
|
|
+
|
|
|
+ // 鉴权操作
|
|
|
+ searchRequest := ldap.NewSearchRequest(
|
|
|
+ confMap[models.BusinessConfLdapBase],
|
|
|
+ ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
+ fmt.Sprintf(confMap[models.BusinessConfLdapUserFilter], userName),
|
|
|
+ []string{"dn"},
|
|
|
+ nil,
|
|
|
+ )
|
|
|
+ //b, _ := json.Marshal(searchRequest)
|
|
|
+ //fmt.Println("searchRequest: ", string(b))
|
|
|
+
|
|
|
+ sr, e := conn.Search(searchRequest)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ldap Search err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证结果
|
|
|
+ if len(sr.Entries) != 1 {
|
|
|
+ utils.FileLog.Info("ldap check fail: user does not exist or too many entries returned")
|
|
|
+ return
|
|
|
}
|
|
|
+ pass = true
|
|
|
return
|
|
|
}
|