Commit d5c49779 authored by Markus Koller's avatar Markus Koller

Consistently use current_user in API entities

parent d05dd81b
......@@ -78,11 +78,11 @@ module API
expose :container_registry_enabled
# Expose old field names with the new permissions methods to keep API compatible
expose(:issues_enabled) { |project, options| project.feature_available?(:issues, options[:user]) }
expose(:merge_requests_enabled) { |project, options| project.feature_available?(:merge_requests, options[:user]) }
expose(:wiki_enabled) { |project, options| project.feature_available?(:wiki, options[:user]) }
expose(:builds_enabled) { |project, options| project.feature_available?(:builds, options[:user]) }
expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:user]) }
expose(:issues_enabled) { |project, options| project.feature_available?(:issues, options[:current_user]) }
expose(:merge_requests_enabled) { |project, options| project.feature_available?(:merge_requests, options[:current_user]) }
expose(:wiki_enabled) { |project, options| project.feature_available?(:wiki, options[:current_user]) }
expose(:builds_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
expose :created_at, :last_activity_at
expose :shared_runners_enabled
......@@ -92,7 +92,7 @@ module API
expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
expose :avatar_url
expose :star_count, :forks_count
expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:user]) && project.default_issues_tracker? }
expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) && project.default_issues_tracker? }
expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
expose :public_builds
expose :shared_with_groups do |project, options|
......@@ -440,12 +440,12 @@ module API
class ProjectWithAccess < Project
expose :permissions do
expose :project_access, using: Entities::ProjectAccess do |project, options|
project.project_members.find_by(user_id: options[:user].id)
project.project_members.find_by(user_id: options[:current_user].id)
end
expose :group_access, using: Entities::GroupAccess do |project, options|
if project.group
project.group.group_members.find_by(user_id: options[:user].id)
project.group.group_members.find_by(user_id: options[:current_user].id)
end
end
end
......
......@@ -38,7 +38,7 @@ module API
groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
groups = groups.reorder(params[:order_by] => params[:sort])
present paginate(groups), with: Entities::Group
present paginate(groups), with: Entities::Group, current_user: current_user
end
desc 'Get list of owned groups for authenticated user' do
......@@ -49,7 +49,7 @@ module API
end
get '/owned' do
groups = current_user.owned_groups
present paginate(groups), with: Entities::Group, user: current_user
present paginate(groups), with: Entities::Group, current_user: current_user
end
desc 'Create a group. Available only for users who can create groups.' do
......@@ -66,7 +66,7 @@ module API
group = ::Groups::CreateService.new(current_user, declared_params(include_missing: false)).execute
if group.persisted?
present group, with: Entities::Group
present group, with: Entities::Group, current_user: current_user
else
render_api_error!("Failed to save group #{group.errors.messages}", 400)
end
......@@ -92,7 +92,7 @@ module API
authorize! :admin_group, group
if ::Groups::UpdateService.new(group, current_user, declared_params(include_missing: false)).execute
present group, with: Entities::GroupDetail
present group, with: Entities::GroupDetail, current_user: current_user
else
render_validation_error!(group)
end
......@@ -103,7 +103,7 @@ module API
end
get ":id" do
group = find_group!(params[:id])
present group, with: Entities::GroupDetail
present group, with: Entities::GroupDetail, current_user: current_user
end
desc 'Remove a group.'
......@@ -134,7 +134,7 @@ module API
projects = GroupProjectsFinder.new(group).execute(current_user)
projects = filter_projects(projects)
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
present paginate(projects), with: entity, user: current_user
present paginate(projects), with: entity, current_user: current_user
end
desc 'Transfer a project to the group namespace. Available only for admin.' do
......@@ -150,7 +150,7 @@ module API
result = ::Projects::TransferService.new(project, current_user).execute(group)
if result
present group, with: Entities::GroupDetail
present group, with: Entities::GroupDetail, current_user: current_user
else
render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
end
......
......@@ -75,7 +75,7 @@ module API
projects = filter_projects(projects)
entity = params[:simple] || !current_user ? Entities::BasicProjectDetails : Entities::ProjectWithAccess
present paginate(projects), with: entity, user: current_user
present paginate(projects), with: entity, current_user: current_user
end
desc 'Get a projects list for authenticated user' do
......@@ -94,7 +94,7 @@ module API
projects = filter_projects(projects)
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::ProjectWithAccess
present paginate(projects), with: entity, user: current_user
present paginate(projects), with: entity, current_user: current_user
end
desc 'Get an owned projects list for authenticated user' do
......@@ -110,7 +110,7 @@ module API
projects = current_user.owned_projects
projects = filter_projects(projects)
present paginate(projects), with: Entities::ProjectWithAccess, user: current_user
present paginate(projects), with: Entities::ProjectWithAccess, current_user: current_user
end
desc 'Gets starred project for the authenticated user' do
......@@ -126,7 +126,7 @@ module API
projects = current_user.viewable_starred_projects
projects = filter_projects(projects)
present paginate(projects), with: Entities::Project, user: current_user
present paginate(projects), with: Entities::Project, current_user: current_user
end
desc 'Get all projects for admin user' do
......@@ -142,7 +142,7 @@ module API
projects = Project.all
projects = filter_projects(projects)
present paginate(projects), with: Entities::ProjectWithAccess, user: current_user
present paginate(projects), with: Entities::ProjectWithAccess, current_user: current_user
end
desc 'Search for projects the current user has access to' do
......@@ -221,7 +221,7 @@ module API
end
get ":id" do
entity = current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails
present user_project, with: entity, user: current_user,
present user_project, with: entity, current_user: current_user,
user_can_admin_project: can?(current_user, :admin_project, user_project)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment