Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle authorization when querying for a specific resource #503

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions app/controllers/apipie/apipies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,39 @@ def get_language
end

def authorized_doc

return if @doc.nil?
return @doc unless Apipie.configuration.authorize

new_doc = { :docs => @doc[:docs].clone }

new_doc[:docs][:resources] = @doc[:docs][:resources].select do |k, v|
if instance_exec(k, nil, v, &Apipie.configuration.authorize)
v[:methods] = v[:methods].select do |h|
instance_exec(k, h[:name], h, &Apipie.configuration.authorize)
end
true
else
false
resources = @doc[:docs][:resources]

if params[:resource].present? and resources.is_a?(Array)
# We assume only one resource in the array when a specific resource was queried
resource_name = params[:resource]
authorized_resources = []
authorized_resources << resources.first if authorize_resource?(resource_name, resources.first)
new_doc[:docs][:resources] = authorized_resources
else
# Assume resource is a hash
new_doc[:docs][:resources] = resources.select do |k, v|
authorize_resource?(k, v)
end
end

new_doc
end

def authorize_resource?(name, value)
if instance_exec(name, nil, value, &Apipie.configuration.authorize)
value[:methods] = value[:methods].select do |h|
instance_exec(name, h[:name], h, &Apipie.configuration.authorize)
end
return true
end

false
end

def get_format
[:resource, :method, :version].each do |par|
if params[par]
Expand Down