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:
		@@ -5,11 +5,15 @@ module Admin
 | 
			
		||||
    before_action :set_account
 | 
			
		||||
 | 
			
		||||
    def new
 | 
			
		||||
      authorize @account, :show?
 | 
			
		||||
 | 
			
		||||
      @account_action  = Admin::AccountAction.new(type: params[:type], report_id: params[:report_id], send_email_notification: true, include_statuses: true)
 | 
			
		||||
      @warning_presets = AccountWarningPreset.all
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def create
 | 
			
		||||
      authorize @account, :show?
 | 
			
		||||
 | 
			
		||||
      account_action                 = Admin::AccountAction.new(resource_params)
 | 
			
		||||
      account_action.target_account  = @account
 | 
			
		||||
      account_action.current_account = current_account
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,8 @@ module Admin
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def batch
 | 
			
		||||
      authorize :account, :index?
 | 
			
		||||
 | 
			
		||||
      @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
      @form.save
 | 
			
		||||
    rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,10 @@ module Admin
 | 
			
		||||
  class ActionLogsController < BaseController
 | 
			
		||||
    before_action :set_action_logs
 | 
			
		||||
 | 
			
		||||
    def index; end
 | 
			
		||||
    def index
 | 
			
		||||
      authorize :audit_log, :index?
 | 
			
		||||
      @auditable_accounts = Account.where(id: Admin::ActionLog.reorder(nil).select('distinct account_id')).select(:id, :username)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,8 @@ module Admin
 | 
			
		||||
 | 
			
		||||
    layout 'admin'
 | 
			
		||||
 | 
			
		||||
    before_action :require_staff!
 | 
			
		||||
    before_action :set_body_classes
 | 
			
		||||
    after_action :verify_authorized
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,8 @@ module Admin
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def batch
 | 
			
		||||
      authorize :custom_emoji, :index?
 | 
			
		||||
 | 
			
		||||
      @form = Form::CustomEmojiBatch.new(form_custom_emoji_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
      @form.save
 | 
			
		||||
    rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,9 @@ module Admin
 | 
			
		||||
    include Redisable
 | 
			
		||||
 | 
			
		||||
    def index
 | 
			
		||||
      @system_checks         = Admin::SystemCheck.perform
 | 
			
		||||
      authorize :dashboard, :index?
 | 
			
		||||
 | 
			
		||||
      @system_checks         = Admin::SystemCheck.perform(current_user)
 | 
			
		||||
      @time_period           = (29.days.ago.to_date...Time.now.utc.to_date)
 | 
			
		||||
      @pending_users_count   = User.pending.count
 | 
			
		||||
      @pending_reports_count = Report.unresolved.count
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,8 @@ module Admin
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def batch
 | 
			
		||||
      authorize :email_domain_block, :index?
 | 
			
		||||
 | 
			
		||||
      @form = Form::EmailDomainBlockBatch.new(form_email_domain_block_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
      @form.save
 | 
			
		||||
    rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,8 @@ module Admin
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def update
 | 
			
		||||
      authorize :follow_recommendation, :show?
 | 
			
		||||
 | 
			
		||||
      @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
      @form.save
 | 
			
		||||
    rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,8 @@ module Admin
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def batch
 | 
			
		||||
      authorize :ip_block, :index?
 | 
			
		||||
 | 
			
		||||
      @form = Form::IpBlockBatch.new(form_ip_block_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
      @form.save
 | 
			
		||||
    rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ module Admin
 | 
			
		||||
    PER_PAGE = 40
 | 
			
		||||
 | 
			
		||||
    def index
 | 
			
		||||
      authorize :account, :index?
 | 
			
		||||
      authorize @account, :show?
 | 
			
		||||
 | 
			
		||||
      @accounts = RelationshipFilter.new(@account, filter_params).results.includes(:account_stat, user: [:ips, :invite_request]).page(params[:page]).per(PER_PAGE)
 | 
			
		||||
      @form     = Form::AccountBatch.new
 | 
			
		||||
 
 | 
			
		||||
@@ -2,20 +2,63 @@
 | 
			
		||||
 | 
			
		||||
module Admin
 | 
			
		||||
  class RolesController < BaseController
 | 
			
		||||
    before_action :set_user
 | 
			
		||||
    before_action :set_role, except: [:index, :new, :create]
 | 
			
		||||
 | 
			
		||||
    def promote
 | 
			
		||||
      authorize @user, :promote?
 | 
			
		||||
      @user.promote!
 | 
			
		||||
      log_action :promote, @user
 | 
			
		||||
      redirect_to admin_account_path(@user.account_id)
 | 
			
		||||
    def index
 | 
			
		||||
      authorize :user_role, :index?
 | 
			
		||||
 | 
			
		||||
      @roles = UserRole.order(position: :desc).page(params[:page])
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def demote
 | 
			
		||||
      authorize @user, :demote?
 | 
			
		||||
      @user.demote!
 | 
			
		||||
      log_action :demote, @user
 | 
			
		||||
      redirect_to admin_account_path(@user.account_id)
 | 
			
		||||
    def new
 | 
			
		||||
      authorize :user_role, :create?
 | 
			
		||||
 | 
			
		||||
      @role = UserRole.new
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def create
 | 
			
		||||
      authorize :user_role, :create?
 | 
			
		||||
 | 
			
		||||
      @role = UserRole.new(resource_params)
 | 
			
		||||
      @role.current_account = current_account
 | 
			
		||||
 | 
			
		||||
      if @role.save
 | 
			
		||||
        redirect_to admin_roles_path
 | 
			
		||||
      else
 | 
			
		||||
        render :new
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def edit
 | 
			
		||||
      authorize @role, :update?
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def update
 | 
			
		||||
      authorize @role, :update?
 | 
			
		||||
 | 
			
		||||
      @role.current_account = current_account
 | 
			
		||||
 | 
			
		||||
      if @role.update(resource_params)
 | 
			
		||||
        redirect_to admin_roles_path
 | 
			
		||||
      else
 | 
			
		||||
        render :edit
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def destroy
 | 
			
		||||
      authorize @role, :destroy?
 | 
			
		||||
      @role.destroy!
 | 
			
		||||
      redirect_to admin_roles_path
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def set_role
 | 
			
		||||
      @role = UserRole.find(params[:id])
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def resource_params
 | 
			
		||||
      params.require(:user_role).permit(:name, :color, :highlighted, :position, permissions_as_keys: [])
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,8 @@ module Admin
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def batch
 | 
			
		||||
      authorize :status, :index?
 | 
			
		||||
 | 
			
		||||
      @status_batch_action = Admin::StatusBatchAction.new(admin_status_batch_action_params.merge(current_account: current_account, report_id: params[:report_id], type: action_from_button))
 | 
			
		||||
      @status_batch_action.save!
 | 
			
		||||
    rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +0,0 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Admin
 | 
			
		||||
  class SubscriptionsController < BaseController
 | 
			
		||||
    def index
 | 
			
		||||
      authorize :subscription, :index?
 | 
			
		||||
      @subscriptions = ordered_subscriptions.page(requested_page)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def ordered_subscriptions
 | 
			
		||||
      Subscription.order(id: :desc).includes(:account)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def requested_page
 | 
			
		||||
      params[:page].to_i
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@@ -2,13 +2,15 @@
 | 
			
		||||
 | 
			
		||||
class Admin::Trends::Links::PreviewCardProvidersController < Admin::BaseController
 | 
			
		||||
  def index
 | 
			
		||||
    authorize :preview_card_provider, :index?
 | 
			
		||||
    authorize :preview_card_provider, :review?
 | 
			
		||||
 | 
			
		||||
    @preview_card_providers = filtered_preview_card_providers.page(params[:page])
 | 
			
		||||
    @form = Trends::PreviewCardProviderBatch.new
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def batch
 | 
			
		||||
    authorize :preview_card_provider, :review?
 | 
			
		||||
 | 
			
		||||
    @form = Trends::PreviewCardProviderBatch.new(trends_preview_card_provider_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
    @form.save
 | 
			
		||||
  rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -2,13 +2,15 @@
 | 
			
		||||
 | 
			
		||||
class Admin::Trends::LinksController < Admin::BaseController
 | 
			
		||||
  def index
 | 
			
		||||
    authorize :preview_card, :index?
 | 
			
		||||
    authorize :preview_card, :review?
 | 
			
		||||
 | 
			
		||||
    @preview_cards = filtered_preview_cards.page(params[:page])
 | 
			
		||||
    @form          = Trends::PreviewCardBatch.new
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def batch
 | 
			
		||||
    authorize :preview_card, :review?
 | 
			
		||||
 | 
			
		||||
    @form = Trends::PreviewCardBatch.new(trends_preview_card_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
    @form.save
 | 
			
		||||
  rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -2,13 +2,15 @@
 | 
			
		||||
 | 
			
		||||
class Admin::Trends::StatusesController < Admin::BaseController
 | 
			
		||||
  def index
 | 
			
		||||
    authorize :status, :index?
 | 
			
		||||
    authorize :status, :review?
 | 
			
		||||
 | 
			
		||||
    @statuses = filtered_statuses.page(params[:page])
 | 
			
		||||
    @form     = Trends::StatusBatch.new
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def batch
 | 
			
		||||
    authorize :status, :review?
 | 
			
		||||
 | 
			
		||||
    @form = Trends::StatusBatch.new(trends_status_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
    @form.save
 | 
			
		||||
  rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
@@ -2,13 +2,15 @@
 | 
			
		||||
 | 
			
		||||
class Admin::Trends::TagsController < Admin::BaseController
 | 
			
		||||
  def index
 | 
			
		||||
    authorize :tag, :index?
 | 
			
		||||
    authorize :tag, :review?
 | 
			
		||||
 | 
			
		||||
    @tags = filtered_tags.page(params[:page])
 | 
			
		||||
    @form = Trends::TagBatch.new
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def batch
 | 
			
		||||
    authorize :tag, :review?
 | 
			
		||||
 | 
			
		||||
    @form = Trends::TagBatch.new(trends_tag_batch_params.merge(current_account: current_account, action: action_from_button))
 | 
			
		||||
    @form.save
 | 
			
		||||
  rescue ActionController::ParameterMissing
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								app/controllers/admin/users/roles_controller.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								app/controllers/admin/users/roles_controller.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Admin
 | 
			
		||||
  class Users::RolesController < BaseController
 | 
			
		||||
    before_action :set_user
 | 
			
		||||
 | 
			
		||||
    def show
 | 
			
		||||
      authorize @user, :change_role?
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def update
 | 
			
		||||
      authorize @user, :change_role?
 | 
			
		||||
 | 
			
		||||
      @user.current_account = current_account
 | 
			
		||||
 | 
			
		||||
      if @user.update(resource_params)
 | 
			
		||||
        redirect_to admin_account_path(@user.account_id), notice: I18n.t('admin.accounts.change_role.changed_msg')
 | 
			
		||||
      else
 | 
			
		||||
        render :show
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def set_user
 | 
			
		||||
      @user = User.find(params[:user_id])
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def resource_params
 | 
			
		||||
      params.require(:user).permit(:role_id)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Admin
 | 
			
		||||
  class TwoFactorAuthenticationsController < BaseController
 | 
			
		||||
  class Users::TwoFactorAuthenticationsController < BaseController
 | 
			
		||||
    before_action :set_target_user
 | 
			
		||||
 | 
			
		||||
    def destroy
 | 
			
		||||
		Reference in New Issue
	
	Block a user