Skip to content

Commit f8d12ed

Browse files
committed
Updated register route
1 parent a900f5f commit f8d12ed

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

labconnect/main/auth_routes.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from datetime import datetime, timedelta
22
from uuid import uuid4
33

4-
from flask import current_app, make_response, redirect, request
4+
from flask import current_app, make_response, redirect, request, abort
55
from flask_jwt_extended import create_access_token, get_jwt_identity, jwt_required
66
from onelogin.saml2.auth import OneLogin_Saml2_Auth
77

88
from labconnect import db
99
from labconnect.helpers import prepare_flask_request
10-
from labconnect.models import User
10+
from labconnect.models import (
11+
User,
12+
UserCourses,
13+
UserDepartments,
14+
UserMajors,
15+
ManagementPermissions,
16+
)
1117

1218
from . import main_blueprint
1319

@@ -95,25 +101,59 @@ def saml_callback():
95101

96102

97103
@main_blueprint.post("/register")
98-
@jwt_required()
99104
def registerUser():
100105

101-
user_id = get_jwt_identity()
102-
103106
# Gather the new user's information
104107
json_data = request.get_json()
108+
if not json_data:
109+
abort(400)
110+
105111
user = User(
106-
email=user_id,
112+
email=json_data.get("email"),
107113
first_name=json_data.get("first_name"),
108114
last_name=json_data.get("last_name"),
109-
preferred_name=json_data.get("preferred_name"),
110-
class_year=json_data.get("class_year"),
111-
profile_picture=json_data.get("profile_pictures"),
112-
website=json_data.get("website"),
113-
description=json_data.get("description"),
115+
preferred_name=json_data.get("preferred_name", ""),
116+
class_year=json_data.get("class_year", ""),
117+
profile_picture=json_data.get(
118+
"profile_picture", "https://www.svgrepo.com/show/206842/professor.svg"
119+
),
120+
website=json_data.get("website", ""),
121+
description=json_data.get("description", ""),
114122
)
115123
db.session.add(user)
116124
db.session.commit()
125+
126+
# Add UserDepartments if provided
127+
if json_data.get("departments"):
128+
for department_id in json_data["departments"]:
129+
user_department = UserDepartments(
130+
user_id=user.id, department_id=department_id
131+
)
132+
db.session.add(user_department)
133+
134+
# Additional auxiliary records (majors, courses, etc.)
135+
if json_data.get("majors"):
136+
for major_id in json_data["majors"]:
137+
user_major = UserMajors(user_id=user.id, major_id=major_id)
138+
db.session.add(user_major)
139+
# Add Courses if provided
140+
if json_data.get("courses"):
141+
for course_id in json_data["courses"]:
142+
user_course = UserCourses(user_id=user.id, course_id=course_id)
143+
db.session.add(user_course)
144+
145+
# Add ManagementPermissions if provided
146+
if json_data.get("permissions"):
147+
permissions = json_data["permissions"]
148+
management_permissions = ManagementPermissions(
149+
user_id=user.id,
150+
super_admin=permissions.get("super_admin", False),
151+
admin=permissions.get("admin", False),
152+
moderator=permissions.get("moderator", False),
153+
)
154+
db.session.add(management_permissions)
155+
156+
db.session.commit()
117157
return {"msg": "New user added"}
118158

119159

0 commit comments

Comments
 (0)