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
efd7efde
Commit
efd7efde
authored
7 years ago
by
Jakob Berger
Committed by
jakob
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
fixed a lot of stuff in ldap (again)
parent
c9264cf7
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
+18
-21
18 additions, 21 deletions
ldap.py
with
18 additions
and
21 deletions
ldap.py
+
18
−
21
View file @
efd7efde
import
ldap3
from
typing
import
List
,
Dict
# TODO: add a paginated search feature utilizing the ldap3 paginated search
class
LdapHandler
:
def
__init__
(
self
,
ldaphost
:
str
,
default_context
:
str
=
''
,
ssl
:
bool
=
False
):
server
=
ldap3
.
Server
(
ldaphost
,
use_ssl
=
ssl
,
get_info
=
ldap3
.
ALL
)
...
...
@@ -8,42 +10,37 @@ class LdapHandler:
self
.
default_context
=
default_context
def
get_ldap_entities
(
self
,
search_filters
:
list
,
silent
:
bool
=
True
,
context
:
str
=
None
,
attributes
:
set
=
[
'
sn
'
,
'
gn
'
,
'
objectclass
'
])
->
l
ist
:
attributes
:
set
=
[
'
sn
'
,
'
gn
'
,
'
objectclass
'
])
->
L
ist
[
ldap3
.
Entry
]
:
if
context
is
None
:
search_context
=
self
.
default_context
else
:
search_context
=
context
first
=
True
search_string
=
"
(|
"
for
search_filter
in
search_filters
:
# if first:
# first = False
# else:
# search_string += '||'
search_string
+=
"
(&
"
for
key
,
value
in
search_filter
.
items
():
search_string
+=
'
(
'
+
key
+
'
=
'
+
value
+
'
)
'
search_string
+=
"
)
"
search_string
+=
'
)
'
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
()))
def
get_ldap_users
(
self
,
usernames
:
List
[
str
],
attributes
:
dict
=
{
'
username
'
:
'
uid
'
,
'
name
'
:
'
cn
'
,
'
mail
'
:
'
mail
'
},
default_vars
:
dict
=
{
'
provider
'
:
'
ldapmain
'
,
'
project_limit
'
:
'
0
'
,
'
confirm
'
:
'
false
'
},
filters
:
dict
=
{})
->
List
[
Dict
]:
"""
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
The default_vars dictionary specifies static default values of the user object. Costumize to your needs.
filters is a dicitonary of filters applied to all user searches, for example an objectclass
"""
query
=
[{
'
uid
'
:
username
}
for
username
in
usernames
]
for
user_dict
in
query
:
user_dict
.
update
(
filters
)
results
=
self
.
get_ldap_entities
(
query
,
attributes
=
set
(
attributes
.
values
()))
users
=
[]
for
entry
in
query
:
for
entry
in
results
:
user
=
default_vars
.
copy
()
for
attr
,
ld
in
attributes
.
items
():
user
[
attr
]
=
entry
[
ld
].
encode
(
'
utf8
'
)
for
user_field
,
ldap_name
in
attributes
.
items
():
user
[
user_field
]
=
entry
.
entry_attributes_as_dict
[
ldap_name
][
0
]
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