初始
This commit is contained in:
64
test/role_test.go
Normal file
64
test/role_test.go
Normal file
@ -0,0 +1,64 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/samber/lo"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
)
|
||||
|
||||
const userID = 145
|
||||
|
||||
// TestGetUserRoles 获取用户角色
|
||||
func TestGetUserRoles(t *testing.T) {
|
||||
var a []model.AppMenusInfoRes
|
||||
// SELECT m.menu_id, m.menu_name
|
||||
// FROM app_menus m
|
||||
// JOIN app_role_menus rm ON m.menu_id = rm.menu_id
|
||||
// JOIN app_user_roles ur ON rm.role_id = ur.role_id
|
||||
// WHERE ur.user_id = 145
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dao.AppMenus.Ctx(ctx).As("m").
|
||||
InnerJoin("app_role_menus rm", "m.menu_id = rm.menu_id").
|
||||
InnerJoin("app_user_roles ur", "rm.role_id = ur.role_id").
|
||||
Where("ur.user_id = ?", userID).
|
||||
Fields("m.menu_id, m.menu_name").
|
||||
Scan(&a)
|
||||
|
||||
data, _ := json.Marshal(a)
|
||||
println(string(data))
|
||||
}
|
||||
|
||||
// 遍历所有用户,给用户添加角色
|
||||
func TestAddUserRole(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
// 获取所有用户
|
||||
users, err := dao.SysUser.Ctx(ctx).Fields("id").Array()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
userIds := lo.FilterMap(users, func(item *gvar.Var, _ int) (int, bool) {
|
||||
return item.Int(), true
|
||||
})
|
||||
|
||||
for _, userId := range userIds {
|
||||
if userId == 144 || userId == 145 {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := dao.AppUserRoles.Ctx(ctx).Data(do.AppUserRoles{
|
||||
UserId: userId,
|
||||
RoleId: 2,
|
||||
}).Insert(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user