Skip to content

Commit

Permalink
feat: force log out
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Oct 21, 2022
1 parent 08954ca commit 8a93548
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 49 deletions.
2 changes: 1 addition & 1 deletion api/api_desc/base.api
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type UUIDReq {
// UUID
// Required: true
// Max length: 36
UUID string `json:"UUID" path:"UUID" validate:"len=36"`
UUID string `json:"UUID" validate:"len=36"`
}

// The base response data | 基础信息
Expand Down
15 changes: 14 additions & 1 deletion api/api_desc/token.api
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ type (
// Max length: 100
Email string `json:"email" validate:"omitempty,email,max=100"`
}

)

@server(
Expand Down Expand Up @@ -169,4 +168,18 @@ service core {
// 500: SimpleMsg
@handler setTokenStatus
post /token/status (SetBooleanStatusReq) returns (SimpleMsg)

// swagger:route POST /token/logout token logout
// Force logging out by user UUID | 根据UUID强制用户退出
// Parameters:
// + name: body
// require: true
// in: body
// type: UUIDReq
// Responses:
// 200: SimpleMsg
// 401: SimpleMsg
// 500: SimpleMsg
@handler logout
post /token/logout (UUIDReq) returns (SimpleMsg)
}
12 changes: 12 additions & 0 deletions api/api_desc/user.api
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ type (
// User's id | 用户Id
Id int64 `json:"id"`

// User's UUID | 用户的UUID
UUID string `json:"UUID"`

// User Name | 用户名
Username string `json:"username"`

Expand Down Expand Up @@ -458,4 +461,13 @@ service core {
// 500: SimpleMsg
@handler updateUserProfile
post /user/profile (ProfileReq) returns (SimpleMsg)

// swagger:route GET /user/logout user logout
// Log out | 退出登陆
// Responses:
// 200: SimpleMsg
// 401: SimpleMsg
// 500: SimpleMsg
@handler logout
get /user/logout returns (SimpleMsg)
}
10 changes: 10 additions & 0 deletions api/internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions api/internal/handler/token/logouthandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package token

import (
"net/http"

"github.com/suyuan32/simple-admin-core/api/internal/logic/token"
"github.com/suyuan32/simple-admin-core/api/internal/svc"
"github.com/suyuan32/simple-admin-core/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

// swagger:route POST /token/logout token logout
// Force logging out by user UUID | 根据UUID强制用户退出
// Parameters:
// + name: body
// require: true
// in: body
// type: UUIDReq
// Responses:
// 200: SimpleMsg
// 401: SimpleMsg
// 500: SimpleMsg

func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UUIDReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}

l := token.NewLogoutLogic(r.Context(), svcCtx)
resp, err := l.Logout(&req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.OkJson(w, resp)
}
}
}
29 changes: 29 additions & 0 deletions api/internal/handler/user/logouthandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package user

import (
"net/http"

"github.com/zeromicro/go-zero/rest/httpx"

"github.com/suyuan32/simple-admin-core/api/internal/logic/user"
"github.com/suyuan32/simple-admin-core/api/internal/svc"
)

// swagger:route GET /user/logout user logout
// Log out | 退出登陆
// Responses:
// 200: SimpleMsg
// 401: SimpleMsg
// 500: SimpleMsg

func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := user.NewLogoutLogic(r.Context(), svcCtx)
resp, err := l.Logout()
if err != nil {
httpx.Error(w, err)
} else {
httpx.OkJson(w, resp)
}
}
}
35 changes: 35 additions & 0 deletions api/internal/logic/token/logoutlogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package token

import (
"context"

"github.com/suyuan32/simple-admin-core/api/internal/svc"
"github.com/suyuan32/simple-admin-core/api/internal/types"
"github.com/suyuan32/simple-admin-core/rpc/types/core"

"github.com/zeromicro/go-zero/core/logx"
)

type LogoutLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
return &LogoutLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *LogoutLogic) Logout(req *types.UUIDReq) (resp *types.SimpleMsg, err error) {
result, err := l.svcCtx.CoreRpc.BlockUserAllToken(context.Background(), &core.UUIDReq{UUID: req.UUID})

if err != nil {
return nil, err
}

return &types.SimpleMsg{Msg: result.Msg}, nil
}
5 changes: 4 additions & 1 deletion api/internal/logic/user/getuserlistlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package user

