fix(frontend/accounts): unfucked everything

This commit is contained in:
Mats Paul Tončik
2026-07-04 23:49:39 +02:00
29 changed files with 676 additions and 294 deletions
@@ -0,0 +1,41 @@
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<boolean>((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 },
);
});
};
@@ -0,0 +1,18 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'status',
})
export class StatusPipe implements PipeTransform {
transform(value: string, ...args: unknown[]): string {
value = value?.toUpperCase() ?? '';
switch (value) {
case 'ACTIVE':
return 'Aktiv';
case 'INACTIVE':
return 'Inaktiv';
default:
return value;
}
}
}
@@ -1,122 +0,0 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { Configuration } from '../../api';
export type AccountRole = 'SUPER_ADMIN' | 'ADMIN' | 'MANAGER' | 'USER';
export interface AccountProfile {
id: string;
email: string;
name: string;
role: AccountRole;
organization_id: string;
organization_name: string;
deleted: boolean;
is_current: boolean;
is_real_account: boolean;
is_impersonating: boolean;
}
export interface AccountPage {
items: AccountProfile[];
total: number;
page: number;
page_size: number;
}
export interface OrganizationProfile {
id: string;
name: string;
openalex_institution_id?: string | null;
}
export interface AccountPayload {
email?: string;
name?: string;
role?: AccountRole;
organization_id?: string;
password?: string;
deleted?: boolean;
}
export interface CreateAccountPayload {
email: string;
name: string;
role: AccountRole;
organization_id: string;
password: string;
}
@Injectable({
providedIn: 'root',
})
export class AccountManagementService {
private readonly httpClient = inject(HttpClient);
private readonly apiConfiguration = inject(Configuration, { optional: true });
getCurrentAccount(): Observable<AccountProfile> {
return this.httpClient.get<AccountProfile>(
`${this.apiBasePath}/api/accounts/me`,
);
}
listAccounts(options: {
page: number;
pageSize: number;
search?: string;
organizationId?: string;
}): Observable<AccountPage> {
let params = new HttpParams()
.set('page', options.page)
.set('page_size', options.pageSize);
if (options.search) {
params = params.set('search', options.search);
}
if (options.organizationId) {
params = params.set('organization_id', options.organizationId);
}
return this.httpClient.get<AccountPage>(
`${this.apiBasePath}/api/accounts`,
{
params,
},
);
}
createAccount(payload: CreateAccountPayload): Observable<AccountProfile> {
return this.httpClient.post<AccountProfile>(
`${this.apiBasePath}/api/accounts`,
payload,
);
}
updateAccount(
accountId: string,
payload: AccountPayload,
): Observable<AccountProfile> {
return this.httpClient.put<AccountProfile>(
`${this.apiBasePath}/api/accounts/${accountId}`,
payload,
);
}
deleteAccount(accountId: string): Observable<void> {
return this.httpClient.delete<void>(
`${this.apiBasePath}/api/accounts/${accountId}`,
);
}
listOrganizations(): Observable<OrganizationProfile[]> {
return this.httpClient.get<OrganizationProfile[]>(
`${this.apiBasePath}/api/accounts/organizations`,
);
}
private get apiBasePath(): string {
return this.apiConfiguration?.basePath ?? '';
}
}
@@ -35,6 +35,9 @@ export class CredentialService {
this.accessToken()?.effective_role === 'ADMIN' ||
this.isEffectiveSuperAdmin(),
);
public readonly isEffectiveManager = computed(
() => this.accessToken()?.effective_role === 'MANAGER',
);
public readonly effectiveOrganizationId = computed(
() => this.accessToken()?.effective_organization_id,
);
@@ -48,7 +48,11 @@
credentialService.isEffectiveSuperAdmin() ||
credentialService.isEffectiveAdmin()) &&
(!itemGroup.superAdminOnly ||
credentialService.isEffectiveSuperAdmin())
credentialService.isEffectiveSuperAdmin()) &&
(!itemGroup.managerOrAdminOnly ||
credentialService.isEffectiveSuperAdmin() ||
credentialService.isEffectiveAdmin() ||
credentialService.isEffectiveManager())
) {
<mat-divider></mat-divider>
@if (!collapsed()) {
@@ -19,6 +19,7 @@ type NavItem = {
queryParams?: Record<string, string | number | boolean>;
adminOnly?: boolean;
superAdminOnly?: boolean;
managerOrAdminOnly?: boolean;
};
type NavItemGroup = {
@@ -26,6 +27,7 @@ type NavItemGroup = {
items: NavItem[];
adminOnly?: boolean;
superAdminOnly?: boolean;
managerOrAdminOnly?: boolean;
};
@Component({
@@ -125,7 +127,7 @@ export class AppMenuComponent {
icon: 'manage_accounts',
route: '/accounts',
disabled: false,
adminOnly: true,
managerOrAdminOnly: true,
},
{
label: 'Organisationen',
@@ -139,15 +141,19 @@ export class AppMenuComponent {
{
label: 'Navigation',
items: this.navItems.filter(
(item) => !item.adminOnly && !item.superAdminOnly,
(item) =>
!item.adminOnly &&
!item.superAdminOnly &&
!item.managerOrAdminOnly,
),
},
{
label: 'Administration',
items: this.navItems.filter(
(item) => item.adminOnly && !item.superAdminOnly,
(item) =>
(item.adminOnly || item.managerOrAdminOnly) && !item.superAdminOnly,
),
adminOnly: true,
managerOrAdminOnly: true,
},
{
label: 'Super Administration',
@@ -1,5 +1,12 @@
<select (change)="_handleSelectionChange($event)" [ngModel]="_urlValue()">
@for (option of options(); track $index) {
<option [value]="option.value">{{ option.display_name }}</option>
<mat-form-field style="width: 100%">
@if (label()) {
<mat-label>{{ label() }}</mat-label>
}
</select>
<mat-select
(selectionChange)="_handleSelectionChange($event)"
[value]="_urlValue()">
@for (option of _options(); track $index) {
<mat-option [value]="option.value">{{ option.display_name }}</mat-option>
}
</mat-select>
</mat-form-field>
@@ -1,6 +1,7 @@
import { Component, computed, inject, input, output } from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { FormsModule } from '@angular/forms';
import { MatSelectChange, MatSelectModule } from '@angular/material/select';
import { ActivatedRoute, Router } from '@angular/router';
export type SelectorOption = {
@@ -10,14 +11,30 @@ export type SelectorOption = {
@Component({
selector: 'app-query-param-selector',
imports: [FormsModule],
imports: [FormsModule, MatSelectModule],
templateUrl: './query-param-selector.component.html',
styleUrl: './query-param-selector.component.scss',
})
export class QueryParamSelectorComponent {
options = input.required<SelectorOption[]>();
queryParamKey = input.required<string>();
handleSelectionChange = output<Event>();
handleSelectionChange = output<MatSelectChange>();
allowEmptyValue = input<boolean>(false);
label = input<string>('');
_options = computed<SelectorOption[]>(() => {
const options = this.options();
if (!options) {
return [];
}
if (this.allowEmptyValue()) {
return [{ value: '', display_name: '' }, ...options];
}
return options;
});
private readonly activatedRoute = inject(ActivatedRoute);
private readonly router = inject(Router);
@@ -33,8 +50,8 @@ export class QueryParamSelectorComponent {
return params && key ? params[key] || '' : '';
});
_handleSelectionChange(event: Event): void {
const newValue = (event.target as HTMLSelectElement).value;
_handleSelectionChange(event: MatSelectChange): void {
const newValue = event.value;
this.router.navigate([], {
queryParams: { [this.queryParamKey()]: newValue },