Skip to content

Commit

Permalink
支持角色权限配置
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinwang committed May 4, 2023
1 parent bdb3099 commit f7f85c7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
4 changes: 2 additions & 2 deletions handler/RolePermissionHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
"horizon/service"
)

func RolePermissionGet(c *gin.Context) {
service.RolePermissionSelectByList(c)
func RolePermissionPost(c *gin.Context) {
service.RolePermissionInsert(c)
}
1 change: 1 addition & 0 deletions model/Menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Menu struct {
Redirect string `gorm:"type:varchar(255);comment:重定向" json:"redirect"`
Path string `gorm:"type:varchar(255);comment:路径" json:"path"`
ActionData datatypes.JSON `gorm:"type:json;null;comment:按钮操作数据" json:"actionData"`
ActionList datatypes.JSON `gorm:"type:json;null;comment:按钮操作数据列表" json:"actionList"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
RolePermission []RolePermission `gorm:"foreignKey:MenuId"`
Expand Down
1 change: 1 addition & 0 deletions router/Router.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func commonRouter(r *gin.Engine) {
role.POST("", handler.RolePost)
role.PUT("", handler.RolePut)
role.DELETE("/:id", handler.RoleDelete)
role.POST("permission", handler.RolePermissionPost)

// menu group
menu := api.Group("/menu", authMiddleware.MiddlewareFunc())
Expand Down
51 changes: 48 additions & 3 deletions service/RolePermissionService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,56 @@ package service

import (
"github.com/gin-gonic/gin"
"gorm.io/datatypes"
"horizon/model"
"net/http"
)

// RolePermissionSelectByList 查询角色权限列表
func RolePermissionSelectByList(c *gin.Context) {
// RolePermissionInsert 新增角色权限
func RolePermissionInsert(c *gin.Context) {
// 参数映射到对象
type permission struct {
model.Menu
Selected datatypes.JSON `json:"selected"`
SelectedData datatypes.JSON `json:"selectedData"`
}
var permBody struct {
Role model.Role `json:"role"`
Permissions []permission `json:"permissions"`
}
if err := c.ShouldBind(&permBody); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "fail", "data": "", "err": err.Error()})
return
}
var rolePermissions []model.RolePermission
parentIds := make(map[uint]uint)
for _, permission := range permBody.Permissions {
if len(permission.Selected) > 0 && permission.Selected.String() != "[]" {
// 添加选择的节点的父节点
if _, ok := parentIds[permission.ParentId]; !ok {
parentIds[permission.ParentId] = permission.ParentId
rolePermissions = append(rolePermissions, model.RolePermission{
RoleId: permBody.Role.ID,
MenuId: permission.ParentId,
ActionData: datatypes.JSON("[]"),
ActionList: datatypes.JSON("[]"),
})
}
// 添加选择的节点
rolePermissions = append(rolePermissions, model.RolePermission{
RoleId: permBody.Role.ID,
MenuId: permission.ID,
ActionData: permission.SelectedData,
ActionList: permission.Selected,
})
}
}

c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "success", "data": "", "err": ""})
model.Db.Debug().Delete(&model.RolePermission{}, "role_id = ?", permBody.Role.ID)
result := model.Db.Debug().Create(&rolePermissions)
if result.Error != nil {
c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "fail", "data": "", "err": result.Error.Error()})
} else {
c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "success", "data": "", "err": ""})
}
}

0 comments on commit f7f85c7

Please sign in to comment.