Skip to content

Commit a2c61d7

Browse files
authored
Merge pull request #24 from nativeapptemplate/add_pundit_policies_for_skipped_controllers
Add Pundit policies for controllers that skipped verify_authorized
2 parents 74dd57f + 7de815d commit a2c61d7

19 files changed

Lines changed: 549 additions & 44 deletions

app/controllers/api/v1/shopkeeper/account/passwords_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Api::V1::Shopkeeper::Account::PasswordsController < Api::V1::Shopkeeper::BaseController
2-
skip_after_action :verify_authorized
3-
42
def update
3+
authorize :password
4+
55
if current_shopkeeper.update_with_password(password_params)
66
render json: {status: 200}, status: :ok
77
else

app/controllers/api/v1/shopkeeper/accounts/accounts_invitations_controller.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
class Api::V1::Shopkeeper::Accounts::AccountsInvitationsController < Api::V1::Shopkeeper::BaseController
22
before_action :set_account
3-
before_action :require_account_admin, except: %i[index show]
43
before_action :set_accounts_invitation, only: %i[show update destroy resend]
5-
skip_after_action :verify_authorized
64

75
def index
6+
authorize AccountsInvitation
7+
88
@accounts_invitations = @account.accounts_invitations.order(name: :asc)
99
render json: AccountsInvitationSerializer.new(@accounts_invitations).serializable_hash
1010
end
1111

1212
def show
13+
authorize @accounts_invitation
14+
1315
options = {}
1416
options[:include] = [:account, :invited_by]
1517
render json: AccountsInvitationSerializer.new(@accounts_invitation, options).serializable_hash
1618
end
1719

1820
def create
21+
authorize AccountsInvitation
22+
1923
accounts_invitation = @account.accounts_invitations.build(invitation_params_create)
2024

2125
if accounts_invitation.save_and_send_invite
@@ -26,6 +30,8 @@ def create
2630
end
2731

2832
def update
33+
authorize @accounts_invitation
34+
2935
if @accounts_invitation.update(invitation_params_update)
3036
render json: AccountsInvitationSerializer.new(@accounts_invitation).serializable_hash
3137
else
@@ -34,17 +40,25 @@ def update
3440
end
3541

3642
def destroy
43+
authorize @accounts_invitation
44+
3745
@accounts_invitation.destroy
3846
render json: {status: 200}, status: :ok
3947
end
4048

4149
def resend
50+
authorize @accounts_invitation
51+
4252
@accounts_invitation.resend_invite
4353
render json: {status: 200}, status: :ok
4454
end
4555

4656
private
4757

58+
def pundit_user
59+
@account.accounts_shopkeepers.find_by!(shopkeeper: current_shopkeeper)
60+
end
61+
4862
def set_account
4963
@account = current_shopkeeper.accounts.find(params[:account_id])
5064
end
@@ -65,11 +79,4 @@ def invitation_params_update
6579
.require(:accounts_invitation)
6680
.permit(:name, AccountsShopkeeper::ROLES)
6781
end
68-
69-
def require_account_admin
70-
accounts_shopkeeper = @account.accounts_shopkeepers.find_by(shopkeeper: current_shopkeeper)
71-
return if accounts_shopkeeper&.admin?
72-
73-
render json: {code: 401, error_message: I18n.t("api.shopkeeper.accounts.admin_required")}, status: :unauthorized
74-
end
7582
end

app/controllers/api/v1/shopkeeper/accounts_controller.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class 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
101103
end

app/controllers/api/v1/shopkeeper/accounts_invitations_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class Api::V1::Shopkeeper::AccountsInvitationsController < Api::V1::Shopkeeper::BaseController
22
before_action :set_accounts_invitation
3-
skip_after_action :verify_authorized
43

54
def show
5+
authorize @accounts_invitation, :show_by_token?
6+
67
if @accounts_invitation.expired?
78
render json: {code: 410, error_message: I18n.t("api.shopkeeper.accounts_invitations.expired")}, status: :gone
89
return
@@ -14,6 +15,8 @@ def show
1415
end
1516

1617
def update
18+
authorize @accounts_invitation, :accept?
19+
1720
if @accounts_invitation.expired?
1821
render json: {code: 410, error_message: I18n.t("api.shopkeeper.accounts_invitations.expired")}, status: :gone
1922
return
@@ -28,6 +31,8 @@ def update
2831
end
2932

3033
def destroy
34+
authorize @accounts_invitation, :reject?
35+
3136
@accounts_invitation.reject!
3237
render json: {status: 200}, status: :ok
3338
end

