Skip to content

Commit

Permalink
[feat]add hotspot
Browse files Browse the repository at this point in the history
  • Loading branch information
piupuer committed Oct 12, 2024
1 parent b9924fe commit 260ea04
Show file tree
Hide file tree
Showing 26 changed files with 591 additions and 419 deletions.
8 changes: 4 additions & 4 deletions cmd/auth/wire_gen.go

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

18 changes: 15 additions & 3 deletions configs/task.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
task:
refresh.hotspot:
name: 'refresh.hotspot'
expr: '0 0/5 * * * * *'
# cron task list
cron:
refresh.hotspot:
name: 'refresh.hotspot'
expr: '0 0/5 * * * * *'
# all group name list(cron or once)
group:
# login failed will delay to update wrong count(once)
loginFailed: 'login.failed'
# login success will delay to update last login time(once)
loginLast: 'login.last'
# refresh hotspot(cron)
refreshHotspot: 'refresh.hotspot'
# refresh hotspot manual(once)
refreshHotspotManual: 'refresh.hotspot.manual'
114 changes: 57 additions & 57 deletions docs/auth-proto/auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,43 +122,78 @@ paths:
"200":
description: OK
content: {}
/captcha:
/idempotent:
get:
tags:
- Auth
operationId: Auth_Captcha
operationId: Auth_Idempotent
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.CaptchaReply'
/idempotent:
$ref: '#/components/schemas/auth.v1.IdempotentReply'
/info:
get:
tags:
- Auth
operationId: Auth_Idempotent
operationId: Auth_Info
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.IdempotentReply'
/info:
$ref: '#/components/schemas/auth.v1.InfoReply'
/logout:
post:
tags:
- Auth
operationId: Auth_Logout
requestBody:
content:
application/json: {}
required: true
responses:
"200":
description: OK
content: {}
/permission:
get:
tags:
- Auth
operationId: Auth_Info
operationId: Auth_Permission
parameters:
- name: resource
in: query
schema:
type: string
- name: method
in: query
schema:
type: string
- name: uri
in: query
schema:
type: string
responses:
"200":
description: OK
content: {}
/pub/captcha:
get:
tags:
- Auth
operationId: Auth_Captcha
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.InfoReply'
/login:
$ref: '#/components/schemas/auth.v1.CaptchaReply'
/pub/login:
post:
tags:
- Auth
Expand All @@ -176,41 +211,38 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.LoginReply'
/logout:
/pub/register:
post:
tags:
- Auth
operationId: Auth_Logout
operationId: Auth_Register
requestBody:
content:
application/json: {}
application/json:
schema:
$ref: '#/components/schemas/auth.v1.RegisterRequest'
required: true
responses:
"200":
description: OK
content: {}
/permission:
/pub/status:
get:
tags:
- Auth
operationId: Auth_Permission
operationId: Auth_Status
parameters:
- name: resource
in: query
schema:
type: string
- name: method
in: query
schema:
type: string
- name: uri
- name: username
in: query
schema:
type: string
responses:
"200":
description: OK
content: {}
content:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.StatusReply'
/pwd:
post:
tags:
Expand Down Expand Up @@ -244,21 +276,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.LoginReply'
/register:
post:
tags:
- Auth
operationId: Auth_Register
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.RegisterRequest'
required: true
responses:
"200":
description: OK
content: {}
/role:
get:
tags:
Expand Down Expand Up @@ -370,23 +387,6 @@ paths:
"200":
description: OK
content: {}
/status:
get:
tags:
- Auth
operationId: Auth_Status
parameters:
- name: username
in: query
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/auth.v1.StatusReply'
/user:
get:
tags:
Expand Down
1 change: 1 addition & 0 deletions internal/biz/hotspot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type HotspotRepo interface {
GetRoleByID(ctx context.Context, id uint64) *Role
GetActionByWord(ctx context.Context, word string) *Action
GetActionByCode(ctx context.Context, code string) *Action
FindActionByCode(ctx context.Context, codes ...string) []Action
FindWhitelistResourceByCategory(ctx context.Context, category uint32) []string
FindUserGroupByUserCode(ctx context.Context, code string) []UserGroup
}
Expand Down
50 changes: 8 additions & 42 deletions internal/biz/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package biz

import (
"context"
"errors"
"strings"

"auth/internal/conf"
"github.com/go-cinch/common/copierx"
"github.com/go-cinch/common/utils"
)

