218 lines
5.4 KiB
Go
218 lines
5.4 KiB
Go
package rbac
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/guxuan/hailin_service/internal/config"
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/api"
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/schema"
|
|
"github.com/guxuan/hailin_service/pkg/logging"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type RBAC struct {
|
|
DB *gorm.DB
|
|
MenuAPI *api.Menu
|
|
RoleAPI *api.Role
|
|
UserAPI *api.User
|
|
LoginAPI *api.Login
|
|
LoggerAPI *api.Logger
|
|
BannerAPI *api.Banner
|
|
ArticleAPI *api.Article
|
|
VideoAPI *api.Video
|
|
UploadAPI *api.Upload
|
|
JobAPI *api.Job
|
|
WebAPI *api.Web
|
|
WebSiteAPI *api.WebSite
|
|
TeamAPI *api.Team
|
|
MemorabiliaAPI *api.Memorabilia
|
|
ProductAPI *api.Product
|
|
|
|
Casbinx *Casbinx
|
|
}
|
|
|
|
func (a *RBAC) AutoMigrate(ctx context.Context) error {
|
|
return a.DB.AutoMigrate(
|
|
new(schema.Menu),
|
|
new(schema.MenuResource),
|
|
new(schema.Role),
|
|
new(schema.RoleMenu),
|
|
new(schema.User),
|
|
new(schema.UserRole),
|
|
new(schema.Banner),
|
|
new(schema.Article),
|
|
new(schema.Video),
|
|
new(schema.Memorabilia),
|
|
new(schema.Job),
|
|
new(schema.JobArea),
|
|
new(schema.WebSite),
|
|
new(schema.Team),
|
|
new(schema.Product),
|
|
new(schema.ProductCategory),
|
|
)
|
|
}
|
|
|
|
func (a *RBAC) Init(ctx context.Context) error {
|
|
if config.C.Storage.DB.AutoMigrate {
|
|
if err := a.AutoMigrate(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := a.Casbinx.Load(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if name := config.C.General.MenuFile; name != "" {
|
|
fullPath := filepath.Join(config.C.General.WorkDir, name)
|
|
if err := a.MenuAPI.MenuBIZ.InitFromFile(ctx, fullPath); err != nil {
|
|
logging.Context(ctx).Error("failed to init menu data", zap.Error(err), zap.String("file", fullPath))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *RBAC) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
|
|
captcha := v1.Group("captcha")
|
|
{
|
|
captcha.GET("id", a.LoginAPI.GetCaptcha)
|
|
captcha.GET("image", a.LoginAPI.ResponseCaptcha)
|
|
}
|
|
|
|
v1.POST("login", a.LoginAPI.Login)
|
|
v1.POST("upload", a.UploadAPI.SaveFile)
|
|
|
|
current := v1.Group("current")
|
|
{
|
|
current.POST("refresh-token", a.LoginAPI.RefreshToken)
|
|
current.GET("user", a.LoginAPI.GetUserInfo)
|
|
current.GET("menus", a.LoginAPI.QueryMenus)
|
|
current.PUT("password", a.LoginAPI.UpdatePassword)
|
|
current.PUT("user", a.LoginAPI.UpdateUser)
|
|
current.POST("logout", a.LoginAPI.Logout)
|
|
}
|
|
|
|
banner := v1.Group("banners")
|
|
{
|
|
banner.GET("", a.BannerAPI.Query)
|
|
banner.GET(":id", a.BannerAPI.Get)
|
|
banner.POST("", a.BannerAPI.Create)
|
|
banner.PUT(":id", a.BannerAPI.Update)
|
|
banner.DELETE(":id", a.BannerAPI.Delete)
|
|
}
|
|
|
|
job := v1.Group("jobs")
|
|
{
|
|
job.GET("", a.JobAPI.Query)
|
|
job.GET("job_areas", a.JobAPI.QueryJobArea)
|
|
job.POST("job_areas", a.JobAPI.CreateJobArea)
|
|
job.PUT("job_areas/:id", a.JobAPI.UpdateJobArea)
|
|
job.DELETE("job_areas/:id", a.JobAPI.DeleteJobArea)
|
|
job.GET(":id", a.JobAPI.Get)
|
|
job.POST("", a.JobAPI.Create)
|
|
job.PUT(":id", a.JobAPI.Update)
|
|
job.DELETE(":id", a.JobAPI.Delete)
|
|
}
|
|
memorabilia := v1.Group("memorabilias")
|
|
{
|
|
memorabilia.GET("", a.MemorabiliaAPI.Query)
|
|
memorabilia.GET(":id", a.MemorabiliaAPI.Get)
|
|
memorabilia.POST("", a.MemorabiliaAPI.Create)
|
|
memorabilia.PUT(":id", a.MemorabiliaAPI.Update)
|
|
memorabilia.DELETE(":id", a.MemorabiliaAPI.Delete)
|
|
}
|
|
team := v1.Group("teams")
|
|
{
|
|
team.GET("", a.TeamAPI.Query)
|
|
team.GET(":id", a.TeamAPI.Get)
|
|
team.POST("", a.TeamAPI.Create)
|
|
team.PUT(":id", a.TeamAPI.Update)
|
|
team.DELETE(":id", a.TeamAPI.Delete)
|
|
}
|
|
webSite := v1.Group("web_site")
|
|
{
|
|
webSite.GET("", a.WebSiteAPI.Query)
|
|
webSite.POST("", a.WebSiteAPI.Create)
|
|
webSite.PUT(":id", a.WebSiteAPI.Update)
|
|
}
|
|
product := v1.Group("products")
|
|
{
|
|
product.GET("", a.ProductAPI.Query)
|
|
product.GET("categorys", a.ProductAPI.QueryCategory)
|
|
product.GET(":id", a.ProductAPI.Get)
|
|
product.POST("", a.ProductAPI.Create)
|
|
product.PUT(":id", a.ProductAPI.Update)
|
|
product.DELETE(":id", a.ProductAPI.Delete)
|
|
}
|
|
|
|
article := v1.Group("articles")
|
|
{
|
|
article.GET("", a.ArticleAPI.Query)
|
|
article.GET(":id", a.ArticleAPI.Get)
|
|
article.POST("", a.ArticleAPI.Create)
|
|
article.PUT(":id", a.ArticleAPI.Update)
|
|
article.DELETE(":id", a.ArticleAPI.Delete)
|
|
}
|
|
video := v1.Group("videos")
|
|
{
|
|
video.GET("", a.VideoAPI.Query)
|
|
video.GET(":id", a.VideoAPI.Get)
|
|
video.POST("", a.VideoAPI.Create)
|
|
video.PUT(":id", a.VideoAPI.Update)
|
|
video.DELETE(":id", a.VideoAPI.Delete)
|
|
}
|
|
|
|
menu := v1.Group("menus")
|
|
{
|
|
menu.GET("", a.MenuAPI.Query)
|
|
menu.GET(":id", a.MenuAPI.Get)
|
|
menu.POST("", a.MenuAPI.Create)
|
|
menu.PUT(":id", a.MenuAPI.Update)
|
|
menu.DELETE(":id", a.MenuAPI.Delete)
|
|
}
|
|
|
|
role := v1.Group("roles")
|
|
{
|
|
role.GET("", a.RoleAPI.Query)
|
|
role.GET(":id", a.RoleAPI.Get)
|
|
role.POST("", a.RoleAPI.Create)
|
|
role.PUT(":id", a.RoleAPI.Update)
|
|
role.DELETE(":id", a.RoleAPI.Delete)
|
|
}
|
|
|
|
user := v1.Group("users")
|
|
{
|
|
user.GET("", a.UserAPI.Query)
|
|
user.GET(":id", a.UserAPI.Get)
|
|
user.POST("", a.UserAPI.Create)
|
|
user.PUT(":id", a.UserAPI.Update)
|
|
user.DELETE(":id", a.UserAPI.Delete)
|
|
user.PATCH(":id/reset-pwd", a.UserAPI.ResetPassword)
|
|
}
|
|
|
|
web := v1.Group("web")
|
|
{
|
|
web.GET("jobs", a.WebAPI.QueryJob)
|
|
web.GET("articles", a.WebAPI.QueryArticle)
|
|
|
|
}
|
|
|
|
logger := v1.Group("loggers")
|
|
{
|
|
logger.GET("", a.LoggerAPI.Query)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *RBAC) Release(ctx context.Context) error {
|
|
if err := a.Casbinx.Release(ctx); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|