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