You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

role.go 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "fmt"
  5. )
  6. type RoleType string
  7. const (
  8. TechProgramAdmin RoleType = "TechProgramAdmin"
  9. )
  10. type Role struct {
  11. Type RoleType
  12. Name string
  13. Description string
  14. }
  15. type UserRole struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. RoleType RoleType
  18. UserId int64 `xorm:"INDEX"`
  19. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  20. }
  21. func NewUserRole(r UserRole) (int64, error) {
  22. return x.Insert(&r)
  23. }
  24. func GetUserRoleByUserAndRole(userId int64, roleType RoleType) (*UserRole, error) {
  25. r := &UserRole{}
  26. has, err := x.Where("role_type = ? and user_id = ?", roleType, userId).Get(r)
  27. if err != nil {
  28. return nil, err
  29. } else if !has {
  30. return nil, ErrRecordNotExist{}
  31. }
  32. return r, nil
  33. }
  34. func GetRoleByCode(code string) (*Role, error) {
  35. r := &Role{}
  36. has, err := x.Where("code = ?", code).Get(r)
  37. if err != nil {
  38. return nil, err
  39. } else if !has {
  40. return nil, ErrRecordNotExist{}
  41. }
  42. return r, nil
  43. }
  44. type ErrRoleNotExists struct {
  45. }
  46. func IsErrRoleNotExists(err error) bool {
  47. _, ok := err.(ErrRoleNotExists)
  48. return ok
  49. }
  50. func (err ErrRoleNotExists) Error() string {
  51. return fmt.Sprintf("role is not exists")
  52. }
  53. type AddRoleReq struct {
  54. UserName string `json:"user_name" binding:"Required"`
  55. RoleType RoleType `json:"role_type" binding:"Required"`
  56. }