11class Api ::V1 ::Shopkeeper ::AccountsController < Api ::V1 ::Shopkeeper ::BaseController
22 before_action :set_account , only : %i[ show update destroy ]
3- before_action :require_account_admin , only : %i[ update ]
4- before_action :require_account_owner , only : %i[ destroy ]
53 before_action :prevent_personal_account_deletion , only : %i[ destroy ]
6- skip_after_action :verify_authorized
74
85 # GET /accounts
96 def index
7+ authorize Account
8+
109 accounts = current_shopkeeper . accounts . sorted
1110 options = {
1211 params : { current_shopkeeper : current_shopkeeper }
@@ -24,6 +23,8 @@ def index
2423
2524 # GET /accounts/1
2625 def show
26+ authorize @account
27+
2728 options = {
2829 include : [ :accounts_shopkeepers , :accounts_invitations ] ,
2930 params : { current_shopkeeper : current_shopkeeper }
@@ -33,6 +34,8 @@ def show
3334
3435 # POST /accounts
3536 def create
37+ authorize Account
38+
3639 account = Account . new ( account_params . merge ( owner : current_shopkeeper ) )
3740 account . accounts_shopkeepers . new ( shopkeeper : current_shopkeeper , admin : true )
3841
@@ -49,6 +52,8 @@ def create
4952
5053 # PATCH/PUT /accounts/1
5154 def update
55+ authorize @account
56+
5257 if @account . update ( account_params )
5358 options = {
5459 params : { current_shopkeeper : current_shopkeeper }
@@ -61,6 +66,8 @@ def update
6166
6267 # DELETE /accounts/1
6368 def destroy
69+ authorize @account
70+
6471 ActsAsTenant . without_tenant do
6572 @account . destroy
6673 end
@@ -80,22 +87,17 @@ def account_params
8087 params . require ( :account ) . permit ( :name )
8188 end
8289
90+ def pundit_user
91+ if @account
92+ @account . accounts_shopkeepers . find_by! ( shopkeeper : current_shopkeeper )
93+ else
94+ super
95+ end
96+ end
97+
8398 def prevent_personal_account_deletion
8499 return unless @account . personal?
85100
86101 render json : { code : 422 , error_message : I18n . t ( "api.shopkeeper.accounts.personal.cannot_delete" ) } , status : :unprocessable_entity
87102 end
88-
89- def require_account_admin
90- accounts_shopkeeper = @account . accounts_shopkeepers . find_by ( shopkeeper : current_shopkeeper )
91- return if accounts_shopkeeper &.admin?
92-
93- render json : { code : 401 , error_message : I18n . t ( "api.shopkeeper.accounts.admin_required" ) } , status : :unauthorized
94- end
95-
96- def require_account_owner
97- return if @account . owner? ( current_shopkeeper )
98-
99- render json : { code : 401 , error_message : I18n . t ( "api.shopkeeper.accounts.owner_required" ) } , status : :unauthorized
100- end
101103end
0 commit comments