-
Notifications
You must be signed in to change notification settings - Fork 23
Consolidate authorization through ActionPolicy and enforce destroy preconditions #958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c6eebbd
b5e2ea7
68c4a06
77765ca
d86cda4
5018c20
e764292
e049311
0d5173e
578b4bf
672b276
07de02a
e8602d4
ecbcba3
b2ad1c7
8382c40
d493dd4
f382c86
7516967
dc76246
a8974d6
ec61276
4a93afe
c26734e
088f670
726e1b5
e8a7de8
db77b57
4879447
415f385
d5b6b16
b87c439
5b2ef68
d52f47e
e0af64e
abb3b0e
d79ca39
fa1f873
37509e7
890bfed
7fda4b8
3294244
44a2da4
f0c8aaf
970a27d
d8996a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,14 +9,15 @@ class Organization < ApplicationRecord | |
| has_many :organization_people, dependent: :restrict_with_error | ||
| has_many :people, through: :organization_people | ||
| has_many :users, through: :people | ||
| has_many :reports, through: :users | ||
| has_many :workshop_logs, through: :users | ||
| has_many :reports | ||
| has_many :workshop_logs | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. old data can't be counted on to always have the proper organization_people records, so, making this a direct association for now |
||
|
|
||
| has_many :categorizable_items, dependent: :destroy, inverse_of: :categorizable, as: :categorizable | ||
| has_many :sectorable_items, as: :sectorable, dependent: :destroy | ||
| # has_many through | ||
| has_many :categories, through: :categorizable_items | ||
| has_many :sectors, through: :sectorable_items | ||
| has_many :workshops, through: :users | ||
|
|
||
| # Asset associations | ||
| has_one_attached :logo, dependent: :purge do |attachable| | ||
|
|
@@ -82,6 +83,13 @@ def self.search_by_params(params) | |
| organizations | ||
| end | ||
|
|
||
| def affiliated_workshop_logs | ||
| direct = WorkshopLog.where(organization_id: id) | ||
| legacy = WorkshopLog.where(organization_id: nil) | ||
| .where(user_id: users.select(:id)) | ||
| direct.or(legacy).distinct | ||
| end | ||
|
|
||
| # Methods | ||
| def led_by?(user) | ||
| return false unless leader | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,13 +73,6 @@ def self.search_by_params(params) | |
| results | ||
| end | ||
|
|
||
| # TODO Remove once all view's use ActionPolicy | ||
| def admin? | ||
| super_user | ||
| end | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed this just for you, @jmilljr24
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will sleep well tonight |
||
|
|
||
|
|
||
|
|
||
| def active_for_authentication? | ||
| super && !inactive? | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,16 @@ class ApplicationPolicy < ActionPolicy::Base | |
| authorize :user, optional: true, allow_nil: true | ||
|
|
||
| default_rule :manage? | ||
| alias_rule :index?, :show?, :new?, :create?, :edit?, :update?, :destroy?, to: :manage? | ||
| alias_rule :index?, :show?, :new?, :create?, :edit?, :update?, to: :manage? | ||
|
|
||
| def manage? | ||
| admin? | ||
| end | ||
|
|
||
| def destroy? | ||
| record.persisted? && manage? | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moving the persistence checks for destroy out of views and into policies so we can minimize logic in views |
||
| end | ||
|
|
||
| private | ||
| # Define shared methods useful for most policies. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ def update? | |
| end | ||
|
|
||
| def destroy? | ||
| admin? || owner? | ||
| record.persisted? && (admin? || owner?) | ||
| end | ||
|
|
||
| # Scoping | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class MonthlyReportPolicy < ApplicationPolicy | ||
| def index? | ||
| authenticated? | ||
| end | ||
|
|
||
| def show? | ||
| admin? || owner? || member? | ||
| end | ||
|
|
||
| def update? | ||
| admin? || owner? || member? | ||
| end | ||
|
|
||
| def destroy? | ||
| record.persisted? && (admin? || owner? || member?) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def member? | ||
| @member ||= begin | ||
| return false unless authenticated? | ||
| return false unless record.organization | ||
| record.organization.organization_people.exists?(person_id: user.person_id) | ||
| end | ||
| end | ||
|
|
||
| relation_scope do |relation| | ||
| next relation if admin? | ||
| relation.where(user_id: user.id) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i changed the installation of action policy from lib to initializer, and made it so the
allowed_to?call is available from views as well as controllers