-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04-ActiveDirectory.py
More file actions
63 lines (45 loc) · 1.28 KB
/
Copy path04-ActiveDirectory.py
File metadata and controls
63 lines (45 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Group(object):
def __init__(self, _name):
self.name = _name
self.groups = []
self.users = []
def add_group(self, group):
self.groups.append(group)
def add_user(self, user):
self.users.append(user)
def get_groups(self):
return self.groups
def get_users(self):
return self.users
def get_name(self):
return self.name
def is_user_in_group(user, group):
"""
Return True if user is in the group, False otherwise.
Args:
user(str): user name/id
group(class:Group): group to check user membership against
"""
# base case: the user is in the group
if user in group.get_users():
return True
else:
for group in group.get_groups():
return(is_user_in_group(user, group))
# The user is not the in the group
return False
# Tests
grandparent = Group("grandparent")
parent = Group("parent")
child = Group("child")
sub_child = Group("subchild")
sub_child_user = "sub_child_user"
sub_child.add_user(sub_child_user)
child.add_group(sub_child)
parent.add_group(child)
print(is_user_in_group(sub_child_user, parent))
# True
print(is_user_in_group(sub_child_user, grandparent))
# False
print(is_user_in_group(sub_child_user, child))
# True