|
1 | 1 | from datetime import datetime, timedelta |
2 | 2 | from uuid import uuid4 |
3 | 3 |
|
4 | | -from flask import current_app, make_response, redirect, request |
| 4 | +from flask import current_app, make_response, redirect, request, abort |
5 | 5 | from flask_jwt_extended import create_access_token, get_jwt_identity, jwt_required |
6 | 6 | from onelogin.saml2.auth import OneLogin_Saml2_Auth |
7 | 7 |
|
8 | 8 | from labconnect import db |
9 | 9 | 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 | +) |
11 | 17 |
|
12 | 18 | from . import main_blueprint |
13 | 19 |
|
@@ -95,25 +101,59 @@ def saml_callback(): |
95 | 101 |
|
96 | 102 |
|
97 | 103 | @main_blueprint.post("/register") |
98 | | -@jwt_required() |
99 | 104 | def registerUser(): |
100 | 105 |
|
101 | | - user_id = get_jwt_identity() |
102 | | - |
103 | 106 | # Gather the new user's information |
104 | 107 | json_data = request.get_json() |
| 108 | + if not json_data: |
| 109 | + abort(400) |
| 110 | + |
105 | 111 | user = User( |
106 | | - email=user_id, |
| 112 | + email=json_data.get("email"), |
107 | 113 | first_name=json_data.get("first_name"), |
108 | 114 | 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", ""), |
114 | 122 | ) |
115 | 123 | db.session.add(user) |
116 | 124 | 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() |
117 | 157 | return {"msg": "New user added"} |
118 | 158 |
|
119 | 159 |
|
|
0 commit comments