Commit 1fd0639a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'feature/ldap_groups_select' of /home/git/repositories/gitlab/gitlab-ee

parents 5cd06603 89654431
......@@ -2,13 +2,14 @@
users_path: "/api/:version/users.json"
user_path: "/api/:version/users/:id.json"
notes_path: "/api/:version/projects/:id/notes.json"
ldap_groups_path: "/api/:version/ldap/groups.json"
# Get 20 (depends on api) recent notes
# Get 20 (depends on api) recent notes
# and sort the ascending from oldest to newest
notes: (project_id, callback) ->
url = Api.buildUrl(Api.notes_path)
url = url.replace(':id', project_id)
$.ajax(
url: url,
data:
......@@ -37,7 +38,7 @@
# Only active users retrieved
users: (query, callback) ->
url = Api.buildUrl(Api.users_path)
$.ajax(
url: url
data:
......@@ -52,3 +53,18 @@
buildUrl: (url) ->
url = gon.relative_url_root + url if gon.relative_url_root?
return url.replace(':version', gon.api_version)
# Return LDAP groups list. Filtered by query
ldap_groups: (query, callback) ->
url = Api.buildUrl(Api.ldap_groups_path)
$.ajax(
url: url
data:
private_token: gon.api_token
search: query
per_page: 20
active: true
dataType: "json"
).done (groups) ->
callback(groups)
$ ->
ldapGroupResult = (group) ->
group.cn
groupFormatSelection = (group) ->
group.cn
$('.ajax-ldap-groups-select').each (i, select) ->
$(select).select2
id: (group) ->
group.cn
placeholder: "Search for a LDAP group"
minimumInputLength: 1
query: (query) ->
Api.ldap_groups query.term, (groups) ->
data = { results: groups }
query.callback(data)
initSelection: (element, callback) ->
id = $(element).val()
if id isnt ""
callback(cn: id)
formatResult: ldapGroupResult
formatSelection: groupFormatSelection
dropdownCssClass: "ajax-groups-dropdown"
......@@ -76,7 +76,7 @@
= f.label :ldap_cn do
LDAP Group cn
.controls
= f.text_field :ldap_cn, placeholder: "Ex. QA group", class: "xxlarge left"
= f.hidden_field :ldap_cn, placeholder: "Ex. QA group", class: "xxlarge ajax-ldap-groups-select"
.control-group
= f.label :ldap_access do
......
......@@ -38,5 +38,6 @@ module API
mount ProjectSnippets
mount DeployKeys
mount ProjectHooks
mount Ldap
end
end
......@@ -136,5 +136,9 @@ module API
expose :target_id, :target_type, :author_id
expose :data, :target_title
end
class LdapGroup < Grape::Entity
expose :cn
end
end
end
module API
# groups API
class Ldap < Grape::API
before { authenticate! }
resource :ldap do
# Get a LDAP groups list. Limit size to 20 of them.
# Filter results by name using search param
#
# Example Request:
# GET /ldap/groups
get 'groups' do
@groups = Gitlab::LDAP::Adapter.new.groups("#{params[:search]}*", 20)
present @groups, with: Entities::LdapGroup
end
end
end
end
......@@ -40,12 +40,14 @@ module Gitlab
# Ex.
# groups("dev*") # return all groups start with 'dev'
#
def groups(cn = "*")
def groups(cn = "*", size = nil)
options = {
base: config['group_base'],
filter: Net::LDAP::Filter.eq("cn", cn)
}
options.merge!(size: size) if size
ldap.search(options).map do |entry|
Gitlab::LDAP::Group.new(entry)
end
......
require 'spec_helper'
describe API::API do
include ApiHelpers
let(:user) { create(:user) }
before do
groups = [
OpenStruct.new(cn: 'developers'),
OpenStruct.new(cn: 'students')
]
Gitlab::LDAP::Adapter.any_instance.stub(
groups: groups
)
end
describe "GET /ldap/groups" do
context "when unauthenticated" do
it "should return authentication error" do
get api("/ldap/groups")
response.status.should == 401
end
end
context "when authenticated as user" do
it "should return an array of ldap groups" do
get api("/ldap/groups", user)
response.status.should == 200
json_response.should be_an Array
json_response.length.should == 2
json_response.first['cn'].should == 'developers'
end
end
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