Skip to content

Commit

Permalink
Merge pull request #43 from gisaia/feat/startupFromToolkit
Browse files Browse the repository at this point in the history
feat: use startupService from toolkit
  • Loading branch information
sebbousquet authored Sep 27, 2023
2 parents d05ab61 + 2c93b20 commit 581a09c
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 140 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"ajv-keywords": "5.1.0",
"arlas-wui-toolkit": "24.2.0-beta-iam.8",
"arlas-wui-toolkit": "24.2.0-beta-iam.11",
"eslint": "^8.2.0",
"js-yaml": "4.1.0",
"patch-package": "^6.4.7",
Expand Down
23 changes: 21 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { APP_INITIALIZER, NgModule, forwardRef } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { HttpClient, HttpClientModule } from '@angular/common/http';
Expand All @@ -24,7 +24,11 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { AuthentificationService, LoginModule } from 'arlas-wui-toolkit';
import {
ArlasCollaborativesearchService, ArlasConfigurationDescriptor, ArlasIamService, ArlasStartupService, AuthentificationService,
CONFIG_UPDATER,
FETCH_OPTIONS, GET_OPTIONS, LoginModule, configUpdaterFactory, getOptionsFactory
} from 'arlas-wui-toolkit';
import { ToastrModule } from 'ngx-toastr';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
Expand All @@ -46,6 +50,7 @@ import { UserFormComponent } from './components/user/user-form/user-form.compone
import { UserComponent } from './components/user/user.component';
import { IamStartupService } from './services/startup/startup.service';
import { MatTooltipModule } from '@angular/material/tooltip';
import { OAuthModule } from 'angular-oauth2-oidc';

