Skip to content
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

feat: introduce user domain #287

Merged
merged 30 commits into from
Feb 16, 2024
Merged

feat: introduce user domain #287

merged 30 commits into from
Feb 16, 2024

Conversation

timonmasberg
Copy link
Member

@timonmasberg timonmasberg commented Jul 21, 2023

Description

Introduces a user domain with a dev in memory user store for local and preview environments, and a service that communicates with the MS Graph API to manage users on our AADB2C tenant. Furthermore, introduces a role hierarchy and a utility decorator to check the minimum required role in the request pipeline.
As a feature, if a user is logged in and gets deactivated, he will be logged out automatically.

Checklist:

  • The title of this PR and the commit history is conform with
    the Conventional Commits specification.

  • I have performed a self-review of my own code.

  • My changes generate no new warnings, SonarCloud reports no Vulnerabilities, Bugs or Code Smells.

  • I have added tests (unit and E2E if user-facing) that prove my fix is effective or that my feature works,
    Coverage > 80% and not less than the current coverage of the main branch.

  • The PR branch is up-to-date with the base branch. In case you merged main into your feature branch, make sure you have run the latest NX migrations (nx migrate --run-migrations).

  • I have included the reset.d.ts in the tsconfig.lib.json

  • I have included the reset.d.ts in the tsconfig.lib.json

  • I have extended the .eslintrc.json with .eslintrc.angular.json

@timonmasberg timonmasberg self-assigned this Jul 21, 2023
@timonmasberg timonmasberg changed the title feat: introduce users feat: introduce user domain Jul 21, 2023
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

89.6% 89.6% Coverage
0.0% 0.0% Duplication

@timonmasberg timonmasberg requested a review from JSPRH July 21, 2023 11:02
# Conflicts:
#	.github/actions/build-and-deploy-api/action.yml
#	.github/workflows/next-deployment.yml
#	apps/api/src/.env.template
#	apps/spa/src/app/app.module.ts
#	apps/spa/src/environments/environment.ts
#	libs/spa/auth/src/lib/components/dev-login.component.ts
#	package-lock.json
#	package.json
# Conflicts:
#	.github/actions/build-and-deploy-api/action.yml
#	.github/workflows/next-deployment.yml
#	apps/api/src/.env.template
#	apps/spa/src/app/app.module.ts
#	apps/spa/src/environments/environment.ts
#	libs/spa/auth/src/lib/components/dev-login.component.ts
#	package-lock.json
#	package.json
# Conflicts:
#	.github/actions/build-and-deploy-api/action.yml
#	.github/workflows/next-deployment.yml
#	apps/api/src/.env.template
#	apps/spa/src/app/app.module.ts
#	apps/spa/src/environments/environment.ts
#	libs/spa/auth/src/lib/components/dev-login.component.ts
#	package-lock.json
#	package.json
@timonmasberg timonmasberg force-pushed the main branch 2 times, most recently from 9b697ff to 99a715d Compare January 23, 2024 13:07
Comment on lines 23 to 24
const IS_NEXT_DEPLOYMENT = process.env.ENVIRONMENT_NAME === 'next';
const IS_PROD_DEPLOYMENT = process.env.ENVIRONMENT_NAME === 'prod';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we introduce a flag e.g., ENABLE_DEV_LOGIN and only if this is set to true we enable dev login, otherwise it is Azure Entra ID? That way we can control the use of Entra ID easier e.g. for local testing as well and use the safer method (Entra ID) by default.

@@ -0,0 +1,11 @@
import { PresentableException } from '@kordis/api/shared';

export class PresentableInsufficientPermissionException extends PresentableException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this defacto the same as the PresentableUnauthorizedException?

apps/spa/src/app/app.module.ts Show resolved Hide resolved
Comment on lines 4 to 7

export const testUsernames = [...TEST_USERS.map((u) => u.userName)] as const;
export type TestUsernames = (typeof testUsernames)[number];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify. Accept suggestion from libs/shared/test-helpers/src/lib/test-users.test-helper.ts to enable code completion for usernames.

Suggested change
export const testUsernames = [...TEST_USERS.map((u) => u.userName)] as const;
export type TestUsernames = (typeof testUsernames)[number];
export type TestUsernames = (typeof TEST_USERS)[number]['userName'];

import { UserDeactivated } from './object-type/user-deactivated.object-type';

@Resolver(User)
export class UserResolver {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put the assertOrgMembership to a whole resolver as a Decorator instead of ensuring that within every query/mutation?

} else if (key === 'role') {
updatePayload[`extension_${this.extensionAppId}_Role`] = value;
} else if (key === 'organizationId') {
updatePayload[`extension_${this.extensionAppId}_OrganizationId`] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never want to update the user's organization id, right? So if the id is not required by Azure, we should not set it here, if it is required, we have to require it in the method's signature as well.

await this.updateUserRequest(userId, updatePayload);
}

async changeEmail(userId: string, email: string): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about e-mail verification. Does Azure AD take responsibility for that?

*/
@Injectable()
export class DevUserService extends BaseUserService {
private readonly users: User[] = [...TEST_USERS];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move the type declaration for the users into the test-helpers file (probably good idea anyway to ensure correct types) and do a deep copy here, the type should be inferred automatically

import { Role } from '@kordis/shared/auth';

// keep in sync with the apps/api/dev-tokens.md file!
export const TEST_USERS = Object.freeze([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use as const instead of Object.freeze the values will be assumed as literals by TypeScript and enable a beautiful code completion.
Also, we should do TEST_UERS: User[] to ensure these users are all legit (especially when we change the User object).

}

async updateUser(
userId: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
userId: string,
private async updateUser(

Comment on lines 75 to 78
} else if (key === 'organizationId') {
updatePayload[`extension_${this.extensionAppId}_OrganizationId`] =
value;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (key === 'organizationId') {
updatePayload[`extension_${this.extensionAppId}_OrganizationId`] =
value;
} else {
} else {

@timonmasberg timonmasberg enabled auto-merge (squash) February 16, 2024 10:23
Copy link

@timonmasberg timonmasberg merged commit 88966bd into main Feb 16, 2024
5 checks passed
@timonmasberg timonmasberg deleted the feat/users branch February 16, 2024 10:29
@timonmasberg
Copy link
Member Author

🎉 This PR is included in version 1.2.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants