Skip to content

Commit

Permalink
refactor(APIv2): introduce the V2::ApplicationPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Nov 14, 2023
1 parent 259ddac commit f7a43d9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
59 changes: 59 additions & 0 deletions app/policies/v2/application_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module V2
# Generic policies for everything, very restrictive. Any model
# should be overriding only the methods that would make sense
# to override.
class ApplicationPolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

def index?
false

Check warning on line 16 in app/policies/v2/application_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/v2/application_policy.rb#L16

Added line #L16 was not covered by tests
end

def show?
false

Check warning on line 20 in app/policies/v2/application_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/v2/application_policy.rb#L20

Added line #L20 was not covered by tests
end

def create?
false

Check warning on line 24 in app/policies/v2/application_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/v2/application_policy.rb#L24

Added line #L24 was not covered by tests
end

def update?
false

Check warning on line 28 in app/policies/v2/application_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/v2/application_policy.rb#L28

Added line #L28 was not covered by tests
end

def destroy?
false

Check warning on line 32 in app/policies/v2/application_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/v2/application_policy.rb#L32

Added line #L32 was not covered by tests
end

alias new? create?
alias edit? update?

private

def match_account?
record.account_id == user.account_id

Check warning on line 41 in app/policies/v2/application_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/v2/application_policy.rb#L41

Added line #L41 was not covered by tests
end

# Generic scope for all models - just matching the account ID.
# To be overridden on individual model policies if needed.
class Scope
attr_reader :user, :scope

def initialize(user, scope)
@user = user
@scope = scope
end

def resolve
scope.all
end
end
end
end
7 changes: 2 additions & 5 deletions app/policies/v2/profile_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module V2
# Policies for accessing Profiles
class ProfilePolicy < ApplicationPolicy
class ProfilePolicy < V2::ApplicationPolicy
def index?
true
end
Expand All @@ -12,10 +12,7 @@ def show?
end

# All users should see all Profiles currently
class Scope < ::ApplicationPolicy::Scope
def resolve
scope.all
end
class Scope < V2::ApplicationPolicy::Scope
end
end
end
5 changes: 1 addition & 4 deletions app/policies/v2/rule_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module V2
# Policies for accessing Rules
class RulePolicy < ApplicationPolicy
class RulePolicy < V2::ApplicationPolicy
def index?
true
end
Expand All @@ -13,9 +13,6 @@ def show?

# All users should see all rules currently
class Scope < ::ApplicationPolicy::Scope
def resolve
scope.all
end
end
end
end
7 changes: 2 additions & 5 deletions app/policies/v2/security_guide_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module V2
# Policies for accessing Security Guides
class SecurityGuidePolicy < ApplicationPolicy
class SecurityGuidePolicy < V2::ApplicationPolicy
def index?
true
end
Expand All @@ -12,10 +12,7 @@ def show?
end

# All users should see all security guides currently
class Scope < ::ApplicationPolicy::Scope
def resolve
scope.all
end
class Scope < V2::ApplicationPolicy::Scope
end
end
end
7 changes: 2 additions & 5 deletions app/policies/v2/value_definition_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module V2
# Policies for accessing Value Definitions
class ValueDefinitionPolicy < ApplicationPolicy
class ValueDefinitionPolicy < V2::ApplicationPolicy
def index?
true
end
Expand All @@ -12,10 +12,7 @@ def show?
end

# All users should see all value definitions currently
class Scope < ::ApplicationPolicy::Scope
def resolve
scope.all
end
class Scope < V2::ApplicationPolicy::Scope
end
end
end

0 comments on commit f7a43d9

Please sign in to comment.