admin: add admin panel for user management
This commit is contained in:
32
web/shared/admin/admin.go
Normal file
32
web/shared/admin/admin.go
Normal file
@@ -0,0 +1,32 @@
|
||||
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 {
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
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))
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package layout
|
||||
|
||||
import "mal/web/components/icons"
|
||||
import (
|
||||
"mal/web/components/icons"
|
||||
"mal/web/shared/admin"
|
||||
)
|
||||
|
||||
templ Layout(title string, showHeader bool) {
|
||||
<!DOCTYPE html>
|
||||
@@ -60,6 +63,14 @@ templ Layout(title string, showHeader bool) {
|
||||
>
|
||||
Watchlist
|
||||
</a>
|
||||
if admin.IsAdminFromContext(ctx) {
|
||||
<a
|
||||
class="text-(--accent) no-underline hover:text-(--accent) hover:no-underline"
|
||||
href="/admin"
|
||||
>
|
||||
Admin
|
||||
</a>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
|
||||
Reference in New Issue
Block a user