feat: 新增第三方登录认证和管理
This commit is contained in:
163
src/modules/system/controller/sys_login_source.go
Normal file
163
src/modules/system/controller/sys_login_source.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/service"
|
||||
)
|
||||
|
||||
// NewLoginSource 实例化控制层
|
||||
var NewSysLoginSource = &SysLoginSourceController{
|
||||
sysLoginSourceService: service.NewSysLoginSource,
|
||||
}
|
||||
|
||||
// SysLoginSourceController 认证源管理 控制层处理
|
||||
//
|
||||
// PATH /sys/login-source
|
||||
type SysLoginSourceController struct {
|
||||
sysLoginSourceService *service.SysLoginSource // 认证源信息服务
|
||||
}
|
||||
|
||||
// List 列表
|
||||
//
|
||||
// GET /list
|
||||
func (s SysLoginSourceController) List(c *gin.Context) {
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysLoginSourceService.FindByPage(query)
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// Info 信息
|
||||
//
|
||||
// GET /:id
|
||||
func (s SysLoginSourceController) Info(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
info := s.sysLoginSourceService.FindById(parse.Number(id))
|
||||
if info.Id == parse.Number(id) {
|
||||
c.JSON(200, resp.OkData(info))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.ErrMsg("id does not exist"))
|
||||
}
|
||||
|
||||
// Add 新增
|
||||
//
|
||||
// POST /
|
||||
func (s SysLoginSourceController) Add(c *gin.Context) {
|
||||
var body model.SysLoginSource
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.Id > 0 {
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id not is empty"))
|
||||
return
|
||||
}
|
||||
if len(body.Config) < 7 || !json.Valid([]byte(body.Config)) {
|
||||
c.JSON(200, resp.ErrMsg("config json format error"))
|
||||
return
|
||||
}
|
||||
configStr, err := s.sysLoginSourceService.CheckConfigJSON(body.Type, body.Config)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
body.Config = configStr
|
||||
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysLoginSourceService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// Edit 更新
|
||||
//
|
||||
// PUT /
|
||||
func (s SysLoginSourceController) Edit(c *gin.Context) {
|
||||
var body model.SysLoginSource
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.Id <= 0 {
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
if len(body.Config) < 7 || !json.Valid([]byte(body.Config)) {
|
||||
c.JSON(200, resp.ErrMsg("config json format error"))
|
||||
return
|
||||
}
|
||||
configStr, err := s.sysLoginSourceService.CheckConfigJSON(body.Type, body.Config)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
body.Config = configStr
|
||||
|
||||
// 查询信息
|
||||
info := s.sysLoginSourceService.FindById(body.Id)
|
||||
if info.Id != body.Id {
|
||||
c.JSON(200, resp.ErrMsg("modification failed, data not exist"))
|
||||
return
|
||||
}
|
||||
|
||||
info.Type = body.Type
|
||||
info.Name = body.Name
|
||||
info.Icon = body.Icon
|
||||
info.Config = body.Config
|
||||
info.ActiveFlag = body.ActiveFlag
|
||||
info.Remark = body.Remark
|
||||
info.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rowsAffected := s.sysLoginSourceService.Update(info)
|
||||
if rowsAffected > 0 {
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// Remove 删除
|
||||
//
|
||||
// DELETE /:id
|
||||
func (s SysLoginSourceController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
|
||||
rows, err := s.sysLoginSourceService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
@@ -550,15 +550,19 @@ func (s *SysUserController) Export(c *gin.Context) {
|
||||
"C1": i18n.TKey(language, "user.export.nick"),
|
||||
"D1": i18n.TKey(language, "user.export.role"),
|
||||
"E1": i18n.TKey(language, "user.export.deptName"),
|
||||
"F1": i18n.TKey(language, "user.export.loginIP"),
|
||||
"G1": i18n.TKey(language, "user.export.loginDate"),
|
||||
"H1": i18n.TKey(language, "user.export.status"),
|
||||
"F1": i18n.TKey(language, "user.export.userType"),
|
||||
"G1": i18n.TKey(language, "user.export.loginIP"),
|
||||
"H1": i18n.TKey(language, "user.export.loginDate"),
|
||||
"I1": i18n.TKey(language, "user.export.status"),
|
||||
// "F1": i18n.TKey(language, "user.export.sex"),
|
||||
// "E1": i18n.TKey(language, "user.export.phone"),
|
||||
// "D1": i18n.TKey(language, "user.export.email"),
|
||||
// "I1": i18n.TKey(language, "user.export.deptID"),
|
||||
// "K1": i18n.TKey(language, "user.export.deptLeader"),
|
||||
}
|
||||
// 读取用户性别字典数据
|
||||
dictSysUserType := service.NewSysDictType.FindDataByType("sys_user_type")
|
||||
|
||||
// 读取用户性别字典数据
|
||||
// dictSysUserSex := s.sysDictDataService.SelectDictDataByType("sys_user_sex")
|
||||
// 从第二行开始的数据
|
||||
@@ -573,6 +577,14 @@ func (s *SysUserController) Export(c *gin.Context) {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// 用户类型
|
||||
userType := row.UserType
|
||||
for _, v := range dictSysUserType {
|
||||
if row.UserType == v.DataValue && row.UserSource != "#" {
|
||||
userType = i18n.TKey(language, v.DataLabel) + " " + row.UserSource
|
||||
break
|
||||
}
|
||||
}
|
||||
// 帐号状态
|
||||
statusValue := i18n.TKey(language, "dictData.disable")
|
||||
if row.StatusFlag == "1" {
|
||||
@@ -581,7 +593,11 @@ func (s *SysUserController) Export(c *gin.Context) {
|
||||
// 用户角色, 默认导出首个
|
||||
userRole := ""
|
||||
if len(row.Roles) > 0 {
|
||||
userRole = i18n.TKey(language, row.Roles[0].RoleName)
|
||||
arr := make([]string, 0)
|
||||
for _, v := range row.Roles {
|
||||
arr = append(arr, i18n.TKey(language, v.RoleName))
|
||||
}
|
||||
userRole = strings.Join(arr, ",")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.UserId,
|
||||
@@ -589,9 +605,10 @@ func (s *SysUserController) Export(c *gin.Context) {
|
||||
"C" + idx: row.NickName,
|
||||
"D" + idx: userRole,
|
||||
"E" + idx: row.Dept.DeptName,
|
||||
"F" + idx: row.LoginIp,
|
||||
"G" + idx: date.ParseDateToStr(row.LoginTime, date.YYYY_MM_DDTHH_MM_SSZ),
|
||||
"H" + idx: statusValue,
|
||||
"F" + idx: userType,
|
||||
"G" + idx: row.LoginIp,
|
||||
"H" + idx: date.ParseDateToStr(row.LoginTime, date.YYYY_MM_DDTHH_MM_SSZ),
|
||||
"I" + idx: statusValue,
|
||||
// "E" + idx: row.PhoneNumber,
|
||||
// "F" + idx: sysUserSex,
|
||||
// "D" + idx: row.Email,
|
||||
@@ -725,7 +742,7 @@ func (s *SysUserController) Import(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证是否存在这个用户
|
||||
newSysUser := s.sysUserService.FindByUserName(row["B"])
|
||||
newSysUser := s.sysUserService.FindByUserName(row["B"], "System", "#")
|
||||
newSysUser.Password = initPassword
|
||||
newSysUser.UserName = row["B"]
|
||||
newSysUser.NickName = row["C"]
|
||||
|
||||
22
src/modules/system/model/sys_login_source.go
Normal file
22
src/modules/system/model/sys_login_source.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package model
|
||||
|
||||
// SysLoginSource 系统第三方认证源 sys_login_source
|
||||
type SysLoginSource struct {
|
||||
Id int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"` // ID
|
||||
UID string `gorm:"column:uid" json:"uid"` // UID 16位长度字符串
|
||||
Type string `gorm:"column:type" json:"type" binding:"required,oneof=LDAP SMTP OAuth2"` // 认证类型 LDAP SMTP OAuth2
|
||||
Name string `gorm:"column:name" json:"name" binding:"required"` // 认证名称
|
||||
Icon string `gorm:"column:icon" json:"icon"` // 图标
|
||||
ActiveFlag string `gorm:"column:active_flag" json:"activeFlag"` // 激活标记(0未激活 1激活)
|
||||
SyncFlag string `gorm:"column:sync_flag" json:"syncFlag"` // 同步标记(0未同步 1同步)
|
||||
Config string `gorm:"column:config" json:"config" binding:"required"` // 配置JSON字符串
|
||||
CreateBy string `gorm:"column:create_by" json:"createBy"` // 创建者
|
||||
CreateTime int64 `gorm:"column:create_time" json:"createTime"` // 创建时间
|
||||
UpdateBy string `gorm:"column:update_by" json:"updateBy"` // 更新者
|
||||
UpdateTime int64 `gorm:"column:update_time" json:"updateTime"` // 更新时间
|
||||
Remark string `gorm:"column:remark" json:"remark"` // 备注
|
||||
}
|
||||
|
||||
func (*SysLoginSource) TableName() string {
|
||||
return "sys_login_source"
|
||||
}
|
||||
@@ -11,6 +11,8 @@ type SysUser struct {
|
||||
Sex string `json:"sex" gorm:"column:sex"` // 用户性别(0未选择 1男 2女)
|
||||
Avatar string `json:"avatar" gorm:"column:avatar"` // 头像地址
|
||||
Password string `json:"-" gorm:"column:password"` // 密码
|
||||
UserType string `json:"userType" gorm:"column:user_type"` // 用户类型(System系统用户)
|
||||
UserSource string `json:"userSource" gorm:"column:user_source"` // 用户来源UID (系统#))
|
||||
StatusFlag string `json:"statusFlag" gorm:"column:status_flag"` // 账号状态(0停用 1正常)
|
||||
DelFlag string `json:"-" gorm:"column:del_flag"` // 删除标记(0存在 1删除)
|
||||
PasswordUpdateTime int64 `json:"passwordUpdateTime" gorm:"column:password_update_time"` // 密码更新时间
|
||||
|
||||
28
src/modules/system/model/vo/login_source.go
Normal file
28
src/modules/system/model/vo/login_source.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package vo
|
||||
|
||||
// SysLoginSourceLDAP LDAP认证源
|
||||
type SysLoginSourceLDAP struct {
|
||||
URL string `json:"url"` // LDAP 服务器(ldap://192.168.9.58:11389)
|
||||
BaseDN string `json:"baseDN"` // base DN(dc=example,dc=org)
|
||||
UserFilter string `json:"userFilter"` // 用户过滤规则((&(objectClass=organizationalPerson)(uid=%s)))
|
||||
BindDN string `json:"bindDN"` // 绑定 DN(cn=admin,dc=example,dc=org)
|
||||
BindPassword string `json:"bindPassword"` // 绑定密码(adminpassword)
|
||||
}
|
||||
|
||||
// SysLoginSourceSMTP SMTP认证源
|
||||
type SysLoginSourceSMTP struct {
|
||||
Host string `json:"host"` // SMTP 服务器(smtp.gmail.com)
|
||||
Port int `json:"port"` // SMTP 端口(587)
|
||||
}
|
||||
|
||||
// SysLoginSourceOAuth2 OAuth2认证源
|
||||
type SysLoginSourceOAuth2 struct {
|
||||
ClientID string `json:"clientID"` // 客户端ID
|
||||
ClientSecret string `json:"clientSecret"` // 客户端密钥
|
||||
AuthURL string `json:"authURL"` // 认证URL
|
||||
TokenURL string `json:"tokenURL"` // 令牌URL
|
||||
Scopes []string `json:"scopes"` // 授权范围
|
||||
RedirectURL string `json:"redirectURL"` // 重定向URL
|
||||
UserURL string `json:"userURL"` // 用户信息URL
|
||||
AccountField string `json:"accountField"` // 账号字段从用户信息中获取
|
||||
}
|
||||
150
src/modules/system/repository/sys_login_source.go
Normal file
150
src/modules/system/repository/sys_login_source.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/modules/system/model"
|
||||
)
|
||||
|
||||
// NewSysLoginSource 实例化数据层
|
||||
var NewSysLoginSource = &SysLoginSource{}
|
||||
|
||||
// SysLoginSource 认证源数据层处理
|
||||
type SysLoginSource struct{}
|
||||
|
||||
// SelectByPage 分页查询集合
|
||||
func (r SysLoginSource) SelectByPage(query map[string]string) ([]model.SysLoginSource, int64) {
|
||||
tx := db.DB("").Model(&model.SysLoginSource{})
|
||||
// 查询条件拼接
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
tx = tx.Where("name like ?", v+"%")
|
||||
}
|
||||
if v, ok := query["type"]; ok && v != "" {
|
||||
tx = tx.Where("type = ?", v)
|
||||
}
|
||||
if v, ok := query["beginTime"]; ok && v != "" {
|
||||
if len(v) == 10 {
|
||||
v = fmt.Sprintf("%s000", v)
|
||||
}
|
||||
tx = tx.Where("create_time >= ?", v)
|
||||
}
|
||||
if v, ok := query["endTime"]; ok && v != "" {
|
||||
if len(v) == 10 {
|
||||
v = fmt.Sprintf("%s999", v)
|
||||
}
|
||||
tx = tx.Where("create_time <= ?", v)
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.SysLoginSource{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
return rows, total
|
||||
}
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// Select 查询集合
|
||||
func (r SysLoginSource) Select(param model.SysLoginSource) []model.SysLoginSource {
|
||||
tx := db.DB("").Model(&model.SysLoginSource{})
|
||||
// 查询条件拼接
|
||||
if param.UID != "" {
|
||||
tx = tx.Where("uid = ?", param.UID)
|
||||
}
|
||||
if param.Type != "" {
|
||||
tx = tx.Where("type = ?", param.Type)
|
||||
}
|
||||
if param.Name != "" {
|
||||
tx = tx.Where("name = ?", param.Name)
|
||||
}
|
||||
if param.ActiveFlag != "" {
|
||||
tx = tx.Where("active_flag = ?", param.ActiveFlag)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
rows := []model.SysLoginSource{}
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询信息
|
||||
func (r SysLoginSource) SelectByIds(ids []int64) []model.SysLoginSource {
|
||||
rows := []model.SysLoginSource{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := db.DB("").Model(&model.SysLoginSource{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 新增信息 返回新增数据ID
|
||||
func (r SysLoginSource) Insert(param model.SysLoginSource) int64 {
|
||||
if param.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
param.UpdateBy = param.CreateBy
|
||||
param.UpdateTime = ms
|
||||
param.CreateTime = ms
|
||||
}
|
||||
// 执行插入
|
||||
if err := db.DB("").Create(¶m).Error; err != nil {
|
||||
logger.Errorf("insert err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return param.Id
|
||||
}
|
||||
|
||||
// Update 修改信息 返回受影响行数
|
||||
func (r SysLoginSource) Update(param model.SysLoginSource) int64 {
|
||||
if param.Id <= 0 {
|
||||
return 0
|
||||
}
|
||||
if param.UpdateBy != "" {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
tx := db.DB("").Model(&model.SysLoginSource{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.Id)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息 返回受影响行数
|
||||
func (r SysLoginSource) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
// 执行删除
|
||||
if err := tx.Delete(&model.SysLoginSource{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
@@ -83,14 +83,15 @@ func (r SysMenu) Insert(sysMenu model.SysMenu) int64 {
|
||||
}
|
||||
|
||||
// 根据菜单类型重置参数
|
||||
if sysMenu.MenuType == constants.MENU_TYPE_BUTTON {
|
||||
switch sysMenu.MenuType {
|
||||
case constants.MENU_TYPE_BUTTON:
|
||||
sysMenu.Component = ""
|
||||
sysMenu.FrameFlag = "1"
|
||||
sysMenu.CacheFlag = "1"
|
||||
sysMenu.VisibleFlag = "1"
|
||||
sysMenu.MenuPath = ""
|
||||
sysMenu.Icon = "#"
|
||||
} else if sysMenu.MenuType == constants.MENU_TYPE_DIR {
|
||||
case constants.MENU_TYPE_DIR:
|
||||
sysMenu.Component = ""
|
||||
sysMenu.FrameFlag = "1"
|
||||
sysMenu.CacheFlag = "1"
|
||||
@@ -118,14 +119,15 @@ func (r SysMenu) Update(sysMenu model.SysMenu) int64 {
|
||||
}
|
||||
|
||||
// 根据菜单类型重置参数
|
||||
if sysMenu.MenuType == constants.MENU_TYPE_BUTTON {
|
||||
switch sysMenu.MenuType {
|
||||
case constants.MENU_TYPE_BUTTON:
|
||||
sysMenu.Component = ""
|
||||
sysMenu.FrameFlag = "1"
|
||||
sysMenu.CacheFlag = "1"
|
||||
sysMenu.VisibleFlag = "1"
|
||||
sysMenu.MenuPath = ""
|
||||
sysMenu.Icon = "#"
|
||||
} else if sysMenu.MenuType == constants.MENU_TYPE_DIR {
|
||||
case constants.MENU_TYPE_DIR:
|
||||
sysMenu.Component = ""
|
||||
sysMenu.FrameFlag = "1"
|
||||
sysMenu.CacheFlag = "1"
|
||||
|
||||
@@ -31,6 +31,12 @@ func (r SysUser) SelectByPage(query map[string]string, dataScopeSQL string) ([]m
|
||||
if v, ok := query["phone"]; ok && v != "" {
|
||||
tx = tx.Where("phone like ?", fmt.Sprintf("%s%%", v))
|
||||
}
|
||||
if v, ok := query["userType"]; ok && v != "" {
|
||||
tx = tx.Where("user_type = ?", v)
|
||||
}
|
||||
if v, ok := query["userSource"]; ok && v != "" {
|
||||
tx = tx.Where("user_source = ?", v)
|
||||
}
|
||||
if v, ok := query["statusFlag"]; ok && v != "" {
|
||||
tx = tx.Where("status_flag = ?", v)
|
||||
}
|
||||
@@ -198,6 +204,12 @@ func (r SysUser) CheckUnique(sysUser model.SysUser) int64 {
|
||||
if sysUser.Email != "" {
|
||||
tx = tx.Where("email = ?", sysUser.Email)
|
||||
}
|
||||
if sysUser.UserType != "" {
|
||||
tx = tx.Where("user_type = ?", sysUser.UserType)
|
||||
}
|
||||
if sysUser.UserSource != "" {
|
||||
tx = tx.Where("user_source = ?", sysUser.UserSource)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
var id int64 = 0
|
||||
@@ -209,14 +221,20 @@ func (r SysUser) CheckUnique(sysUser model.SysUser) int64 {
|
||||
}
|
||||
|
||||
// SelectByUserName 通过登录账号查询信息
|
||||
func (r SysUser) SelectByUserName(userName string) model.SysUser {
|
||||
func (r SysUser) SelectByUserName(userName, userType, userSource string) model.SysUser {
|
||||
item := model.SysUser{}
|
||||
if userName == "" {
|
||||
return item
|
||||
}
|
||||
if userType == "" {
|
||||
userType = "System"
|
||||
}
|
||||
if userSource == "" {
|
||||
userSource = "#"
|
||||
}
|
||||
tx := db.DB("").Model(&model.SysUser{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("user_name = ? and del_flag = '0'", userName)
|
||||
tx = tx.Where("user_name = ? and user_type = ? and user_source = ? and del_flag = '0'", userName, userType, userSource)
|
||||
// 查询数据
|
||||
if err := tx.Limit(1).Find(&item).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
|
||||
95
src/modules/system/service/sys_login_source.go
Normal file
95
src/modules/system/service/sys_login_source.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"be.ems/src/framework/utils/generate"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/model/vo"
|
||||
"be.ems/src/modules/system/repository"
|
||||
)
|
||||
|
||||
// NewSysLoginSource 实例化服务层
|
||||
var NewSysLoginSource = &SysLoginSource{
|
||||
sysLoginSourceRepository: repository.NewSysLoginSource,
|
||||
}
|
||||
|
||||
// SysLoginSource 认证源 服务层处理
|
||||
type SysLoginSource struct {
|
||||
sysLoginSourceRepository *repository.SysLoginSource // 认证源表
|
||||
}
|
||||
|
||||
// FindByPage 分页查询
|
||||
func (s SysLoginSource) FindByPage(query map[string]string) ([]model.SysLoginSource, int64) {
|
||||
return s.sysLoginSourceRepository.SelectByPage(query)
|
||||
}
|
||||
|
||||
// FindById 查询ID
|
||||
func (s SysLoginSource) FindById(id int64) model.SysLoginSource {
|
||||
rows := s.sysLoginSourceRepository.SelectByIds([]int64{id})
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysLoginSource{}
|
||||
}
|
||||
|
||||
// Insert 新增
|
||||
func (s SysLoginSource) Insert(param model.SysLoginSource) int64 {
|
||||
param.UID = generate.Code(8)
|
||||
return s.sysLoginSourceRepository.Insert(param)
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
func (s SysLoginSource) Update(param model.SysLoginSource) int64 {
|
||||
return s.sysLoginSourceRepository.Update(param)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除
|
||||
func (s SysLoginSource) DeleteByIds(ids []int64) (int64, error) {
|
||||
// 检查是否存在
|
||||
arr := s.sysLoginSourceRepository.SelectByIds(ids)
|
||||
if len(arr) <= 0 {
|
||||
// return 0, fmt.Errorf("没有权限访问认证源数据!")
|
||||
return 0, fmt.Errorf("no permission to access authentication source data")
|
||||
}
|
||||
if len(arr) == len(ids) {
|
||||
return s.sysLoginSourceRepository.DeleteByIds(ids), nil
|
||||
}
|
||||
// return 0, fmt.Errorf("删除认证源信息失败!")
|
||||
return 0, fmt.Errorf("failed to delete authentication source information")
|
||||
}
|
||||
|
||||
// FindByActive 查询激活
|
||||
func (s SysLoginSource) FindByActive(uid string) []model.SysLoginSource {
|
||||
param := model.SysLoginSource{
|
||||
ActiveFlag: "1",
|
||||
}
|
||||
if uid != "" {
|
||||
param.UID = uid
|
||||
}
|
||||
return s.sysLoginSourceRepository.Select(param)
|
||||
}
|
||||
|
||||
// CheckConfigJSON 检查配置JSON
|
||||
func (s SysLoginSource) CheckConfigJSON(sType, sConfig string) (string, error) {
|
||||
var source any
|
||||
switch sType {
|
||||
case "LDAP":
|
||||
source = new(vo.SysLoginSourceLDAP)
|
||||
case "SMTP":
|
||||
source = new(vo.SysLoginSourceSMTP)
|
||||
case "OAuth2":
|
||||
source = new(vo.SysLoginSourceOAuth2)
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported login source type: %s", sType)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(sConfig), &source); err != nil {
|
||||
return "", fmt.Errorf("config json format error for %s type: %s", sType, err.Error())
|
||||
}
|
||||
configByte, err := json.Marshal(source)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("config json format error")
|
||||
}
|
||||
return string(configByte), nil
|
||||
}
|
||||
@@ -189,7 +189,9 @@ func (s SysUser) DeleteByIds(userIds []int64) (int64, error) {
|
||||
// CheckUniqueByUserName 检查用户名称是否唯一
|
||||
func (s SysUser) CheckUniqueByUserName(userName string, userId int64) bool {
|
||||
uniqueId := s.sysUserRepository.CheckUnique(model.SysUser{
|
||||
UserName: userName,
|
||||
UserName: userName,
|
||||
UserType: "System",
|
||||
UserSource: "#",
|
||||
})
|
||||
if uniqueId == userId {
|
||||
return true
|
||||
@@ -200,7 +202,9 @@ func (s SysUser) CheckUniqueByUserName(userName string, userId int64) bool {
|
||||
// CheckUniqueByPhone 检查手机号码是否唯一
|
||||
func (s SysUser) CheckUniqueByPhone(phone string, userId int64) bool {
|
||||
uniqueId := s.sysUserRepository.CheckUnique(model.SysUser{
|
||||
Phone: phone,
|
||||
Phone: phone,
|
||||
UserType: "System",
|
||||
UserSource: "#",
|
||||
})
|
||||
if uniqueId == userId {
|
||||
return true
|
||||
@@ -211,7 +215,9 @@ func (s SysUser) CheckUniqueByPhone(phone string, userId int64) bool {
|
||||
// CheckUniqueByEmail 检查Email是否唯一
|
||||
func (s SysUser) CheckUniqueByEmail(email string, userId int64) bool {
|
||||
uniqueId := s.sysUserRepository.CheckUnique(model.SysUser{
|
||||
Email: email,
|
||||
Email: email,
|
||||
UserType: "System",
|
||||
UserSource: "#",
|
||||
})
|
||||
if uniqueId == userId {
|
||||
return true
|
||||
@@ -220,8 +226,10 @@ func (s SysUser) CheckUniqueByEmail(email string, userId int64) bool {
|
||||
}
|
||||
|
||||
// FindByUserName 通过用户名查询用户信息
|
||||
func (s SysUser) FindByUserName(userName string) model.SysUser {
|
||||
userinfo := s.sysUserRepository.SelectByUserName(userName)
|
||||
// userType 系统sys
|
||||
// userSource 系统#
|
||||
func (s SysUser) FindByUserName(userName, userType, userSource string) model.SysUser {
|
||||
userinfo := s.sysUserRepository.SelectByUserName(userName, userType, userSource)
|
||||
if userinfo.UserName != userName {
|
||||
return userinfo
|
||||
}
|
||||
|
||||
@@ -429,6 +429,35 @@ func Setup(router *gin.Engine) {
|
||||
controller.NewSysUser.Import,
|
||||
)
|
||||
}
|
||||
|
||||
// 第三方认证-配置认证源
|
||||
sysLoginSource := controller.NewSysLoginSource
|
||||
sysLoginSourceGroup := router.Group("/system/login-source")
|
||||
{
|
||||
sysLoginSourceGroup.GET("/list",
|
||||
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"system:loginSource:list"}}),
|
||||
sysLoginSource.List,
|
||||
)
|
||||
sysLoginSourceGroup.GET("/:id",
|
||||
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"system:loginSource:query"}}),
|
||||
sysLoginSource.Info,
|
||||
)
|
||||
sysLoginSourceGroup.POST("",
|
||||
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"system:loginSource:add"}}),
|
||||
middleware.OperateLog(middleware.OptionNew("log.operate.title.sysLoginSource", middleware.BUSINESS_TYPE_INSERT)),
|
||||
sysLoginSource.Add,
|
||||
)
|
||||
sysLoginSourceGroup.PUT("",
|
||||
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"system:loginSource:edit"}}),
|
||||
middleware.OperateLog(middleware.OptionNew("log.operate.title.sysLoginSource", middleware.BUSINESS_TYPE_UPDATE)),
|
||||
sysLoginSource.Edit,
|
||||
)
|
||||
sysLoginSourceGroup.DELETE("/:id",
|
||||
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"system:loginSource:remove"}}),
|
||||
middleware.OperateLog(middleware.OptionNew("log.operate.title.sysLoginSource", middleware.BUSINESS_TYPE_DELETE)),
|
||||
sysLoginSource.Remove,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// InitLoad 初始参数
|
||||
|
||||
Reference in New Issue
Block a user