Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pygitlab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fsl
pygitlab
Commits
c9264cf7
Commit
c9264cf7
authored
7 years ago
by
Jakob Berger
Committed by
jakob
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Lots of LDAP changes/fixes
parent
dbcc087b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ldap.py
+37
-10
37 additions, 10 deletions
ldap.py
with
37 additions
and
10 deletions
ldap.py
+
37
−
10
View file @
c9264cf7
import
ldap3
import
ldap3
class
LdapHandler
:
class
LdapHandler
:
def
__init__
(
self
,
ldaphost
:
str
,
default_context
:
str
=
''
,
ssl
:
bool
=
False
):
def
__init__
(
self
,
ldaphost
:
str
,
default_context
:
str
=
''
,
ssl
:
bool
=
False
):
server
=
ldap3
.
Server
(
ldaphost
,
use_ssl
=
ssl
,
get_info
=
ldap3
.
ALL
)
server
=
ldap3
.
Server
(
ldaphost
,
use_ssl
=
ssl
,
get_info
=
ldap3
.
ALL
)
self
.
connection
=
ldap3
.
Connection
(
server
,
auto_bind
=
True
)
self
.
connection
=
ldap3
.
Connection
(
server
,
auto_bind
=
True
)
self
.
default_context
=
default_context
self
.
default_context
=
default_context
def
get_ldap_
user
(
self
,
search_filter
:
dic
t
,
silent
:
bool
=
True
,
context
:
str
=
'
__USEDEFAULT__
'
,
def
get_ldap_
entities
(
self
,
search_filter
s
:
lis
t
,
silent
:
bool
=
True
,
context
:
str
=
None
,
attributes
:
li
st
=
[
'
sn
'
,
'
gn
'
,
'
objectclass
'
]):
attributes
:
s
e
t
=
[
'
sn
'
,
'
gn
'
,
'
objectclass
'
])
->
list
:
if
context
==
'
__USEDEFAULT__
'
:
if
context
is
None
:
search_context
=
self
.
default_context
search_context
=
self
.
default_context
else
:
else
:
search_context
=
context
search_context
=
context
first
=
True
search_string
=
"
(&
"
search_string
=
"
(|
"
for
key
,
value
in
search_filter
.
items
():
for
search_filter
in
search_filters
:
search_string
+=
'
(
'
+
key
+
'
=
'
+
value
+
'
)
'
# if first:
# first = False
# else:
# search_string += '||'
search_string
+=
"
(&
"
for
key
,
value
in
search_filter
.
items
():
search_string
+=
'
(
'
+
key
+
'
=
'
+
value
+
'
)
'
search_string
+=
"
)
"
search_string
+=
'
)
'
search_string
+=
'
)
'
self
.
connection
.
search
(
search_context
,
search_string
,
attributes
=
attributes
)
self
.
connection
.
search
(
search_context
,
search_string
,
attributes
=
attributes
)
return
self
.
connection
.
entries
def
get_ldap_users
(
self
,
usernames
:
list
,
attributes
:
dict
=
{
'
username
'
:
'
uid
'
,
'
name
'
:
'
cn
'
,
'
mail
'
:
'
mail
'
,
'
location
'
:
'
l
'
,
'
extern_uid
'
:
'
dn
'
},
default_vars
:
dict
=
{
'
provider
'
:
'
ldapmain
'
,
'
project_limit
'
:
'
0
'
,
'
confirm
'
:
'
false
'
})
->
list
:
"""
queries the ldap and prefills the user object for you.
The attributes dictionary maps the names of the user object attributes to the names of the ldap fields
"""
query
=
self
.
get_ldap_entities
([{
'
uid
'
:
username
}
for
username
in
usernames
],
attributes
=
set
(
attributes
.
values
()))
users
=
[]
for
entry
in
query
:
user
=
default_vars
.
copy
()
for
attr
,
ld
in
attributes
.
items
():
user
[
attr
]
=
entry
[
ld
].
encode
(
'
utf8
'
)
users
.
append
(
user
)
return
users
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment