Commit bcd58069 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add access-level filter support for projects select

This also refactores ProjectSelect adding some decorator-like functions.
parent a91101b1
class @ProjectSelect class @ProjectSelect
constructor: -> constructor: ->
$('.ajax-project-select').each (i, select) -> $('.ajax-project-select').each (i, select) =>
@groupId = $(select).data('group-id') @groupId = $(select).data('group-id')
@includeGroups = $(select).data('include-groups') @includeGroups = $(select).data('include-groups')
@orderBy = $(select).data('order-by') || 'id' @orderBy = $(select).data('order-by') || 'id'
@selectId = $(select).data('select-id') || 'web_url' @selectId = $(select).data('select-id') || 'web_url'
@accessLevel = $(select).data('access-level')
placeholder = "Search for project" placeholder = "Search for project"
placeholder += " or group" if @includeGroups placeholder += " or group" if @includeGroups
...@@ -12,25 +13,11 @@ class @ProjectSelect ...@@ -12,25 +13,11 @@ class @ProjectSelect
$(select).select2 $(select).select2
placeholder: placeholder placeholder: placeholder
minimumInputLength: 0 minimumInputLength: 0
query: (query) => query: (options) =>
finalCallback = (projects) ->
data = { results: projects }
query.callback(data)
if @includeGroups
projectsCallback = (projects) ->
groupsCallback = (groups) ->
data = groups.concat(projects)
finalCallback(data)
Api.groups query.term, false, groupsCallback
else
projectsCallback = finalCallback
if @groupId if @groupId
Api.groupProjects @groupId, query.term, projectsCallback Api.groupProjects @groupId, options.term, @createCallback(options)
else else
Api.projects query.term, @orderBy, projectsCallback Api.projects options.term, @orderBy, @createCallback(options)
id: (project) => id: (project) =>
project[@selectId] project[@selectId]
...@@ -39,3 +26,36 @@ class @ProjectSelect ...@@ -39,3 +26,36 @@ class @ProjectSelect
project.name_with_namespace || project.name project.name_with_namespace || project.name
dropdownCssClass: "ajax-project-dropdown" dropdownCssClass: "ajax-project-dropdown"
createCallback: (options) =>
finalCallback = (projects) ->
options.callback({ results: projects })
@accessLevelCallbackDecorator(
@groupsCallbackDecorator(
finalCallback
)
)
groupsCallbackDecorator: (callback) =>
return callback unless @includeGroups
(projects) =>
Api.groups options.term, false, (groups) =>
data = groups.concat(projects)
callback(data)
accessLevelCallbackDecorator: (callback) =>
return callback unless @accessLevel
##
# Requires ECMAScript >= 5
#
(projects) =>
data = projects.filter (i) =>
max = Math.max(i.permissions.group_access?.access_level ? 0,
i.permissions.project_access?.access_level ? 0)
max >= @accessLevel
callback(data)
...@@ -429,7 +429,7 @@ class User < ActiveRecord::Base ...@@ -429,7 +429,7 @@ class User < ActiveRecord::Base
Group.where("namespaces.id IN (#{union.to_sql})") Group.where("namespaces.id IN (#{union.to_sql})")
end end
# Returns the groups a user is authorized to access. # Returns projects user is authorized to access.
def authorized_projects def authorized_projects
Project.where("projects.id IN (#{projects_union.to_sql})") Project.where("projects.id IN (#{projects_union.to_sql})")
end end
......
...@@ -15,7 +15,7 @@ module Issues ...@@ -15,7 +15,7 @@ module Issues
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# New issue tasks # New issue tasks
# #
open_new_issue create_new_issue
rewrite_notes rewrite_notes
add_moved_from_note add_moved_from_note
...@@ -43,7 +43,7 @@ module Issues ...@@ -43,7 +43,7 @@ module Issues
can?(@current_user, :admin_issue, @project_new) can?(@current_user, :admin_issue, @project_new)
end end
def open_new_issue def create_new_issue
@issue_new.iid = nil @issue_new.iid = nil
@issue_new.project = @project_new @issue_new.project = @project_new
@issue_new.labels = [] @issue_new.labels = []
......
...@@ -73,7 +73,8 @@ ...@@ -73,7 +73,8 @@
= f.label :move_to_project_id, 'Move', class: 'control-label' = f.label :move_to_project_id, 'Move', class: 'control-label'
.col-sm-10 .col-sm-10
= project_select_tag("#{issuable.class.model_name.param_key}[move_to_project_id]", = project_select_tag("#{issuable.class.model_name.param_key}[move_to_project_id]",
placeholder: 'Select project', class: 'custom-form-control', data: { 'select-id' => 'id' }) placeholder: 'Select project', class: 'custom-form-control',
data: { 'select-id' => 'id', 'access-level' => Gitlab::Access::REPORTER })
- if issuable.is_a?(MergeRequest) - if issuable.is_a?(MergeRequest)
%hr %hr
......
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