-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_4.py
70 lines (49 loc) · 1.48 KB
/
problem_4.py
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
64
65
66
67
68
69
70
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_name, 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
"""
if user_name in group.get_users():
return True
if len(group.get_groups()) == 0:
return False
else:
for group_name in group.get_groups():
result = is_user_in_group(user_name, group_name)
return result
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',sub_child))
# True
print(is_user_in_group(user_name='child_user', group=parent))
# False
print(is_user_in_group(user_name='sub_child_user', group=parent))
# True
print(is_user_in_group(user_name='parent_user', group=parent))
# False
print(is_user_in_group(user_name='', group=parent))
# False
print(is_user_in_group(user_name='', group=child))
# False