]> git.openstreetmap.org Git - rails.git/blob - app/abilities/api_ability.rb
Update bundle
[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, Relation, OldNode, OldWay, OldRelation]
19       can :read, UserBlock
20
21       if user&.active?
22         can [:create, :comment, :close, :reopen], Note if scope?(token, :write_notes)
23         can [:create, :destroy], NoteSubscription if scope?(token, :write_notes)
24
25         can :read, Trace if scope?(token, :read_gpx)
26         can [:create, :update, :destroy], Trace if scope?(token, :write_gpx)
27
28         can :details, User if scope?(token, :read_prefs)
29         can :read, UserPreference if scope?(token, :read_prefs)
30         can [:update, :update_all, :destroy], UserPreference if scope?(token, :write_prefs)
31
32         can [:read, :update, :destroy], Message if scope?(token, :consume_messages)
33         can :create, Message if scope?(token, :send_messages)
34
35         if user.terms_agreed?
36           can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scope?(token, :write_api)
37           can :create, ChangesetComment if scope?(token, :write_api)
38           can [:create, :update, :destroy], [Node, Way, Relation] if scope?(token, :write_api)
39         end
40
41         if user.moderator?
42           can [:destroy, :restore], ChangesetComment if scope?(token, :write_api)
43
44           can :destroy, Note if scope?(token, :write_notes)
45
46           can :redact, [OldNode, OldWay, OldRelation] if user&.terms_agreed? && scope?(token, :write_redactions)
47         end
48       end
49     end
50
51     # Define abilities for the passed in user here. For example:
52     #
53     #   user ||= User.new # guest user (not logged in)
54     #   if user.admin?
55     #     can :manage, :all
56     #   else
57     #     can :read, :all
58     #   end
59     #
60     # The first argument to `can` is the action you are giving the user
61     # permission to do.
62     # If you pass :manage it will apply to every action. Other common actions
63     # here are :read, :create, :update and :destroy.
64     #
65     # The second argument is the resource the user can perform the action on.
66     # If you pass :all it will apply to every resource. Otherwise pass a Ruby
67     # class of the resource.
68     #
69     # The third argument is an optional hash of conditions to further filter the
70     # objects.
71     # For example, here the user can only update published articles.
72     #
73     #   can :update, Article, :published => true
74     #
75     # See the wiki for details:
76     # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
77   end
78
79   private
80
81   def scope?(token, scope)
82     token&.includes_scope?(scope)
83   end
84 end