]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/permissions_controller.rb
Merge remote-tracking branch 'upstream/pull/4978'
[rails.git] / app / controllers / api / permissions_controller.rb
1 module Api
2   class PermissionsController < ApiController
3     authorize_resource :class => false
4
5     before_action :setup_user_auth
6     before_action :set_request_formats
7     around_action :api_call_handle_error, :api_call_timeout
8
9     # External apps that use the api are able to query which permissions
10     # they have. This currently returns a list of permissions granted to the current user:
11     # * if authenticated via OAuth, this list will contain all permissions granted by the user to the access_token.
12     # * if authenticated via basic auth all permissions are granted, so the list will contain all permissions.
13     # * unauthenticated users have no permissions, so the list will be empty.
14     def show
15       @permissions = if doorkeeper_token.present?
16                        doorkeeper_token.scopes.map { |s| :"allow_#{s}" }
17                      elsif current_token.present?
18                        ClientApplication.all_permissions.select { |p| current_token.read_attribute(p) }
19                      elsif current_user
20                        ClientApplication.all_permissions
21                      else
22                        []
23                      end
24
25       respond_to do |format|
26         format.xml
27         format.json
28       end
29     end
30   end
31 end