summaryrefslogtreecommitdiff
path: root/handlers/admin.go
diff options
context:
space:
mode:
authorhaturatu <taro@eyes4you.org>2025-10-12 17:04:07 +0900
committerhaturatu <taro@eyes4you.org>2025-10-12 17:04:07 +0900
commit957dc75eddf69ccf26bd387828b9e205a5425f30 (patch)
tree15a3f5581dded804a383c1d42e5b7d4b967da11e /handlers/admin.go
parent10c90d8654b94916bf0a4ffb8263e512a8e6be04 (diff)
add(api): api endpoint authorized Bearer token
Diffstat (limited to 'handlers/admin.go')
-rw-r--r--handlers/admin.go16
1 files changed, 10 insertions, 6 deletions
diff --git a/handlers/admin.go b/handlers/admin.go
index d9fb310..44c36a5 100644
--- a/handlers/admin.go
+++ b/handlers/admin.go
@@ -24,7 +24,9 @@ func AdminPageHandler(w http.ResponseWriter, r *http.Request) {
"Paths": config.Paths,
"Users": users,
}
- templates.ExecuteTemplate(w, "admin.html", data)
+ if err := templates.ExecuteTemplate(w, "admin.html", data); err != nil {
+ logging.AppLog.Error("Failed to execute admin template", "error", err)
+ }
}
// GetUsersHandler returns a list of all users.
@@ -38,14 +40,16 @@ func GetUsersHandler(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(users)
+ if err := json.NewEncoder(w).Encode(users); err != nil {
+ logging.AppLog.Error("Failed to encode users to JSON", "error", err, "ip", ip)
+ }
}
// UpdateUserRoleHandler updates a user's role.
func UpdateUserRoleHandler(w http.ResponseWriter, r *http.Request) {
ip := logging.GetClientIP(r)
// Get admin user from context
- adminUser, ok := r.Context().Value(types.SessionDataContextKey).(*models.User)
+ adminUser, ok := r.Context().Value(types.SessionDataContextKey).(*types.User)
if !ok || adminUser == nil {
logging.AppLog.Error("Failed to get user from context for admin action", "ip", ip)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
@@ -90,7 +94,7 @@ func UpdateUserRoleHandler(w http.ResponseWriter, r *http.Request) {
func DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
ip := logging.GetClientIP(r)
// Get admin user from context
- adminUser, ok := r.Context().Value(types.SessionDataContextKey).(*models.User)
+ adminUser, ok := r.Context().Value(types.SessionDataContextKey).(*types.User)
if !ok || adminUser == nil {
logging.AppLog.Error("Failed to get user from context for admin action", "ip", ip)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
@@ -119,7 +123,7 @@ func DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
func SetUserActiveStatusHandler(w http.ResponseWriter, r *http.Request) {
ip := logging.GetClientIP(r)
// Get admin user from context
- adminUser, ok := r.Context().Value(types.SessionDataContextKey).(*models.User)
+ adminUser, ok := r.Context().Value(types.SessionDataContextKey).(*types.User)
if !ok || adminUser == nil {
logging.AppLog.Error("Failed to get user from context for admin action", "ip", ip)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
@@ -152,4 +156,4 @@ func SetUserActiveStatusHandler(w http.ResponseWriter, r *http.Request) {
logging.SecurityLog.Info("ADMIN ACTION", "action", "set_active_status", "performed_by", adminUsername, "target_user_id", id, "new_status", req.IsActive, "ip", ip)
w.WriteHeader(http.StatusOK)
-} \ No newline at end of file
+}