Files
oidc-server/handlers/admin_handler.go

218 lines
5.4 KiB
Go

package handlers
import (
"math"
"net/http"
"strconv"
"oidc-oauth2-server/services"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type AdminHandler struct {
adminService *services.AdminService
clientService *services.ClientService
}
func NewAdminHandler(adminService *services.AdminService, clientService *services.ClientService) *AdminHandler {
return &AdminHandler{adminService: adminService, clientService: clientService}
}
func (h *AdminHandler) ShowAdminLogin(c *gin.Context) {
c.HTML(http.StatusOK, "admin_login.html", gin.H{})
}
func (h *AdminHandler) HandleAdminLogin(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
admin, err := h.adminService.Authenticate(username, password)
if err != nil {
c.HTML(http.StatusBadRequest, "admin_login.html", gin.H{
"error": "Invalid credentials",
})
return
}
session := sessions.Default(c)
session.Set("admin_id", admin.ID)
session.Save()
c.Redirect(http.StatusFound, "/admin/dashboard")
}
func (h *AdminHandler) Dashboard(c *gin.Context) {
c.HTML(http.StatusOK, "admin_dashboard.html", gin.H{})
}
func (h *AdminHandler) ListUsers(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
users, total, err := h.adminService.ListUsers(page, pageSize)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.HTML(http.StatusOK, "admin_users.html", gin.H{
"users": users,
"total": total,
"page": page,
"pageSize": pageSize,
})
}
// ListClients 显示客户端列表页面
func (h *AdminHandler) ListClients(c *gin.Context) {
page := 1
pageSize := 10
// 从查询参数获取分页信息
if pageStr := c.Query("page"); pageStr != "" {
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
page = p
}
}
if pageSizeStr := c.Query("page_size"); pageSizeStr != "" {
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
pageSize = ps
}
}
// 获取客户端列表
clients, total, err := h.clientService.GetClients(page, pageSize)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"error": err.Error(),
})
return
}
c.HTML(http.StatusOK, "admin_clients.html", gin.H{
"clients": clients,
"page": page,
"pageSize": pageSize,
"total": total,
"lastPage": int(math.Ceil(float64(total) / float64(pageSize))),
})
}
// ShowCreateUser 显示创建用户页面
func (h *AdminHandler) ShowCreateUser(c *gin.Context) {
c.HTML(http.StatusOK, "admin_create_user.html", gin.H{
"title": "创建用户",
})
}
// HandleCreateUser 处理创建用户请求
func (h *AdminHandler) HandleCreateUser(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
email := c.PostForm("email")
if username == "" || password == "" || email == "" {
c.HTML(http.StatusBadRequest, "admin_create_user.html", gin.H{
"title": "创建用户",
"error": "用户名、密码和邮箱都不能为空",
})
return
}
_, err := h.adminService.CreateUser(username, password, email)
if err != nil {
c.HTML(http.StatusBadRequest, "admin_create_user.html", gin.H{
"title": "创建用户",
"error": "创建用户失败:" + err.Error(),
})
return
}
c.Redirect(http.StatusFound, "/admin/users")
}
// ShowEditUser 显示编辑用户页面
func (h *AdminHandler) ShowEditUser(c *gin.Context) {
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"error": "无效的用户ID",
})
return
}
user, err := h.adminService.GetUser(uint(userID))
if err != nil {
c.HTML(http.StatusNotFound, "error.html", gin.H{
"error": "用户不存在",
})
return
}
c.HTML(http.StatusOK, "admin_edit_user.html", gin.H{
"title": "编辑用户",
"user": user,
})
}
// HandleEditUser 处理编辑用户请求
func (h *AdminHandler) HandleEditUser(c *gin.Context) {
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户ID"})
return
}
username := c.PostForm("username")
email := c.PostForm("email")
isActiveStr := c.PostForm("is_active")
var isActive *bool
if isActiveStr != "" {
active := isActiveStr == "true"
isActive = &active
}
_, err = h.adminService.UpdateUser(uint(userID), username, email, isActive)
if err != nil {
c.HTML(http.StatusBadRequest, "admin_edit_user.html", gin.H{
"title": "编辑用户",
"error": "更新用户失败:" + err.Error(),
})
return
}
// 如果提供了新密码,则更新密码
newPassword := c.PostForm("password")
if newPassword != "" {
err = h.adminService.UpdateUserPassword(uint(userID), newPassword)
if err != nil {
c.HTML(http.StatusBadRequest, "admin_edit_user.html", gin.H{
"title": "编辑用户",
"error": "更新密码失败:" + err.Error(),
})
return
}
}
c.Redirect(http.StatusFound, "/admin/users")
}
// HandleDeleteUser 处理删除用户请求
func (h *AdminHandler) HandleDeleteUser(c *gin.Context) {
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户ID"})
return
}
err = h.adminService.DeleteUser(uint(userID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除用户失败:" + err.Error()})
return
}
c.Redirect(http.StatusFound, "/admin/users")
}