fix: resolve context key cycle for admin check
This commit is contained in:
@@ -1,15 +1,9 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mal/internal/db"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const userContextKey contextKey = "user"
|
||||
|
||||
const AdminEmail = "mikkelelvers@outlook.com"
|
||||
|
||||
func IsAdmin(user *database.User) bool {
|
||||
@@ -19,14 +13,8 @@ func IsAdmin(user *database.User) bool {
|
||||
return user.Username == AdminEmail
|
||||
}
|
||||
|
||||
func GetUser(ctx context.Context) *database.User {
|
||||
user, ok := ctx.Value(userContextKey).(*database.User)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func IsAdminFromContext(ctx context.Context) bool {
|
||||
return IsAdmin(GetUser(ctx))
|
||||
}
|
||||
func IsAdminFromContext(ctx interface{ Value(key interface{}) interface{} }) bool {
|
||||
const userKey = "mal:user"
|
||||
user, _ := ctx.Value(userKey).(*database.User)
|
||||
return IsAdmin(user)
|
||||
}
|
||||
Reference in New Issue
Block a user