Add customizable user roles (#18641)
* Add customizable user roles * Various fixes and improvements * Add migration for old settings and fix tootctl role management
This commit is contained in:
parent
1b4054256f
commit
44b2ee3485
187 changed files with 1945 additions and 1032 deletions
|
@ -2,11 +2,11 @@
|
|||
|
||||
class AccountModerationNotePolicy < ApplicationPolicy
|
||||
def create?
|
||||
staff?
|
||||
role.can?(:manage_reports)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin? || owner?
|
||||
owner? || (role.can?(:manage_reports) && role.overrides?(record.account.user_role))
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -2,74 +2,66 @@
|
|||
|
||||
class AccountPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_users)
|
||||
end
|
||||
|
||||
def show?
|
||||
staff?
|
||||
role.can?(:manage_users)
|
||||
end
|
||||
|
||||
def warn?
|
||||
staff? && !record.user&.staff?
|
||||
role.can?(:manage_users, :manage_reports) && role.overrides?(record.user_role)
|
||||
end
|
||||
|
||||
def suspend?
|
||||
staff? && !record.user&.staff? && !record.instance_actor?
|
||||
role.can?(:manage_users, :manage_reports) && role.overrides?(record.user_role) && !record.instance_actor?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
record.suspended_temporarily? && admin?
|
||||
record.suspended_temporarily? && role.can?(:delete_user_data)
|
||||
end
|
||||
|
||||
def unsuspend?
|
||||
staff? && record.suspension_origin_local?
|
||||
role.can?(:manage_users) && record.suspension_origin_local?
|
||||
end
|
||||
|
||||
def sensitive?
|
||||
staff? && !record.user&.staff?
|
||||
role.can?(:manage_users, :manage_reports) && role.overrides?(record.user_role)
|
||||
end
|
||||
|
||||
def unsensitive?
|
||||
staff?
|
||||
role.can?(:manage_users)
|
||||
end
|
||||
|
||||
def silence?
|
||||
staff? && !record.user&.staff?
|
||||
role.can?(:manage_users, :manage_reports) && role.overrides?(record.user_role)
|
||||
end
|
||||
|
||||
def unsilence?
|
||||
staff?
|
||||
role.can?(:manage_users)
|
||||
end
|
||||
|
||||
def redownload?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def remove_avatar?
|
||||
staff?
|
||||
role.can?(:manage_users, :manage_reports) && role.overrides?(record.user_role)
|
||||
end
|
||||
|
||||
def remove_header?
|
||||
staff?
|
||||
end
|
||||
|
||||
def subscribe?
|
||||
admin?
|
||||
end
|
||||
|
||||
def unsubscribe?
|
||||
admin?
|
||||
role.can?(:manage_users, :manage_reports) && role.overrides?(record.user_role)
|
||||
end
|
||||
|
||||
def memorialize?
|
||||
admin? && !record.user&.admin? && !record.instance_actor?
|
||||
role.can?(:delete_user_data) && role.overrides?(record.user_role) && !record.instance_actor?
|
||||
end
|
||||
|
||||
def unblock_email?
|
||||
staff?
|
||||
role.can?(:manage_users)
|
||||
end
|
||||
|
||||
def review?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
class AccountWarningPolicy < ApplicationPolicy
|
||||
def show?
|
||||
target? || staff?
|
||||
target? || role.can?(:manage_appeals)
|
||||
end
|
||||
|
||||
def appeal?
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
class AccountWarningPresetPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
|
||||
def create?
|
||||
staff?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
|
||||
def update?
|
||||
staff?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
staff?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
class AnnouncementPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_announcements)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_announcements)
|
||||
end
|
||||
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_announcements)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_announcements)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
class AppealPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_appeals)
|
||||
end
|
||||
|
||||
def approve?
|
||||
record.pending? && staff?
|
||||
record.pending? && role.can?(:manage_appeals)
|
||||
end
|
||||
|
||||
alias reject? approve?
|
||||
def reject?
|
||||
record.pending? && role.can?(:manage_appeals)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,8 +8,6 @@ class ApplicationPolicy
|
|||
@record = record
|
||||
end
|
||||
|
||||
delegate :admin?, :moderator?, :staff?, to: :current_user, allow_nil: true
|
||||
|
||||
private
|
||||
|
||||
def current_user
|
||||
|
@ -19,4 +17,8 @@ class ApplicationPolicy
|
|||
def user_signed_in?
|
||||
!current_user.nil?
|
||||
end
|
||||
|
||||
def role
|
||||
current_user&.role || UserRole.nobody
|
||||
end
|
||||
end
|
||||
|
|
7
app/policies/audit_log_policy.rb
Normal file
7
app/policies/audit_log_policy.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AuditLogPolicy < ApplicationPolicy
|
||||
def index?
|
||||
role.can?(:view_audit_log)
|
||||
end
|
||||
end
|
|
@ -2,30 +2,30 @@
|
|||
|
||||
class CustomEmojiPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
|
||||
def copy?
|
||||
admin?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
|
||||
def enable?
|
||||
staff?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
|
||||
def disable?
|
||||
staff?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_custom_emojis)
|
||||
end
|
||||
end
|
||||
|
|
7
app/policies/dashboard_policy.rb
Normal file
7
app/policies/dashboard_policy.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class DashboardPolicy < ApplicationPolicy
|
||||
def index?
|
||||
role.can?(:view_dashboard)
|
||||
end
|
||||
end
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class DeliveryPolicy < ApplicationPolicy
|
||||
def clear_delivery_errors?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def restart_delivery?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def stop_delivery?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
class DomainAllowPolicy < ApplicationPolicy
|
||||
def index?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def show?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,22 +2,22 @@
|
|||
|
||||
class DomainBlockPolicy < ApplicationPolicy
|
||||
def index?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def show?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class EmailDomainBlockPolicy < ApplicationPolicy
|
||||
def index?
|
||||
admin?
|
||||
role.can?(:manage_blocks)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_blocks)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_blocks)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class FollowRecommendationPolicy < ApplicationPolicy
|
||||
def show?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def suppress?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def unsuppress?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class InstancePolicy < ApplicationPolicy
|
||||
def index?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def show?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,19 +2,19 @@
|
|||
|
||||
class InvitePolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_invites)
|
||||
end
|
||||
|
||||
def create?
|
||||
min_required_role?
|
||||
role.can?(:invite_users)
|
||||
end
|
||||
|
||||
def deactivate_all?
|
||||
admin?
|
||||
role.can?(:manage_invites)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
owner? || (Setting.min_invite_role == 'admin' ? admin? : staff?)
|
||||
owner? || role.can?(:manage_invites)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -22,8 +22,4 @@ class InvitePolicy < ApplicationPolicy
|
|||
def owner?
|
||||
record.user_id == current_user&.id
|
||||
end
|
||||
|
||||
def min_required_role?
|
||||
current_user&.role?(Setting.min_invite_role)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class IpBlockPolicy < ApplicationPolicy
|
||||
def index?
|
||||
admin?
|
||||
role.can?(:manage_blocks)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_blocks)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_blocks)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
class PreviewCardPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def review?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
class PreviewCardProviderPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def review?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
class RelayPolicy < ApplicationPolicy
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_federation)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
class ReportNotePolicy < ApplicationPolicy
|
||||
def create?
|
||||
staff?
|
||||
role.can?(:manage_reports)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin? || owner?
|
||||
owner? || (role.can?(:manage_reports) && role.overrides?(record.account.user_role))
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class ReportPolicy < ApplicationPolicy
|
||||
def update?
|
||||
staff?
|
||||
role.can?(:manage_reports)
|
||||
end
|
||||
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_reports)
|
||||
end
|
||||
|
||||
def show?
|
||||
staff?
|
||||
role.can?(:manage_reports)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
class RulePolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_rules)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_rules)
|
||||
end
|
||||
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_rules)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_rules)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
class SettingsPolicy < ApplicationPolicy
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
|
||||
def show?
|
||||
admin?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_settings)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ class StatusPolicy < ApplicationPolicy
|
|||
end
|
||||
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_reports, :manage_users)
|
||||
end
|
||||
|
||||
def show?
|
||||
|
@ -32,17 +32,17 @@ class StatusPolicy < ApplicationPolicy
|
|||
end
|
||||
|
||||
def destroy?
|
||||
staff? || owned?
|
||||
role.can?(:manage_reports) || owned?
|
||||
end
|
||||
|
||||
alias unreblog? destroy?
|
||||
|
||||
def update?
|
||||
staff? || owned?
|
||||
role.can?(:manage_reports) || owned?
|
||||
end
|
||||
|
||||
def review?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
class TagPolicy < ApplicationPolicy
|
||||
def index?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def show?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def update?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
|
||||
def review?
|
||||
staff?
|
||||
role.can?(:manage_taxonomies)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,52 +2,38 @@
|
|||
|
||||
class UserPolicy < ApplicationPolicy
|
||||
def reset_password?
|
||||
staff? && !record.staff?
|
||||
role.can?(:manage_user_access) && role.overrides?(record.role)
|
||||
end
|
||||
|
||||
def change_email?
|
||||
staff? && !record.staff?
|
||||
role.can?(:manage_user_access) && role.overrides?(record.role)
|
||||
end
|
||||
|
||||
def disable_2fa?
|
||||
admin? && !record.staff?
|
||||
role.can?(:manage_user_access) && role.overrides?(record.role)
|
||||
end
|
||||
|
||||
def change_role?
|
||||
role.can?(:manage_roles) && role.overrides?(record.role)
|
||||
end
|
||||
|
||||
def confirm?
|
||||
staff? && !record.confirmed?
|
||||
role.can?(:manage_user_access) && !record.confirmed?
|
||||
end
|
||||
|
||||
def enable?
|
||||
staff?
|
||||
role.can?(:manage_users)
|
||||
end
|
||||
|
||||
def approve?
|
||||
staff? && !record.approved?
|
||||
role.can?(:manage_users) && !record.approved?
|
||||
end
|
||||
|
||||
def reject?
|
||||
staff? && !record.approved?
|
||||
role.can?(:manage_users) && !record.approved?
|
||||
end
|
||||
|
||||
def disable?
|
||||
staff? && !record.admin?
|
||||
end
|
||||
|
||||
def promote?
|
||||
admin? && promotable?
|
||||
end
|
||||
|
||||
def demote?
|
||||
admin? && !record.admin? && demoteable?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def promotable?
|
||||
record.approved? && (!record.staff? || !record.admin?)
|
||||
end
|
||||
|
||||
def demoteable?
|
||||
record.staff?
|
||||
role.can?(:manage_users) && role.overrides?(record.role)
|
||||
end
|
||||
end
|
||||
|
|
19
app/policies/user_role_policy.rb
Normal file
19
app/policies/user_role_policy.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class UserRolePolicy < ApplicationPolicy
|
||||
def index?
|
||||
role.can?(:manage_roles)
|
||||
end
|
||||
|
||||
def create?
|
||||
role.can?(:manage_roles)
|
||||
end
|
||||
|
||||
def update?
|
||||
role.can?(:manage_roles) && role.overrides?(record)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
!record.everyone? && role.can?(:manage_roles) && role.overrides?(record) && role.id != record.id
|
||||
end
|
||||
end
|
|
@ -2,34 +2,34 @@
|
|||
|
||||
class WebhookPolicy < ApplicationPolicy
|
||||
def index?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def create?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def show?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def update?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def enable?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def disable?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def rotate_secret?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
|
||||
def destroy?
|
||||
admin?
|
||||
role.can?(:manage_webhooks)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue