Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component({
- selector: 'login',
- templateUrl: './login.component.html',
- styleUrls: ['./login.component.css']
- })
- @NgModule({
- providers:[AuthenticationService]
- })
- export class LoginComponent implements OnInit {
- public user: User;
- public isUserAuthenticated = false;
- // For testing we are going to use Dummy Configuration.
- // Actual data will come from Rest Service
- private dummyConfiguration:Object={
- "user":"dummy",
- "component":[{
- "name":"CustomerComponent",
- "access":"ENABLED"
- },
- {
- "name":"InvoicingComponent",
- "access":"HIDDEN"
- }
- ]
- };
- constructor(private router: Router,private authenticationService : AuthenticationService) {
- this.user = new User();
- }
- login() {
- this.isUserAuthenticated = true;
- this.authenticationService.saveInSession(this.dummyConfiguration);
- this.router.navigate(['/site', {}]);
- }
- ngOnInit() {
- }
- }
- export class User {
- public email: string;
- public password: string;
- }
- @Component({
- selector: 'site',
- templateUrl: './site.component.html',
- styleUrls: ['./site.component.css']
- })
- export class SiteComponent extends SuperParentComponent{
- constructor(private router: Router, private authenticationService: AuthenticationService) {
- super();
- this.validateSession(router,authenticationService);
- }
- }
- export class SuperParentComponent {
- constructor(){
- }
- validateSession( router: Router, authenticationService: AuthenticationService) {
- if (!authenticationService.isUserLoggedIn()) {
- router.navigate(['/login', {}]);
- }
- }
- }
- export class AuthenticationService {
- @SessionStorage() public userConfiguration: Object;
- isAuthentic(component: string):boolean {
- if (this.isComponentAllowed(component)){
- return true;
- }
- }
- public getUserConfiguration():Object {
- return this.userConfiguration;
- }
- saveInSession(data: Object) {
- this.userConfiguration = data;
- }
- isUserLoggedIn():boolean{
- if(this.userConfiguration==null){
- return false;
- }
- return true;
- }
- isComponentAllowed(component:string){
- var result:Array<Object>;
- if(this.userConfiguration=={}){
- return false;
- }
- if(this.userConfiguration.hasOwnProperty("component")){
- result=this.userConfiguration["component"];
- for (var i = 0; i < result.length; i++) {
- var currentComponent:Object=result[i];
- if (currentComponent["name"] ==component && currentComponent["access"]== AccessType.ENABLED) {
- return true;
- }
- }
- }
- return false;
- }
- }
- enum AccessType {
- ENABLED=<any>"ENABLED",
- HIDDEN=<any>"HIDDEN"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement