-
Notifications
You must be signed in to change notification settings - Fork 19
/
admin_views.py
204 lines (186 loc) · 6.46 KB
/
admin_views.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from django import forms
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from apps.db_data.models import (
Officer,
Officership,
Politburo,
PolitburoMembership,
Semester,
)
from .utils import (
add_group_member,
get_all_groups,
get_group_members,
get_officers,
get_user_creation_time,
get_user_gecos,
remove_group_members,
user_exists,
)
class LdapGroupForm(forms.Form):
add_user = forms.CharField(label="User to add", max_length=32)
verify_user = forms.BooleanField(
label="Verify that username exists", initial=True, required=False
)
class LdapUserForm(forms.Form):
# WIP
poop = forms.CharField(label="Poop", max_length=32)
@staff_member_required
def admin(request, groupname=None):
if "user" in request.GET:
return HttpResponseRedirect(
reverse("ldap_admin_user", kwargs={"username": request.GET.get("user")})
)
groups = get_all_groups()
relevant_groups = [
"root",
"officers",
"prosp-officers",
"excomm",
"president",
"vptech",
"vpindrel",
"outreach",
"externalevents",
"internalevents",
"secretary",
]
return render(
request,
"ldap_admin.html",
{"groups": groups, "relevant_groups": relevant_groups},
)
@staff_member_required
def admin_group(request, groupname=None):
form = LdapGroupForm()
group_members = get_group_members(groupname)
if request.method == "POST":
if groupname == "root":
messages.error(request, "I'm sorry Dave, I'm afraid I can't do that.")
elif "add_user" in request.POST:
form = LdapGroupForm(request.POST)
if form.is_valid():
user_to_add = form.cleaned_data["add_user"]
if not form.cleaned_data["verify_user"] or user_exists(user_to_add):
success, message = add_group_member(groupname, user_to_add)
if success:
messages.info(
request,
"Successfully added {0} to {1}".format(
user_to_add, groupname
),
)
return HttpResponseRedirect(
reverse("ldap_admin_group", kwargs={"groupname": groupname})
)
else:
messages.error(request, message)
else:
messages.error(
request, "User {0} does not exist".format(user_to_add)
)
elif "do_delete" in request.POST:
users_to_remove = [
user[len("delete_") :]
for user in request.POST
if user.startswith("delete_")
]
if users_to_remove:
success, message = remove_group_members(groupname, users_to_remove)
if success:
for user in users_to_remove:
messages.info(
request,
"Successfully removed {0} from {1}".format(user, groupname),
)
return HttpResponseRedirect(
reverse("ldap_admin_group", kwargs={"groupname": groupname})
)
elif "do_verify_all" in request.POST:
invalid_usernames = []
for username in group_members:
if not user_exists(username):
invalid_usernames.append(username)
if invalid_usernames:
for invalid_username in invalid_usernames:
messages.error(
request, "{0} is not a valid username".format(invalid_username)
)
else:
messages.info(request, "All usernames are valid")
return HttpResponseRedirect(
reverse("ldap_admin_group", kwargs={"groupname": groupname})
)
return render(
request,
"ldap_admin_group.html",
{"form": form, "groupname": groupname, "members": group_members},
)
@staff_member_required
def admin_user(request, username=None):
if request.method == "POST":
form = LdapUserForm(request.POST)
if form.is_valid():
messages.info(request, form.cleaned_data["poop"])
return HttpResponseRedirect(
reverse("ldap_admin_user", kwargs={"username": username})
)
else:
form = LdapUserForm()
gecos = get_user_gecos(username)
creation_time = get_user_creation_time(username)
return render(
request,
"ldap_admin_user.html",
{
"form": form,
"username": username,
"gecos": gecos,
"creation_time": creation_time,
},
)
@staff_member_required
def admin_validate(request):
semester = Semester.objects.get(current=True)
roles_ldap = {}
roles_sql = {}
roles_ldap["excomm"] = get_group_members("excomm")
roles_sql["excomm"] = [
pbm.person.username
for pbm in PolitburoMembership.objects.filter(semester=semester)
]
for ldap_group, position in [
["president"] * 2,
["vpindrel", "indrel"],
["vptech", "vp"],
["secretary", "treasurer"],
["internalevents"] * 2,
["externalevents"] * 2,
["outreach"] * 2,
]:
roles_ldap[position] = get_group_members(ldap_group)
roles_sql[position] = list(
pbm.person.username
for pbm in PolitburoMembership.objects.filter(
politburo__position=position, semester=semester
)
)
roles_ldap["officers"] = get_officers()
roles_sql["officers"] = [
o.officer.username for o in Officership.objects.filter(semester=semester)
]
roles = {}
for role in roles_ldap.keys():
ldap = roles_ldap[role]
sql = roles_sql[role]
common = set(ldap) & set(sql)
sql_only = set(sql) - set(ldap)
ldap_only = set(ldap) - set(sql)
roles[role] = [
sorted(list(username_set)) for username_set in [common, sql_only, ldap_only]
]
return render(request, "ldap_admin_validate.html", {"roles": roles})