]> git.openstreetmap.org Git - rails.git/blob - app/abilities/api_ability.rb
Declare api way relations as nested resources
[rails.git] / app / abilities / api_ability.rb
1 # frozen_string_literal: true
2
3 class ApiAbility
4   include CanCan::Ability
5
6   def initialize(token)
7     can :read, [:version, :capability, :permission, :map]
8
9     if Settings.status != "database_offline"
10       user = User.find(token.resource_owner_id) if token
11
12       can [:read, :feed, :search], Note
13       can :create, Note unless token
14
15       can [:read, :download], Changeset
16       can :read, Tracepoint
17       can :read, User
18       can :read, [Node, Way]
19       can [:read, :relations_for_relation], Relation
20       can [:history, :read], [OldNode, OldWay, OldRelation]
21       can :read, UserBlock
22
23       if user&.active?
24         can [:create, :comment, :close, :reopen], Note if scope?(token, :write_notes)
25         can [:create, :destroy], NoteSubscription if scope?(token, :write_notes)
26
27         can :read, Trace if scope?(token, :read_gpx)
28         can [:create, :update, :destroy], Trace if scope?(token, :write_gpx)
29
30         can :details, User if scope?(token, :read_prefs)
31         can :read, UserPreference if scope?(token, :read_prefs)
32         can [:update, :update_all, :destroy], UserPreference if scope?(token, :write_prefs)
33
34         can [:read, :update, :destroy], Message if scope?(token, :consume_messages)
35         can :create, Message if scope?(token, :send_messages)
36
37         if user.terms_agreed?
38           can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scope?(token, :write_api)
39           can :create, ChangesetComment if scope?(token, :write_api)
40           can [:create, :update, :destroy], [Node, Way, Relation] if scope?(token, :write_api)
41         end
42
43         if user.moderator?
44           can [:destroy, :restore], ChangesetComment if scope?(token, :write_api)
45
46           can :destroy, Note if scope?(token, :write_notes)
47
48           can :redact, [OldNode, OldWay, OldRelation] if user&.terms_agreed? && scope?(token, :write_redactions)
49         end
50       end
51     end
52
53     # Define abilities for the passed in user here. For example:
54     #
55     #   user ||= User.new # guest user (not logged in)
56     #   if user.admin?
57     #     can :manage, :all
58     #   else
59     #     can :read, :all
60     #   end
61     #
62     # The first argument to `can` is the action you are giving the user
63     # permission to do.
64     # If you pass :manage it will apply to every action. Other common actions
65     # here are :read, :create, :update and :destroy.
66     #
67     # The second argument is the resource the user can perform the action on.
68     # If you pass :all it will apply to every resource. Otherwise pass a Ruby
69     # class of the resource.
70     #
71     # The third argument is an optional hash of conditions to further filter the
72     # objects.
73     # For example, here the user can only update published articles.
74     #
75     #   can :update, Article, :published => true
76     #
77     # See the wiki for details:
78     # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
79   end
80
81   private
82
83   def scope?(token, scope)
84     token&.includes_scope?(scope)
85   end
86 end