更新 Go 版本至 1.23.0,添加管理员功能,包括管理员登录、用户和客户端管理,新增相应的模板和中间件,优化数据库模型以支持管理员管理。
This commit is contained in:
82
handlers/admin_handler.go
Normal file
82
handlers/admin_handler.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"oidc-oauth2-server/services"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AdminHandler struct {
|
||||
adminService *services.AdminService
|
||||
}
|
||||
|
||||
func NewAdminHandler(adminService *services.AdminService) *AdminHandler {
|
||||
return &AdminHandler{adminService: adminService}
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AdminHandler) ListClients(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
||||
|
||||
clients, total, err := h.adminService.ListClients(page, pageSize)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "admin_clients.html", gin.H{
|
||||
"clients": clients,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
}
|
||||
42
handlers/auth_handler.go
Normal file
42
handlers/auth_handler.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ShowSignup 显示注册页面
|
||||
func (h *AuthHandler) ShowSignup(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "signup.html", gin.H{
|
||||
"title": "注册",
|
||||
})
|
||||
}
|
||||
|
||||
// HandleSignup 处理用户注册
|
||||
func (h *AuthHandler) HandleSignup(c *gin.Context) {
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
email := c.PostForm("email")
|
||||
|
||||
if username == "" || password == "" || email == "" {
|
||||
c.HTML(http.StatusBadRequest, "signup.html", gin.H{
|
||||
"title": "注册",
|
||||
"error": "用户名、密码和邮箱都不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 创建新用户
|
||||
_, err := h.authService.CreateUser(username, password, email)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "signup.html", gin.H{
|
||||
"title": "注册",
|
||||
"error": "注册失败:" + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 注册成功后重定向到登录页
|
||||
c.Redirect(http.StatusFound, "/login")
|
||||
}
|
||||
Reference in New Issue
Block a user