implement account locking/unlocking

This commit is contained in:
Bilal
2020-09-08 16:25:32 +03:00
parent 3db230de9b
commit 9bafbe36db
12 changed files with 83 additions and 3 deletions

View File

@@ -0,0 +1,31 @@
class AccountLocksController < ApplicationController
before_action :set_account
def create
authorize :account_lock, :create?
@account.update(locked: true)
redirect_to admin_accounts_path, notice: 'Account locked'
end
def destroy
authorize :account_lock, :destroy?
@account.update(locked: false)
redirect_to admin_accounts_path, notice: 'Account unlocked'
end
private
def set_account
if params[:id].present?
@account = Account.find(params[:id])
else
failure_redirect
end
rescue ActiveRecord::RecordNotFound
failure_redirect
end
def failure_redirect
redirect_to admin_accounts_path, alert: 'Failed to find the account'
end
end

View File

@@ -1,4 +1,5 @@
class AccountSessionsController < ApplicationController
skip_before_action :redirect_locked_accounts
def update
authorize :account_session, :update?
session[:active_account] = account_session_params[:account_id]

View File

@@ -13,6 +13,7 @@ class ApplicationController < ActionController::Base
include SetCurrentRequestDetails
before_action :redirect_accountless
before_action :redirect_locked_accounts
private
@@ -29,6 +30,12 @@ class ApplicationController < ActionController::Base
end
end
def redirect_locked_accounts
if Current.user && Current.account.locked?
redirect_to locked_account_path
end
end
def signed_in_as_admin?
signed_in? && current_user.admin?
end

View File

@@ -0,0 +1,10 @@
class LockedAccountsController < ApplicationController
skip_before_action :redirect_locked_accounts
skip_after_action :verify_policy_scoped
def index
unless Current.account.locked?
redirect_to projects_path
end
end
end

View File

@@ -0,0 +1,9 @@
class AccountLockPolicy < ApplicationPolicy
def create?
user.admin?
end
def destroy?
user.admin?
end
end

View File

@@ -30,6 +30,11 @@
<%= link_to fa_icon("arrow-right", text: "Overview"), admin_account_path(account), class: "dropdown-item" %>
<%= link_to fa_icon("pencil", text: "Edit"), edit_admin_account_path(account), class: "dropdown-item" %>
<%= link_to fa_icon("arrow-right", text: "Account Managers"), account_auths_path({ account_id: account.id}), class: "dropdown-item" %>
<% if account.locked? %>
<%= link_to fa_icon("unlock", text: "Unlock Account"), [:account, :lock, id: account.id], method: :delete, class: "dropdown-item" %>
<% else %>
<%= link_to fa_icon("lock", text: "Lock Account"), [:account, :lock, id: account.id], method: :post, class: "dropdown-item" %>
<% end %>
</div>
</div>
</td>

View File

@@ -0,0 +1 @@
<p><%= t '.account_locked_message' %></p>

View File

@@ -1651,3 +1651,6 @@ en:
edit: Edit
report: Report
generating: Generating...
locked_accounts:
index:
account_locked_message: This account is locked. Please contact a BIG admin.

View File

@@ -705,3 +705,6 @@ es:
production_elements_logs: Production Elements Logs, and more (ES)
reduces_labor_cost: Reduces labor costs (ES)
simplifies_cue_sheets: Simplifies Music Cue Sheets, Graphic Cue Sheets (ES)
locked_accounts:
index:
account_locked_message: This account is locked. Please contact a BIG admin. (ES)

View File

@@ -48,7 +48,10 @@ Rails.application.routes.draw do
scope "(:locale)", locale: AVAILABLE_LOCALES_REGEX do
resource :account_session, only: [:update]
resource :session, only: [:destroy]
resource :account, only: [:new, :create, :update]
resource :account, only: [:new, :create, :update] do
resource :account_locks, path: :lock, as: :lock, only: [:create, :destroy]
get 'locked' => 'locked_accounts#index'
end
resources :account_auths, only: [:index, :create, :update, :destroy]
resources :projects, shallow: true do
resources :acquired_media_releases, except: [:show], concerns: [:contractable, :notable, :file_uploadable]

View File

@@ -0,0 +1,5 @@
class AddLockedToAccounts < ActiveRecord::Migration[6.0]
def change
add_column :accounts, :locked, :boolean, default: false
end
end

View File

@@ -95,7 +95,8 @@ CREATE TABLE public.accounts (
slug character varying,
plan_uid character varying,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
updated_at timestamp without time zone NOT NULL,
locked boolean DEFAULT false
);
@@ -4027,6 +4028,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200812060406'),
('20200819070738'),
('20200820082501'),
('20200824171649');
('20200824171649'),
('20200908085319');