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
7e31fa20
Commit
7e31fa20
authored
8 years ago
by
Jakob Berger
Committed by
jakob
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
initial commit
parents
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
__init__.py
+67
-0
67 additions, 0 deletions
__init__.py
ldap.py
+31
-0
31 additions, 0 deletions
ldap.py
with
98 additions
and
0 deletions
__init__.py
0 → 100644
+
67
−
0
View file @
7e31fa20
import
urllib.request
,
urllib
.
parse
,
urllib
.
error
import
json
class
Gitlab
:
def
__init__
(
self
,
host
,
private_token
,
urltemplate
=
'
https://%s/api/v3/
'
,
token_type
=
'
private
'
):
self
.
apiurl
=
urltemplate
%
host
if
token_type
is
'
private
'
:
self
.
auth
=
{
'
PRIVATE-TOKEN
'
:
private_token
}
elif
token_type
is
'
oauth
'
:
self
.
auth
=
{
'
AUTHORIZATION
'
:
'
Bearer %s
'
%
private_token
}
def
post_data
(
self
,
url
:
str
,
post_data_dict
:
dict
)
->
object
:
data
=
urllib
.
parse
.
urlencode
(
post_data_dict
).
encode
(
"
utf-8
"
)
req
=
urllib
.
request
.
Request
(
"
%s/api/v3/%s
"
%
(
self
.
apiurl
,
url
),
data
,
{
'
AUTHORIZATION
'
:
'
Bearer %s
'
%
auth_token
})
return
json
.
loads
(
urllib
.
request
.
urlopen
(
req
).
read
().
decode
(
"
utf-8
"
))
def
iterate_over_objects
(
self
,
url
:
str
,
page_filter_func
:
staticmethod
,
*
args
)
->
list
:
results
=
[]
stop
=
False
page
=
[
'
yo
'
]
page_no
=
1
while
page
is
not
None
and
not
stop
:
request
=
urllib
.
request
.
Request
(
"
%s%s?per_page=100&page=%d
"
%
(
self
.
apiurl
,
url
,
page_no
),
None
,
self
.
auth
)
json_text
=
urllib
.
request
.
urlopen
(
request
).
read
().
decode
(
"
utf-8
"
)
page
=
json
.
loads
(
json_text
)
stop
=
page_filter_func
(
page
,
results
,
*
args
)
page_no
+=
1
return
results
def
find_single_object
(
self
,
url
,
eval_single_object_func
,
*
args
):
def
__find_single_object_abstract
(
page
,
results
,
*
args
):
for
gitlabobject
in
page
:
result
=
eval_single_object_func
(
gitlabobject
,
*
args
)
if
result
is
True
:
results
.
append
(
gitlabobject
)
return
True
return
False
return
self
.
iterate_over_objects
(
url
,
__find_single_object_abstract
,
*
args
)
def
find_objects_by_attributes
(
self
,
url
,
attributes
):
return
self
.
iterate_over_objects
(
url
,
_filter_page_by_attribute
,
attributes
)
def
find_single_object_by_attributes
(
self
,
url
,
attributes
):
return
self
.
find_single_object
(
url
,
_object_matches_attributes
,
attributes
)
def
_object_matches_attributes
(
gitlabobject
,
attributes
):
is_match
=
True
for
attribute
in
attributes
.
items
():
if
gitlabobject
[
attribute
[
0
]]
is
not
attribute
[
1
]:
is_match
=
False
break
return
is_match
def
_filter_page_by_attribute
(
page
,
results
,
attributes
):
for
gitlabobject
in
page
:
if
_object_matches_attributes
(
gitlabobject
,
attributes
):
results
.
append
(
gitlabobject
)
return
False
#return false to keep appending following pages
def
_append_objects_to_results
(
page
,
results
,
*
args
):
for
element
in
page
:
results
.
append
(
element
)
return
False
#return false to keep appending pages
\ No newline at end of file
This diff is collapsed.
Click to expand it.
ldap.py
0 → 100644
+
31
−
0
View file @
7e31fa20
import
ldap3
class
LdapHandler
:
def
__init__
(
self
,
ldaphost
:
str
,
default_query
:
str
=
''
,
ssl
:
bool
=
False
):
server
=
ldap3
.
Server
(
ldaphost
,
use_ssl
=
ssl
,
get_info
=
ldap3
.
ALL
)
self
.
connection
=
ldap3
.
Connection
(
server
,
auto_bind
=
True
)
self
.
default_query
=
default_query
def
get_ldap_users
(
self
,
user_list
,
query
,
silent
=
False
):
results
=
[]
i
=
1
for
user
in
user_list
:
user
=
user
.
strip
()
if
not
silent
:
print
(
"
%s (%d/%d)
"
%
(
user
,
i
,
len
(
results
)))
search_no
=
self
.
ldap
.
search
(
query
,
ldap3
.
SCOPE_SUBTREE
,
"
uid=%s
"
%
user
,
None
)
search_res
=
self
.
ldap
.
result
(
search_no
,
0
)
if
search_res
[
0
]
==
100
:
dn
=
search_res
[
1
][
0
][
0
]
name
=
search_res
[
1
][
0
][
1
][
'
cn
'
][
0
]
mail
=
search_res
[
1
][
0
][
1
][
'
mail
'
][
0
]
location
=
search_res
[
1
][
0
][
1
][
'
l
'
][
0
]
results
.
append
({
'
dn
'
:
dn
,
'
username
'
:
user
,
'
name
'
:
name
,
'
mail
'
:
mail
,
'
location
'
:
location
})
return
results
def
connect
(
ldaphost
:
str
,
default_query
:
str
=
''
,
ssl
:
bool
=
False
)
->
LdapHandler
:
return
LdapHandler
(
ldaphost
,
default_query
,
ssl
)
\ No newline at end of file
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