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