-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path__init__.py
More file actions
254 lines (210 loc) · 7.1 KB
/
Copy path__init__.py
File metadata and controls
254 lines (210 loc) · 7.1 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os
import csh_ldap
import ldap
import sentry_sdk
from werkzeug.exceptions import NotFound
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from flask_pyoidc.provider_configuration import ProviderConfiguration, ClientMetadata
from flask_sqlalchemy import SQLAlchemy
from flask_uploads import IMAGES, UploadSet, configure_uploads
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sqlalchemy.exc import SQLAlchemyError
app = Flask(__name__)
# Get app config from absolute file path
if os.path.exists(os.path.join(os.getcwd(), "config.py")):
app.config.from_pyfile(os.path.join(os.getcwd(), "config.py"))
else:
app.config.from_pyfile(os.path.join(os.getcwd(), "config.env.py"))
auth = OIDCAuthentication(
{
"default": ProviderConfiguration(
issuer=app.config["OIDC_ISSUER"],
client_metadata=ClientMetadata(
app.config["OIDC_CLIENT_CONFIG"]["client_id"],
app.config["OIDC_CLIENT_CONFIG"]["client_secret"],
),
)
},
app,
)
# Sentry
# pylint: disable=abstract-class-instantiated
sentry_sdk.init(
dsn=app.config["SENTRY_DSN"],
integrations=[FlaskIntegration(), SqlalchemyIntegration()],
)
# LDAP
_ldap = csh_ldap.CSHLDAP(app.config["LDAP_BIND_DN"], app.config["LDAP_BIND_PASS"])
photos = UploadSet("photos", IMAGES)
app.config["UPLOADED_PHOTOS_DEST"] = "static/img"
configure_uploads(app, photos)
# Import ldap model after instantiating object
# pylint: disable=wrong-import-position
from profiles.ldap import (
BadQueryError,
_ldap_get_group_members,
get_gravatar,
get_image,
ldap_get_active_members,
ldap_get_all_members,
ldap_get_current_students,
ldap_get_eboard,
ldap_get_group_desc,
ldap_get_groups,
ldap_get_intro_members,
ldap_get_member,
ldap_get_onfloor_members,
ldap_get_year,
ldap_is_active,
ldap_is_rtp,
ldap_search_members,
ldap_update_profile,
proxy_image,
)
from profiles.utils import before_request, get_member_info, process_image
# pylint: enable=wrong-import-position
@app.route("/", methods=["GET"])
@auth.oidc_auth("default")
@before_request
def home(info=None):
print(session["userinfo"], '\n', info)
return redirect("/user/" + info["uid"], code=302)
@app.route("/user/<uid>", methods=["GET"])
@auth.oidc_auth("default")
@before_request
def user(uid=None, info=None):
try:
return render_template(
"profile.html", info=info,
member_info=get_member_info(uid), **app.config['DATADOG_RUM_CONFIG']
)
except BadQueryError as bqe:
# ldap_get_member() returns a BadQueryError if getting the user's information fails.
# Flask already treats a stray BadQueryError as a 404, but actually handling it prevents the traceback
# from getting dumped into the log.
return render_template("404.html", message=bqe, **app.config['DATADOG_RUM_CONFIG']), 404
@app.route("/results", methods=["POST"])
@auth.oidc_auth("default")
@before_request
def results():
searched = request.form["query"]
return redirect(f"/search/{searched}", 302)
@app.route("/search", methods=["GET"])
@auth.oidc_auth("default")
@before_request
def search(searched=None, info=None):
# return jsonify(ldap_search_members(searched))
searched = request.args.get("q").strip()
members = ldap_search_members(searched)
if len(members) == 1:
return redirect("/user/" + members[0].uid, 302)
return render_template(
"listing.html", info=info, title="Search Results: " + searched,
members=members, **app.config['DATADOG_RUM_CONFIG']
)
@app.route("/group/<_group>", methods=["GET"])
@auth.oidc_auth("default")
@before_request
def group(_group=None, info=None):
group_desc = ldap_get_group_desc(_group)
if _group == "eboard":
return render_template(
"listing.html", info=info, title=group_desc,
members=ldap_get_eboard(), **app.config['DATADOG_RUM_CONFIG']
)
return render_template(
"listing.html",
info=info,
title=group_desc,
members=_ldap_get_group_members(_group),
**app.config['DATADOG_RUM_CONFIG']
)
@app.route("/year/<_year>", methods=["GET"])
@auth.oidc_auth("default")
@before_request
def year(_year=None, info=None):
print(_year)
return render_template(
"listing.html",
info=info,
title="Year: " + _year,
members=ldap_get_year(_year),
**app.config['DATADOG_RUM_CONFIG']
)
@app.route("/update", methods=["POST"])
@auth.oidc_auth("default")
@before_request
def update(info=None):
if "photo" in request.form:
process_image(request.form["photo"][22:], info["uid"])
get_image.cache_clear()
ldap_update_profile(request.json, info["uid"])
return jsonify({"success": True}), 200
@app.route("/upload", methods=["POST"])
@auth.oidc_auth("default")
@before_request
def upload(info=None):
if "photo" in request.form:
process_image(request.form["photo"][22:], info["uid"])
get_image.cache_clear()
return redirect("/", 302)
@app.route("/logout")
@auth.oidc_logout
def logout():
return redirect("/", 302)
@app.route("/image/<uid>", methods=["GET"])
def image(uid):
try:
return get_image(uid)
except BadQueryError as bqe:
return render_template("404.html", message=bqe, **app.config['DATADOG_RUM_CONFIG']), 404
@app.route("/clearcache")
@auth.oidc_auth("default")
@before_request
def clear_cache(info=None):
if not ldap_is_rtp(info["user_obj"]):
return redirect("/")
ldap_get_active_members.cache_clear()
ldap_get_intro_members.cache_clear()
ldap_get_onfloor_members.cache_clear()
ldap_get_current_students.cache_clear()
ldap_get_all_members.cache_clear()
ldap_get_groups.cache_clear()
ldap_get_group_desc.cache_clear()
ldap_get_eboard.cache_clear()
ldap_search_members.cache_clear()
ldap_get_year.cache_clear()
get_image.cache_clear()
get_gravatar.cache_clear()
proxy_image.cache_clear()
flash("Cache cleared!")
return redirect(request.referrer, 302)
@app.errorhandler(404)
@app.errorhandler(500)
def handle_internal_error(e):
if isinstance(e, NotFound):
return render_template("404.html", message=str(e), **app.config['DATADOG_RUM_CONFIG']), 404
if isinstance(e.original_exception, BadQueryError):
return render_template("404.html", message=e.original_exception, **app.config['DATADOG_RUM_CONFIG']), 404
raise e.original_exception
@app.route("/api/v1/healthcheck", methods=["GET"])
def health_check():
# Return codes
# 200 - Success
# 512 - LDAP failure
return_code = 200
jsonout = {
"ldap": {
"status": True,
"server": None,
}
}
try:
jsonout["ldap"]["server"] = _ldap.server_uri
ldap_get_active_members()
except ldap.LDAPError:
jsonout["ldap"]["status"] = False
return_code = 512
return jsonify(jsonout), return_code