Files
kevin cefaeac618 feat: add admin user management with role-based access and self-protection
- Add AdminRequired middleware for admin-role routes
- Add login status check to reject disabled/locked accounts
- Add CRUD handlers (list, create, edit, delete) for users
- Add user_list and user_form templates with role/status badges
- Add nav entry and dashboard button for user management
- Add dynamic user count on admin dashboard
- Fix userIDFromSession to handle all numeric session types
- Self-protection: cannot delete/disable self or demote last admin
- Add EN/ZH i18n keys for all user management strings
2026-06-22 18:11:39 +08:00

36 lines
878 B
Go

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"go_blog/models"
"gorm.io/gorm"
)
// AdminDashboard renders the protected admin dashboard.
func AdminDashboard(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
tr := getTr(c)
username, _ := c.Get("username")
data := DefaultData(c)
data["Title"] = tr["dash_page_title"]
data["Username"] = username
// Article counts: total and published.
var postCount, publishedCount int64
db.Model(&models.Article{}).Count(&postCount)
db.Model(&models.Article{}).Where("status = ?", models.ArticlePublished).Count(&publishedCount)
data["PostCount"] = postCount
data["PublishedCount"] = publishedCount
// User count for the stats card.
var userCount int64
db.Model(&models.User{}).Count(&userCount)
data["UserCount"] = userCount
c.HTML(http.StatusOK, "dashboard", data)
}
}