import (
"context"

"github.com/suyuan32/simple-admin-core/rpc/types/core"

"github.com/zeromicro/go-zero/core/logx"

"github.com/suyuan32/simple-admin-core/api/internal/svc"
"github.com/suyuan32/simple-admin-core/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
Expand Down Expand Up @@ -49,6 +51,7 @@ func (l *GetUserListLogic) GetUserList(req *types.GetUserListReq) (resp *types.U
Status: v.Status,
CreateAt: v.CreateAt,
UpdateAt: v.UpdateAt,
UUID: v.UUID,
})
}
resp = &types.UserListResp{}
Expand Down
36 changes: 36 additions & 0 deletions api/internal/logic/user/logoutlogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package user

import (
"context"

"github.com/suyuan32/simple-admin-core/api/internal/svc"
"github.com/suyuan32/simple-admin-core/api/internal/types"
"github.com/suyuan32/simple-admin-core/rpc/types/core"

"github.com/zeromicro/go-zero/core/logx"
)

type LogoutLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
return &LogoutLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *LogoutLogic) Logout() (resp *types.SimpleMsg, err error) {
result, err := l.svcCtx.CoreRpc.BlockUserAllToken(context.Background(),
&core.UUIDReq{UUID: l.ctx.Value("userId").(string)})

if err != nil {
return nil, err
}

return &types.SimpleMsg{Msg: result.Msg}, nil
}
4 changes: 3 additions & 1 deletion api/internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rpc/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,5 @@ service core {
rpc deleteToken (IDReq) returns (BaseResp);
rpc getTokenList (TokenListReq) returns (TokenListResp);
rpc setTokenStatus (SetStatusReq) returns (BaseResp);
rpc blockUserAllToken (UUIDReq) returns (BaseResp);
}
6 changes: 6 additions & 0 deletions rpc/coreclient/core.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions rpc/internal/logic/blockuseralltokenlogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package logic

import (
"context"
"errors"
"time"

"github.com/zeromicro/go-zero/core/errorx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"

"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"

"github.com/zeromicro/go-zero/core/logx"
)

type BlockUserAllTokenLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}

func NewBlockUserAllTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BlockUserAllTokenLogic {
return &BlockUserAllTokenLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}

func (l *BlockUserAllTokenLogic) BlockUserAllToken(in *core.UUIDReq) (*core.BaseResp, error) {
result := l.svcCtx.DB.Model(&model.Token{}).Where("uuid = ?", in.UUID).Update("status", 0)
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error()))
return nil, status.Error(codes.Internal, errorx.DatabaseError)
}

var tokens []model.Token
tokenData := l.svcCtx.DB.Where("uuid = ?", in.UUID).Where("status = ?", 0).
Where("expire_at > ?", time.Now()).Find(&tokens)

if tokenData.Error != nil && !errors.Is(tokenData.Error, gorm.ErrRecordNotFound) {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error()))
return nil, status.Error(codes.Internal, errorx.DatabaseError)
}

for _, v := range tokens {
err := l.svcCtx.Redis.Set("token_"+v.Token, "1")
if err != nil {
logx.Errorw(logmessage.RedisError, logx.Field("Detail", err.Error()))
return nil, status.Error(codes.Internal, errorx.RedisError)
}
}

return &core.BaseResp{Msg: errorx.UpdateSuccess}, nil
}
12 changes: 12 additions & 0 deletions rpc/internal/logic/initdatabaselogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ func (l *InitDatabaseLogic) insertApiData() error {
ApiGroup: "user",
Method: "POST",
},
{
Path: "/user/logout",
Description: "api_desc.logout",
ApiGroup: "user",
Method: "GET",
},
// role
{
Path: "/role",
Expand Down Expand Up @@ -469,6 +475,12 @@ func (l *InitDatabaseLogic) insertApiData() error {
ApiGroup: "token",
Method: "POST",
},
{
Path: "/token/logout",
Description: "sys.user.forceLoggingOut",
ApiGroup: "token",
Method: "POST",
},
}
result := l.svcCtx.DB.CreateInBatches(apis, 100)
if result.Error != nil {
Expand Down
5 changes: 5 additions & 0 deletions rpc/internal/server/coreserver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8a93548

Please sign in to comment.