feat: 新增第三方登录认证和管理
This commit is contained in:
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
|
||||
}
|
||||
@@ -13,6 +13,11 @@ type ISysUser interface {
|
||||
// SelectAllocatedPage 根据条件分页查询分配用户角色列表
|
||||
SelectAllocatedPage(query map[string]any, dataScopeSQL string) map[string]any
|
||||
|
||||
// FindByUserName 通过用户名查询用户信息
|
||||
// userType 系统sys
|
||||
// userSource 系统#
|
||||
FindByUserName(userName, userType, userSource string) model.SysUser
|
||||
|
||||
// SelectUserByUserName 通过用户名查询用户
|
||||
SelectUserByUserName(userName string) model.SysUser
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ var NewSysUserImpl = &SysUserImpl{
|
||||
sysUserRepository: repository.NewSysUserImpl,
|
||||
sysUserRoleRepository: repository.NewSysUserRoleImpl,
|
||||
sysUserPostRepository: repository.NewSysUserPostImpl,
|
||||
sysDeptRepository: repository.NewSysDeptImpl,
|
||||
sysRoleRepository: repository.NewSysRoleImpl,
|
||||
}
|
||||
|
||||
// SysUserImpl 用户 服务层处理
|
||||
@@ -23,6 +25,10 @@ type SysUserImpl struct {
|
||||
sysUserRoleRepository repository.ISysUserRole
|
||||
// 用户与岗位服务
|
||||
sysUserPostRepository repository.ISysUserPost
|
||||
// 部门服务
|
||||
sysDeptRepository repository.ISysDept
|
||||
// 角色服务
|
||||
sysRoleRepository repository.ISysRole
|
||||
}
|
||||
|
||||
// SelectUserPage 根据条件分页查询用户列表
|
||||
@@ -40,6 +46,30 @@ func (r *SysUserImpl) SelectAllocatedPage(query map[string]any, dataScopeSQL str
|
||||
return r.sysUserRepository.SelectAllocatedPage(query, dataScopeSQL)
|
||||
}
|
||||
|
||||
// FindByUserName 通过用户名查询用户信息
|
||||
// userType 系统sys
|
||||
// userSource 系统#
|
||||
func (s SysUserImpl) FindByUserName(userName, userType, userSource string) model.SysUser {
|
||||
userinfo := s.sysUserRepository.SelectByUserName(userName, userType, userSource)
|
||||
if userinfo.UserName != userName {
|
||||
return userinfo
|
||||
}
|
||||
// 部门
|
||||
deptInfo := s.sysDeptRepository.SelectById(userinfo.DeptID)
|
||||
userinfo.Dept = deptInfo
|
||||
// 角色
|
||||
roleArr := s.sysRoleRepository.SelectByUserId(userinfo.UserID)
|
||||
roles := make([]model.SysRole, 0)
|
||||
roleIds := make([]string, 0)
|
||||
for _, role := range roleArr {
|
||||
roles = append(roles, role)
|
||||
roleIds = append(roleIds, role.RoleID)
|
||||
}
|
||||
userinfo.Roles = roles
|
||||
userinfo.RoleIDs = roleIds
|
||||
return userinfo
|
||||
}
|
||||
|
||||
// SelectUserByUserName 通过用户名查询用户
|
||||
func (r *SysUserImpl) SelectUserByUserName(userName string) model.SysUser {
|
||||
return r.sysUserRepository.SelectUserByUserName(userName)
|
||||
@@ -147,7 +177,9 @@ func (r *SysUserImpl) DeleteUserByIds(userIds []string) (int64, error) {
|
||||
// CheckUniqueUserName 校验用户名称是否唯一
|
||||
func (r *SysUserImpl) CheckUniqueUserName(userName, userId string) bool {
|
||||
uniqueId := r.sysUserRepository.CheckUniqueUser(model.SysUser{
|
||||
UserName: userName,
|
||||
UserName: userName,
|
||||
UserType: "System",
|
||||
UserSource: "#",
|
||||
})
|
||||
if uniqueId == userId {
|
||||
return true
|
||||
@@ -159,6 +191,8 @@ func (r *SysUserImpl) CheckUniqueUserName(userName, userId string) bool {
|
||||
func (r *SysUserImpl) CheckUniquePhone(phonenumber, userId string) bool {
|
||||
uniqueId := r.sysUserRepository.CheckUniqueUser(model.SysUser{
|
||||
PhoneNumber: phonenumber,
|
||||
UserType: "System",
|
||||
UserSource: "#",
|
||||
})
|
||||
if uniqueId == userId {
|
||||
return true
|
||||
@@ -169,7 +203,9 @@ func (r *SysUserImpl) CheckUniquePhone(phonenumber, userId string) bool {
|
||||
// CheckUniqueEmail 校验email是否唯一
|
||||
func (r *SysUserImpl) CheckUniqueEmail(email, userId string) bool {
|
||||
uniqueId := r.sysUserRepository.CheckUniqueUser(model.SysUser{
|
||||
Email: email,
|
||||
Email: email,
|
||||
UserType: "System",
|
||||
UserSource: "#",
|
||||
})
|
||||
if uniqueId == userId {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user