refac: debt on role backend
This commit is contained in:
1 parent
de142f6c5f
commit
a043f7f41a
130 files changed
+3591
-512
No files matched your search
@@ -3,20 +3,27 @@ package access
|
||||
import (
|
||||
"RenewCMS/api/pkg/apperr"
|
||||
"RenewCMS/internal/domain/role"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type grantsContextKey struct{}
|
||||
type Caller = useCases.ContentCaller
|
||||
|
||||
func NewContext(ctx context.Context, grants role.GrantSet) context.Context {
|
||||
return context.WithValue(ctx, grantsContextKey{}, grants)
|
||||
type callerContextKey struct{}
|
||||
|
||||
func WithCaller(ctx context.Context, caller Caller) context.Context {
|
||||
return context.WithValue(ctx, callerContextKey{}, caller)
|
||||
}
|
||||
|
||||
func CallerOf(r *http.Request) Caller {
|
||||
caller, _ := r.Context().Value(callerContextKey{}).(Caller)
|
||||
return caller
|
||||
}
|
||||
|
||||
func Grants(r *http.Request) role.GrantSet {
|
||||
grants, _ := r.Context().Value(grantsContextKey{}).(role.GrantSet)
|
||||
return grants
|
||||
return CallerOf(r).Grants
|
||||
}
|
||||
|
||||
func WriteMissingPermission(w http.ResponseWriter, key string) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package access
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/user"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type currentUserContextKey struct{}
|
||||
|
||||
type currentUserHolder struct {
|
||||
loaded bool
|
||||
userID uint32
|
||||
account user.User
|
||||
err error
|
||||
}
|
||||
|
||||
func WithCurrentUser(r *http.Request) *http.Request {
|
||||
if _, present := r.Context().Value(currentUserContextKey{}).(*currentUserHolder); present {
|
||||
return r
|
||||
}
|
||||
return r.WithContext(context.WithValue(r.Context(), currentUserContextKey{}, ¤tUserHolder{}))
|
||||
}
|
||||
|
||||
func LoadUser(getUser *useCases.GetUserUseCase, r *http.Request, userID uint32) (user.User, error) {
|
||||
holder, present := r.Context().Value(currentUserContextKey{}).(*currentUserHolder)
|
||||
if !present {
|
||||
return getUser.GetUser(userID)
|
||||
}
|
||||
if !holder.loaded || holder.userID != userID {
|
||||
holder.account, holder.err = getUser.GetUser(userID)
|
||||
holder.userID = userID
|
||||
holder.loaded = true
|
||||
}
|
||||
return holder.account, holder.err
|
||||
}
|
||||
Reference in new issue
Block a user