Guest User

Untitled

a guest
Nov 6th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import { ISecurityService } from './ISecurity.service';
  2. import { NgModule, ModuleWithProviders, Optional, SkipSelf, InjectionToken } from '@angular/core';
  3. import { CommonModule } from '@angular/common';
  4. import { UserComponent } from './user/user.component';
  5.  
  6. export const SECURITY_SERVICE = new InjectionToken<ISecurityService>('security.service');
  7.  
  8. @NgModule({
  9. imports: [
  10. CommonModule
  11. ],
  12. exports: [ UserComponent ],
  13. declarations: [ UserComponent ]
  14. })
  15. export class SecurityModule {
  16.  
  17. static forRoot(securityService: ISecurityService): ModuleWithProviders {
  18. return {
  19. ngModule: SecurityModule,
  20. providers: [{
  21. provide: SECURITY_SERVICE,
  22. useValue: securityService
  23. }]
  24. };
  25. }
  26.  
  27. constructor (@Optional() @SkipSelf() parentModule: SecurityModule) {
  28. if (parentModule) {
  29. throw new Error(
  30. 'SecurityModule is already loaded. Import it in the AppModule only');
  31. }
  32. }
  33. }
  34.  
  35. import { BrowserModule } from '@angular/platform-browser';
  36. import { NgModule } from '@angular/core';
  37.  
  38. import { AppComponent } from './app.component';
  39. import { SecurityModule, SecurityMockService } from '@savantly/ngx-security';
  40.  
  41. export const mockSecurity = new SecurityMockService();
  42.  
  43. @NgModule({
  44. declarations: [
  45. AppComponent
  46. ],
  47. imports: [
  48. BrowserModule,
  49. SecurityModule.forRoot(mockSecurity)
  50. ],
  51. providers: [],
  52. bootstrap: [AppComponent]
  53. })
  54. export class AppModule { }
  55.  
  56. @Injectable()
  57. export class SecurityMockService implements ISecurityService {
  58. logout: () => void;
  59. login: (username: string, password: string) => boolean;
  60. user: IUser;
  61.  
  62. constructor() {
  63. this.user = {
  64. principal: 'demoUser',
  65. displayName: 'Demo User',
  66. authenticated: false,
  67. roles: ['ADMIN', 'USER']
  68. };
  69.  
  70. this.login = (_username: string, _password: string) => {
  71. this.user.authenticated = true;
  72. return true;
  73. };
  74.  
  75. this.logout = () => {
  76. this.user.authenticated = false;
  77. };
  78. }
  79. }
Add Comment
Please, Sign In to add comment