diff --git a/service/src/main/inse/auth/model.py b/service/src/main/inse/auth/model.py index 06596ff4..3b8d5d97 100644 --- a/service/src/main/inse/auth/model.py +++ b/service/src/main/inse/auth/model.py @@ -23,7 +23,9 @@ ROLE_PRIORITY = { } -def normalize_roles(roles: List[UserRole] | List[str] | UserRole | str | None) -> List[UserRole]: +def normalize_roles( + roles: List[UserRole] | List[str] | UserRole | str | None, +) -> List[UserRole]: if roles is None: return [UserRole.USER] if isinstance(roles, (str, UserRole)): @@ -40,7 +42,9 @@ def normalize_roles(roles: List[UserRole] | List[str] | UserRole | str | None) - return sorted(normalized or [UserRole.USER], key=lambda role: ROLE_PRIORITY[role]) -def effective_role_for_roles(roles: List[UserRole] | List[str] | UserRole | str | None) -> UserRole: +def effective_role_for_roles( + roles: List[UserRole] | List[str] | UserRole | str | None, +) -> UserRole: return normalize_roles(roles)[0] diff --git a/service/src/main/inse/auth/service.py b/service/src/main/inse/auth/service.py index 7fcf10ac..24f6fce0 100644 --- a/service/src/main/inse/auth/service.py +++ b/service/src/main/inse/auth/service.py @@ -113,7 +113,9 @@ def create_predefined_accounts(session: Session, file_path: str): session.add(organization) for account_data in data.get("accounts", []): organization_id = uuid.UUID(account_data["organization_id"]) - roles = normalize_roles(account_data.get("roles", account_data.get("role"))) + roles = normalize_roles( + account_data.get("roles", account_data.get("role")) + ) email = account_data["email"] password_hash = _hash_password(account_data["password"]) account = Account( diff --git a/webapp/src/app/app.routes.ts b/webapp/src/app/app.routes.ts index 34caa730..4ac3ff1f 100644 --- a/webapp/src/app/app.routes.ts +++ b/webapp/src/app/app.routes.ts @@ -1,6 +1,4 @@ import { Routes } from '@angular/router'; -import { userGuard } from './shared/guards/user.guard'; -import { superAdminGuard } from './shared/guards/super-admin.guard'; import { guestOnlyGuard } from './shared/guards/guest-only.guard'; import { collaborationNetworkResolver } from './resolvers/collaboration-network.resolver'; import { personListResolver } from './resolvers/person-list.resolver'; @@ -21,7 +19,6 @@ import { fundingProjectPageResolver } from './resolvers/funding-project-page.res import { euSingleProjectResolver } from './resolvers/eu-single-project-resolver'; import { publicationResolver } from './resolvers/publication-resolver'; import { accountResolver } from './resolvers/account.resolver'; -import { accountViewerGuard } from './shared/guards/account-viewer.guard'; import { accountsPageResolver } from './resolvers/accounts-page.resolver'; import { accountsOrganizationResolver } from './resolvers/accounts-organizations.resolver'; import { organizationsOrganizationsResolver } from './resolvers/organizations-organizations.resolver'; @@ -35,6 +32,8 @@ import { authorWorkPurposesResolver } from './resolvers/author-work-purposes.res import { workPageResolver } from './resolvers/work-page.resolver'; import { selectedTopicsResolver } from './resolvers/selected-topics.resolver'; import { selectedInstitutionsResolver } from './resolvers/selected-institutions.resolver'; +import { roleGuard } from './shared/guards/role.guard'; +import { UserRole } from './api'; export const routes: Routes = [ { path: '', @@ -66,7 +65,14 @@ export const routes: Routes = [ }, { path: 'dashboard', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/dashboard/dashboard.component').then( (mod) => mod.DashboardComponent, @@ -74,7 +80,14 @@ export const routes: Routes = [ }, { path: 'bookmarks', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/bookmarks/bookmarks.component').then( (mod) => mod.BookmarksComponent, @@ -82,7 +95,14 @@ export const routes: Routes = [ }, { path: 'persons', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/person-list/person-list.component').then( (mod) => mod.PersonListComponent, @@ -115,7 +135,14 @@ export const routes: Routes = [ }, { path: 'keywords', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/keywords-search/keywords-search.component').then( (mod) => mod.KeywordsSearchComponent, @@ -139,13 +166,27 @@ export const routes: Routes = [ }, { path: 'news', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/news/news.component').then((mod) => mod.NewsComponent), }, { path: 'account', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/account/account.component').then( (mod) => mod.AccountComponent, @@ -156,7 +197,9 @@ export const routes: Routes = [ }, { path: 'accounts', - canActivate: [accountViewerGuard], + canActivate: [ + roleGuard([UserRole.Manager, UserRole.Admin, UserRole.SuperAdmin]), + ], loadComponent: () => import('./modules/accounts/accounts.component').then( (mod) => mod.AccountsComponent, @@ -169,7 +212,7 @@ export const routes: Routes = [ }, { path: 'organizations', - canActivate: [superAdminGuard], + canActivate: [roleGuard([UserRole.SuperAdmin])], loadComponent: () => import('./modules/organizations/organizations.component').then( (mod) => mod.OrganizationsComponent, @@ -182,7 +225,14 @@ export const routes: Routes = [ { path: 'news/:id', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/news-detail/news-detail.component').then( (mod) => mod.NewsDetailComponent, @@ -190,7 +240,14 @@ export const routes: Routes = [ }, { path: 'publication/:publicationId', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/publication-detail/publication-detail.component').then( (mod) => mod.PublicationDetailComponent, @@ -201,7 +258,14 @@ export const routes: Routes = [ }, { path: 'patent/:patentId', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/patent-detail/patent-detail.component').then( (mod) => mod.PatentDetailComponent, @@ -209,7 +273,14 @@ export const routes: Routes = [ }, { path: 'projects/dfg/:id', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], pathMatch: 'full', loadComponent: () => import('./modules/projects/dfg-details/dfg-details.component').then( @@ -219,7 +290,14 @@ export const routes: Routes = [ }, { path: 'projects/bund/:id', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], pathMatch: 'full', loadComponent: () => import('./modules/projects/bund-details/bund-details.component').then( @@ -229,7 +307,14 @@ export const routes: Routes = [ }, { path: 'projects/eu/:id', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], pathMatch: 'full', loadComponent: () => import('./modules/projects/eu-cordis-details/eu-details.component').then( @@ -241,7 +326,14 @@ export const routes: Routes = [ }, { path: 'person/:id', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/person/person.component').then( (mod) => mod.PersonComponent, @@ -358,7 +450,14 @@ export const routes: Routes = [ }, { path: 'institutions', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/institutions/institutions.component').then( (mod) => mod.InstitutionsComponent, @@ -373,7 +472,14 @@ export const routes: Routes = [ }, { path: 'institution/:institutionId', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/institution/institution.component').then( (mod) => mod.InstitutionComponent, @@ -425,7 +531,14 @@ export const routes: Routes = [ }, { path: 'projects', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/projects/projects.component').then( (mod) => mod.ProjectsComponent, @@ -447,7 +560,14 @@ export const routes: Routes = [ }, { path: 'patents', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/patents/patents.component').then( (mod) => mod.PatentsComponent, @@ -455,13 +575,27 @@ export const routes: Routes = [ }, { path: 'news', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/news/news.component').then((mod) => mod.NewsComponent), }, { path: 'news/:id', - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], loadComponent: () => import('./modules/news-detail/news-detail.component').then( (mod) => mod.NewsDetailComponent, @@ -486,6 +620,13 @@ export const routes: Routes = [ routeMessage: 'Die gesuchte Seite existiert leider nicht oder wurde verschoben.', }, - canActivate: [userGuard], + canActivate: [ + roleGuard([ + UserRole.User, + UserRole.Manager, + UserRole.Admin, + UserRole.SuperAdmin, + ]), + ], }, ]; diff --git a/webapp/src/app/shared/guards/account-viewer.guard.ts b/webapp/src/app/shared/guards/account-viewer.guard.ts deleted file mode 100644 index 9eeac48d..00000000 --- a/webapp/src/app/shared/guards/account-viewer.guard.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { effect, inject, Injector } from '@angular/core'; -import { CanActivateFn, Router } from '@angular/router'; -import { CredentialService } from '../services/credential.service'; - -export const accountViewerGuard: CanActivateFn = () => { - const credentialService = inject(CredentialService); - const router = inject(Router); - const injector = inject(Injector); - - return new Promise((resolve) => { - const ref = effect( - () => { - if (credentialService.isValidating()) { - return; - } - - ref.destroy(); - - if (!credentialService.isAuthenticated()) { - void router.navigate(['/login']); - resolve(false); - return; - } - - const mayViewAccounts = - credentialService.isEffectiveSuperAdmin() || - credentialService.isEffectiveAdmin() || - credentialService.isEffectiveManager(); - - if (!mayViewAccounts) { - void router.navigate(['/dashboard']); - resolve(false); - return; - } - - resolve(true); - }, - { injector }, - ); - }); -}; diff --git a/webapp/src/app/shared/guards/admin.guard.ts b/webapp/src/app/shared/guards/admin.guard.ts deleted file mode 100644 index a8876ced..00000000 --- a/webapp/src/app/shared/guards/admin.guard.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { effect, inject, Injector } from '@angular/core'; -import { CanActivateFn, Router } from '@angular/router'; -import { CredentialService } from '../services/credential.service'; - -export const adminGuard: CanActivateFn = () => { - const credentialService = inject(CredentialService); - const router = inject(Router); - const injector = inject(Injector); - - return new Promise((resolve) => { - const ref = effect( - () => { - if (credentialService.isValidating()) { - return; - } - - ref.destroy(); - - if (!credentialService.isAuthenticated()) { - void router.navigate(['/login']); - resolve(false); - return; - } - - if (!credentialService.isEffectiveAdmin()) { - void router.navigate(['/dashboard']); - resolve(false); - return; - } - - resolve(true); - }, - { injector }, - ); - }); -}; diff --git a/webapp/src/app/shared/guards/role.guard.ts b/webapp/src/app/shared/guards/role.guard.ts new file mode 100644 index 00000000..c29c0b98 --- /dev/null +++ b/webapp/src/app/shared/guards/role.guard.ts @@ -0,0 +1,25 @@ +import { CanActivateFn, RedirectCommand, Router } from '@angular/router'; +import { inject } from '@angular/core'; +import { CredentialService } from '../services/credential.service'; +import { UserRole } from '../../api'; + +export function roleGuard(roles: UserRole[]): CanActivateFn { + return (route, state) => { + const credentialService = inject(CredentialService); + const router = inject(Router); + + if (!credentialService.isAuthenticated()) { + return new RedirectCommand(router.parseUrl('/login')); + } + + const accessTokenMetadata = credentialService.accessToken(); + const effectiveRoles = accessTokenMetadata?.effective_roles ?? []; + + for (const effectiveRole of roles) { + if (effectiveRoles.includes(effectiveRole)) { + return true; + } + } + return new RedirectCommand(router.parseUrl('/dashboard')); + }; +} diff --git a/webapp/src/app/shared/guards/super-admin.guard.ts b/webapp/src/app/shared/guards/super-admin.guard.ts deleted file mode 100644 index 993f00a5..00000000 --- a/webapp/src/app/shared/guards/super-admin.guard.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { effect, inject, Injector } from '@angular/core'; -import { CanActivateFn, Router } from '@angular/router'; -import { CredentialService } from '../services/credential.service'; - -export const superAdminGuard: CanActivateFn = () => { - const credentialService = inject(CredentialService); - const router = inject(Router); - const injector = inject(Injector); - - return new Promise((resolve) => { - const ref = effect( - () => { - if (credentialService.isValidating()) { - return; - } - - ref.destroy(); - - if (!credentialService.isAuthenticated()) { - void router.navigate(['/login']); - resolve(false); - return; - } - - if (!credentialService.isEffectiveSuperAdmin()) { - void router.navigate(['/dashboard']); - resolve(false); - return; - } - - resolve(true); - }, - { injector }, - ); - }); -}; diff --git a/webapp/src/app/shared/guards/user.guard.ts b/webapp/src/app/shared/guards/user.guard.ts deleted file mode 100644 index 7145e5ea..00000000 --- a/webapp/src/app/shared/guards/user.guard.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { effect, inject, Injector } from '@angular/core'; -import { CanActivateFn, Router } from '@angular/router'; -import { CredentialService } from '../services/credential.service'; - -export const userGuard: CanActivateFn = () => { - const credentialService = inject(CredentialService); - const router = inject(Router); - const injector = inject(Injector); - - return new Promise((resolve) => { - const ref = effect( - () => { - if (credentialService.isValidating()) { - return; - } - - ref.destroy(); - - if (!credentialService.isAuthenticated()) { - void router.navigate(['/login']); - resolve(false); - return; - } - - resolve(true); - }, - { injector }, - ); - }); -}; diff --git a/webapp/src/app/shared/services/credential.service.ts b/webapp/src/app/shared/services/credential.service.ts index 3a376467..6b81fc90 100644 --- a/webapp/src/app/shared/services/credential.service.ts +++ b/webapp/src/app/shared/services/credential.service.ts @@ -20,6 +20,7 @@ import { IdTokenMetadata, AccessToken, LoginResponse, + UserRole, } from '../../api'; import { Router } from '@angular/router'; @@ -73,15 +74,17 @@ export class CredentialService { () => this.accessToken() !== undefined, ); public readonly isEffectiveSuperAdmin = computed( - () => this.accessToken()?.effective_role === 'SUPER_ADMIN', + () => + this.accessToken()?.effective_roles?.includes(UserRole.SuperAdmin) ?? + false, ); public readonly isEffectiveAdmin = computed( () => - this.accessToken()?.effective_role === 'ADMIN' || - this.isEffectiveSuperAdmin(), + this.accessToken()?.effective_roles?.includes(UserRole.Admin) ?? false, ); public readonly isEffectiveManager = computed( - () => this.accessToken()?.effective_role === 'MANAGER', + () => + this.accessToken()?.effective_roles?.includes(UserRole.Manager) ?? false, ); public readonly effectiveOrganizationId = computed( () => this.accessToken()?.effective_organization_id,