Commit eea317795ea970f762a1331beba6fde20e7a8aeb

Authored by Dmitriy Zaporozhets
1 parent a91a6491

Add current user permissions info to /api/projects/:id.json

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing 2 changed files with 24 additions and 2 deletions   Show diff stats
lib/api/entities.rb
... ... @@ -44,7 +44,7 @@ module API
44 44 expose :id, :description, :default_branch
45 45 expose :public?, as: :public
46 46 expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
47   - expose :owner, using: Entities::UserBasic
  47 + expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
48 48 expose :name, :name_with_namespace
49 49 expose :path, :path_with_namespace
50 50 expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at
... ... @@ -175,5 +175,27 @@ module API
175 175 class Namespace < Grape::Entity
176 176 expose :id, :path, :kind
177 177 end
  178 +
  179 + class ProjectAccess < Grape::Entity
  180 + expose :project_access, as: :access_level
  181 + expose :notification_level
  182 + end
  183 +
  184 + class GroupAccess < Grape::Entity
  185 + expose :group_access, as: :access_level
  186 + expose :notification_level
  187 + end
  188 +
  189 + class ProjectWithAccess < Project
  190 + expose :permissions do
  191 + expose :project_access, using: Entities::ProjectAccess do |project, options|
  192 + project.users_projects.find_by(user_id: options[:user].id)
  193 + end
  194 +
  195 + expose :group_access, using: Entities::GroupAccess do |project, options|
  196 + project.group.users_groups.find_by(user_id: options[:user].id)
  197 + end
  198 + end
  199 + end
178 200 end
179 201 end
... ...
lib/api/projects.rb
... ... @@ -48,7 +48,7 @@ module API
48 48 # Example Request:
49 49 # GET /projects/:id
50 50 get ":id" do
51   - present user_project, with: Entities::Project
  51 + present user_project, with: Entities::ProjectWithAccess, user: current_user
52 52 end
53 53  
54 54 # Get a single project events
... ...