68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
commonConstants "be.ems/src/framework/constants/common"
|
|
tokenConstants "be.ems/src/framework/constants/token"
|
|
"be.ems/src/framework/i18n"
|
|
"be.ems/src/framework/reqctx"
|
|
"be.ems/src/framework/resp"
|
|
tokenUtils "be.ems/src/framework/utils/token"
|
|
"be.ems/src/framework/vo/result"
|
|
"be.ems/src/modules/common/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// LDAP LDAP认证登录
|
|
//
|
|
// POST /auth/ldap
|
|
//
|
|
// @Tags common/authorization
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Summary System Login
|
|
// @Description System Login
|
|
// @Router /auth/ldap [post]
|
|
func (s AccountController) LDAP(c *gin.Context) {
|
|
language := reqctx.AcceptLanguage(c)
|
|
var body model.LoginSourceBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
|
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
|
return
|
|
}
|
|
|
|
// 当前请求信息
|
|
ipaddr, location := reqctx.IPAddrLocation(c)
|
|
os, browser := reqctx.UaOsBrowser(c)
|
|
|
|
// 登录用户信息
|
|
loginUser, err := s.accountService.ByLDAP(body)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
|
return
|
|
}
|
|
|
|
// 生成令牌,创建系统访问记录
|
|
tokenStr := tokenUtils.Create(&loginUser, ipaddr, location, os, browser)
|
|
if tokenStr == "" {
|
|
c.JSON(200, result.Err(nil))
|
|
return
|
|
} else {
|
|
s.accountService.UpdateLoginDateAndIP(&loginUser)
|
|
// 登录成功
|
|
s.sysLogLoginService.CreateSysLogLogin(
|
|
body.Username, commonConstants.STATUS_YES, "app.common.loginSuccess",
|
|
ipaddr, location, os, browser,
|
|
)
|
|
}
|
|
|
|
c.JSON(200, result.OkData(map[string]any{
|
|
tokenConstants.RESPONSE_FIELD: tokenStr,
|
|
}))
|
|
}
|