user.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package models
  2. import (
  3. "time"
  4. "github.com/qor5/admin/role"
  5. "github.com/qor5/x/login"
  6. "gorm.io/gorm"
  7. )
  8. const (
  9. RoleAdmin = "Admin"
  10. RoleManager = "Manager"
  11. RoleEditor = "Editor"
  12. RoleViewer = "Viewer"
  13. OAuthProviderGoogle = "google"
  14. OAuthProviderMicrosoftOnline = "microsoftonline"
  15. OAuthProviderGithub = "github"
  16. )
  17. var DefaultRoles = []string{
  18. RoleAdmin,
  19. RoleManager,
  20. RoleEditor,
  21. RoleViewer,
  22. }
  23. var OAuthProviders = []string{
  24. OAuthProviderGoogle,
  25. OAuthProviderMicrosoftOnline,
  26. OAuthProviderGithub,
  27. }
  28. type User struct {
  29. gorm.Model
  30. Name string
  31. Company string
  32. Roles []role.Role `gorm:"many2many:user_role_join;"`
  33. Status string
  34. UpdatedAt time.Time
  35. CreatedAt time.Time
  36. FavorPostID uint
  37. RegistrationDate time.Time `gorm:"type:date"`
  38. Position string
  39. IsSubscribed bool
  40. IsInfoCompleted bool
  41. // Username is email
  42. login.UserPass
  43. login.OAuthInfo
  44. login.SessionSecure
  45. }
  46. func (u User) GetName() string {
  47. return u.Name
  48. }
  49. func (u User) GetID() uint {
  50. return u.ID
  51. }
  52. func (u User) GetRoles() (rs []string) {
  53. for _, r := range u.Roles {
  54. rs = append(rs, r.Name)
  55. }
  56. if len(rs) == 0 {
  57. rs = []string{RoleViewer}
  58. }
  59. return
  60. }
  61. func (u User) IsOAuthUser() bool {
  62. return u.OAuthProvider != "" && u.OAuthIdentifier != ""
  63. }