This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/internal/common/helper.go
Victor_Jay ed1a969e68 feat(footer): 页脚新增管理面板入口链接(moderator+可见)
chore: 站点名称保持硬编码 MetaLab,后台移除该设置项,恢复 Nav 双拼样式
       site.brand 后端逻辑保留,页脚仍可配置
2026-05-27 15:33:03 +08:00

70 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import (
"metazone.cc/metalab/internal/model"
"github.com/gin-gonic/gin"
)
// BuildPageData 构建页面模板数据,自动注入登录状态、站点信息与 CSRF token
func BuildPageData(c *gin.Context, extra gin.H) gin.H {
data := gin.H{}
for k, v := range extra {
data[k] = v
}
if username, exists := c.Get("username"); exists {
data["IsLoggedIn"] = true
data["Username"] = username
}
// 注入 CSRF token由 CSRF 中间件设置到上下文中)
if token, exists := c.Get("csrf_token"); exists {
data["CSRFToken"] = token
}
// 注入站点信息(由 InjectSiteInfo 中间件设置)
injectSiteInfo(c, data)
// 注入管理入口权限moderator 及以上可看到页脚管理面板链接)
if role, exists := c.Get("role"); exists {
data["CanAccessAdmin"] = model.HasMinRole(role.(string), model.RoleModerator)
}
return data
}
// BuildAdminPageData 构建管理后台模板数据
// 额外注入 UID、Role、CanManageUsers、CurrentPath 等权限信息
func BuildAdminPageData(c *gin.Context, extra gin.H) gin.H {
data := BuildPageData(c, extra)
data["IsAdmin"] = true
data["CurrentPath"] = c.Request.URL.Path
if uid, exists := c.Get("uid"); exists {
data["UID"] = uid
}
if role, exists := c.Get("role"); exists {
roleStr := role.(string)
data["Role"] = roleStr
if name, ok := model.RoleDisplayNames[roleStr]; ok {
data["RoleDisplayName"] = name
} else {
data["RoleDisplayName"] = roleStr
}
data["CanManageUsers"] = model.HasMinRole(roleStr, model.RoleAdmin)
data["CanManageAudits"] = model.HasMinRole(roleStr, model.RoleAdmin)
data["CanManageSettings"] = model.HasMinRole(roleStr, model.RoleOwner)
data["IsOwner"] = model.HasMinRole(roleStr, model.RoleOwner)
}
return data
}
// injectSiteInfo 从 context 注入站点展示信息
func injectSiteInfo(c *gin.Context, data gin.H) {
if brand, exists := c.Get("site_brand"); exists {
data["Brand"] = brand
}
if framework, exists := c.Get("site_framework"); exists {
data["Framework"] = framework
}
if copyrightStart, exists := c.Get("site_copyright_start"); exists {
data["CopyrightStart"] = copyrightStart
}
}