package svc import ( "applet/app/md" "applet/app/utils" "applet/app/utils/cache" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/implement" "code.fnuoos.com/zhimeng/model.git/src/model" "encoding/json" "errors" "github.com/gin-gonic/gin" "regexp" "strings" "time" ) func CheckUserRole(c *gin.Context, cacheKey, uri string, admId int) (isHasPermission bool, err error) { uri = utils.UriFilterExcludeQueryString(uri) //去除uri中?后的query参数 isHasPermission = false var rolePermission []string var rolePermissionString string rolePermissionString, _ = cache.GetString(cacheKey) //TODO::判断是否在白名单中 if utils.InArr(uri, md.WhiteUri) { isHasPermission = true return } if rolePermissionString != "" { //if false { if err = json.Unmarshal([]byte(rolePermissionString), &rolePermission); err != nil { return } } else { adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)]) list, _, err1 := adminDb.GetAdminRolePermission(admId) if err1 != nil { return isHasPermission, err1 } for _, v := range list { rolePermission = append(rolePermission, v.Permission.Action) } marshal, err1 := json.Marshal(rolePermission) if err1 != nil { return isHasPermission, err1 } rolePermissionString = string(marshal) _, err = cache.SetEx(cacheKey, rolePermissionString, md.AdminRolePermissionCacheTime) } if utils.InArr(uri, rolePermission) { isHasPermission = true } else { //正则匹配占位符情况 compileRegex := regexp.MustCompile("[0-9]+") matchArr := compileRegex.FindAllString(uri, -1) if len(matchArr) > 0 { uri = strings.Replace(uri, matchArr[len(matchArr)-1], ":id", 1) if utils.InArr(uri, rolePermission) { isHasPermission = true } } } return } func DeleteRole(c *gin.Context, roleId int) (err error) { session := db.Db.NewSession() defer session.Close() session.Begin() //1、删除 `role` roleDb := implement.NewRoleDb(db.DBs[GetMasterId(c)], roleId) _, err = roleDb.RoleDeleteBySession(session, roleId) if err != nil { _ = session.Rollback() return } //2、删除 `role_permission_group` rolePermissionGroupDb := implement.NewRolePermissionGroupDb(db.DBs[GetMasterId(c)]) _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, roleId) if err != nil { _ = session.Rollback() return } //3、删除 `admin_role` adminRoleDb := implement.NewAdminRoleDb(db.DBs[GetMasterId(c)]) _, err = adminRoleDb.AdminRoleDeleteForRoleBySession(session, roleId) if err != nil { _ = session.Rollback() return } return session.Commit() } func RoleBindPermissionGroup(c *gin.Context, req md.RoleBindPermissionGroupReq) (err error) { session := db.Db.NewSession() defer session.Close() session.Begin() //1、查询 `role` roleDb := implement.NewRoleDb(db.DBs[GetMasterId(c)], req.RoleId) role, err := roleDb.GetRole() if err != nil { return } if role == nil { return errors.New("未查询到相应记录") } //1、删除 `role_permission_group` rolePermissionGroupDb := implement.NewMediumRolePermissionGroupDb(db.DBs[GetMasterId(c)]) _, err = rolePermissionGroupDb.MediumRolePermissionGroupDeleteForRoleBySession(session, req.RoleId) if err != nil { _ = session.Rollback() return } //2、新增 `role_permission_group`` var mm []*model.MediumRolePermissionGroup now := time.Now() for _, v := range req.PermissionIds { mm = append(mm, &model.MediumRolePermissionGroup{ RoleId: role.Id, GroupId: v, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), }) } _, err = rolePermissionGroupDb.BatchAddMediumRolePermissionGroupBySession(session, mm) if err != nil { _ = session.Rollback() return } return session.Commit() } func BindAdminRole(c *gin.Context, req md.BindAdminRoleReq) (err error) { session := db.Db.NewSession() defer session.Close() session.Begin() //1、查询 `role` adminDb := implement.NewMediumDb(db.DBs[GetMasterId(c)]) role, err := adminDb.GetMedium(req.Id) if err != nil { return } if role == nil { return errors.New("未查询到相应记录") } //1、删除 `admin_role` adminRoleDb := implement.NewMediumWithRoleDb(db.DBs[GetMasterId(c)]) _, err = adminRoleDb.MediumWithRoleDeleteBySession(session, req.Id) if err != nil { _ = session.Rollback() return } //2、新增 `删除 `admin_role`` var mm []*model.MediumWithRole now := time.Now() for _, v := range req.RoleIds { mm = append(mm, &model.MediumWithRole{ MediumId: req.Id, RoleId: v, State: 1, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), }) } _, err = adminRoleDb.BatchAddMediumWithRoleBySession(session, mm) if err != nil { _ = session.Rollback() return } return session.Commit() }