Skip to content

Commit

Permalink
feat: remove login cache when update user
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor-Lan committed Aug 23, 2022
1 parent 2d3b70b commit 4eaa4ae
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package user
import (
"context"
"encoding/json"
"strconv"

"ark-admin-zero/app/core/cmd/api/internal/svc"
"ark-admin-zero/app/core/cmd/api/internal/types"
"ark-admin-zero/common/config"
"ark-admin-zero/common/errorx"

"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
)
Expand Down Expand Up @@ -37,9 +40,11 @@ func (l *UpdateSysUserLogic) UpdateSysUser(req *types.UpdateSysUserReq) error {

bytes, err := json.Marshal(req.RoleIds)
if err != nil {
return err
return errorx.NewDefaultError(errorx.ServerErrorCode)
}

_, err = l.svcCtx.Redis.Del(config.SysPermMenuCachePrefix + strconv.FormatInt(sysUser.Id, 10))
_, err = l.svcCtx.Redis.Del(config.SysOnlineUserCachePrefix + strconv.FormatInt(sysUser.Id, 10))
sysUser.RoleIds = string(bytes)
err = l.svcCtx.SysUserModel.Update(l.ctx, sysUser)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions app/core/cmd/api/internal/logic/user/getuserpermmenulogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func NewGetUserPermMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G
func (l *GetUserPermMenuLogic) GetUserPermMenu() (resp *types.UserPermMenuResp, err error) {
userId := utils.GetUserId(l.ctx)

online, err := l.svcCtx.Redis.Get(config.SysOnlineUserCachePrefix + strconv.FormatInt(userId, 10))
if err != nil || online == "" {
return nil, errorx.NewDefaultError(errorx.AuthErrorCode)
}

// 查询用户信息
user, err := l.svcCtx.SysUserModel.FindOne(l.ctx, userId)
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions app/core/cmd/api/internal/logic/user/loginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"context"
"net/http"
"strconv"
"time"

"ark-admin-zero/app/core/cmd/api/internal/svc"
Expand Down Expand Up @@ -53,14 +54,19 @@ func (l *LoginLogic) Login(req *types.LoginReq, r *http.Request) (resp *types.Lo
_, err = l.svcCtx.Redis.Del(req.CaptchaId)

loginLog := model.SysLog{
UserId: sysUser.Id,
Ip: r.RemoteAddr,
Uri: r.RequestURI,
Type: 1,
Status: 1,
UserId: sysUser.Id,
Ip: r.RemoteAddr,
Uri: r.RequestURI,
Type: 1,
Status: 1,
}
_, err = l.svcCtx.SysLogModel.Insert(l.ctx, &loginLog)

err = l.svcCtx.Redis.Setex(config.SysOnlineUserCachePrefix+strconv.FormatInt(sysUser.Id, 10), "1", int(l.svcCtx.Config.JwtAuth.AccessExpire))
if err != nil {
return nil, errorx.NewDefaultError(errorx.ServerErrorCode)
}

return &types.LoginResp{
Token: token,
}, nil
Expand Down
10 changes: 9 additions & 1 deletion app/core/cmd/api/internal/middleware/permmenuauthmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"strings"

"ark-admin-zero/common/errorx"
"ark-admin-zero/common/config"
"ark-admin-zero/common/errorx"
"ark-admin-zero/common/utils"

"github.com/zeromicro/go-zero/core/stores/redis"
Expand All @@ -27,6 +27,14 @@ func (m *PermMenuAuthMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc
return func(w http.ResponseWriter, r *http.Request) {
if len(r.Header.Get("Authorization")) > 0 {
userId := utils.GetUserId(r.Context())
online, err := m.Redis.Get(config.SysOnlineUserCachePrefix + strconv.FormatInt(userId, 10))
if err != nil || online == "" {
httpx.Error(w, errorx.NewDefaultError(errorx.AuthErrorCode))
var erring any
erring = "Auth fail"
panic(erring)
}

uri := strings.Split(r.RequestURI, "?")
is, err := m.Redis.Sismember(config.SysPermMenuCachePrefix+strconv.FormatInt(userId, 10), uri[0])
if err != nil || is != true {
Expand Down
1 change: 1 addition & 0 deletions common/config/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
const (
SysJwtUserId = "userId"
SysPermMenuCachePrefix = "cache:arkAdmin:permMenu:"
SysOnlineUserCachePrefix = "cache:arkAdmin:online:"
SysLoginCaptchaCachePrefix = "cache:arkAdmin:captcha:"
SysSuperAdminUserId = 1
SysSuperAdminRoleId = 1
Expand Down
2 changes: 2 additions & 0 deletions common/errorx/errormsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
SetParentTypeErrorCode = 1023
AddConfigErrorCode = 1024
AddDictionaryErrorCode = 1025
AuthErrorCode = 1026
)

func init() {
Expand Down Expand Up @@ -58,6 +59,7 @@ func init() {
errorMsg[SetParentTypeErrorCode] = "权限类型不能作为父级菜单"
errorMsg[AddConfigErrorCode] = "配置已存在"
errorMsg[AddDictionaryErrorCode] = "字典已存在"
errorMsg[AuthErrorCode] = "授权已失效,请重新登录"
}

func MapErrMsg(errCode int) string {
Expand Down

0 comments on commit 4eaa4ae

Please sign in to comment.