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