Skip to content

Commit

Permalink
fix: 修复普通用户登陆之后菜单未能正常获取的问题 (opsre#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf authored Mar 9, 2023
1 parent e8c6bc3 commit 8c05e7d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 36 deletions.
25 changes: 12 additions & 13 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
push:
branches:
- main
release:
types: [created,published] # 表示在创建新的 Release 时触发

# Allows you to run this workflow manually from the Actions tab
# 可以手动触发
Expand All @@ -24,38 +26,35 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Get current date
id: date
run: echo "::set-output name=today::$(date +'%Y-%m-%d_%H-%M')"
- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v2

- name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}

- name: Login to DockerHub
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
# 所需要的体系结构,可以在 Available platforms 步骤中获取所有的可用架构
platforms: linux/amd64,linux/arm64/v8
# 镜像推送时间
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/arm64,linux/amd64
# 给清单打上多个标签
tags: |
eryajf/go-ldap-admin-server:${{ steps.date.outputs.today }}
eryajf/go-ldap-admin-server:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.GITHUB_REPOSITORY_NAME_PART }}:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.GITHUB_REPOSITORY_NAME_PART }}:${{ env.GITHUB_REF_NAME }}
16 changes: 8 additions & 8 deletions controller/menu_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import (

type MenuController struct{}

// // List 记录列表
// func (m *MenuController) List(c *gin.Context) {
// req := new(request.MenuListReq)
// Run(c, req, func() (interface{}, interface{}) {
// return logic.Menu.List(c, req)
// })
// }

// GetTree 菜单树
func (m *MenuController) GetTree(c *gin.Context) {
req := new(request.MenuGetTreeReq)
Expand All @@ -25,6 +17,14 @@ func (m *MenuController) GetTree(c *gin.Context) {
})
}

// GetUserMenuTreeByUserId 获取用户菜单树
func (m *MenuController) GetAccessTree(c *gin.Context) {
req := new(request.MenuGetAccessTreeReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.Menu.GetAccessTree(c, req)
})
}

// Add 新建
func (m *MenuController) Add(c *gin.Context) {
req := new(request.MenuAddReq)
Expand Down
32 changes: 31 additions & 1 deletion logic/menu_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (l MenuLogic) GetTree(c *gin.Context, req interface{}) (data interface{}, r
return nil, ReqAssertErr
}
_ = c

menus, err := isql.Menu.List()
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取资源列表失败: " + err.Error()))
Expand All @@ -178,3 +177,34 @@ func (l MenuLogic) GetTree(c *gin.Context, req interface{}) (data interface{}, r

return tree, nil
}

// GetAccessTree 获取用户菜单树
func (l MenuLogic) GetAccessTree(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.MenuGetAccessTreeReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
// 校验
filter := tools.H{"id": r.ID}
if !isql.User.Exist(filter) {
return nil, tools.NewValidatorError(fmt.Errorf("该用户不存在"))
}
user := new(model.User)
err := isql.User.Find(filter, user)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("在MySQL查询用户失败: " + err.Error()))
}
var roleIds []uint
for _, role := range user.Roles {
roleIds = append(roleIds, role.ID)
}
menus, err := isql.Menu.ListUserMenus(roleIds)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取资源列表失败: " + err.Error()))
}

tree := isql.GenMenuTree(0, menus)

return tree, nil
}
2 changes: 1 addition & 1 deletion model/request/group_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type GroupListReq struct {
Remark string `json:"remark" form:"remark"`
PageNum int `json:"pageNum" form:"pageNum"`
PageSize int `json:"pageSize" form:"pageSize"`
SyncState uint `json:"syncState" form:"syncState" `
SyncState uint `json:"syncState" form:"syncState"`
}

// GroupListAllReq 获取资源列表结构体,不分页
Expand Down
5 changes: 5 additions & 0 deletions model/request/menu_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ type MenuDeleteReq struct {
// MenuGetTreeReq 获取菜单树结构体
type MenuGetTreeReq struct {
}

// MenuGetAccessTreeReq 获取用户菜单树
type MenuGetAccessTreeReq struct {
ID uint `json:"id" form:"id"`
}
20 changes: 8 additions & 12 deletions public/common/init_mysql_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ func InitData() {
Remark: "获取菜单树",
Creator: "系统",
},
{
Method: "GET",
Path: "/menu/access/tree",
Category: "menu",
Remark: "获取用户菜单树",
Creator: "系统",
},
{
Method: "POST",
Path: "/menu/add",
Expand Down Expand Up @@ -667,19 +674,8 @@ func InitData() {
"/base/changePwd",
"/base/dashboard",
"/user/info",
"/user/list",
"/user/changePwd",
"/group/list",
"/group/tree",
"/group/useringroup",
"/group/usernoingroup",
"/role/list",
"/role/getmenulist",
"/role/getapilist",
"/menu/tree",
"/menu/list",
"/api/list",
"/api/tree",
"/menu/access/tree",
"/log/operation/list",
}

Expand Down
2 changes: 1 addition & 1 deletion routes/menu_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func InitMenuRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) gi
menu.Use(middleware.CasbinMiddleware())
{
menu.GET("/tree", controller.Menu.GetTree)
// menu.GET("/list", controller.Menu.List)
menu.GET("/access/tree", controller.Menu.GetAccessTree)
menu.POST("/add", controller.Menu.Add)
menu.POST("/update", controller.Menu.Update)
menu.POST("/delete", controller.Menu.Delete)
Expand Down

0 comments on commit 8c05e7d

Please sign in to comment.