Skip to content

Commit 41614ec

Browse files
committed
feat: use token roles
1 parent f581bf2 commit 41614ec

File tree

65 files changed

+320
-779
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+320
-779
lines changed

src/app/account/account.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ export class AccountComponent {
3737
readonly #forumsClient = inject(ForumsClient);
3838

3939
protected readonly items$: Observable<SidebarItem[]> = combineLatest([
40-
this.#auth.getUser$(),
41-
this.#auth.getUser$().pipe(
42-
switchMap((user) => {
43-
if (!user) {
40+
this.#auth.user$,
41+
this.#auth.authenticated$.pipe(
42+
switchMap((authenticated) => {
43+
if (!authenticated) {
4444
return of(null);
4545
}
4646
return this.#forumsClient.getUserSummary(new Empty());

src/app/account/contacts/contacts.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {LanguageService} from '@services/language';
1010
import {PageEnvService} from '@services/page-env.service';
1111
import {TimeAgoPipe} from '@utils/time-ago.pipe';
1212
import Keycloak from 'keycloak-js';
13-
import {BehaviorSubject, EMPTY, Observable} from 'rxjs';
13+
import {BehaviorSubject, EMPTY, Observable, of} from 'rxjs';
1414
import {catchError, map, switchMap} from 'rxjs/operators';
1515

1616
import {ToastsService} from '../../toasts/toasts.service';
@@ -32,16 +32,16 @@ export class AccountContactsComponent implements OnInit {
3232

3333
readonly #reload$ = new BehaviorSubject<void>(void 0);
3434

35-
protected readonly items$: Observable<Contact[]> = this.#auth.getUser$().pipe(
36-
map((user) => {
37-
if (!user) {
35+
protected readonly items$: Observable<Contact[]> = this.#auth.authenticated$.pipe(
36+
switchMap((authenticated) => {
37+
if (!authenticated) {
3838
this.#keycloak.login({
3939
locale: this.#languageService.language,
4040
redirectUri: window.location.href,
4141
});
4242
return EMPTY;
4343
}
44-
return user;
44+
return of(authenticated);
4545
}),
4646
switchMap(() => this.#reload$),
4747
switchMap(() => this.#contactsService.getContacts$()),

src/app/account/delete/delete.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ export class AccountDeleteComponent implements OnInit {
3636
}
3737

3838
protected submit() {
39-
this.#auth
40-
.getUser$()
39+
this.#auth.user$
4140
.pipe(
4241
switchMap((user) =>
4342
user
@@ -59,7 +58,7 @@ export class AccountDeleteComponent implements OnInit {
5958
}
6059
},
6160
next: () => {
62-
this.#auth.signOut$();
61+
this.#auth.signOut$().subscribe();
6362
this.#router.navigate(['/account/delete/deleted']);
6463
},
6564
});

src/app/account/inbox-pictures/inbox-pictures.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class AccountInboxPicturesComponent implements OnInit {
3232
distinctUntilChanged(),
3333
debounceTime(10),
3434
),
35-
this.#auth.getUser$(),
35+
this.#auth.user$,
3636
]).pipe(
3737
switchMap(([page, user]) =>
3838
user

src/app/account/profile/profile.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ export class AccountProfileComponent implements OnDestroy, OnInit {
6363
ngOnInit(): void {
6464
setTimeout(() => this.#pageEnv.set({pageId: 129}), 0);
6565

66-
this.#sub = this.#auth
67-
.getUser$()
66+
this.#sub = this.#auth.user$
6867
.pipe(
6968
switchMap((user) => {
7069
if (!user) {

src/app/account/specs-conflicts/specs-conflicts.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ <h1 i18n id="header">Conflicts</h1>
33
</div>
44

55
@if (user$ | async; as user) {
6-
@if (user.specsWeight !== null) {
6+
@if (user.specsWeight !== 0) {
77
<p class="float-end"><ng-container i18n>Weight</ng-container>: {{ user.specsWeight.toFixed(2) }}</p>
88
}
99
} @else {

src/app/account/specs-conflicts/specs-conflicts.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class AccountSpecsConflictsComponent implements OnInit {
8181
map((filter) => mapFilter(filter)),
8282
);
8383

84-
protected readonly user$: Observable<APIUser | null> = this.auth.getUser$();
84+
protected readonly user$: Observable<APIUser | null> = this.auth.user$;
8585

8686
readonly #itemsCache = new Map<string, Observable<APIItem>>();
8787

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
import {inject, Injectable} from '@angular/core';
22
import {GetMessagesRequest, ModeratorAttention} from '@grpc/spec.pb';
33
import {CommentsClient} from '@grpc/spec.pbsc';
4-
import {ACLService, Privilege, Resource} from '@services/acl.service';
4+
import {AuthService, Role} from '@services/auth.service';
55
import {Observable, of} from 'rxjs';
66
import {map, shareReplay, switchMap} from 'rxjs/operators';
77

88
@Injectable({
99
providedIn: 'root',
1010
})
1111
export class APICommentsService {
12-
readonly #acl = inject(ACLService);
12+
readonly #auth = inject(AuthService);
1313
readonly #commentsClient = inject(CommentsClient);
1414

15-
public readonly attentionCommentsCount$: Observable<null | number> = this.#acl
16-
.isAllowed$(Resource.GLOBAL, Privilege.MODERATE)
17-
.pipe(
18-
switchMap((isModer) => {
19-
if (!isModer) {
20-
return of(null);
21-
}
15+
public readonly attentionCommentsCount$: Observable<null | number> = this.#auth.hasRole$(Role.MODER).pipe(
16+
switchMap((isModer) => {
17+
if (!isModer) {
18+
return of(null);
19+
}
2220

23-
return this.#commentsClient
24-
.getMessages(
25-
new GetMessagesRequest({
26-
limit: 0,
27-
moderatorAttention: ModeratorAttention.REQUIRED,
28-
}),
29-
)
30-
.pipe(map((response) => (response.paginator ? response.paginator.totalItemCount : null)));
31-
}),
32-
shareReplay({bufferSize: 1, refCount: false}),
33-
);
21+
return this.#commentsClient
22+
.getMessages(
23+
new GetMessagesRequest({
24+
limit: 0,
25+
moderatorAttention: ModeratorAttention.REQUIRED,
26+
}),
27+
)
28+
.pipe(map((response) => (response.paginator ? response.paginator.totalItemCount : null)));
29+
}),
30+
shareReplay({bufferSize: 1, refCount: false}),
31+
);
3432
}

src/app/api/picture-moder-vote-template/picture-moder-vote-template.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class APIPictureModerVoteTemplateService {
2121
readonly #change$ = new BehaviorSubject<void>(void 0);
2222

2323
public getTemplates$(): Observable<ModerVoteTemplate[]> {
24-
return combineLatest([this.#change$, this.#auth.getUser$()]).pipe(
24+
return combineLatest([this.#change$, this.#auth.authenticated$]).pipe(
2525
switchMap(() => this.#pictures.getModerVoteTemplates(new Empty({}))),
2626
map((response) => (response.items ? response.items : [])),
2727
shareReplay({bufferSize: 1, refCount: false}),

src/app/app.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
</form>
7878
}
7979
<ul class="nav navbar-nav navbar-right">
80-
@if (user$ | async) {
80+
@if (authenticated$ | async) {
8181
<li class="nav-item">
8282
<a class="nav-link" routerLink="/account/messages" routerLinkActive="active">
8383
<i class="bi bi-chat-fill" aria-hidden="true"></i>
@@ -120,7 +120,7 @@
120120
<i class="bi bi-people-fill" aria-hidden="true"></i>
121121
<ng-container i18n>Who is online?</ng-container>
122122
</a>
123-
@if (user$ | async) {
123+
@if (authenticated$ | async) {
124124
<a routerLink="/account/contacts" class="dropdown-item" routerLinkActive="active">
125125
<i class="bi bi-people-fill" aria-hidden="true"></i>
126126
<ng-container i18n>Contacts</ng-container>
@@ -134,7 +134,7 @@
134134
<i class="bi bi-info" aria-hidden="true"></i>
135135
<ng-container i18n>About us</ng-container>
136136
</a>
137-
@if (user$ | async) {
137+
@if (authenticated$ | async) {
138138
<a href="#" (click)="signOut()" class="dropdown-item">
139139
<i class="bi bi-box-arrow-right" aria-hidden="true"></i>
140140
<ng-container i18n>Sign out</ng-container>

src/app/app.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {AsyncPipe, NgClass} from '@angular/common';
22
import {Component, inject, Renderer2} from '@angular/core';
33
import {NavigationStart, Router, RouterLink, RouterLinkActive, RouterOutlet} from '@angular/router';
44
import {environment} from '@environment/environment';
5-
import {APIUser, ItemFields, ItemListOptions, ItemsRequest, ItemType} from '@grpc/spec.pb';
5+
import {ItemFields, ItemListOptions, ItemsRequest, ItemType} from '@grpc/spec.pb';
66
import {ItemsClient} from '@grpc/spec.pbsc';
77
import {
88
NgbCollapse,
@@ -12,7 +12,6 @@ import {
1212
NgbModal,
1313
NgbTooltip,
1414
} from '@ng-bootstrap/ng-bootstrap';
15-
import {ACLService} from '@services/acl.service';
1615
import {AuthService} from '@services/auth.service';
1716
import {Language, LanguageService} from '@services/language';
1817
import {MessageService} from '@services/message';
@@ -48,8 +47,7 @@ import {UsersOnlineComponent} from './users/online/online.component';
4847
templateUrl: './app.component.html',
4948
})
5049
export class AppComponent {
51-
protected readonly auth = inject(AuthService);
52-
protected readonly acl = inject(ACLService);
50+
readonly #auth = inject(AuthService);
5351
protected readonly router = inject(Router);
5452
readonly #messageService = inject(MessageService);
5553
readonly #pageEnv = inject(PageEnvService);
@@ -61,7 +59,7 @@ export class AppComponent {
6159

6260
protected languages: Language[] = environment.languages;
6361
protected readonly layoutParams$: Observable<LayoutParams> = this.#pageEnv.layoutParams$.asObservable();
64-
protected readonly user$: Observable<APIUser | null> = this.auth.getUser$();
62+
protected readonly authenticated$: Observable<boolean> = this.#auth.authenticated$;
6563
protected readonly newPersonalMessages$ = this.#messageService
6664
.getNew$()
6765
.pipe(shareReplay({bufferSize: 1, refCount: false}));
@@ -123,7 +121,7 @@ export class AppComponent {
123121
}
124122

125123
protected signOut() {
126-
this.auth.signOut$().subscribe({
124+
this.#auth.signOut$().subscribe({
127125
error: (error: unknown) => {
128126
console.error(error);
129127
},

src/app/app.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {environment} from '@environment/environment';
99
import {NgbCollapseModule, NgbDropdownModule, NgbModule, NgbTooltipModule} from '@ng-bootstrap/ng-bootstrap';
1010
import {GRPC_INTERCEPTORS, GrpcCoreModule} from '@ngx-grpc/core';
1111
import {GrpcWebClientModule} from '@ngx-grpc/grpc-web-client';
12-
import {ACLService, APIACL} from '@services/acl.service';
1312
import {authInterceptor$, GrpcAuthInterceptor, GrpcLogInterceptor} from '@services/api.service';
1413
import {AuthService} from '@services/auth.service';
1514
import {ContactsService} from '@services/contacts';
@@ -71,9 +70,7 @@ export const appConfig: ApplicationConfig = {
7170
}),
7271
AutoRefreshTokenService,
7372
UserActivityService,
74-
APIACL,
7573
AuthService,
76-
ACLService,
7774
PictureService,
7875
ItemService,
7976
ReCaptchaService,

src/app/auth.guard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export const authGuard: CanActivateFn = () => {
1010
const keycloak = inject(Keycloak);
1111
const language = inject(LanguageService);
1212

13-
return auth.getUser$().pipe(
14-
map((user) => {
15-
if (!user) {
13+
return auth.authenticated$.pipe(
14+
map((authenticated) => {
15+
if (!authenticated) {
1616
keycloak.login({
1717
locale: language.language,
1818
redirectUri: window.location.href,

src/app/cars/attrs-change-log/attrs-change-log.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from '@grpc/spec.pb';
1515
import {AttrsClient, ItemsClient, UsersClient} from '@grpc/spec.pbsc';
1616
import {NgbTooltip, NgbTypeahead, NgbTypeaheadSelectItemEvent} from '@ng-bootstrap/ng-bootstrap';
17-
import {ACLService, Privilege, Resource} from '@services/acl.service';
17+
import {AuthService, Role} from '@services/auth.service';
1818
import {LanguageService} from '@services/language';
1919
import {PageEnvService} from '@services/page-env.service';
2020
import {UserService} from '@services/user';
@@ -44,7 +44,7 @@ interface AttrUserValueListItem {
4444
export class CarsAttrsChangeLogComponent implements OnDestroy, OnInit {
4545
readonly #route = inject(ActivatedRoute);
4646
readonly #router = inject(Router);
47-
readonly #acl = inject(ACLService);
47+
readonly #auth = inject(AuthService);
4848
readonly #pageEnv = inject(PageEnvService);
4949
readonly #toastService = inject(ToastsService);
5050
readonly #usersClient = inject(UsersClient);
@@ -58,7 +58,7 @@ export class CarsAttrsChangeLogComponent implements OnDestroy, OnInit {
5858

5959
#querySub?: Subscription;
6060

61-
protected readonly isModer$ = this.#acl.isAllowed$(Resource.GLOBAL, Privilege.MODERATE);
61+
protected readonly isModer$ = this.#auth.hasRole$(Role.MODER);
6262

6363
protected readonly userID$: Observable<string> = this.#route.queryParamMap.pipe(
6464
map((params) => params.get('user_id') ?? ''),

src/app/cars/specifications-editor/engine/engine.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {APIItem, UpdateItemRequest} from '@grpc/spec.pb';
55
import {ItemFields, ItemRequest} from '@grpc/spec.pb';
66
import {ItemsClient} from '@grpc/spec.pbsc';
77
import {FieldMask} from '@ngx-grpc/well-known-types';
8-
import {ACLService, Privilege, Resource} from '@services/acl.service';
8+
import {AuthService, Role} from '@services/auth.service';
99
import {LanguageService} from '@services/language';
1010
import {BehaviorSubject, Observable, of} from 'rxjs';
1111
import {shareReplay, switchMap} from 'rxjs/operators';
@@ -18,7 +18,7 @@ import {ToastsService} from '../../../toasts/toasts.service';
1818
templateUrl: './engine.component.html',
1919
})
2020
export class CarsSpecificationsEditorEngineComponent {
21-
readonly #acl = inject(ACLService);
21+
readonly #auth = inject(AuthService);
2222
readonly #itemsClient = inject(ItemsClient);
2323
readonly #toastService = inject(ToastsService);
2424
readonly #languageService = inject(LanguageService);
@@ -29,8 +29,8 @@ export class CarsSpecificationsEditorEngineComponent {
2929
protected readonly item$ = new BehaviorSubject<APIItem | null>(null);
3030

3131
@Output() changed = new EventEmitter<void>();
32-
protected readonly isAllowedEditEngine$ = this.#acl
33-
.isAllowed$(Resource.SPECIFICATIONS, Privilege.EDIT_ENGINE)
32+
protected readonly isAllowedEditEngine$ = this.#auth
33+
.hasRole$(Role.CARS_MODER)
3434
.pipe(shareReplay({bufferSize: 1, refCount: false}));
3535

3636
protected readonly engine$: Observable<APIItem | null> = this.item$.pipe(

src/app/cars/specifications-editor/spec/spec.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ export class CarsSpecificationsEditorSpecComponent {
104104
protected loading = 0;
105105
readonly #change$ = new BehaviorSubject<void>(void 0);
106106

107-
protected readonly user$ = this.#auth.getUser$();
108-
109107
// fields: 'options,childs.options',
110108
protected readonly attributes$: Observable<APIAttrAttributeInSpecEditor[]> = this.item$.pipe(
111109
distinctUntilChanged(),
@@ -126,7 +124,7 @@ export class CarsSpecificationsEditorSpecComponent {
126124

127125
protected readonly currentUserValues$: Observable<{[p: string]: AttrUserValue}> = combineLatest([
128126
this.item$,
129-
this.user$,
127+
this.#auth.user$,
130128
this.attributes$,
131129
this.#change$,
132130
]).pipe(

src/app/cars/specifications-editor/specifications-editor.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import {Component, inject} from '@angular/core';
33
import {ActivatedRoute, Router, RouterLink} from '@angular/router';
44
import {APIItem, ItemFields, ItemRequest, ItemType, RefreshInheritanceRequest} from '@grpc/spec.pb';
55
import {ItemsClient} from '@grpc/spec.pbsc';
6-
import {ACLService, Privilege, Resource} from '@services/acl.service';
7-
import {AuthService} from '@services/auth.service';
6+
import {AuthService, Role} from '@services/auth.service';
87
import {LanguageService} from '@services/language';
98
import {PageEnvService} from '@services/page-env.service';
109
import {MarkdownComponent} from '@utils/markdown/markdown.component';
@@ -29,7 +28,6 @@ import {CarsSpecificationsEditorSpecComponent} from './spec/spec.component';
2928
templateUrl: './specifications-editor.component.html',
3029
})
3130
export class CarsSpecificationsEditorComponent {
32-
readonly #acl = inject(ACLService);
3331
readonly #router = inject(Router);
3432
readonly #route = inject(ActivatedRoute);
3533
readonly #pageEnv = inject(PageEnvService);
@@ -39,10 +37,10 @@ export class CarsSpecificationsEditorComponent {
3937
readonly #languageService = inject(LanguageService);
4038

4139
readonly #change$ = new BehaviorSubject<void>(void 0);
42-
protected readonly isModer$ = this.#acl.isAllowed$(Resource.GLOBAL, Privilege.MODERATE);
43-
protected readonly isSpecsAdmin$ = this.#acl.isAllowed$(Resource.SPECIFICATIONS, Privilege.ADMIN);
40+
protected readonly isModer$ = this.#auth.hasRole$(Role.MODER);
41+
protected readonly isSpecsAdmin$ = this.#auth.hasRole$(Role.ADMIN);
4442
protected readonly tab$ = this.#route.queryParamMap.pipe(map((params) => params.get('tab') ?? 'info'));
45-
protected readonly user$ = this.#auth.getUser$();
43+
protected readonly user$ = this.#auth.user$;
4644

4745
protected readonly data$: Observable<APIItem> = this.#route.queryParamMap.pipe(
4846
map((params) => params.get('item_id') ?? ''),

0 commit comments

Comments
 (0)