Pass scopes instead of token to ApiAbility
This commit is contained in:
parent
304e0ef638
commit
77a2657d33
4 changed files with 55 additions and 60 deletions
|
@ -3,7 +3,7 @@
|
|||
class ApiAbility
|
||||
include CanCan::Ability
|
||||
|
||||
def initialize(user, token)
|
||||
def initialize(user, scopes)
|
||||
can :read, [:version, :capability, :permission, :map]
|
||||
|
||||
if Settings.status != "database_offline"
|
||||
|
@ -17,31 +17,31 @@ class ApiAbility
|
|||
can :read, UserBlock
|
||||
|
||||
if user&.active?
|
||||
can [:create, :comment, :close, :reopen], Note if scope?(token, :write_notes)
|
||||
can [:create, :destroy], NoteSubscription if scope?(token, :write_notes)
|
||||
can [:create, :comment, :close, :reopen], Note if scopes.include?("write_notes")
|
||||
can [:create, :destroy], NoteSubscription if scopes.include?("write_notes")
|
||||
|
||||
can :read, Trace if scope?(token, :read_gpx)
|
||||
can [:create, :update, :destroy], Trace if scope?(token, :write_gpx)
|
||||
can :read, Trace if scopes.include?("read_gpx")
|
||||
can [:create, :update, :destroy], Trace if scopes.include?("write_gpx")
|
||||
|
||||
can :details, User if scope?(token, :read_prefs)
|
||||
can :read, UserPreference if scope?(token, :read_prefs)
|
||||
can [:update, :update_all, :destroy], UserPreference if scope?(token, :write_prefs)
|
||||
can :details, User if scopes.include?("read_prefs")
|
||||
can :read, UserPreference if scopes.include?("read_prefs")
|
||||
can [:update, :update_all, :destroy], UserPreference if scopes.include?("write_prefs")
|
||||
|
||||
can [:read, :update, :destroy], Message if scope?(token, :consume_messages)
|
||||
can :create, Message if scope?(token, :send_messages)
|
||||
can [:read, :update, :destroy], Message if scopes.include?("consume_messages")
|
||||
can :create, Message if scopes.include?("send_messages")
|
||||
|
||||
if user.terms_agreed?
|
||||
can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scope?(token, :write_api)
|
||||
can :create, ChangesetComment if scope?(token, :write_api)
|
||||
can [:create, :update, :destroy], [Node, Way, Relation] if scope?(token, :write_api)
|
||||
can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scopes.include?("write_api")
|
||||
can :create, ChangesetComment if scopes.include?("write_api")
|
||||
can [:create, :update, :destroy], [Node, Way, Relation] if scopes.include?("write_api")
|
||||
end
|
||||
|
||||
if user.moderator?
|
||||
can [:destroy, :restore], ChangesetComment if scope?(token, :write_api)
|
||||
can [:destroy, :restore], ChangesetComment if scopes.include?("write_api")
|
||||
|
||||
can :destroy, Note if scope?(token, :write_notes)
|
||||
can :destroy, Note if scopes.include?("write_notes")
|
||||
|
||||
can :redact, [OldNode, OldWay, OldRelation] if user&.terms_agreed? && scope?(token, :write_redactions)
|
||||
can :redact, [OldNode, OldWay, OldRelation] if user&.terms_agreed? && scopes.include?("write_redactions")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -73,10 +73,4 @@ class ApiAbility
|
|||
# See the wiki for details:
|
||||
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def scope?(token, scope)
|
||||
token&.includes_scope?(scope)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue