Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
isgphys
python
Commits
ebe828dd
Commit
ebe828dd
authored
7 years ago
by
Sven Mäder
Browse files
Options
Downloads
Patches
Plain Diff
Add abstraction classes Group, User
parent
9c53c8e5
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bin/showgroup.py
+14
-4
14 additions, 4 deletions
bin/showgroup.py
lib/isg/argparser.py
+2
-0
2 additions, 0 deletions
lib/isg/argparser.py
lib/isg/dphysldap.py
+37
-9
37 additions, 9 deletions
lib/isg/dphysldap.py
with
53 additions
and
13 deletions
bin/showgroup.py
+
14
−
4
View file @
ebe828dd
...
...
@@ -12,6 +12,7 @@ OPTS['access'] = 'accessRight'
OPTS
[
'
blocked
'
]
=
'
blocked
'
OPTS
[
'
home
'
]
=
'
homeDirectory
'
OPTS
[
'
shell
'
]
=
'
loginShell
'
OPTS
[
'
report
'
]
=
[
'
reportEnabled
'
,
'
reportUid
'
]
OPTS
[
'
mail
'
]
=
'
mail
'
OPTS
[
'
telephone
'
]
=
'
telephoneNumber
'
...
...
@@ -24,7 +25,12 @@ def main():
ldap
=
dphysldap
.
Ldap
()
groups
=
dphysldap
.
Groups
(
ldap
,
[
'
cn
'
,
'
gidNumber
'
,
'
memberUid
'
])
group_attrs
=
[
'
cn
'
,
'
gidNumber
'
,
'
owner
'
,
'
memberUid
'
]
if
args
[
'
report
'
]:
group_attrs
.
extend
(
OPTS
[
'
report
'
])
groups
=
dphysldap
.
Groups
(
ldap
,
group_attrs
)
groups
.
search
(
args
[
'
group
'
])
if
not
groups
:
...
...
@@ -36,11 +42,15 @@ def main():
else
:
group
=
groups
[
0
]
cn
=
group
[
'
cn
'
]
gid
=
group
[
'
gidNumber
'
]
members
=
group
[
'
memberUid
'
]
print
(
'
Members of {} ({}):
'
.
format
(
cn
,
gid
))
print
(
'
Group {} ({}):
'
.
format
(
group
[
'
cn
'
],
group
[
'
gidNumber
'
]))
print
(
'
Owner: {}
'
.
format
(
group
[
'
owner
'
]))
print
(
'
Report: {}
'
.
format
(
group
[
'
reportEnabled
'
]))
print
(
'
Reported to: {}
'
.
format
(
group
[
'
reportUid
'
]))
print
(
'
Members:
'
)
del
OPTS
[
'
report
'
]
attrs
=
[
'
uid
'
,
'
uidNumber
'
,
'
gecos
'
]
attrs
.
extend
([
v
for
k
,
v
in
OPTS
.
items
()
if
args
[
k
]])
...
...
This diff is collapsed.
Click to expand it.
lib/isg/argparser.py
+
2
−
0
View file @
ebe828dd
...
...
@@ -13,6 +13,8 @@ def simple(description, arg=None, arg_help='', opts=dict()):
parser
.
add_argument
(
arg
,
help
=
arg_help
)
for
k
,
v
in
opts
.
items
():
if
isinstance
(
v
,
list
):
v
=
'
,
'
.
join
(
v
)
parser
.
add_argument
(
'
-
'
+
k
[:
1
],
'
--
'
+
k
,
dest
=
k
,
action
=
'
store_const
'
,
const
=
True
,
help
=
'
Show
'
+
v
)
...
...
This diff is collapsed.
Click to expand it.
lib/isg/dphysldap.py
+
37
−
9
View file @
ebe828dd
...
...
@@ -30,27 +30,34 @@ class AttributeValue(list):
return
delimit
.
join
([
str
(
e
)
for
e
in
self
])
class
Entry
(
collections
.
abc
.
Mapping
):
class
Entry
(
collections
.
abc
.
Mutable
Mapping
):
"""
Abstraction class for LDAP Entry imitating dict
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_dict
=
dict
(
*
args
,
**
kwargs
)
self
.
_dict
=
dict
()
self
.
update
(
dict
(
*
args
,
**
kwargs
))
def
__getitem__
(
self
,
key
):
return
self
.
_dict
[
key
]
def
__setitem__
(
self
,
key
,
value
):
"""
If value is a list, convert it to AttributeValue
"""
value
=
self
.
_dict
[
key
]
if
isinstance
(
value
,
list
):
return
AttributeValue
(
value
)
return
value
self
.
_dict
[
key
]
=
AttributeValue
(
value
)
else
:
self
.
_dict
[
key
]
=
value
def
__delitem__
(
self
,
key
):
del
self
.
_dict
[
key
]
def
__iter__
(
self
):
return
iter
(
self
.
_dict
)
def
__len__
(
self
):
return
len
(
self
.
_
storage
)
return
len
(
self
.
_
dict
)
def
__str__
(
self
):
"""
...
...
@@ -59,6 +66,25 @@ class Entry(collections.abc.Mapping):
return
'
\n
'
.
join
([
'
:
'
.
join
([
k
,
str
(
v
)])
for
k
,
v
in
self
.
items
()
if
v
])
class
Group
(
Entry
):
"""
Abstraction class for LDAP Group imitating Entry
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
Entry
.
__init__
(
self
,
*
args
,
**
kwargs
)
if
'
owner
'
in
self
:
for
i
,
owner
in
enumerate
(
self
[
'
owner
'
]):
self
[
'
owner
'
][
i
]
=
owner
.
split
(
'
=
'
,
1
)[
1
].
split
(
'
,
'
,
1
)[
0
]
class
User
(
Entry
):
"""
Abstraction class for LDAP User imitating Entry
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
Entry
.
__init__
(
self
,
*
args
,
**
kwargs
)
class
Ldap
(
object
):
"""
LDAP connection to random server in pool
...
...
@@ -104,7 +130,8 @@ class Ldap(object):
"""
if
not
self
.
obj_user
:
self
.
obj_user
=
ldap3
.
ObjectDef
(
self
.
user_classes
,
self
.
connection
)
return
self
.
get_entries
(
self
.
obj_user
,
query
=
query
,
attributes
=
attributes
)
entries
=
self
.
get_entries
(
self
.
obj_user
,
query
=
query
,
attributes
=
attributes
)
return
[
User
(
e
)
for
e
in
entries
]
def
get_groups
(
self
,
query
=
''
,
attributes
=
None
):
"""
...
...
@@ -112,7 +139,8 @@ class Ldap(object):
"""
if
not
self
.
obj_group
:
self
.
obj_group
=
ldap3
.
ObjectDef
(
self
.
group_classes
,
self
.
connection
)
return
self
.
get_entries
(
self
.
obj_group
,
query
=
query
,
attributes
=
attributes
)
entries
=
self
.
get_entries
(
self
.
obj_group
,
query
=
query
,
attributes
=
attributes
)
return
[
Group
(
e
)
for
e
in
entries
]
class
Groups
(
list
):
...
...
@@ -134,7 +162,7 @@ class Groups(list):
return
tabulate
.
tabulate
(
table
,
tablefmt
=
FMT
,
headers
=
headers
)
#def sort(self, attr):
# self = sorted(self, key=lambda k: k[attr])
# self = sorted(self, key=lambda k: k[attr]
.__str__()
)
def
search
(
self
,
cn
):
"""
...
...
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