Commit b9a67499 by Tuukka Kivilahti

koodit turvaan

1 parent 110ee6a0
......@@ -6,7 +6,7 @@
<div id="contentarea">
<div id="topbar">
<moya-top-menu></moya-top-menu>
<button mat-raised-button (click)="openLoginDialog()" tabindex="8">{{ "login.login" | translate}}</button>
<moya-login-dialog></moya-login-dialog>
<moya-locale></moya-locale>
</div>
<div id="content">
......
......@@ -22,25 +22,5 @@ export class AppComponent {
}
openLoginDialog(): void {
let conf = new MatDialogConfig();
conf.autoFocus = true;
/*conf.height = "800px";
conf.width = "500px";*/
let dialogRef = this.dialog.open(LoginComponent, conf);
dialogRef.componentInstance.loginEvent.subscribe((success) => {
if(success) {
dialogRef.close();
} else {
dialogRef.close();
this.router.navigateByUrl("/login");
}
});
}
}
<p>
login-dialog works!
</p>
ei toimi
<button mat-raised-button (click)="openLoginDialog()" tabindex="8">{{ "login.login" | translate}}</button>
import { Component, OnInit } from '@angular/core';
import {Component, NgZone, OnInit} from '@angular/core';
import {MatDialog, MatDialogConfig} from "@angular/material";
import {LoginComponent} from "../login-component/login.component";
import {Router} from "@angular/router";
@Component({
selector: 'moya-login-dialog',
......@@ -7,9 +10,29 @@ import { Component, OnInit } from '@angular/core';
})
export class LoginDialogComponent implements OnInit {
constructor() { }
constructor(private router: Router, private dialog: MatDialog) { }
ngOnInit() {
}
openLoginDialog(): void {
let conf = new MatDialogConfig();
conf.autoFocus = true;
/*conf.height = "800px";
conf.width = "500px";*/
let dialogRef = this.dialog.open(LoginComponent, conf);
dialogRef.componentInstance.loginEvent.subscribe((success) => {
if(success) {
dialogRef.close();
} else {
dialogRef.close();
this.router.navigateByUrl("/login");
}
});
}
}
......@@ -2,10 +2,9 @@ import {Injectable} from '@angular/core';
import gql from 'graphql-tag';
import {Permissions} from '../models/permissions.model';
import {Apollo, QueryRef} from 'apollo-angular';
import {Observable, Observer} from 'rxjs';
import {BehaviorSubject, Observable, Observer, Subject} from 'rxjs';
import {MOYA_BASE_URL, MOYA_REST_URL} from '../config/moya.config';
import {map, share} from 'rxjs/operators';
import {Vip} from '../../modules/viplist/models/vip.model';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {EventUser} from "../models/event-user.model";
......@@ -45,11 +44,11 @@ export class SessionServiceService {
private permissionsObservable: Observable<Permissions>;
private permissionsApollo: QueryRef<ApiPermissionRoot>;
private currentuserObservable: Observable<EventUser>;
private currentuserObserver: Observer<EventUser>;
private currentuserSubject: BehaviorSubject<EventUser>;
constructor(private apollo: Apollo, private http: HttpClient) {
// Permissions apollo call.
// rewrite permissions from API -format to moya-angular -format
//
......@@ -64,9 +63,12 @@ export class SessionServiceService {
.reduce((permObject, perm) => (permObject[perm.feature] = {'ADMIN': perm.admin, 'USER': perm.user, 'INFO': perm.info}), {}) as Permissions));
// currentUserObservable
Observable.create()
this.currentuserObservable = this.http.get<EventUser>(MOYA_REST_URL + "/v2/user/current").pipe(share());
// make subject to share the current user.
// This way we can later on update it to everyone who is listening this beautiful observable
this.currentuserSubject = new BehaviorSubject(null);
this.updateCurrentUser();
}
......@@ -75,12 +77,27 @@ export class SessionServiceService {
}
public getCurrentUser() {
/**
* Returns observable with current user. If we are not logged in, current user is Anonymous.
*
* If there is any change of user status, new user is automatically pushed into this observable.
*/
public getCurrentUser(): Observable<EventUser> {
return this.currentuserSubject;
}
/**
* Request update of current user.
*
* This will push currently logged in user into currentUser -observable.
*/
public updateCurrentUser() {
this.currentuserObservable.
this.http.get<EventUser>(MOYA_REST_URL + "/v2/user/current").subscribe(
(currentUser) => {
this.currentuserSubject.next(currentUser);
});
}
......@@ -107,9 +124,11 @@ export class SessionServiceService {
apolloObservable.subscribe((x) => {
this.permissionsApollo.refetch();
this.updateCurrentUser();
});
return apolloObservable;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!