app/controllers/api/v1/shopkeeper/accounts_shopkeepers_controller.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ class Api::V1::Shopkeeper::AccountsShopkeepersController < Api::V1::Shopkeeper::
22
before_action :set_account
33
before_action :require_non_personal_account!, only: %i[show update destroy]
44
before_action :set_accounts_shopkeeper, only: %i[show update destroy]
5-
before_action :require_account_admin, except: %i[index show]
65
before_action :safeguard_account_owner_deletion!, only: %i[destroy]
7-
skip_after_action :verify_authorized
86

97
def index
8+
authorize AccountsShopkeeper
9+
1010
if @account.personal?
1111
render json: AccountsShopkeeperSerializer.new([]).serializable_hash and return
1212
end
@@ -19,13 +19,17 @@ def index
1919
end
2020

2121
def show
22+
authorize @accounts_shopkeeper
23+
2224
options = {}
2325
options[:include] = [:account, :shopkeeper]
2426

2527
render json: AccountsShopkeeperSerializer.new(@accounts_shopkeeper, options).serializable_hash
2628
end
2729

2830
def update
31+
authorize @accounts_shopkeeper
32+
2933
if @accounts_shopkeeper.update(accounts_shopkeeper_params)
3034
options = {}
3135
options[:include] = [:account, :shopkeeper]
@@ -37,12 +41,18 @@ def update
3741
end
3842

3943
def destroy
44+
authorize @accounts_shopkeeper
45+
4046
@accounts_shopkeeper.destroy
4147
render json: {status: 200}, status: :ok
4248
end
4349

4450
private
4551

52+
def pundit_user
53+
@account.accounts_shopkeepers.find_by!(shopkeeper: current_shopkeeper)
54+
end
55+
4656
def set_account
4757
@account = current_shopkeeper.accounts.find(params[:account_id])
4858
end
@@ -68,11 +78,4 @@ def safeguard_account_owner_deletion!
6878

6979
render json: {code: 401, error_message: I18n.t("unauthorized")}, status: :unauthorized
7080
end
71-
72-
def require_account_admin
73-
accounts_shopkeeper = @account.accounts_shopkeepers.find_by(shopkeeper: current_shopkeeper)
74-
return if accounts_shopkeeper&.admin?
75-
76-
render json: {code: 401, error_message: I18n.t("api.shopkeeper.accounts.admin_required")}, status: :unauthorized
77-
end
7881
end

app/controllers/api/v1/shopkeeper/me_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
class Api::V1::Shopkeeper::MeController < Api::V1::Shopkeeper::BaseController
22
before_action :set_shopkeeper, only: %i[update_confirmed_privacy_version update_confirmed_terms_version]
3-
skip_after_action :verify_authorized
43

54
def update_confirmed_privacy_version
5+
authorize :me
6+
67
@shopkeeper.confirmed_privacy_version = PrivacyVersion.current_version
78
@shopkeeper.save!(validate: false)
89
render json: {status: 200}, status: :ok
910
end
1011

1112
def update_confirmed_terms_version
13+
authorize :me
14+
1215
@shopkeeper.confirmed_terms_version = TermsVersion.current_version
1316
@shopkeeper.save!(validate: false)
1417
render json: {status: 200}, status: :ok
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Api::Shopkeeper::AccountPolicy < Api::Shopkeeper::BasePolicy
2+
include Api::Shopkeeper::Concerns::Authorization
3+
4+
def index?
5+
true
6+
end
7+
8+
def show?
9+
true
10+
end
11+
12+
def create?
13+
true
14+
end
15+
16+
def update?
17+
admin?
18+
end
19+
20+
def destroy?
21+
owner?
22+
end
23+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Api::Shopkeeper::AccountsInvitationPolicy < Api::Shopkeeper::BasePolicy
2+
include Api::Shopkeeper::Concerns::Authorization
3+
4+
def index?
5+
true
6+
end
7+
8+
def show?
9+
true
10+
end
11+
12+
def create?
13+
admin?
14+
end
15+
16+
def update?
17+
admin?
18+
end
19+
20+
def destroy?
21+
admin?
22+
end
23+
24+
def resend?
25+
admin?
26+
end
27+
28+
# Token-based actions (any authenticated shopkeeper with the token)
29+
def show_by_token?
30+
true
31+
end
32+
33+
def accept?
34+
true
35+
end
36+
37+
def reject?
38+
true
39+
end
40+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Api::Shopkeeper::AccountsShopkeeperPolicy < Api::Shopkeeper::BasePolicy
2+
include Api::Shopkeeper::Concerns::Authorization
3+
4+
def index?
5+
true
6+
end
7+
8+
def show?
9+
true
10+
end
11+
12+
def update?
13+
admin?
14+
end
15+
16+
def destroy?
17+
admin?
18+
end
19+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Api::Shopkeeper::MePolicy < Api::Shopkeeper::BasePolicy
2+
def update_confirmed_privacy_version?
3+
true
4+
end
5+
6+
def update_confirmed_terms_version?
7+
true
8+
end
9+
end

0 commit comments

Comments
 (0)