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

[#877] apply formatting and linting to services module #1056

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ui/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ src/app/NS
src/app/pipeline-details
src/app/pipelines
src/app/profile
src/app/services
src/scss
1 change: 0 additions & 1 deletion ui/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ src/app/NS
src/app/pipeline-details
src/app/pipelines
src/app/profile
src/app/services
src/scss
146 changes: 98 additions & 48 deletions ui/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@ import { RoleModel } from '../_models/auth.model';

@Injectable()
export class AuthService {

public authToken$: BehaviorSubject<string> = new BehaviorSubject<string>(undefined);
public user$: BehaviorSubject<UserInfo> = new BehaviorSubject<UserInfo>(undefined);
public isLoggedIn$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
public darkMode$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

constructor(private restApi: RestApi,
private tokenStorage: JwtTokenStorageService,
private router: Router,
private loginService: LoginService) {
public authToken$: BehaviorSubject<string> = new BehaviorSubject<string>(
undefined,
);
public user$: BehaviorSubject<UserInfo> = new BehaviorSubject<UserInfo>(
undefined,
);
public isLoggedIn$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(
false,
);
public darkMode$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(
false,
);

constructor(
private restApi: RestApi,
private tokenStorage: JwtTokenStorageService,
private router: Router,
private loginService: LoginService,
) {
if (this.authenticated()) {
this.authToken$.next(tokenStorage.getToken());
this.user$.next(tokenStorage.getUser());
this.isLoggedIn$.next(true);

} else {
this.logout();
}
Expand All @@ -71,10 +79,15 @@ export class AuthService {
}

public authenticated(): boolean {
const token = this.authToken$.getValue() || this.tokenStorage.getToken();
const token =
this.authToken$.getValue() || this.tokenStorage.getToken();
const jwtHelper: JwtHelperService = new JwtHelperService({});

return token !== null && token !== undefined && !jwtHelper.isTokenExpired(token);
return (
token !== null &&
token !== undefined &&
!jwtHelper.isTokenExpired(token)
);
}

public decodeJwtToken(token: string): any {
Expand All @@ -83,27 +96,38 @@ export class AuthService {
}

checkConfiguration(): Observable<boolean> {
return Observable.create((observer) => this.restApi.configured().subscribe(response => {
if (response.configured) {
observer.next(true);
} else {
observer.next(false);
}
}, error => {
observer.error();
}));
return Observable.create(observer =>
this.restApi.configured().subscribe(
response => {
if (response.configured) {
observer.next(true);
} else {
observer.next(false);
}
},
error => {
observer.error();
},
),
);
}

scheduleTokenRenew() {
this.authToken$.pipe(
filter((token: any) => !!token),
map((token: any) => new JwtHelperService({}).getTokenExpirationDate(token)),
switchMap((expiresIn: Date) => timer(expiresIn.getTime() - Date.now() - 60000)),
).subscribe(() => {
if (this.authenticated()) {
this.updateTokenAndUserInfo();
}
});
this.authToken$
.pipe(
filter((token: any) => !!token),
map((token: any) =>
new JwtHelperService({}).getTokenExpirationDate(token),
),
switchMap((expiresIn: Date) =>
timer(expiresIn.getTime() - Date.now() - 60000),
),
)
.subscribe(() => {
if (this.authenticated()) {
this.updateTokenAndUserInfo();
}
});
}

updateTokenAndUserInfo() {
Expand All @@ -113,39 +137,53 @@ export class AuthService {
}

watchTokenExpiration() {
this.authToken$.pipe(
filter((token: any) => !!token),
map((token: any) => new JwtHelperService({}).getTokenExpirationDate(token)),
switchMap((expiresIn: Date) => timer(expiresIn.getTime() - Date.now() + 1))
).subscribe(() => {
this.logout();
this.router.navigate(['login']);
});
this.authToken$
.pipe(
filter((token: any) => !!token),
map((token: any) =>
new JwtHelperService({}).getTokenExpirationDate(token),
),
switchMap((expiresIn: Date) =>
timer(expiresIn.getTime() - Date.now() + 1),
),
)
.subscribe(() => {
this.logout();
this.router.navigate(['login']);
});
}

getUserRoles(): string[] {
return this.getCurrentUser().roles;
}

public hasRole(role: RoleModel): boolean {
return this.getUserRoles().includes('ROLE_ADMIN') || this.getUserRoles().includes(role);
return (
this.getUserRoles().includes('ROLE_ADMIN') ||
this.getUserRoles().includes(role)
);
}

public hasAnyRole(roles: RoleModel[]): boolean {
if (Array.isArray(roles)) {
return roles.reduce((aggregator: false, role: RoleModel) => aggregator || this.hasRole(role), false);
return roles.reduce(
(aggregator: false, role: RoleModel) =>
aggregator || this.hasRole(role),
false,
);
}

return false;
}

isAnyAccessGranted(pageNames: PageName[],
redirect?: boolean): boolean {
isAnyAccessGranted(pageNames: PageName[], redirect?: boolean): boolean {
if (!pageNames || pageNames.length === 0) {
return true;
}

const result = pageNames.some(pageName => this.isAccessGranted(pageName));
const result = pageNames.some(pageName =>
this.isAccessGranted(pageName),
);
if (!result && redirect) {
this.router.navigate(['']);
}
Expand All @@ -162,17 +200,29 @@ export class AuthService {
case PageName.PIPELINE_EDITOR:
return this.hasAnyRole(['ROLE_PIPELINE_ADMIN']);
case PageName.PIPELINE_OVERVIEW:
return this.hasAnyRole(['ROLE_PIPELINE_ADMIN', 'ROLE_PIPELINE_USER']);
return this.hasAnyRole([
'ROLE_PIPELINE_ADMIN',
'ROLE_PIPELINE_USER',
]);
case PageName.CONNECT:
return this.hasAnyRole(['ROLE_CONNECT_ADMIN']);
case PageName.DASHBOARD:
return this.hasAnyRole(['ROLE_DASHBOARD_USER', 'ROLE_DASHBOARD_ADMIN']);
return this.hasAnyRole([
'ROLE_DASHBOARD_USER',
'ROLE_DASHBOARD_ADMIN',
]);
case PageName.DATA_EXPLORER:
return this.hasAnyRole(['ROLE_DATA_EXPLORER_ADMIN', 'ROLE_DATA_EXPLORER_USER']);
return this.hasAnyRole([
'ROLE_DATA_EXPLORER_ADMIN',
'ROLE_DATA_EXPLORER_USER',
]);
case PageName.APPS:
return this.hasAnyRole(['ROLE_APP_USER']);
case PageName.FILE_UPLOAD:
return this.hasAnyRole(['ROLE_CONNECT_ADMIN', 'ROLE_PIPELINE_ADMIN']);
return this.hasAnyRole([
'ROLE_CONNECT_ADMIN',
'ROLE_PIPELINE_ADMIN',
]);
case PageName.INSTALL_PIPELINE_ELEMENTS:
return this.hasAnyRole(['ROLE_ADMIN']);
case PageName.NOTIFICATIONS:
Expand Down
80 changes: 61 additions & 19 deletions ui/src/app/services/available-roles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,66 @@ import { UserRole } from '../_enums/user-role.enum';

@Injectable()
export class AvailableRolesService {
availableRoles: RoleDescription[] = [
{ role: UserRole.ROLE_ADMIN, roleTitle: 'Admin', roleDescription: '' },
{
role: UserRole.ROLE_SERVICE_ADMIN,
roleTitle: 'Service Admin',
roleDescription: '',
},
{
role: UserRole.ROLE_APP_USER,
roleTitle: 'App User',
roleDescription: '',
},
{
role: UserRole.ROLE_DASHBOARD_USER,
roleTitle: 'Dashboard User',
roleDescription: '',
},
{
role: UserRole.ROLE_DASHBOARD_ADMIN,
roleTitle: 'Dashboard Admin',
roleDescription: '',
},
{
role: UserRole.ROLE_DATA_EXPLORER_USER,
roleTitle: 'Data Explorer User',
roleDescription: '',
},
{
role: UserRole.ROLE_DATA_EXPLORER_ADMIN,
roleTitle: 'Data Explorer Admin',
roleDescription: '',
},
{
role: UserRole.ROLE_CONNECT_ADMIN,
roleTitle: 'Connect Admin',
roleDescription: '',
},
{
role: UserRole.ROLE_PIPELINE_USER,
roleTitle: 'Pipeline User',
roleDescription: '',
},
{
role: UserRole.ROLE_PIPELINE_ADMIN,
roleTitle: 'Pipeline Admin',
roleDescription: '',
},
{
role: UserRole.ROLE_ASSET_USER,
roleTitle: 'Asset User',
roleDescription: '',
},
{
role: UserRole.ROLE_ASSET_ADMIN,
roleTitle: 'Asset Admin',
roleDescription: '',
},
];

availableRoles: RoleDescription[] = [
{role: UserRole.ROLE_ADMIN, roleTitle: 'Admin', roleDescription: ''},
{role: UserRole.ROLE_SERVICE_ADMIN, roleTitle: 'Service Admin', roleDescription: ''},
{role: UserRole.ROLE_APP_USER, roleTitle: 'App User', roleDescription: ''},
{role: UserRole.ROLE_DASHBOARD_USER, roleTitle: 'Dashboard User', roleDescription: ''},
{role: UserRole.ROLE_DASHBOARD_ADMIN, roleTitle: 'Dashboard Admin', roleDescription: ''},
{role: UserRole.ROLE_DATA_EXPLORER_USER, roleTitle: 'Data Explorer User', roleDescription: ''},
{role: UserRole.ROLE_DATA_EXPLORER_ADMIN, roleTitle: 'Data Explorer Admin', roleDescription: ''},
{role: UserRole.ROLE_CONNECT_ADMIN, roleTitle: 'Connect Admin', roleDescription: ''},
{role: UserRole.ROLE_PIPELINE_USER, roleTitle: 'Pipeline User', roleDescription: ''},
{role: UserRole.ROLE_PIPELINE_ADMIN, roleTitle: 'Pipeline Admin', roleDescription: ''},
{role: UserRole.ROLE_ASSET_USER, roleTitle: 'Asset User', roleDescription: ''},
{role: UserRole.ROLE_ASSET_ADMIN, roleTitle: 'Asset Admin', roleDescription: ''},
];


public getAvailableRoles(): RoleDescription[] {
return this.availableRoles;
}
public getAvailableRoles(): RoleDescription[] {
return this.availableRoles;
}
}
1 change: 0 additions & 1 deletion ui/src/app/services/get-element-icon-text.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Injectable } from '@angular/core';

@Injectable()
export class ElementIconText {

constructor() {}

getElementIconText(s) {
Expand Down
37 changes: 18 additions & 19 deletions ui/src/app/services/jwt-token-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,27 @@ const TOKEN_KEY = 'auth-token';
const USER_KEY = 'auth-user';

export class JwtTokenStorageService {
constructor() {}

constructor() { }
clearTokens(): void {
window.localStorage.clear();
}

clearTokens(): void {
window.localStorage.clear();
}
public saveToken(token: string): void {
window.localStorage.removeItem(TOKEN_KEY);
window.localStorage.setItem(TOKEN_KEY, token);
}

public saveToken(token: string): void {
window.localStorage.removeItem(TOKEN_KEY);
window.localStorage.setItem(TOKEN_KEY, token);
}
public getToken(): string {
return localStorage.getItem(TOKEN_KEY);
}

public getToken(): string {
return localStorage.getItem(TOKEN_KEY);
}
public saveUser(user: UserInfo): void {
window.localStorage.removeItem(USER_KEY);
window.localStorage.setItem(USER_KEY, JSON.stringify(user));
}

public saveUser(user: UserInfo): void {
window.localStorage.removeItem(USER_KEY);
window.localStorage.setItem(USER_KEY, JSON.stringify(user));
}

public getUser(): UserInfo {
return JSON.parse(localStorage.getItem(USER_KEY));
}
public getUser(): UserInfo {
return JSON.parse(localStorage.getItem(USER_KEY));
}
}
11 changes: 4 additions & 7 deletions ui/src/app/services/notification-count-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ import { BehaviorSubject } from 'rxjs';

@Injectable()
export class NotificationCountService {

public unreadNotificationCount$ = new BehaviorSubject(0);
lockNotificationId: string;
lockActive = false;

constructor(private restApi: RestApi) {
}
constructor(private restApi: RestApi) {}

loadUnreadNotifications() {
this.restApi.getUnreadNotificationsCount()
.subscribe(response => {
this.unreadNotificationCount$.next(response.count);
});
this.restApi.getUnreadNotificationsCount().subscribe(response => {
this.unreadNotificationCount$.next(response.count);
});
}

decreaseNotificationCount() {
Expand Down
Loading