Commit fa922ca922739b5160eca42977b8d2ef8a1d85de

Authored by ABNER SILVA DE OLIVEIRA
2 parents d2590816 d3439b88

Merge branch 'ngforward' of softwarepublico.gov.br:noosfero-themes/angular-theme into ngforward

src/app/components/auth/auth_controller.spec.ts
@@ -23,5 +23,6 @@ describe("AuthController", () => { @@ -23,5 +23,6 @@ describe("AuthController", () => {
23 expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials); 23 expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials);
24 24
25 }); 25 });
  26 +
26 27
27 }); 28 });
src/app/components/auth/auth_service.spec.ts
@@ -57,11 +57,21 @@ describe("Auth Service", () => { @@ -57,11 +57,21 @@ describe("Auth Service", () => {
57 57
58 expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!"); 58 expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!");
59 }); 59 });
  60 +
  61 + it("should return the current logged in user", () => {
  62 + authService.login(credentials);
  63 + $httpBackend.flush();
  64 + let actual: User = authService.currentUser();
  65 + expect(actual).toEqual(user, "The returned user must be present");
  66 + });
60 67
  68 + it("should not return the current user after logout", () => {
  69 + authService.logout();
  70 + let actual: any = authService.currentUser();
  71 + expect(actual).toEqual(undefined, "The returned user must not be defined");
  72 + });
61 }); 73 });
62 -  
63 -  
64 - 74 +
65 75
66 }); 76 });
67 77
68 \ No newline at end of file 78 \ No newline at end of file
src/app/components/auth/auth_service.ts
@@ -53,13 +53,17 @@ export class AuthService { @@ -53,13 +53,17 @@ export class AuthService {
53 } 53 }
54 54
55 public isAuthenticated() { 55 public isAuthenticated() {
56 - return !!this.session.getCurrentUser(); 56 + return !!this.session.currentUser();
  57 + }
  58 +
  59 + public currentUser(): User {
  60 + return this.session.currentUser();
57 } 61 }
58 62
59 public isAuthorized(authorizedRoles: string | string[]) { 63 public isAuthorized(authorizedRoles: string | string[]) {
60 if (!angular.isArray(authorizedRoles)) { 64 if (!angular.isArray(authorizedRoles)) {
61 authorizedRoles = [<string>authorizedRoles]; 65 authorizedRoles = [<string>authorizedRoles];
62 } 66 }
63 - return (this.isAuthenticated() && authorizedRoles.indexOf(this.session.getCurrentUser().userRole) !== -1); 67 + return (this.isAuthenticated() && authorizedRoles.indexOf(this.session.currentUser().userRole) !== -1);
64 } 68 }
65 } 69 }
66 \ No newline at end of file 70 \ No newline at end of file
src/app/components/auth/session.ts
@@ -20,7 +20,7 @@ export class Session { @@ -20,7 +20,7 @@ export class Session {
20 this.$log.debug('User session destroyed.'); 20 this.$log.debug('User session destroyed.');
21 }; 21 };
22 22
23 - getCurrentUser(): User { 23 + currentUser(): User {
24 return this.$localStorage.currentUser; 24 return this.$localStorage.currentUser;
25 }; 25 };
26 26
src/app/index.run.ts
@@ -3,8 +3,8 @@ import {Session} from &quot;./components/auth/session&quot;; @@ -3,8 +3,8 @@ import {Session} from &quot;./components/auth/session&quot;;
3 /** @ngInject */ 3 /** @ngInject */
4 export function noosferoAngularRunBlock($log: ng.ILogService, Restangular: restangular.IService, Session: Session) { 4 export function noosferoAngularRunBlock($log: ng.ILogService, Restangular: restangular.IService, Session: Session) {
5 Restangular.addFullRequestInterceptor((element: any, operation: string, route: string, url: string, headers: string) => { 5 Restangular.addFullRequestInterceptor((element: any, operation: string, route: string, url: string, headers: string) => {
6 - if (Session.getCurrentUser()) {  
7 - (<any>headers)["Private-Token"] = Session.getCurrentUser().private_token; 6 + if (Session.currentUser()) {
  7 + (<any>headers)["Private-Token"] = Session.currentUser().private_token;
8 } 8 }
9 return <any>{ headers: <any>headers }; 9 return <any>{ headers: <any>headers };
10 }); 10 });
src/app/main/main.component.ts
@@ -45,8 +45,9 @@ export class MainContent { @@ -45,8 +45,9 @@ export class MainContent {
45 component: MainContent, 45 component: MainContent,
46 name: 'main', 46 name: 'main',
47 resolve: { 47 resolve: {
48 - currentUser: function(AuthService: AuthService) {  
49 - return AuthService.loginFromCookie(); 48 + currentUser: function(AuthService: AuthService, $log: ng.ILogService) {
  49 + $log.debug("Main URL service...");
  50 + return AuthService.currentUser();
50 } 51 }
51 } 52 }
52 }, 53 },