Skip to content

Commit

Permalink
refractor: core module (openMF#289)
Browse files Browse the repository at this point in the history
* Shift services into their own folders.
* Add comments.

Fixes #288
  • Loading branch information
abhaychawla authored Aug 11, 2018
1 parent 1526235 commit 90087bf
Show file tree
Hide file tree
Showing 37 changed files with 1,441 additions and 1,247 deletions.
7 changes: 7 additions & 0 deletions src/app/core/alert/alert.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Alert model.
*/
export interface Alert {
type: string;
message: string;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
/** Angular Imports */
import { Injectable, EventEmitter } from '@angular/core';

export interface Alert {
type: string;
message: string;
}
/** Custom Model */
import { Alert } from './alert.model';

/**
* Alert service.
*/
@Injectable({
providedIn: 'root'
})
export class AlertService {

/** Alert event. */
public alertEvent: EventEmitter<Alert>;

/**
* Initializes alert event.
*/
constructor() {
this.alertEvent = new EventEmitter();
}

/**
* Emits an alert event.
* @param {Alert} alertEvent Alert event.
*/
alert(alertEvent: Alert) {
this.alertEvent.emit(alertEvent);
}
Expand Down
17 changes: 16 additions & 1 deletion src/app/core/authentication/authentication.guard.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
/** Angular Imports */
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';

import { Logger } from '../logger.service';
/** Custom Services */
import { Logger } from '../logger/logger.service';
import { AuthenticationService } from './authentication.service';

/** Initialize logger */
const log = new Logger('AuthenticationGuard');

/**
* Route access authorization.
*/
@Injectable()
export class AuthenticationGuard implements CanActivate {

/**
* @param {Router} router Router for navigation.
* @param {AuthenticationService} authenticationService Authentication Service.
*/
constructor(private router: Router,
private authenticationService: AuthenticationService) { }

/**
* Ensures route access is authorized only when user is authenticated, otherwise redirects to login.
*
* @returns {boolean} True if user is authenticated.
*/
canActivate(): boolean {
if (this.authenticationService.isAuthenticated()) {
return true;
Expand Down
39 changes: 34 additions & 5 deletions src/app/core/authentication/authentication.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,74 @@
/** Angular Imports */
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';

/** rxjs Imports */
import { Observable } from 'rxjs';

/** Environment Configuration */
import { environment } from '../../../environments/environment';

/** Http request options headers. */
const httpOptions = {
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Fineract-Platform-TenantId': environment.fineractPlatformTenantId
}
};

/** Authorization header. */
const authorizationHeader = 'Authorization';
/** Two factor access token header. */
const twoFactorAccessTokenHeader = 'Fineract-Platform-TFA-Token';

/**
* Http Request interceptor to set the request headers.
*/
@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {

constructor() {}

/**
* Intercepts a Http request and sets the request headers.
*/
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({ setHeaders: httpOptions.headers });
return next.handle(request);
}

/**
* Sets the basic/oauth authorization header depending on the configuration.
* @param {string} authenticationKey Authentication key.
*/
setAuthorizationToken(authenticationKey: string) {
if (environment.oauth.enabled) {
httpOptions.headers['Authorization'] = `Bearer ${authenticationKey}`;
httpOptions.headers[authorizationHeader] = `Bearer ${authenticationKey}`;
} else {
httpOptions.headers['Authorization'] = `Basic ${authenticationKey}`;
httpOptions.headers[authorizationHeader] = `Basic ${authenticationKey}`;
}
}

/**
* Sets the two factor access token header.
* @param {string} twoFactorAccessToken Two factor access token.
*/
setTwoFactorAccessToken(twoFactorAccessToken: string) {
httpOptions.headers['Fineract-Platform-TFA-Token'] = twoFactorAccessToken;
httpOptions.headers[twoFactorAccessTokenHeader] = twoFactorAccessToken;
}

/**
* Removes the authorization header.
*/
removeAuthorization() {
delete httpOptions.headers['Authorization'];
delete httpOptions.headers[authorizationHeader];
}

/**
* Removes the two factor access token header.
*/
removeTwoFactorAuthorization() {
delete httpOptions.headers['Fineract-Platform-TFA-Token'];
delete httpOptions.headers[twoFactorAccessTokenHeader];
}

}
Loading

0 comments on commit 90087bf

Please sign in to comment.