Chore: Upgrade to Angular v18
Chore: Upgrade to Python 3.12 Chore: Upgrade to psycopg3
This commit is contained in:
@ -1,34 +1,26 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AuthGuard {
|
||||
constructor(
|
||||
private router: Router,
|
||||
private authService: AuthService,
|
||||
private toaster: ToasterService,
|
||||
) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||
const { user } = this.authService;
|
||||
const permission =
|
||||
route.data['permission'] === undefined
|
||||
? route.data['permission']
|
||||
: route.data['permission'].replace(/ /g, '-').toLowerCase();
|
||||
if (!user) {
|
||||
// not logged in so redirect to login page with the return url
|
||||
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
||||
return false;
|
||||
}
|
||||
if (permission !== undefined && !this.authService.allowed(permission)) {
|
||||
this.toaster.show('Error', 'You do not have the permission to access this area.');
|
||||
return false;
|
||||
}
|
||||
// logged in so return true
|
||||
return true;
|
||||
export const authGuard: CanActivateFn = (route, state) => {
|
||||
const user = inject(AuthService).user;
|
||||
const permission =
|
||||
route.data['permission'] === undefined
|
||||
? route.data['permission']
|
||||
: route.data['permission'].replace(/ /g, '-').toLowerCase();
|
||||
if (!user) {
|
||||
// not logged in so redirect to login page with the return url
|
||||
inject(Router).navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (permission !== undefined && user.perms.indexOf(permission) === -1) {
|
||||
inject(ToasterService).show('Danger', 'You do not have the permission to access this area.');
|
||||
return false;
|
||||
}
|
||||
// logged in so return true
|
||||
return true;
|
||||
};
|
||||
|
||||
@ -3,7 +3,7 @@ import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { environment } from '../../environments/environment';
|
||||
import { environment } from '../app.environment';
|
||||
import { Device } from '../core/device';
|
||||
import { Section } from '../core/section';
|
||||
import { User } from '../core/user';
|
||||
@ -18,6 +18,7 @@ export class AuthService {
|
||||
public currentUser: Observable<User | null>;
|
||||
public device: Device;
|
||||
private currentUserSubject: BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null);
|
||||
public isRefreshing = false;
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
|
||||
@ -18,7 +18,9 @@
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<mat-divider></mat-divider>
|
||||
<h2 *ngIf="unregisteredDevice">Sorry, device {{ deviceName }} is not enabled.</h2>
|
||||
@if (unregisteredDevice) {
|
||||
<h2>Sorry, device {{ deviceName }} is not enabled.</h2>
|
||||
}
|
||||
</form>
|
||||
</mat-card-content>
|
||||
<mat-card-actions>
|
||||
|
||||
@ -1,5 +1,11 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { FormControl, FormGroup, Validators } from '@angular/forms';
|
||||
import { FormControl, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
|
||||
import { MatButton } from '@angular/material/button';
|
||||
import { MatCard, MatCardHeader, MatCardTitle, MatCardContent, MatCardActions } from '@angular/material/card';
|
||||
import { MatDivider } from '@angular/material/divider';
|
||||
import { MatFormField, MatLabel, MatSuffix } from '@angular/material/form-field';
|
||||
import { MatIcon } from '@angular/material/icon';
|
||||
import { MatInput } from '@angular/material/input';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
@ -10,6 +16,22 @@ import { AuthService } from '../auth.service';
|
||||
selector: 'app-login',
|
||||
templateUrl: './login.component.html',
|
||||
styleUrls: ['./login.component.css'],
|
||||
standalone: true,
|
||||
imports: [
|
||||
MatCard,
|
||||
MatCardHeader,
|
||||
MatCardTitle,
|
||||
MatCardContent,
|
||||
ReactiveFormsModule,
|
||||
MatFormField,
|
||||
MatLabel,
|
||||
MatInput,
|
||||
MatIcon,
|
||||
MatSuffix,
|
||||
MatDivider,
|
||||
MatCardActions,
|
||||
MatButton,
|
||||
],
|
||||
})
|
||||
export class LoginComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
||||
@ -61,17 +83,17 @@ export class LoginComponent implements OnInit, AfterViewInit {
|
||||
this.auth
|
||||
.login(username.trim(), password.trim())
|
||||
// .pipe(first())
|
||||
.subscribe(
|
||||
() => {
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.router.navigateByUrl(this.returnUrl);
|
||||
},
|
||||
(error) => {
|
||||
if (error.status === 401 && error.error.detail === 'Device is not registered') {
|
||||
error: (error) => {
|
||||
if (error.status === 401 && error.detail === 'Device is not registered') {
|
||||
this.unregisteredDevice = true;
|
||||
this.deviceName = this.cs.getCookie('device');
|
||||
}
|
||||
this.toaster.show('Error', error.error.detail);
|
||||
this.toaster.show('Error', error.detail);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import { AuthService } from '../auth.service';
|
||||
@Component({
|
||||
selector: 'app-logout',
|
||||
template: '',
|
||||
standalone: true,
|
||||
})
|
||||
export class LogoutComponent implements OnInit {
|
||||
constructor(private auth: AuthService) {}
|
||||
|
||||
Reference in New Issue
Block a user