-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
require_login
with Pundit in Webui::Users::SubscriptionsCon…
…troller This is a PR of a series which replaces `require_login` with `Pundit`. You can find further relevant info in #10083. Tackles Webui::Users::SubscriptionsController Ref #10083
- Loading branch information
Showing
4 changed files
with
88 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class EventSubscription | ||
class FormPolicy < ApplicationPolicy | ||
def initialize(user, record, opts = {}) | ||
super(user, record, opts.merge(ensure_logged_in: true)) | ||
end | ||
|
||
def index? | ||
user_is_subscriber? | ||
end | ||
|
||
def update? | ||
user_is_subscriber? | ||
end | ||
|
||
private | ||
|
||
def user_is_subscriber? | ||
return true unless record.subscriber | ||
|
||
user == record.subscriber | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/api/spec/policies/event_subscription/form_policy_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe EventSubscription::FormPolicy do | ||
let(:user) { create(:user_with_groups) } | ||
let(:other_user) { create(:user_with_groups) } | ||
let(:user_nobody) { build(:user_nobody) } | ||
let(:event_subscription_form) { EventSubscription::Form.new } | ||
let(:event_subscription_form_user) { EventSubscription::Form.new(user) } | ||
|
||
subject { described_class } | ||
|
||
permissions :index?, :update? do | ||
it { is_expected.to permit(user, event_subscription_form_user) } | ||
it { is_expected.to permit(other_user, event_subscription_form) } | ||
it { is_expected.not_to permit(other_user, event_subscription_form_user) } | ||
end | ||
|
||
it "doesn't permit anonymous user" do | ||
expect { described_class.new(user_nobody, event_subscription_form) } | ||
.to raise_error(an_instance_of(Pundit::NotAuthorizedError).and(having_attributes(reason: :anonymous_user))) | ||
end | ||
end |