闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
852 B

  1. # frozen_string_literal: true
  2. module UserRoles
  3. extend ActiveSupport::Concern
  4. included do
  5. scope :admins, -> { where(admin: true) }
  6. scope :moderators, -> { where(moderator: true) }
  7. scope :staff, -> { admins.or(moderators) }
  8. end
  9. def staff?
  10. admin? || moderator?
  11. end
  12. def role
  13. if admin?
  14. 'admin'
  15. elsif moderator?
  16. 'moderator'
  17. else
  18. 'user'
  19. end
  20. end
  21. def role?(role)
  22. case role
  23. when 'user'
  24. true
  25. when 'moderator'
  26. staff?
  27. when 'admin'
  28. admin?
  29. else
  30. false
  31. end
  32. end
  33. def promote!
  34. if moderator?
  35. update!(moderator: false, admin: true)
  36. elsif !admin?
  37. update!(moderator: true)
  38. end
  39. end
  40. def demote!
  41. if admin?
  42. update!(admin: false, moderator: true)
  43. elsif moderator?
  44. update!(moderator: false)
  45. end
  46. end
  47. end