permission.go 738 B

123456789101112131415161718192021222324252627282930
  1. package actions
  2. import (
  3. "github.com/beego/beego/v2/server/web/context"
  4. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/beegouser"
  5. "gorm.io/gorm"
  6. )
  7. type DataPermission struct {
  8. UserId int
  9. DeptId int
  10. }
  11. func Permission(tableName string, p *DataPermission) func(db *gorm.DB) *gorm.DB {
  12. return func(db *gorm.DB) *gorm.DB {
  13. return db.Where(tableName+".dept_id = ? ", p.DeptId)
  14. }
  15. }
  16. func getPermissionFromContext(c *context.Context) *DataPermission {
  17. p := new(DataPermission)
  18. p.UserId = beegouser.GetUserId(c)
  19. p.DeptId = beegouser.GetDeptId(c)
  20. return p
  21. }
  22. // GetPermissionFromContext 提供非action写法数据范围约束
  23. func GetPermissionFromContext(c *context.Context) *DataPermission {
  24. return getPermissionFromContext(c)
  25. }