Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. @Component({
  2. selector: 'login',
  3. templateUrl: './login.component.html',
  4. styleUrls: ['./login.component.css']
  5. })
  6.  
  7. @NgModule({
  8. providers:[AuthenticationService]
  9. })
  10. export class LoginComponent implements OnInit {
  11. public user: User;
  12. public isUserAuthenticated = false;
  13. // For testing we are going to use Dummy Configuration.
  14. // Actual data will come from Rest Service
  15.  
  16.  
  17. private dummyConfiguration:Object={
  18. "user":"dummy",
  19. "component":[{
  20. "name":"CustomerComponent",
  21. "access":"ENABLED"
  22. },
  23. {
  24. "name":"InvoicingComponent",
  25. "access":"HIDDEN"
  26.  
  27. }
  28.  
  29. ]
  30. };
  31.  
  32. constructor(private router: Router,private authenticationService : AuthenticationService) {
  33. this.user = new User();
  34. }
  35.  
  36. login() {
  37. this.isUserAuthenticated = true;
  38. this.authenticationService.saveInSession(this.dummyConfiguration);
  39. this.router.navigate(['/site', {}]);
  40. }
  41.  
  42. ngOnInit() {
  43. }
  44. }
  45.  
  46. export class User {
  47. public email: string;
  48. public password: string;
  49. }
  50.  
  51. @Component({
  52. selector: 'site',
  53. templateUrl: './site.component.html',
  54. styleUrls: ['./site.component.css']
  55. })
  56. export class SiteComponent extends SuperParentComponent{
  57. constructor(private router: Router, private authenticationService: AuthenticationService) {
  58. super();
  59. this.validateSession(router,authenticationService);
  60. }
  61. }
  62.  
  63. export class SuperParentComponent {
  64.  
  65. constructor(){
  66. }
  67. validateSession( router: Router, authenticationService: AuthenticationService) {
  68. if (!authenticationService.isUserLoggedIn()) {
  69. router.navigate(['/login', {}]);
  70. }
  71. }
  72. }
  73.  
  74. export class AuthenticationService {
  75.  
  76.  
  77. @SessionStorage() public userConfiguration: Object;
  78.  
  79. isAuthentic(component: string):boolean {
  80. if (this.isComponentAllowed(component)){
  81. return true;
  82. }
  83. }
  84.  
  85. public getUserConfiguration():Object {
  86. return this.userConfiguration;
  87. }
  88.  
  89. saveInSession(data: Object) {
  90. this.userConfiguration = data;
  91. }
  92.  
  93. isUserLoggedIn():boolean{
  94. if(this.userConfiguration==null){
  95. return false;
  96. }
  97. return true;
  98. }
  99.  
  100. isComponentAllowed(component:string){
  101. var result:Array<Object>;
  102. if(this.userConfiguration=={}){
  103. return false;
  104. }
  105. if(this.userConfiguration.hasOwnProperty("component")){
  106. result=this.userConfiguration["component"];
  107. for (var i = 0; i < result.length; i++) {
  108. var currentComponent:Object=result[i];
  109. if (currentComponent["name"] ==component && currentComponent["access"]== AccessType.ENABLED) {
  110. return true;
  111. }
  112. }
  113. }
  114. return false;
  115.  
  116.  
  117. }
  118.  
  119. }
  120.  
  121. enum AccessType {
  122. ENABLED=<any>"ENABLED",
  123. HIDDEN=<any>"HIDDEN"
  124.  
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement