feat: 统一sql命名、函数命名
This commit is contained in:
@@ -30,43 +30,7 @@ func NewAuthMiddleware(store *db.Store, cache *cache.Caches) *AuthMiddleware {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *AuthMiddleware) Middleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
|
||||
claims, ok := GetClaims(ctx)
|
||||
if !ok || claims.UserID == 0 {
|
||||
httputil.Fail(w, errs.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否有管理员权限 目前只判断uid是否为1
|
||||
isAdmin := userIsAdmin(claims.UserID)
|
||||
|
||||
if isAdmin {
|
||||
ctx = context.WithValue(ctx, IsAdminKey, isAdmin)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
|
||||
// 不是管理员 判断api权限
|
||||
hasPermission, err := userHasApiPermission(ctx, r, m.store, claims.UserID, m.cache)
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !hasPermission {
|
||||
httputil.Fail(w, errs.ErrPermissionDenied)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func userIsAdmin(uid int32) bool {
|
||||
func UserIsAdmin(uid int32) bool {
|
||||
if uid == 1 {
|
||||
return true
|
||||
}
|
||||
@@ -74,31 +38,79 @@ func userIsAdmin(uid int32) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func userHasApiPermission(ctx context.Context, r *http.Request, store *db.Store, uid int32, c *cache.Caches) (bool, error) {
|
||||
func IsAdmin(ctx context.Context) bool {
|
||||
isAdmin, ok := ctx.Value(IsAdminKey).(bool)
|
||||
return ok && isAdmin
|
||||
}
|
||||
|
||||
func (m *AuthMiddleware) hasApiPermission(ctx context.Context, uid int32, requestMethod string, requestPath string) (bool, error) {
|
||||
var (
|
||||
apis []sqlc.GetSysUserApisRow
|
||||
apis []sqlc.ListUserApisRow
|
||||
err error
|
||||
)
|
||||
|
||||
// 先从缓存中获取api数据
|
||||
k := cachekey.UserApiPermissions(uid)
|
||||
apis, err = cache.GetOrSetJSON[[]sqlc.GetSysUserApisRow](ctx, c, k, 0, func() ([]sqlc.GetSysUserApisRow, error) {
|
||||
return store.GetSysUserApis(ctx, uid)
|
||||
apis, err = cache.GetOrSetJSON[[]sqlc.ListUserApisRow](ctx, m.cache, k, 0, func() ([]sqlc.ListUserApisRow, error) {
|
||||
return m.store.ListUserApis(ctx, uid)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
requestPath := chi.RouteContext(r.Context()).RoutePattern()
|
||||
requestPath = strings.TrimPrefix(requestPath, "/api")
|
||||
requestMethod := r.Method
|
||||
|
||||
for _, api := range apis {
|
||||
if api.Path == requestPath && api.Method == requestMethod {
|
||||
if api.Method != requestMethod {
|
||||
continue
|
||||
}
|
||||
|
||||
if api.Path == requestPath {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (m *AuthMiddleware) Middleware(router chi.Router) func(handler http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
claims, ok := GetClaims(ctx)
|
||||
if !ok || claims.UserID == 0 {
|
||||
httputil.Fail(w, errs.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否有管理员权限
|
||||
isAdmin := UserIsAdmin(claims.UserID)
|
||||
|
||||
if isAdmin {
|
||||
ctx = context.WithValue(ctx, IsAdminKey, isAdmin)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
|
||||
// 如果不是管理员 需要判断api权限
|
||||
rctx := chi.NewRouteContext()
|
||||
requestMethod := r.Method
|
||||
// https://github.com/go-chi/chi/pull/872
|
||||
api := router.Find(rctx, requestMethod, r.URL.Path)
|
||||
requestPath := strings.TrimPrefix(api, "/api")
|
||||
|
||||
hasPermission, err := m.hasApiPermission(ctx, claims.UserID, requestMethod, requestPath)
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !hasPermission {
|
||||
httputil.Fail(w, errs.ErrPermissionDenied)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"server/internal/model/common"
|
||||
"server/internal/utils"
|
||||
"server/internal/pkg/httputil"
|
||||
"time"
|
||||
|
||||
gonanoid "github.com/matoous/go-nanoid/v2"
|
||||
@@ -71,7 +71,7 @@ func (m *LoggerMiddleware) Middleware(next http.Handler) http.Handler {
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
ip := utils.ClientIP(r)
|
||||
ip := httputil.ClientIP(r)
|
||||
|
||||
fullPath := r.URL.Path
|
||||
if r.URL.RawQuery != "" {
|
||||
|
||||
Reference in New Issue
Block a user