-
Notifications
You must be signed in to change notification settings - Fork 176
Split authentication hooks into appropriate files #1607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d84ff73
754c252
899729d
5a025a4
26df242
e251183
f212757
31ed548
4250408
ba5241d
ec042f3
bf05acf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { | ||
| ApplicationError, | ||
| ErrorCode, | ||
| isNil, | ||
| PrincipalType, | ||
| Project, | ||
| ProjectMemberRole, | ||
| User, | ||
| } from '@openops/shared'; | ||
| import { organizationService } from '../../organization/organization.service'; | ||
| import { projectService } from '../../project/project-service'; | ||
| import { userService } from '../../user/user-service'; | ||
| import { accessTokenManager } from './access-token-manager'; | ||
|
|
||
| export async function getProjectAndToken( | ||
| user: User, | ||
| tablesRefreshToken: string, | ||
| ): Promise<{ | ||
| user: User; | ||
| project: Project; | ||
| token: string; | ||
| tablesRefreshToken: string; | ||
| projectRole: ProjectMemberRole; | ||
| }> { | ||
| const updatedUser = await userService.getOneOrFail({ id: user.id }); | ||
|
|
||
| const project = await projectService.getOneForUser(updatedUser); | ||
| if (isNil(project)) { | ||
| throw new ApplicationError({ | ||
| code: ErrorCode.INVITATION_ONLY_SIGN_UP, | ||
| params: { | ||
| message: 'No project found for user', | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| const organization = await organizationService.getOneOrThrow( | ||
| project.organizationId, | ||
| ); | ||
|
|
||
| const token = await accessTokenManager.generateToken({ | ||
| id: user.id, | ||
| type: PrincipalType.USER, | ||
| projectId: project.id, | ||
| organization: { | ||
| id: organization.id, | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| user: updatedUser, | ||
| token, | ||
| project, | ||
| tablesRefreshToken, | ||
| projectRole: ProjectMemberRole.ADMIN, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| import { cryptoUtils } from '@openops/server-shared'; | ||
| import { | ||
| ApplicationError, | ||
| assertValidEmail, | ||
| assertValidPassword, | ||
| ErrorCode, | ||
| isEmpty, | ||
| OrganizationRole, | ||
| Provider, | ||
| User, | ||
| UserStatus, | ||
| } from '@openops/shared'; | ||
| import { QueryFailedError } from 'typeorm'; | ||
| import { openopsTables } from '../../openops-tables'; | ||
| import { userService } from '../../user/user-service'; | ||
| import { authenticationServiceHooks as hooks } from '../authentication-service/hooks'; | ||
| import { Provider } from '../authentication-service/hooks/authentication-service-hooks'; | ||
|
|
||
| type NewUserParams = { | ||
| email: string; | ||
|
|
@@ -37,11 +39,18 @@ const assertValidSignUpParams = async ({ | |
| email: string; | ||
| password: string; | ||
| }): Promise<void> => { | ||
| await hooks.get().preSignUp({ | ||
| name, | ||
| email, | ||
| password, | ||
| }); | ||
| assertValidEmail(email); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved from community-authentication-hooks.ts |
||
| assertValidPassword(password); | ||
|
|
||
| if (isEmpty(name)) { | ||
| throw new ApplicationError({ | ||
| code: ErrorCode.INVALID_NAME_FOR_USER, | ||
| params: { | ||
| name, | ||
| message: 'First name and last name were not provided correctly.', | ||
| }, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const createEditorUser = async ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { authenticateDefaultUserInOpenOpsTables } from '@openops/common'; | ||
| import { AppSystemProp, system } from '@openops/server-shared'; | ||
| import { ApplicationError, ErrorCode, isNil, User } from '@openops/shared'; | ||
| import { openopsTables } from '../../openops-tables'; | ||
| import { organizationService } from '../../organization/organization.service'; | ||
| import { userService } from '../../user/user-service'; | ||
|
|
||
| export async function assignDefaultOrganization(user: User): Promise<void> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved from community-authentication-hooks.ts |
||
| let organization = await organizationService.getOldestOrganization(); | ||
|
|
||
| const adminUser = await userService.getUserByEmailOrFail({ | ||
| email: system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL), | ||
| }); | ||
|
|
||
| organization = !isNil(adminUser.organizationId) | ||
| ? await organizationService.getOne(adminUser.organizationId) | ||
| : organization; | ||
|
|
||
| if (!organization) { | ||
| throw new ApplicationError({ | ||
| code: ErrorCode.ENTITY_NOT_FOUND, | ||
| params: { | ||
| message: 'Admin organization not found', | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| await userService.addUserToOrganization({ | ||
| id: user.id, | ||
| organizationId: organization.id, | ||
| }); | ||
|
|
||
| await addUserToDefaultWorkspace({ | ||
| email: user.email, | ||
| workspaceId: organization.tablesWorkspaceId, | ||
| }); | ||
| } | ||
|
|
||
| async function addUserToDefaultWorkspace(values: { | ||
| email: string; | ||
| workspaceId: number; | ||
| }): Promise<void> { | ||
| const { token: defaultToken } = | ||
| await authenticateDefaultUserInOpenOpsTables(); | ||
|
|
||
| await openopsTables.addUserToWorkspace(defaultToken, { | ||
| ...values, | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved from community-authentication-hooks.ts