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