diff --git a/src/Squidex.Domain.Users/UserManagerExtensions.cs b/src/Squidex.Domain.Users/UserManagerExtensions.cs index ad5d656f2..c38652539 100644 --- a/src/Squidex.Domain.Users/UserManagerExtensions.cs +++ b/src/Squidex.Domain.Users/UserManagerExtensions.cs @@ -54,14 +54,23 @@ namespace Squidex.Domain.Users { var user = factory.Create(email); - user.UpdateDisplayName(displayName); - user.SetPictureUrlFromGravatar(email); + try + { + user.UpdateDisplayName(displayName); + user.SetPictureUrlFromGravatar(email); - await DoChecked(() => userManager.CreateAsync(user), "Cannot create user."); + await DoChecked(() => userManager.CreateAsync(user), "Cannot create user."); - if (!string.IsNullOrWhiteSpace(password)) + if (!string.IsNullOrWhiteSpace(password)) + { + await DoChecked(() => userManager.AddPasswordAsync(user, password), "Cannot create user."); + } + } + catch { - await DoChecked(() => userManager.AddPasswordAsync(user, password), "Cannot create user."); + await userManager.DeleteAsync(user); + + throw; } return user; diff --git a/src/Squidex/app/features/apps/pages/apps-page.component.ts b/src/Squidex/app/features/apps/pages/apps-page.component.ts index 197e5bc17..4de9a5afc 100644 --- a/src/Squidex/app/features/apps/pages/apps-page.component.ts +++ b/src/Squidex/app/features/apps/pages/apps-page.component.ts @@ -24,7 +24,7 @@ import { export class AppsPageComponent implements OnInit { public addAppDialog = new ModalView(); - public apps = this.appsStore.apps.map(a => a || []); + public apps = this.appsStore.apps; constructor( private readonly appsStore: AppsStoreService diff --git a/src/Squidex/app/framework/angular/focus-on-change.directive.spec.ts b/src/Squidex/app/framework/angular/focus-on-change.directive.spec.ts index a23e86f9c..0276faf91 100644 --- a/src/Squidex/app/framework/angular/focus-on-change.directive.spec.ts +++ b/src/Squidex/app/framework/angular/focus-on-change.directive.spec.ts @@ -23,8 +23,8 @@ describe('FocusOnChangeDirective', () => { const calledElements: any[] = []; const renderer = { - invokeElementMethod: (element: any, method: any, args: any) => { - calledElements.push(element); + invokeElementMethod: (elem: any, method: any, args: any) => { + calledElements.push(elem); calledMethods.push(method); } }; diff --git a/src/Squidex/app/framework/angular/focus-on-init.directive.spec.ts b/src/Squidex/app/framework/angular/focus-on-init.directive.spec.ts index 31c3bab06..bce92105c 100644 --- a/src/Squidex/app/framework/angular/focus-on-init.directive.spec.ts +++ b/src/Squidex/app/framework/angular/focus-on-init.directive.spec.ts @@ -23,8 +23,8 @@ describe('FocusOnInitDirective', () => { const calledElements: any[] = []; const renderer = { - invokeElementMethod: (element: any, method: any, args: any) => { - calledElements.push(element); + invokeElementMethod: (elem: any, method: any, args: any) => { + calledElements.push(elem); calledMethods.push(method); } }; diff --git a/src/Squidex/app/framework/angular/root-view.directive.spec.ts b/src/Squidex/app/framework/angular/root-view.directive.spec.ts index 4ebe5dc3d..1169d1f5b 100644 --- a/src/Squidex/app/framework/angular/root-view.directive.spec.ts +++ b/src/Squidex/app/framework/angular/root-view.directive.spec.ts @@ -7,6 +7,8 @@ import { RootViewDirective } from './../'; +/* tslint:disable:no-unused-expression */ + describe('RootViewDirective', () => { it('should call init of service in ctor', () => { let viewRef = {}; diff --git a/src/Squidex/app/shared/components/app.component-base.ts b/src/Squidex/app/shared/components/app.component-base.ts index f3b807c5e..280f28f3b 100644 --- a/src/Squidex/app/shared/components/app.component-base.ts +++ b/src/Squidex/app/shared/components/app.component-base.ts @@ -27,7 +27,7 @@ export abstract class AppComponentBase extends ComponentBase { } public appNameOnce(): Observable { - return this.appName$.take(1); + return this.appName$.first(); } } diff --git a/src/Squidex/app/shared/guards/resolve-app-languages.guard.ts b/src/Squidex/app/shared/guards/resolve-app-languages.guard.ts index 88e6950d7..8dd836c19 100644 --- a/src/Squidex/app/shared/guards/resolve-app-languages.guard.ts +++ b/src/Squidex/app/shared/guards/resolve-app-languages.guard.ts @@ -39,7 +39,7 @@ export class ResolveAppLanguagesGuard implements Resolve { } return dto; - }).catch(() => { + }, error => { this.router.navigate(['/404']); return null; diff --git a/src/Squidex/app/shared/guards/resolve-content.guard.ts b/src/Squidex/app/shared/guards/resolve-content.guard.ts index 951a3df9a..2d707e66e 100644 --- a/src/Squidex/app/shared/guards/resolve-content.guard.ts +++ b/src/Squidex/app/shared/guards/resolve-content.guard.ts @@ -51,7 +51,7 @@ export class ResolveContentGuard implements Resolve { } return dto; - }).catch(() => { + }, error => { this.router.navigate(['/404']); return null; diff --git a/src/Squidex/app/shared/guards/resolve-published-schema.guard.ts b/src/Squidex/app/shared/guards/resolve-published-schema.guard.ts index dbd0cacc3..1c04ff128 100644 --- a/src/Squidex/app/shared/guards/resolve-published-schema.guard.ts +++ b/src/Squidex/app/shared/guards/resolve-published-schema.guard.ts @@ -45,7 +45,7 @@ export class ResolvePublishedSchemaGuard implements Resolve { } return dto; - }).catch(() => { + }, error => { this.router.navigate(['/404']); return null; diff --git a/src/Squidex/app/shared/guards/resolve-schema.guard.ts b/src/Squidex/app/shared/guards/resolve-schema.guard.ts index adfeac533..e6ac61945 100644 --- a/src/Squidex/app/shared/guards/resolve-schema.guard.ts +++ b/src/Squidex/app/shared/guards/resolve-schema.guard.ts @@ -45,7 +45,7 @@ export class ResolveSchemaGuard implements Resolve { } return dto; - }).catch(() => { + }, error => { this.router.navigate(['/404']); return null; diff --git a/src/Squidex/app/shared/guards/resolve-user.guard.ts b/src/Squidex/app/shared/guards/resolve-user.guard.ts index dba6b79b1..d03d98478 100644 --- a/src/Squidex/app/shared/guards/resolve-user.guard.ts +++ b/src/Squidex/app/shared/guards/resolve-user.guard.ts @@ -39,7 +39,7 @@ export class ResolveUserGuard implements Resolve { } return dto; - }).catch(() => { + }, error => { this.router.navigate(['/404']); return null; diff --git a/src/Squidex/app/shared/services/apps-store.service.spec.ts b/src/Squidex/app/shared/services/apps-store.service.spec.ts index d7f63c230..6fe17f1a0 100644 --- a/src/Squidex/app/shared/services/apps-store.service.spec.ts +++ b/src/Squidex/app/shared/services/apps-store.service.spec.ts @@ -59,36 +59,6 @@ describe('AppsStoreService', () => { appsService.verifyAll(); }); - it('should reload value from apps-service when called', () => { - authService.setup(x => x.isAuthenticated) - .returns(() => Observable.of(true)) - .verifiable(Times.once()); - - appsService.setup(x => x.getApps()) - .returns(() => Observable.of(oldApps)) - .verifiable(Times.exactly(2)); - - const store = new AppsStoreService(authService.object, appsService.object); - - let result1: AppDto[] | null = null; - let result2: AppDto[] | null = null; - - store.apps.subscribe(x => { - result1 = x; - }).unsubscribe(); - - store.reload(); - - store.apps.subscribe(x => { - result2 = x; - }).unsubscribe(); - - expect(result1).toEqual(oldApps); - expect(result2).toEqual(oldApps); - - appsService.verifyAll(); - }); - it('should add app to cache when created', () => { authService.setup(x => x.isAuthenticated) .returns(() => Observable.of(true)) diff --git a/src/Squidex/app/shared/services/apps-store.service.ts b/src/Squidex/app/shared/services/apps-store.service.ts index 56bb8b680..ab516d41f 100644 --- a/src/Squidex/app/shared/services/apps-store.service.ts +++ b/src/Squidex/app/shared/services/apps-store.service.ts @@ -6,7 +6,7 @@ */ import { Injectable } from '@angular/core'; -import { BehaviorSubject, Observable, Subject } from 'rxjs'; +import { BehaviorSubject, Observable, Observer, ReplaySubject } from 'rxjs'; import { DateTime } from 'framework'; @@ -20,70 +20,45 @@ import { AuthService } from './auth.service'; @Injectable() export class AppsStoreService { - private readonly apps$ = new Subject(); - private readonly appName$ = new BehaviorSubject(null); - private lastApps: AppDto[] | null = null; - private isAuthenticated = false; - - private readonly appsPublished$ = - this.apps$ - .distinctUntilChanged() - .publishReplay(1); - - private readonly selectedApp$ = - this.appsPublished$.combineLatest(this.appName$, (apps, name) => apps && name ? apps.find(x => x.name === name) || null : null) - .distinctUntilChanged() - .publishReplay(1); + private readonly apps$ = new ReplaySubject(); + private readonly app$ = new BehaviorSubject(null); public get apps(): Observable { - return this.appsPublished$; + return this.apps$; } public get selectedApp(): Observable { - return this.selectedApp$; + return this.app$; } constructor( private readonly authService: AuthService, private readonly appsService: AppsService ) { - if (!authService || !appsService) { + if (!appsService) { return; } - this.selectedApp$.connect(); - - this.appsPublished$.connect(); - this.appsPublished$.subscribe(apps => { - this.lastApps = apps; - }); - - this.authService.isAuthenticated.subscribe(isAuthenticated => { - this.isAuthenticated = isAuthenticated; - - if (isAuthenticated) { - this.load(); - } - }); - } - - public reload() { - if (this.isAuthenticated) { - this.load(); - } - } - - private load() { - this.appsService.getApps() + this.authService.isAuthenticated.filter(t => !!t).first() + .switchMap(() => this.appsService.getApps()) .subscribe(apps => { this.apps$.next(apps); }); } public selectApp(name: string | null): Promise { - this.appName$.next(name); + return Observable.create((observer: Observer) => { + this.apps$.subscribe(apps => { + const app = apps.find(x => x.name === name) || null; - return this.selectedApp.skip(1).map(app => app !== null).toPromise(); + this.app$.next(app); + + observer.next(app !== null); + observer.complete(); + }, error => { + observer.error(error); + }); + }).toPromise(); } public createApp(dto: CreateAppDto, now?: DateTime): Observable { @@ -91,14 +66,14 @@ export class AppsStoreService { .map(created => { now = now || DateTime.now(); - const app = new AppDto(created.id, dto.name, 'Owner', now, now); - - return app; + return new AppDto(created.id, dto.name, 'Owner', now, now); }) .do(app => { - if (this.lastApps && app) { - this.apps$.next(this.lastApps.concat([app])); - } + this.apps$.defaultIfEmpty().first().subscribe(apps => { + if (apps) { + this.apps$.next(apps.concat([app])); + } + }); }); } } \ No newline at end of file diff --git a/src/Squidex/app/shared/services/auth.service.ts b/src/Squidex/app/shared/services/auth.service.ts index 4d5616a20..69b9c68b3 100644 --- a/src/Squidex/app/shared/services/auth.service.ts +++ b/src/Squidex/app/shared/services/auth.service.ts @@ -107,11 +107,10 @@ export class AuthService { public checkLogin(): Promise { if (this.loginCompleted) { return Promise.resolve(this.currentUser !== null); - } else if (this.loginCache) { - return this.loginCache; } else { - this.loginCache = this.checkState(this.userManager.signinSilent()); - + if (!this.loginCache) { + this.loginCache = this.checkState(this.userManager.signinSilent()); + } return this.loginCache; } } diff --git a/src/Squidex/app/shell/pages/internal/apps-menu.component.html b/src/Squidex/app/shell/pages/internal/apps-menu.component.html index 557d87e12..efa40fd9b 100644 --- a/src/Squidex/app/shell/pages/internal/apps-menu.component.html +++ b/src/Squidex/app/shell/pages/internal/apps-menu.component.html @@ -5,12 +5,12 @@