-
Notifications
You must be signed in to change notification settings - Fork 7
Allow admins to manage all resources #1511
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3705ef
Alow admins to manage all resources
louispt1 6768e4c
Add specs for admin token abilities
noracato 3495809
Updated token ability to enable admins correctly and refactor for cla…
louispt1 e64a6ab
Return early if token doesn't have the correct scopes
louispt1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,42 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
# Describes the abilities of someone accessing the API with an access a token. | ||
# Describes the abilities of someone accessing the API with an access token. | ||
# Admins can read, write, and delete all scenarios, provided they have the correct scope in the token. | ||
# Users can read public scenarios and scenarios where they are viewers. | ||
# Users with write scope can create, update, and clone scenarios where they are collaborators. | ||
# Users with delete scope can delete scenarios where they are owners. | ||
class TokenAbility | ||
include CanCan::Ability | ||
|
||
def initialize(token, user) | ||
can :read, Scenario, private: false | ||
scopes = token[:scopes] | ||
@scopes = token[:scopes] | ||
@user = user | ||
|
||
allow_public_read | ||
allow_read if read_scope? | ||
allow_write if write_scope? | ||
allow_delete if delete_scope? | ||
end | ||
|
||
# scenarios:read | ||
# -------------- | ||
return unless scopes.include?('scenarios:read') | ||
can :read, Scenario, id: ScenarioUser.where(user_id: user.id, role_id: User::ROLES.key(:scenario_viewer)..).pluck(:scenario_id) | ||
# scenarios:write | ||
# --------------- | ||
private | ||
|
||
# Methods to allow access to scenarios based on the role. | ||
# Everyone can read public scenarios. | ||
def allow_public_read | ||
can :read, Scenario, private: false | ||
end | ||
|
||
return unless scopes.include?('scenarios:write') | ||
def allow_read | ||
if admin? | ||
can :read, Scenario | ||
else | ||
can :read, Scenario, id: viewer_scenario_ids | ||
end | ||
end | ||
|
||
def allow_write | ||
can :create, Scenario | ||
|
||
# Unowned public scenario. | ||
can :update, Scenario, private: false | ||
cannot :update, Scenario, private: false, id: ScenarioUser.pluck(:scenario_id) | ||
if admin? | ||
# Admins with write scope can update and clone all scenarios. | ||
can :update, Scenario | ||
can :clone, Scenario | ||
else | ||
# Non-admins | ||
# Allow updating unowned public scenarios except when any association exists. | ||
can :update, Scenario, private: false | ||
cannot :update, Scenario, private: false, id: ScenarioUser.pluck(:scenario_id) | ||
# Allow updating scenarios where the user is a collaborator. | ||
can :update, Scenario, id: collaborator_scenario_ids | ||
|
||
# Self-owned scenario. | ||
can :update, Scenario, id: ScenarioUser.where(user_id: user.id, role_id: User::ROLES.key(:scenario_collaborator)..).pluck(:scenario_id) | ||
# Allow cloning both unowned public scenarios and self-owned scenarios. | ||
can :clone, Scenario, private: false | ||
can :clone, Scenario, id: collaborator_scenario_ids | ||
end | ||
end | ||
|
||
# Actions that involve reading one scenario and writing to another. | ||
can :clone, Scenario, private: false | ||
can :clone, Scenario, id: ScenarioUser.where(user_id: user.id, role_id: User::ROLES.key(:scenario_collaborator)..).pluck(:scenario_id) | ||
def allow_delete | ||
if admin? | ||
can :destroy, Scenario | ||
else | ||
can :destroy, Scenario, id: owner_scenario_ids | ||
end | ||
end | ||
|
||
# scenarios:delete | ||
# ---------------- | ||
# Methods to get the scenario ids for the user based on the role. | ||
def viewer_scenario_ids | ||
ScenarioUser.where( | ||
user_id: @user.id, | ||
role_id: User::ROLES.key(:scenario_viewer).. | ||
).pluck(:scenario_id) | ||
end | ||
|
||
return unless scopes.include?('scenarios:delete') | ||
def collaborator_scenario_ids | ||
ScenarioUser.where( | ||
user_id: @user.id, | ||
role_id: User::ROLES.key(:scenario_collaborator).. | ||
).pluck(:scenario_id) | ||
end | ||
|
||
def owner_scenario_ids | ||
ScenarioUser.where( | ||
user_id: @user.id, | ||
role_id: User::ROLES.key(:scenario_owner) | ||
).pluck(:scenario_id) | ||
end | ||
|
||
# Methods to check the scopes of the token. | ||
def read_scope? | ||
@scopes.include?('scenarios:read') | ||
end | ||
|
||
def write_scope? | ||
@scopes.include?('scenarios:write') | ||
end | ||
|
||
def delete_scope? | ||
@scopes.include?('scenarios:delete') | ||
end | ||
|
||
can :destroy, Scenario, id: ScenarioUser.where(user_id: user.id, role_id: User::ROLES.key(:scenario_owner)).pluck(:scenario_id) | ||
def admin? | ||
@user&.admin? | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.