From d3439b8891511935448c1a1aedf9a7989252e369 Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Fri, 4 Mar 2016 09:28:07 -0300 Subject: [PATCH] Changed auth service current user method --- src/app/components/auth/auth_controller.spec.ts | 1 + src/app/components/auth/auth_service.spec.ts | 16 +++++++++++++--- src/app/components/auth/auth_service.ts | 8 ++++++-- src/app/components/auth/session.ts | 2 +- src/app/index.run.ts | 4 ++-- src/app/main/main.component.ts | 5 +++-- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/app/components/auth/auth_controller.spec.ts b/src/app/components/auth/auth_controller.spec.ts index ab31e0e..04572c7 100644 --- a/src/app/components/auth/auth_controller.spec.ts +++ b/src/app/components/auth/auth_controller.spec.ts @@ -23,5 +23,6 @@ describe("AuthController", () => { expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials); }); + }); diff --git a/src/app/components/auth/auth_service.spec.ts b/src/app/components/auth/auth_service.spec.ts index 5b784ae..5a10ed3 100644 --- a/src/app/components/auth/auth_service.spec.ts +++ b/src/app/components/auth/auth_service.spec.ts @@ -57,11 +57,21 @@ describe("Auth Service", () => { expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!"); }); + + it("should return the current logged in user", () => { + authService.login(credentials); + $httpBackend.flush(); + let actual: User = authService.currentUser(); + expect(actual).toEqual(user, "The returned user must be present"); + }); + it("should not return the current user after logout", () => { + authService.logout(); + let actual: any = authService.currentUser(); + expect(actual).toEqual(undefined, "The returned user must not be defined"); + }); }); - - - + }); \ No newline at end of file diff --git a/src/app/components/auth/auth_service.ts b/src/app/components/auth/auth_service.ts index 94edc1a..95fad2d 100644 --- a/src/app/components/auth/auth_service.ts +++ b/src/app/components/auth/auth_service.ts @@ -53,13 +53,17 @@ export class AuthService { } public isAuthenticated() { - return !!this.session.getCurrentUser(); + return !!this.session.currentUser(); + } + + public currentUser(): User { + return this.session.currentUser(); } public isAuthorized(authorizedRoles: string | string[]) { if (!angular.isArray(authorizedRoles)) { authorizedRoles = [authorizedRoles]; } - return (this.isAuthenticated() && authorizedRoles.indexOf(this.session.getCurrentUser().userRole) !== -1); + return (this.isAuthenticated() && authorizedRoles.indexOf(this.session.currentUser().userRole) !== -1); } } \ No newline at end of file diff --git a/src/app/components/auth/session.ts b/src/app/components/auth/session.ts index ce818f4..24b5f0d 100644 --- a/src/app/components/auth/session.ts +++ b/src/app/components/auth/session.ts @@ -20,7 +20,7 @@ export class Session { this.$log.debug('User session destroyed.'); }; - getCurrentUser(): User { + currentUser(): User { return this.$localStorage.currentUser; }; diff --git a/src/app/index.run.ts b/src/app/index.run.ts index 27038c0..e23172f 100644 --- a/src/app/index.run.ts +++ b/src/app/index.run.ts @@ -3,8 +3,8 @@ import {Session} from "./components/auth/session"; /** @ngInject */ export function noosferoAngularRunBlock($log: ng.ILogService, Restangular: restangular.IService, Session: Session) { Restangular.addFullRequestInterceptor((element: any, operation: string, route: string, url: string, headers: string) => { - if (Session.getCurrentUser()) { - (headers)["Private-Token"] = Session.getCurrentUser().private_token; + if (Session.currentUser()) { + (headers)["Private-Token"] = Session.currentUser().private_token; } return { headers: headers }; }); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index d899a2e..c15a75d 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -45,8 +45,9 @@ export class MainContent { component: MainContent, name: 'main', resolve: { - currentUser: function(AuthService: AuthService) { - return AuthService.loginFromCookie(); + currentUser: function(AuthService: AuthService, $log: ng.ILogService) { + $log.debug("Main URL service..."); + return AuthService.currentUser(); } } }, -- libgit2 0.21.2