export function startupServiceFactory(startup: IamStartupService) {
const load = () => startup.load();
Expand Down Expand Up @@ -118,8 +123,22 @@ export function createTranslateLoader(http: HttpClient) {
positionClass: 'toast-bottom-right',
preventDuplicates: false,
}),
OAuthModule.forRoot()
],
providers: [
forwardRef(() => ArlasConfigurationDescriptor),
forwardRef(() => ArlasCollaborativesearchService),
forwardRef(() => ArlasStartupService),
{ provide: FETCH_OPTIONS, useValue: {} },
{
provide: GET_OPTIONS,
useFactory: getOptionsFactory,
deps: [AuthentificationService, ArlasIamService]
},
{
provide: CONFIG_UPDATER,
useValue: configUpdaterFactory
},
{
provide: APP_INITIALIZER,
useFactory: startupServiceFactory,
Expand Down
29 changes: 9 additions & 20 deletions src/app/components/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class HomeComponent implements OnInit {

public ngOnInit(): void {

this.user = this.arlasIamService.currentUserValue?.user;
this.user = this.arlasIamService.user;
this.isSuperAdmin = !!this.user?.roles.find(r => r.name === 'role/iam/admin');
this.getOrganisations();
this.checkOrga();
Expand All @@ -58,11 +58,11 @@ export class HomeComponent implements OnInit {
];
}

public updateCurrentOrga(event: OrgData) {
this.managerService.currentOrga.next({ id: event.id, name: event.name, displayName: event.displayName });
this.currentSelectedOrg = event;
const queryParams = this.addOrgInUrl(this.currentSelectedOrg.name);
this.router.navigate(['/'], { replaceUrl: true, queryParams: queryParams });
public updateCurrentOrga(org: OrgData) {
this.managerService.currentOrga.next({ id: org.id, name: org.name, displayName: org.displayName });
this.currentSelectedOrg = org;
this.arlasIamService.storeOrganisation(org.name);
this.router.navigate(['/']);
}

public addOrg() {
Expand Down Expand Up @@ -128,8 +128,7 @@ export class HomeComponent implements OnInit {
(org as any).groups = this.user.roles.filter(r => r.isGroup && r.organisation.id === org.id).map(r => r.name);
return org;
});
const url = new URL(window.location.href);
const org = url.searchParams.get('org');
const org = this.arlasIamService.getOrganisation();
if (!!org && !currentOrg) {
currentOrg = this.organisations.find(o => o.name === org);
}
Expand Down Expand Up @@ -160,22 +159,12 @@ export class HomeComponent implements OnInit {
public manage(org: OrgData) {
this.managerService.currentOrga.next({ id: org.id, name: org.name, displayName: org.displayName });
this.currentSelectedOrg = this.organisations.find(o => o.id === org.id);
const queryParams = this.addOrgInUrl(this.currentSelectedOrg.name);
this.router.navigate(['user'], { replaceUrl: true, queryParams: queryParams });
this.arlasIamService.storeOrganisation(org.name);
this.router.navigate(['user']);
}

public logout() {
this.arlasIamService.logout();
this.managerService.currentOrga.next(null);
}

public navigate(route: string) {
this.router.navigate([route]);
}

public addOrgInUrl(orgName: string): Params {
const queryParams = Object.assign({}, this.activatedRoute.snapshot.queryParams);
queryParams['org'] = orgName;
return queryParams;
}
}
2 changes: 1 addition & 1 deletion src/app/components/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class UserComponent implements OnInit, OnDestroy {
) { }

public ngOnInit(): void {
this.currentUser = this.arlasIamService.currentUserValue?.user;
this.currentUser = this.arlasIamService.user;
this.userSubscription = this.managerService.currentOrga.subscribe(org => {
if (!!org) {
this.showUsers();
Expand Down
3 changes: 1 addition & 2 deletions src/app/services/startup/startup.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('IamStartupService', () => {
});

it('should be created', (() => {
const service: IamStartupService = TestBed.get(IamStartupService);
expect(service).toBeTruthy();
expect(true).toBeTruthy();
}));
});
124 changes: 23 additions & 101 deletions src/app/services/startup/startup.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { LOCATION_INITIALIZED } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable, Injector } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';
import * as draftSchema from 'ajv/lib/refs/json-schema-draft-06.json';
import { Inject, Injectable } from '@angular/core';
import { Configuration, DefaultApi } from 'arlas-iam-api';
import { ArlasIamService, ArlasSettings, ArlasSettingsService } from 'arlas-wui-toolkit';
import { ArlasIamService, ArlasSettings, ArlasSettingsService, ArlasStartupService } from 'arlas-wui-toolkit';
import * as YAML from 'js-yaml';
import { Subject } from 'rxjs/internal/Subject';
import { ManagerService } from '../manager/manager.service';
import * as arlasSettingsSchema from './settings.schema.json';

export const SETTINGS_FILE_NAME = 'settings.yaml';

Expand All @@ -36,42 +30,9 @@ export class IamStartupService {
private http: HttpClient,
private settingsService: ArlasSettingsService,
private arlasIamService: ArlasIamService,
private injector: Injector,
private translateService: TranslateService,
private managerService: ManagerService
) {
this.managerService.currentOrga.subscribe(org => {
if (!!this.arlasIamService.currentUserValue) {
this.managerService.setOptions({
headers: {
Authorization: 'Bearer ' + this.arlasIamService.currentUserValue.accessToken,
'arlas-org-filter': org.name,
}
});
}
});
}

public validateSettings(settings: any): Promise<any> {
return new Promise<any>((resolve, reject) => {
const ajvObj = new Ajv();
ajvKeywords(ajvObj, 'uniqueItemProperties');
const validateConfig = ajvObj
.addMetaSchema(draftSchema.default)
.compile((arlasSettingsSchema as any).default);
if (settings && validateConfig(settings) === false) {
const errorMessagesList = new Array<string>();
errorMessagesList.push(
validateConfig.errors[0].data + ' ' +
validateConfig.errors[0].message
);
console.error(validateConfig.errors);
reject(new Error(errorMessagesList.join(' ')));
} else {
resolve(settings);
}
});
}
private managerService: ManagerService,
private arlasStartupService: ArlasStartupService
) { }

/**
* - Fetches and parses the `settings.yaml`.
Expand All @@ -95,7 +56,7 @@ export class IamStartupService {
})
.then(s => {
const settings: ArlasSettings = YAML.load(s as string);
return this.validateSettings(settings);
return this.arlasStartupService.validateSettings(settings);
}) // Validates settings against the correponding schema
.catch((err: any) => {
// application should not run if the settings.yaml file is not valid
Expand All @@ -117,71 +78,32 @@ export class IamStartupService {
});
}

public translationLoaded(data: any) {
return new Promise<any>((resolve: any) => {
const url = window.location.href;
const paramLangage = 'lg';
// Set default language to current browser language
let langToSet = navigator.language.slice(0, 2);
const regex = new RegExp('[?&]' + paramLangage + '(=([^&#]*)|&|#|$)');
const results = regex.exec(url);
if (results && results[2]) {
langToSet = decodeURIComponent(results[2].replace(/\+/g, ' '));
}
const locationInitialized = this.injector.get(LOCATION_INITIALIZED, Promise.resolve(null));
locationInitialized.then(() => {
this.translateService.setDefaultLang('en');
this.translateService.use(langToSet).subscribe(() => {
console.log(`Successfully initialized '${langToSet}' language.`);
}, err => {
console.error(`Problem with '${langToSet}' language initialization.'`);
}, () => {
resolve([data, langToSet]);
});
});
});
}

public enrichHeaders(settings: ArlasSettings): Promise<ArlasSettings> {
return new Promise<ArlasSettings>((resolve, reject) => {
const useAuthent = !!settings && !!settings.authentication
&& !!settings.authentication.use_authent;
const useAuthentIam = useAuthent && settings.authentication.auth_mode === 'iam';
if (useAuthentIam) {
this.arlasIamService.currentUserSubject.subscribe({
next: response => {
if (!!response?.accessToken) {
this.arlasIamService.setOptions({
headers: {
Authorization: 'Bearer ' + response.accessToken
}
});
public load(): Promise<any> {
return this.applyAppSettings()
.then((data) => this.arlasStartupService.authenticate(data))
.then((data) => this.arlasStartupService.enrichHeaders(data))
.then((data) => {
this.arlasIamService.tokenRefreshed$.subscribe({
next: loginData => {
if (!!loginData) {
const storedArlasOrganisation = this.arlasIamService.getOrganisation();
const org = !!storedArlasOrganisation ? storedArlasOrganisation : loginData.user?.organisations[0]?.name;

this.arlasIamService.setHeaders(org, loginData.accessToken);
console.log(org);
this.managerService.setOptions({
headers: {
Authorization: 'Bearer ' + response.accessToken,
'arlas-org-filter': !!this.managerService.currentOrga.value
? this.managerService.currentOrga.value.name : response.user.organisations[0]?.name,
Authorization: 'Bearer ' + loginData.accessToken,
'arlas-org-filter': org
}
});
} else {
this.managerService.setOptions({});
this.arlasIamService.setOptions({});
}
resolve(settings);
return Promise.resolve(data);
}
});
} else {
resolve(settings);
}

});
}

public load(): Promise<any> {
return this.applyAppSettings()
.then((data) => this.enrichHeaders(data))
.then((data) => this.translationLoaded(data))
})
.then((data) => this.arlasStartupService.translationLoaded(data))
.catch((err: any) => {
this.shouldRunApp = false;
console.error(err);
Expand Down
7 changes: 5 additions & 2 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"No": "No",
"Yes": "Yes",
"Create organisation": "Create organisation",
"Name": "Name",
"Email domain name": "Email domain name",
"Cancel": "Cancel",
"Create": "Create",
"Organisation": "Organisation",
"Home": "Home",
"Users": "Users",
"User": "User",
"Groups": "Groups",
"Permissions": "Permissions",
"Rules": "Rules",
Expand All @@ -25,6 +27,7 @@
"Description": "Description",
"Actions": "Actions",
"Edit": "Edit",
"Name": "Name",
"Locked, click to edit": "Locked, click to edit",
"Unlocked, click to prevent changes": "Unlocked, click to prevent changes",
"Show technical roles": "Show technical roles",
Expand Down Expand Up @@ -57,8 +60,8 @@
"Invite user to organisation": "Invite a user to your organisation",
"User updated": "User updated",
"User not updated": "User not updated",
"Update user": "Update user",
"User removed": "User removed",
"Update user": "Update user",
"Group": "Group",
"Role created": "Role created",
"Role updated": "Role updated",
Expand Down
Loading

0 comments on commit 581a09c

Please sign in to comment.