type Permission struct {
Expand All @@ -25,20 +21,18 @@ type CheckPermission struct {

type PermissionRepo interface {
Check(ctx context.Context, item CheckPermission) bool
GetByUserCode(ctx context.Context, code string) (*Permission, error)
GetByUserCode(ctx context.Context, code string) *Permission
}

type PermissionUseCase struct {
c *conf.Bootstrap
repo PermissionRepo
cache Cache
c *conf.Bootstrap
repo PermissionRepo
}

func NewPermissionUseCase(c *conf.Bootstrap, repo PermissionRepo, cache Cache) *PermissionUseCase {
func NewPermissionUseCase(c *conf.Bootstrap, repo PermissionRepo) *PermissionUseCase {
return &PermissionUseCase{
c: c,
repo: repo,
cache: cache.WithPrefix("permission"),
c: c,
repo: repo,
}
}

Expand All @@ -47,35 +41,7 @@ func (uc *PermissionUseCase) Check(ctx context.Context, item CheckPermission) (r
return
}

func (uc *PermissionUseCase) GetByUserCode(ctx context.Context, code string) (rp *Permission, err error) {
rp = &Permission{}
action := strings.Join([]string{"get_by_user_code", code}, "_")
str, err := uc.cache.Get(ctx, action, func(ctx context.Context) (string, error) {
return uc.getByUserCode(ctx, action, code)
})
if err != nil {
return
}
utils.Json2Struct(&rp, str)
func (uc *PermissionUseCase) GetByUserCode(ctx context.Context, code string) (rp *Permission) {
rp = uc.repo.GetByUserCode(ctx, code)
return
}

func (uc *PermissionUseCase) getByUserCode(ctx context.Context, action string, code string) (res string, err error) {
// read data from db and write to cache
rp := &Permission{}
permission, err := uc.repo.GetByUserCode(ctx, code)
notFound := errors.Is(err, ErrRecordNotFound(ctx))
if err != nil && !notFound {
return
}
copierx.Copy(&rp, permission)
res = utils.Struct2Json(rp)
uc.cache.Set(ctx, action, res, notFound)
return
}

func (uc *PermissionUseCase) FlushCache(ctx context.Context) {
uc.cache.Flush(ctx, func(ctx context.Context) (err error) {
return
})
}
16 changes: 6 additions & 10 deletions internal/biz/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ type UserRepo interface {
WrongPwd(ctx context.Context, req LoginTime) error
UpdatePassword(ctx context.Context, item *User) error
IdExists(ctx context.Context, id uint64) error
GetByCode(ctx context.Context, code string) (*User, error)
}

type UserUseCase struct {
Expand Down Expand Up @@ -173,10 +172,7 @@ func (uc *UserUseCase) Update(ctx context.Context, item *UpdateUser) error {
func (uc *UserUseCase) Delete(ctx context.Context, ids ...uint64) error {
return uc.tx.Tx(ctx, func(ctx context.Context) error {
return uc.cache.Flush(ctx, func(ctx context.Context) (err error) {
info, err := uc.InfoFromCtx(ctx)
if err != nil {
return
}
info := uc.InfoFromCtx(ctx)
if funk.ContainsUInt64(ids, info.Id) {
err = ErrDeleteYourself(ctx)
return
Expand All @@ -187,8 +183,8 @@ func (uc *UserUseCase) Delete(ctx context.Context, ids ...uint64) error {
})
}

func (uc *UserUseCase) GetUserByCode(ctx context.Context, code string) (rp *User, err error) {
return uc.repo.GetByCode(ctx, code)
func (uc *UserUseCase) GetUserByCode(ctx context.Context, code string) (rp *User) {
return uc.hotspot.GetUserByCode(ctx, code)
}

func (uc *UserUseCase) Find(ctx context.Context, condition *FindUser) (rp []User, err error) {
Expand Down Expand Up @@ -217,14 +213,14 @@ func (uc *UserUseCase) find(ctx context.Context, action string, condition *FindU
return
}

func (uc *UserUseCase) InfoFromCtx(ctx context.Context) (rp *UserInfo, err error) {
func (uc *UserUseCase) InfoFromCtx(ctx context.Context) (rp *UserInfo) {
user := jwt.FromServerContext(ctx)
return uc.Info(ctx, user.Code)
}

func (uc *UserUseCase) Info(ctx context.Context, code string) (rp *UserInfo, err error) {
func (uc *UserUseCase) Info(ctx context.Context, code string) (rp *UserInfo) {
rp = &UserInfo{}
user, err := uc.GetUserByCode(ctx, code)
user := uc.hotspot.GetUserByCode(ctx, code)
utils.Struct2StructByJson(rp, user)
return
}
Expand Down
1 change: 0 additions & 1 deletion internal/biz/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type UserGroupRepo interface {
Find(ctx context.Context, condition *FindUserGroup) []UserGroup
Update(ctx context.Context, item *UpdateUserGroup) error
Delete(ctx context.Context, ids ...uint64) error
FindGroupByUserCode(ctx context.Context, code string) []UserGroup
}

type UserGroupUseCase struct {
Expand Down
5 changes: 2 additions & 3 deletions internal/biz/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ func (uc *WhitelistUseCase) find(ctx context.Context, action string, condition *
return
}

func (uc *WhitelistUseCase) Has(ctx context.Context, condition *HasWhitelist) (rp bool, err error) {
rp = uc.repo.Has(ctx, condition)
return
func (uc *WhitelistUseCase) Has(ctx context.Context, condition *HasWhitelist) (rp bool) {
return uc.repo.Has(ctx, condition)
}

func (uc *WhitelistUseCase) Update(ctx context.Context, item *UpdateWhitelist) error {
Expand Down
Loading

0 comments on commit 260ea04

Please sign in to comment.