2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-22 20:51:46 +08:00
|
|
|
|
"mayfly-go/internal/common/utils"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
msgapp "mayfly-go/internal/msg/application"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/api/form"
|
|
|
|
|
|
"mayfly-go/internal/sys/api/vo"
|
|
|
|
|
|
"mayfly-go/internal/sys/application"
|
2023-12-18 22:39:32 +08:00
|
|
|
|
"mayfly-go/internal/sys/consts"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
2024-11-20 22:43:53 +08:00
|
|
|
|
"mayfly-go/internal/sys/imsg"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"mayfly-go/pkg/contextx"
|
2022-07-14 11:39:12 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/cryptox"
|
2024-03-02 19:08:19 +08:00
|
|
|
|
"mayfly-go/pkg/utils/structx"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
2024-03-21 17:15:52 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/may-fly/cast"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
const (
|
|
|
|
|
|
OtpStatusNone = -1 // 未启用otp校验
|
|
|
|
|
|
OtpStatusReg = 1 // 用户otp secret已注册
|
|
|
|
|
|
OtpStatusNoReg = 2 // 用户otp secret未注册
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
type Account struct {
|
2024-01-21 22:52:20 +08:00
|
|
|
|
AccountApp application.Account `inject:""`
|
|
|
|
|
|
ResourceApp application.Resource `inject:""`
|
|
|
|
|
|
RoleApp application.Role `inject:""`
|
|
|
|
|
|
MsgApp msgapp.Msg `inject:""`
|
|
|
|
|
|
ConfigApp application.Config `inject:""`
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
// 获取当前登录用户的菜单与权限码
|
2023-04-13 20:11:22 +08:00
|
|
|
|
func (a *Account) GetPermissions(rc *req.Ctx) {
|
2023-11-07 21:05:21 +08:00
|
|
|
|
account := rc.GetLoginAccount()
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var resources vo.AccountResourceVOList
|
|
|
|
|
|
// 获取账号菜单资源
|
2023-10-26 17:15:49 +08:00
|
|
|
|
biz.ErrIsNil(a.ResourceApp.GetAccountResources(account.Id, &resources))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 菜单树与权限code数组
|
|
|
|
|
|
var menus vo.AccountResourceVOList
|
|
|
|
|
|
var permissions []string
|
|
|
|
|
|
for _, v := range resources {
|
|
|
|
|
|
if v.Type == entity.ResourceTypeMenu {
|
|
|
|
|
|
menus = append(menus, v)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
permissions = append(permissions, *v.Code)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-09 16:58:57 +08:00
|
|
|
|
// 保存该账号的权限codes
|
2023-01-14 16:29:52 +08:00
|
|
|
|
req.SavePermissionCodes(account.Id, permissions)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ResData = collx.M{
|
2023-04-13 20:11:22 +08:00
|
|
|
|
"menus": menus.ToTrees(0),
|
|
|
|
|
|
"permissions": permissions,
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) ChangePassword(rc *req.Ctx) {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
ctx := rc.MetaCtx
|
|
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
form := req.BindJsonAndValid(rc, new(form.AccountChangePasswordForm))
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originOldPwd, err := cryptox.DefaultRsaDecrypt(form.OldPassword, true)
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "Wrong to decrypt old password: %s")
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2022-08-02 21:44:01 +08:00
|
|
|
|
account := &entity.Account{Username: form.Username}
|
2024-04-28 23:45:57 +08:00
|
|
|
|
err = a.AccountApp.GetByCond(model.NewModelCond(account).Columns("Id", "Username", "Password", "Status"))
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNilI(ctx, err, imsg.ErrOldPasswordWrong)
|
|
|
|
|
|
biz.IsTrueI(ctx, cryptox.CheckPwdHash(originOldPwd, account.Password), imsg.ErrOldPasswordWrong)
|
|
|
|
|
|
biz.IsTrue(account.IsEnable(), "This account is not available")
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originNewPwd, err := cryptox.DefaultRsaDecrypt(form.NewPassword, true)
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "Wrong to decrypt new password: %s")
|
|
|
|
|
|
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(originNewPwd), imsg.ErrAccountPasswordNotFollowRule)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
|
|
|
|
|
updateAccount := new(entity.Account)
|
|
|
|
|
|
updateAccount.Id = account.Id
|
2023-07-21 17:07:04 +08:00
|
|
|
|
updateAccount.Password = cryptox.PwdHash(originNewPwd)
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(a.AccountApp.Update(ctx, updateAccount), "failed to update account password: %s")
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
|
2024-11-20 22:43:53 +08:00
|
|
|
|
contextx.WithLoginAccount(ctx, &model.LoginAccount{
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Id: account.Id,
|
|
|
|
|
|
Username: account.Username,
|
|
|
|
|
|
})
|
2022-07-18 20:36:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-11 14:04:09 +08:00
|
|
|
|
// 获取个人账号信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) AccountInfo(rc *req.Ctx) {
|
2021-09-11 14:04:09 +08:00
|
|
|
|
ap := new(vo.AccountPersonVO)
|
|
|
|
|
|
// 角色信息
|
2023-12-18 22:39:32 +08:00
|
|
|
|
ap.Roles = a.getAccountRoles(rc.GetLoginAccount().Id)
|
2021-09-11 14:04:09 +08:00
|
|
|
|
rc.ResData = ap
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新个人账号信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) UpdateAccount(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
updateAccount := req.BindJsonAndCopyTo[*entity.Account](rc, new(form.AccountUpdateForm), new(entity.Account))
|
2021-09-11 14:04:09 +08:00
|
|
|
|
// 账号id为登录者账号
|
2023-11-07 21:05:21 +08:00
|
|
|
|
updateAccount.Id = rc.GetLoginAccount().Id
|
2021-09-11 14:04:09 +08:00
|
|
|
|
|
2024-11-20 22:43:53 +08:00
|
|
|
|
ctx := rc.MetaCtx
|
2021-09-11 14:04:09 +08:00
|
|
|
|
if updateAccount.Password != "" {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(updateAccount.Password), imsg.ErrAccountPasswordNotFollowRule)
|
2023-07-21 17:07:04 +08:00
|
|
|
|
updateAccount.Password = cryptox.PwdHash(updateAccount.Password)
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
2023-07-24 22:36:07 +08:00
|
|
|
|
|
2024-05-05 14:53:30 +08:00
|
|
|
|
oldAcc, err := a.AccountApp.GetById(updateAccount.Id)
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "Account does not exist: %s")
|
2023-07-24 22:36:07 +08:00
|
|
|
|
// 账号创建十分钟内允许修改用户名(兼容oauth2首次登录修改用户名),否则不允许修改
|
|
|
|
|
|
if oldAcc.CreateTime.Add(10 * time.Minute).Before(time.Now()) {
|
|
|
|
|
|
// 禁止更新用户名,防止误传被更新
|
|
|
|
|
|
updateAccount.Username = ""
|
|
|
|
|
|
}
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(ctx, updateAccount))
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 后台账号操作 **/
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// @router /accounts [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) Accounts(rc *req.Ctx) {
|
2024-02-29 22:12:50 +08:00
|
|
|
|
condition := &entity.AccountQuery{}
|
2024-02-25 12:46:18 +08:00
|
|
|
|
condition.Username = rc.Query("username")
|
2024-02-29 22:12:50 +08:00
|
|
|
|
condition.Name = rc.Query("name")
|
2024-02-25 12:46:18 +08:00
|
|
|
|
res, err := a.AccountApp.GetPageList(condition, rc.GetPageParam(), new([]vo.AccountManageVO))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ResData = res
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-29 22:12:50 +08:00
|
|
|
|
func (a *Account) SimpleAccounts(rc *req.Ctx) {
|
|
|
|
|
|
condition := &entity.AccountQuery{}
|
|
|
|
|
|
condition.Username = rc.Query("username")
|
|
|
|
|
|
condition.Name = rc.Query("name")
|
|
|
|
|
|
idsStr := rc.Query("ids")
|
|
|
|
|
|
if idsStr != "" {
|
|
|
|
|
|
condition.Ids = collx.ArrayMap[string, uint64](strings.Split(idsStr, ","), func(val string) uint64 {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
return cast.ToUint64(val)
|
2024-02-29 22:12:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
res, err := a.AccountApp.GetPageList(condition, rc.GetPageParam(), new([]vo.SimpleAccountVO))
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-02 19:08:19 +08:00
|
|
|
|
// 获取账号详情
|
|
|
|
|
|
func (a *Account) AccountDetail(rc *req.Ctx) {
|
|
|
|
|
|
accountId := uint64(rc.PathParamInt("id"))
|
2024-05-05 14:53:30 +08:00
|
|
|
|
account, err := a.AccountApp.GetById(accountId)
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "Account does not exist: %s")
|
2024-03-02 19:08:19 +08:00
|
|
|
|
accountvo := new(vo.SimpleAccountVO)
|
|
|
|
|
|
structx.Copy(accountvo, account)
|
|
|
|
|
|
|
|
|
|
|
|
accountvo.Roles = a.getAccountRoles(accountId)
|
|
|
|
|
|
rc.ResData = accountvo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-26 20:15:36 +08:00
|
|
|
|
// @router /accounts
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) SaveAccount(rc *req.Ctx) {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
form := &form.AccountCreateForm{}
|
2024-02-24 16:30:29 +08:00
|
|
|
|
account := req.BindJsonAndCopyTo(rc, form, new(entity.Account))
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-07-08 20:05:55 +08:00
|
|
|
|
form.Password = "*****"
|
|
|
|
|
|
rc.ReqParam = form
|
2024-11-20 22:43:53 +08:00
|
|
|
|
ctx := rc.MetaCtx
|
2022-08-26 20:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
if account.Id == 0 {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.NotEmpty(account.Password, "password is required")
|
|
|
|
|
|
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(account.Password), imsg.ErrAccountPasswordNotFollowRule)
|
|
|
|
|
|
account.Password = cryptox.PwdHash(account.Password)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Create(rc.MetaCtx, account))
|
2022-08-26 20:15:36 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if account.Password != "" {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(account.Password), imsg.ErrAccountPasswordNotFollowRule)
|
2023-07-21 17:07:04 +08:00
|
|
|
|
account.Password = cryptox.PwdHash(account.Password)
|
2022-08-26 20:15:36 +08:00
|
|
|
|
}
|
2023-07-24 22:36:07 +08:00
|
|
|
|
// 更新操作不允许修改用户名、防止误传更新
|
|
|
|
|
|
account.Username = ""
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(ctx, account))
|
2022-08-26 20:15:36 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) ChangeStatus(rc *req.Ctx) {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
account := &entity.Account{}
|
2024-02-25 12:46:18 +08:00
|
|
|
|
account.Id = uint64(rc.PathParamInt("id"))
|
2024-01-19 21:33:37 +08:00
|
|
|
|
|
2024-02-25 12:46:18 +08:00
|
|
|
|
status := entity.AccountStatus(int8(rc.PathParamInt("status")))
|
2024-01-19 21:33:37 +08:00
|
|
|
|
biz.ErrIsNil(entity.AccountStatusEnum.Valid(status))
|
|
|
|
|
|
account.Status = status
|
|
|
|
|
|
|
2023-10-12 21:50:55 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("accountId", account.Id, "status", account.Status)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) DeleteAccount(rc *req.Ctx) {
|
2024-02-25 12:46:18 +08:00
|
|
|
|
idsStr := rc.PathParam("id")
|
2023-07-01 14:34:42 +08:00
|
|
|
|
rc.ReqParam = idsStr
|
|
|
|
|
|
ids := strings.Split(idsStr, ",")
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range ids {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Delete(rc.MetaCtx, cast.ToUint64(v)))
|
2023-07-01 14:34:42 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
// 获取账号角色信息列表
|
|
|
|
|
|
func (a *Account) AccountRoles(rc *req.Ctx) {
|
2024-02-25 12:46:18 +08:00
|
|
|
|
rc.ResData = a.getAccountRoles(uint64(rc.PathParamInt("id")))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO {
|
|
|
|
|
|
vos := make([]*vo.AccountRoleVO, 0)
|
|
|
|
|
|
|
|
|
|
|
|
accountRoles, err := a.RoleApp.GetAccountRoles(accountId)
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
|
|
|
|
|
|
if len(accountRoles) == 0 {
|
|
|
|
|
|
return vos
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取角色信息进行组装
|
|
|
|
|
|
roleIds := collx.ArrayMap[*entity.AccountRole, uint64](accountRoles, func(val *entity.AccountRole) uint64 {
|
|
|
|
|
|
return val.RoleId
|
|
|
|
|
|
})
|
|
|
|
|
|
roles, err := a.RoleApp.ListByQuery(&entity.RoleQuery{Ids: roleIds})
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
roleId2Role := collx.ArrayToMap[*entity.Role, uint64](roles, func(val *entity.Role) uint64 {
|
|
|
|
|
|
return val.Id
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
for _, ac := range accountRoles {
|
|
|
|
|
|
role := roleId2Role[ac.RoleId]
|
|
|
|
|
|
if role == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
vos = append(vos, &vo.AccountRoleVO{
|
|
|
|
|
|
RoleId: ac.RoleId,
|
|
|
|
|
|
RoleName: role.Name,
|
|
|
|
|
|
Code: role.Code,
|
|
|
|
|
|
Status: role.Status,
|
|
|
|
|
|
CreateTime: ac.CreateTime, // 分配时间
|
|
|
|
|
|
Creator: ac.Creator, // 分配者
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return vos
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) AccountResources(rc *req.Ctx) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var resources vo.ResourceManageVOList
|
|
|
|
|
|
// 获取账号菜单资源
|
2024-02-25 12:46:18 +08:00
|
|
|
|
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(rc.PathParamInt("id")), &resources))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
rc.ResData = resources.ToTrees(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
// 关联账号角色
|
|
|
|
|
|
func (a *Account) RelateRole(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
form := req.BindJsonAndValid(rc, new(form.AccountRoleForm))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
rc.ReqParam = form
|
2023-12-18 22:39:32 +08:00
|
|
|
|
biz.ErrIsNil(a.RoleApp.RelateAccountRole(rc.MetaCtx, form.Id, form.RoleId, consts.AccountRoleRelateType(form.RelateType)))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-06-17 15:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 重置otp秘钥
|
|
|
|
|
|
func (a *Account) ResetOtpSecret(rc *req.Ctx) {
|
|
|
|
|
|
account := &entity.Account{OtpSecret: "-"}
|
2024-02-25 12:46:18 +08:00
|
|
|
|
accountId := uint64(rc.PathParamInt("id"))
|
2023-06-17 15:15:03 +08:00
|
|
|
|
account.Id = accountId
|
2023-10-12 21:50:55 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("accountId", accountId)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
|
2023-06-17 15:15:03 +08:00
|
|
|
|
}
|