From 760be3f27daeece5194f07fb302799764ac0e4ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=20K=C3=A4sler?= <tom.kaesler@mni.thm.de>
Date: Fri, 8 Mar 2019 16:33:03 +0100
Subject: [PATCH] add json config to repository

specifig config.json is loaded based on the angular environment
login page checks guest login availability via config
---
 src/app/app.config.ts                         | 20 +++++++++++++++++++
 src/app/app.module.ts                         | 12 ++++++++++-
 .../home/new-landing/new-landing.component.ts |  4 +++-
 src/app/models/app-config.model.ts            | 14 +++++++++++++
 src/assets/config/config.dev.json             | 14 +++++++++++++
 src/assets/config/config.prod.json            | 14 +++++++++++++
 src/environments/environment.prod.ts          |  1 +
 src/environments/environment.ts               |  1 +
 8 files changed, 78 insertions(+), 2 deletions(-)
 create mode 100644 src/app/app.config.ts
 create mode 100644 src/app/models/app-config.model.ts
 create mode 100644 src/assets/config/config.dev.json
 create mode 100644 src/assets/config/config.prod.json

diff --git a/src/app/app.config.ts b/src/app/app.config.ts
new file mode 100644
index 000000000..fb3eec3ad
--- /dev/null
+++ b/src/app/app.config.ts
@@ -0,0 +1,20 @@
+import { Injectable } from '@angular/core';
+import { HttpClient } from '@angular/common/http';
+import { environment } from '../environments/environment';
+import { IAppConfig } from './models/app-config.model';
+@Injectable()
+export class AppConfig {
+    static settings: IAppConfig;
+    constructor(private http: HttpClient) {}
+    load() {
+        const jsonFile = `assets/config/config.${environment.name}.json`;
+        return new Promise<void>((resolve, reject) => {
+            this.http.get(jsonFile).toPromise().then((response: IAppConfig) => {
+               AppConfig.settings = <IAppConfig>response;
+               resolve();
+            }).catch((response: any) => {
+               reject(`Could not load file '${jsonFile}': ${JSON.stringify(response)}`);
+            });
+        });
+    }
+}
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index b0dbf9400..83012f4e1 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -1,4 +1,4 @@
-import { NgModule } from '@angular/core';
+import { NgModule, APP_INITIALIZER } from '@angular/core';
 import { AppComponent } from './app.component';
 import { RegisterComponent } from './components/home/_dialogs/register/register.component';
 import { PasswordResetComponent } from './components/home/_dialogs/password-reset/password-reset.component';
@@ -26,10 +26,15 @@ import { NewLandingComponent } from './components/home/new-landing/new-landing.c
 import { HomePageComponent } from './components/home/home-page/home-page.component';
 import { InjectableRxStompConfig, RxStompService, rxStompServiceFactory } from '@stomp/ng2-stompjs';
 import { myRxStompConfig } from './rx-stomp.config';
+import { AppConfig } from './app.config';
 
 export function dialogClose(dialogResult: any) {
 }
 
+export function initializeApp(appConfig: AppConfig) {
+  return () => appConfig.load();
+}
+
 @NgModule({
   declarations: [
     AppComponent,
@@ -52,6 +57,11 @@ export function dialogClose(dialogResult: any) {
     SharedModule
   ],
   providers: [
+    AppConfig,
+    { provide: APP_INITIALIZER,
+      useFactory: initializeApp,
+      deps: [AppConfig], multi: true
+    },
     {
       provide: HTTP_INTERCEPTORS,
       useClass: AuthenticationInterceptor,
diff --git a/src/app/components/home/new-landing/new-landing.component.ts b/src/app/components/home/new-landing/new-landing.component.ts
index ef110d46d..5cd13c6be 100644
--- a/src/app/components/home/new-landing/new-landing.component.ts
+++ b/src/app/components/home/new-landing/new-landing.component.ts
@@ -8,6 +8,7 @@ import { AuthenticationService } from '../../../services/http/authentication.ser
 import { User } from '../../../models/user';
 import { UserRole } from '../../../models/user-roles.enum';
 import { LoginComponent } from '../../shared/login/login.component';
+import { AppConfig } from '../../../app.config';
 
 @Component({
   selector: 'app-new-landing',
@@ -17,6 +18,7 @@ import { LoginComponent } from '../../shared/login/login.component';
 export class NewLandingComponent implements OnInit {
 
   user: User;
+  authentication = AppConfig.settings.authentication;
 
   constructor(private router: Router,
               public dialog: MatDialog,
@@ -51,7 +53,7 @@ export class NewLandingComponent implements OnInit {
     });
     dialogRef.componentInstance.role = UserRole.CREATOR;
     dialogRef.componentInstance.isStandard = false;
-    dialogRef.componentInstance.guestAllowed = false;
+    dialogRef.componentInstance.guestAllowed = this.authentication.lecturer.allowGuest;
     dialogRef.afterClosed()
       .subscribe(result => {
           if (this.user && !this.user.isGuest) {
diff --git a/src/app/models/app-config.model.ts b/src/app/models/app-config.model.ts
new file mode 100644
index 000000000..0586e7da3
--- /dev/null
+++ b/src/app/models/app-config.model.ts
@@ -0,0 +1,14 @@
+export interface IAppConfig {
+    env: {
+        name: string;
+    };
+
+    authentication: {
+        lecturer: {
+            allowGuest: boolean;
+        };
+        student: {
+            allowGuest: boolean;
+        }
+    };
+}
diff --git a/src/assets/config/config.dev.json b/src/assets/config/config.dev.json
new file mode 100644
index 000000000..bfcafa864
--- /dev/null
+++ b/src/assets/config/config.dev.json
@@ -0,0 +1,14 @@
+{
+    "env": {
+        "name": "dev"
+    },
+    
+    "authentication": {
+        "lecturer": {
+            "allowGuest": false
+        },
+        "student": {
+            "allowGuest": true
+        }
+    }
+}
diff --git a/src/assets/config/config.prod.json b/src/assets/config/config.prod.json
new file mode 100644
index 000000000..5de67db17
--- /dev/null
+++ b/src/assets/config/config.prod.json
@@ -0,0 +1,14 @@
+{
+    "env": {
+        "name": "prod"
+    },
+    
+    "authentication": {
+        "lecturer": {
+            "allowGuest": true
+        },
+        "student": {
+            "allowGuest": true
+        }
+    }
+}
diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts
index 3612073bc..3f956e160 100644
--- a/src/environments/environment.prod.ts
+++ b/src/environments/environment.prod.ts
@@ -1,3 +1,4 @@
 export const environment = {
+  name: 'prod',
   production: true
 };
diff --git a/src/environments/environment.ts b/src/environments/environment.ts
index b7f639aec..b89261c79 100644
--- a/src/environments/environment.ts
+++ b/src/environments/environment.ts
@@ -4,5 +4,6 @@
 // The list of which env maps to which file can be found in `.angular-cli.json`.
 
 export const environment = {
+  name: 'dev',
   production: false
 };
-- 
GitLab