]> git.openstreetmap.org Git - rails.git/blobdiff - app/abilities/api_ability.rb
Merge pull request #5677 from tomhughes/user-list-enhancements
[rails.git] / app / abilities / api_ability.rb
index 36cfee0bbd7c997970d79ead0722b93c0964fc1c..ec4de8f8e03b99dc795f88cecf6421aefb11c00e 100644 (file)
@@ -3,37 +3,49 @@
 class ApiAbility
   include CanCan::Ability
 
-  def initialize(user)
+  def initialize(user, scopes)
     can :read, [:version, :capability, :permission, :map]
 
     if Settings.status != "database_offline"
+      can [:read, :feed, :search], Note
+      can :create, Note unless user
+
       can [:read, :download], Changeset
-      can [:read, :create, :feed, :search], Note
       can :read, Tracepoint
       can :read, User
-      can :read, Node
-      can [:read, :full, :ways_for_node], Way
-      can [:read, :full, :relations_for_node, :relations_for_way, :relations_for_relation], Relation
-      can [:history, :read], [OldNode, OldWay, OldRelation]
+      can :read, [Node, Way, Relation, OldNode, OldWay, OldRelation]
       can :read, UserBlock
 
       if user&.active?
-        can [:comment, :close, :reopen], Note
-        can [:read, :create, :update, :destroy], Trace
-        can [:details, :gpx_files], User
-        can [:read, :update, :update_all, :destroy], UserPreference
+        can [:create, :comment, :close, :reopen], Note if scopes.include?("write_notes")
+        can [:create, :destroy], NoteSubscription if scopes.include?("write_notes")
+
+        can :read, Trace if scopes.include?("read_gpx")
+        can [:create, :update, :destroy], Trace if scopes.include?("write_gpx")
+
+        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 scopes.include?("consume_messages")
+        can :create, Message if scopes.include?("send_messages")
+
+        can :read, :active_user_blocks_list if scopes.include?("read_prefs")
 
         if user.terms_agreed?
-          can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset
-          can :create, ChangesetComment
-          can [:create, :update, :delete], [Node, Way, Relation]
+          can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scopes.include?("write_map")
+          can :create, ChangesetComment if scopes.include?("write_changeset_comments")
+          can [:create, :update, :destroy], [Node, Way, Relation] if scopes.include?("write_map")
         end
 
         if user.moderator?
-          can [:destroy, :restore], ChangesetComment
-          can :destroy, Note
+          can [:destroy, :restore], ChangesetComment if scopes.include?("write_changeset_comments")
+
+          can :destroy, Note if scopes.include?("write_notes")
+
+          can :redact, [OldNode, OldWay, OldRelation] if user.terms_agreed? && scopes.include?("write_redactions")
 
-          can :redact, [OldNode, OldWay, OldRelation] if user.terms_agreed?
+          can :create, UserBlock if scopes.include?("write_blocks")
         end
       end
     end