]> git.openstreetmap.org Git - rails.git/blob - app/abilities/api_ability.rb
Merge branch 'pull/5678'
[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)
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, :download], Changeset
14       can :read, Tracepoint
15       can :read, User
16       can :read, [Node, Way, Relation, OldNode, OldWay, OldRelation]
17       can :read, UserBlock
18
19       if user&.active?
20         can [:create, :comment, :close, :reopen], Note if scopes.include?("write_notes")
21         can [:create, :destroy], NoteSubscription if scopes.include?("write_notes")
22
23         can :read, Trace if scopes.include?("read_gpx")
24         can [:create, :update, :destroy], Trace if scopes.include?("write_gpx")
25
26         can :details, User if scopes.include?("read_prefs")
27         can :read, UserPreference if scopes.include?("read_prefs")
28         can [:update, :update_all, :destroy], UserPreference if scopes.include?("write_prefs")
29
30         can [:read, :update, :destroy], Message if scopes.include?("consume_messages")
31         can :create, Message if scopes.include?("send_messages")
32
33         can :read, :active_user_blocks_list if scopes.include?("read_prefs")
34
35         if user.terms_agreed?
36           can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scopes.include?("write_map")
37           can :create, ChangesetComment if scopes.include?("write_changeset_comments")
38           can [:create, :update, :destroy], [Node, Way, Relation] if scopes.include?("write_map")
39         end
40
41         if user.moderator?
42           can [:destroy, :restore], ChangesetComment if scopes.include?("write_changeset_comments")
43
44           can :destroy, Note if scopes.include?("write_notes")
45
46           can :redact, [OldNode, OldWay, OldRelation] if user.terms_agreed? && scopes.include?("write_redactions")
47
48           can :create, UserBlock if scopes.include?("write_blocks")
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 end