Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dfbb3a9acc | |||
| 19fa8b381c |
@@ -490,12 +490,23 @@ type RevokeReq struct {
|
|||||||
|
|
||||||
// Introspect 令牌检查端点
|
// Introspect 令牌检查端点
|
||||||
func Introspect(ctx *fiber.Ctx) error {
|
func Introspect(ctx *fiber.Ctx) error {
|
||||||
// 验证权限
|
authCtx := GetAuthCtx(ctx)
|
||||||
authCtx, err := GetAuthCtx(ctx).PermitUser()
|
|
||||||
if err != nil {
|
// 尝试验证用户权限
|
||||||
return err
|
if _, err := authCtx.PermitUser(); err == nil {
|
||||||
|
return introspectUser(ctx, authCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 尝试验证管理员权限
|
||||||
|
if _, err := authCtx.PermitAdmin(); err == nil {
|
||||||
|
return introspectAdmin(ctx, authCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ErrAuthenticateForbidden
|
||||||
|
}
|
||||||
|
|
||||||
|
// introspectUser 获取并返回用户信息
|
||||||
|
func introspectUser(ctx *fiber.Ctx, authCtx *AuthCtx) error {
|
||||||
// 获取用户信息
|
// 获取用户信息
|
||||||
profile, err := q.User.
|
profile, err := q.User.
|
||||||
Where(q.User.ID.Eq(authCtx.User.ID)).
|
Where(q.User.ID.Eq(authCtx.User.ID)).
|
||||||
@@ -519,12 +530,33 @@ func Introspect(ctx *fiber.Ctx) error {
|
|||||||
if profile.IDNo != nil && *profile.IDNo != "" {
|
if profile.IDNo != nil && *profile.IDNo != "" {
|
||||||
profile.IDNo = u.P(maskIdNo(*profile.IDNo))
|
profile.IDNo = u.P(maskIdNo(*profile.IDNo))
|
||||||
}
|
}
|
||||||
return ctx.JSON(IntrospectResp{*profile, hasPassword})
|
|
||||||
|
return ctx.JSON(struct {
|
||||||
|
m.User
|
||||||
|
HasPassword bool `json:"has_password"` // 是否设置了密码
|
||||||
|
}{*profile, hasPassword})
|
||||||
}
|
}
|
||||||
|
|
||||||
type IntrospectResp struct {
|
// introspectAdmin 获取并返回管理员信息
|
||||||
m.User
|
func introspectAdmin(ctx *fiber.Ctx, authCtx *AuthCtx) error {
|
||||||
HasPassword bool `json:"has_password"` // 是否设置了密码
|
// 获取管理员信息
|
||||||
|
profile, err := q.Admin.
|
||||||
|
Where(q.Admin.ID.Eq(authCtx.Admin.ID)).
|
||||||
|
Omit(q.Admin.DeletedAt).
|
||||||
|
Take()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 不返回密码
|
||||||
|
profile.Password = ""
|
||||||
|
|
||||||
|
// 掩码敏感信息
|
||||||
|
if profile.Phone != nil && *profile.Phone != "" {
|
||||||
|
profile.Phone = u.P(maskPhone(*profile.Phone))
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func maskPhone(phone string) string {
|
func maskPhone(phone string) string {
|
||||||
|
|||||||
@@ -28,12 +28,21 @@ func PageUserByAdmin(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// 查询用户列表
|
// 查询用户列表
|
||||||
users, total, err := q.User.
|
users, total, err := q.User.
|
||||||
|
Preload(q.User.Admin).
|
||||||
Omit(q.User.Password).
|
Omit(q.User.Password).
|
||||||
FindByPage(req.GetOffset(), req.GetLimit())
|
FindByPage(req.GetOffset(), req.GetLimit())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
if user.Admin != nil {
|
||||||
|
user.Admin = &m.Admin{
|
||||||
|
Name: user.Admin.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 返回结果
|
// 返回结果
|
||||||
return c.JSON(core.PageResp{
|
return c.JSON(core.PageResp{
|
||||||
Total: int(total),
|
Total: int(total),
|
||||||
|
|||||||
Reference in New Issue
Block a user