|
12 | 12 | from controller.user import manager as user_manager |
13 | 13 | from controller.organization import manager as organization_manager |
14 | 14 | from submodules.model import enums, exceptions |
15 | | -from submodules.model.business_objects import organization |
| 15 | +from submodules.model.business_objects import general, organization |
16 | 16 | from submodules.model.business_objects.user import check_email_in_full_admin |
17 | 17 | from submodules.model.models import Organization, Project, User |
18 | 18 | import sqlalchemy |
@@ -183,32 +183,44 @@ def invite_users( |
183 | 183 | team_ids: Optional[List[str]] = None, |
184 | 184 | ): |
185 | 185 | user_ids = [] |
| 186 | + recovery_links = [] |
| 187 | + organization = organization_manager.get_organization_by_name(organization_name) |
| 188 | + if organization is None: |
| 189 | + raise exceptions.EntityNotFoundException("Organization not found") |
186 | 190 | for email in emails: |
187 | 191 | # Create accounts for the email |
188 | 192 | user = kratos.create_user_kratos(email, provider) |
189 | 193 | if not user: |
190 | 194 | raise AuthManagerError("User creation failed") |
191 | 195 | user_ids.append(user["id"]) |
192 | | - # Assign the account to the organization |
193 | | - user_manager.update_organization_of_user(organization_name, email) |
| 196 | + user_database = user_manager.get_or_create_user(user["id"], with_commit=False) |
| 197 | + if not user_database: |
| 198 | + raise AuthManagerError("User creation in database failed") |
194 | 199 |
|
195 | | - # Assign the user role |
196 | | - user_manager.update_user_role(user["id"], user_role) |
| 200 | + user_database.language_display = language |
197 | 201 |
|
198 | | - # Add the preferred language |
199 | | - user_manager.update_user_field(user["id"], "language_display", language) |
| 202 | + try: |
| 203 | + role = enums.UserRoles[user_role.upper()].value |
| 204 | + except KeyError: |
| 205 | + raise ValueError(f"Invalid role: {role}") |
| 206 | + user_database.role = role |
| 207 | + user_database.organization_id = organization.id |
200 | 208 |
|
201 | 209 | # Add the user to the teams |
202 | 210 | if team_ids: |
203 | | - user_manager.add_user_to_teams(creation_user_id, user["id"], team_ids) |
| 211 | + user_manager.add_user_to_teams( |
| 212 | + creation_user_id, user["id"], team_ids, with_commit=False |
| 213 | + ) |
204 | 214 |
|
205 | 215 | # Get the recovery link for the email |
206 | 216 | recovery_link = kratos.get_recovery_link(user["id"]) |
207 | 217 | if not recovery_link: |
208 | 218 | raise AuthManagerError("Failed to get recovery link") |
209 | | - |
210 | | - # Send the recovery link to the email |
211 | | - kratos.email_with_link(email, recovery_link["recovery_link"]) |
| 219 | + recovery_links.append(recovery_link["recovery_link"]) |
| 220 | + general.commit() |
| 221 | + kratos.send_bulk_emails(emails, recovery_links) |
| 222 | + kratos.__refresh_identity_cache() |
| 223 | + organization_manager.sync_organization_sharepoint_integrations(organization.id) |
212 | 224 | return user_ids |
213 | 225 |
|
214 | 226 |
|
|
0 commit comments