diff --git a/src/app/components/auth/auth.service.js b/src/app/components/auth/auth.service.js deleted file mode 100644 index c6667fd..0000000 --- a/src/app/components/auth/auth.service.js +++ /dev/null @@ -1,88 +0,0 @@ -(function() { - 'use strict'; - - angular - .module('noosferoApp') - .factory('Session', Session) - .factory('AuthService', AuthService); - - /** @ngInject */ - function AuthService($q, $http, $rootScope, Session, $log, AUTH_EVENTS) { - - function login (credentials) { - var url = '/api/v1/login'; - var encodedData = 'login=' + credentials.username + '&password=' + credentials.password; - return $http.post(url, encodedData).then(loginSuccessCallback, loginFailedCallback); - } - - function loginFromCookie() { - var url = '/api/v1/login_from_cookie'; - return $http.post(url).then(loginSuccessCallback, loginFailedCallback); - } - - function loginSuccessCallback(response) { - $log.debug('AuthService.login [SUCCESS] response', response); - var currentUser = Session.create(response.data); - $rootScope.currentUser = currentUser; - $rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser); - return currentUser; - } - - function loginFailedCallback(response) { - $log.debug('AuthService.login [FAIL] response', response); - $rootScope.$broadcast(AUTH_EVENTS.loginFailed); - // return $q.reject(response); - return null; - } - - function logout () { - Session.destroy(); - $rootScope.currentUser = undefined; - $rootScope.$broadcast(AUTH_EVENTS.logoutSuccess); - $http.jsonp('/account/logout'); //FIXME logout from noosfero to sync login state - } - - function isAuthenticated () { - return !!Session.userId; - } - - function isAuthorized (authorizedRoles) { - if (!angular.isArray(authorizedRoles)) { - authorizedRoles = [authorizedRoles]; - } - return (service.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1); - } - - var service = { - login: login, - loginFromCookie: loginFromCookie, - logout: logout, - isAuthenticated: isAuthenticated, - isAuthorized: isAuthorized - }; - return service; - } - - /** @ngInject */ - function Session($localStorage, $log) { - var service = {}; - - service.create = function(data) { - $localStorage.currentUser = data.user; - $log.debug('User session created.', $localStorage.currentUser); - return $localStorage.currentUser; - }; - - service.destroy = function() { - delete $localStorage.currentUser; - $log.debug('User session destroyed.'); - }; - - service.getCurrentUser = function () { - return $localStorage.currentUser; - }; - - return service; - } - -})(); diff --git a/src/app/components/auth/auth_events.ts b/src/app/components/auth/auth_events.ts new file mode 100644 index 0000000..db69755 --- /dev/null +++ b/src/app/components/auth/auth_events.ts @@ -0,0 +1,13 @@ + + +export interface IAuthEvents { + loginSuccess: string; + loginFailed: string; + logoutSuccess: string; +} + +export const AUTH_EVENTS: IAuthEvents = { + loginSuccess: "auth-login-success", + loginFailed: "auth-login-failed", + logoutSuccess: "auth-logout-success" +}; \ No newline at end of file diff --git a/src/app/components/auth/auth_service.ts b/src/app/components/auth/auth_service.ts new file mode 100644 index 0000000..fc76393 --- /dev/null +++ b/src/app/components/auth/auth_service.ts @@ -0,0 +1,58 @@ +import {Injectable, Inject} from "ng-forward"; + +import {Credentials} from "./../../models/interfaces"; +import {Session} from "./session"; +import {NoosferoRootScope} from "./noosfero_root_scope"; +import {AUTH_EVENTS, IAuthEvents} from "./auth_events"; + +@Injectable +@Inject("$q", "$http", "$rootScope", Session, "$log", "AUTH_EVENTS") +export class AuthService { + constructor(private $q: ng.IQService, + private $http: ng.IHttpService, + private $rootScope: NoosferoRootScope, + private Session: Session, + private $log: ng.ILogService, + private AUTH_EVENTS: IAuthEvents) { + + } + + private loginSuccessCallback(response) { + this.$log.debug('AuthService.login [SUCCESS] response', response); + let currentUser = this.Session.create(response.data); + this.$rootScope.currentUser = currentUser; + this.$rootScope.$broadcast(this.AUTH_EVENTS.loginSuccess, currentUser); + return currentUser; + } + + login(credentials: Credentials) { + let url = '/api/v1/login'; + let encodedData = 'login=' + credentials.username + '&password=' + credentials.password; + return this.$http.post(url, encodedData).then(this.loginSuccessCallback, this.loginFailedCallback); + } + + private loginFailedCallback(response) { + this.$log.debug('AuthService.login [FAIL] response', response); + this.$rootScope.$broadcast(AUTH_EVENTS.loginFailed); + // return $q.reject(response); + return null; + } + + public logout() { + this.Session.destroy(); + this.$rootScope.currentUser = undefined; + this.$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess); + this.$http.jsonp('/account/logout'); //FIXME logout from noosfero to sync login state + } + + public isAuthenticated() { + return !!this.Session.getCurrentUser(); + } + + public isAuthorized(authorizedRoles) { + if (!angular.isArray(authorizedRoles)) { + authorizedRoles = [authorizedRoles]; + } + return (this.isAuthenticated() && authorizedRoles.indexOf(this.Session.getCurrentUser().userRole) !== -1); + } +} \ No newline at end of file diff --git a/src/app/components/auth/noosfero_root_scope.ts b/src/app/components/auth/noosfero_root_scope.ts new file mode 100644 index 0000000..39a72b8 --- /dev/null +++ b/src/app/components/auth/noosfero_root_scope.ts @@ -0,0 +1,5 @@ +import {User} from "./../../models/interfaces"; + +export interface NoosferoRootScope extends ng.IScope { + currentUser: User; +} \ No newline at end of file diff --git a/src/app/components/auth/session.ts b/src/app/components/auth/session.ts new file mode 100644 index 0000000..80b39a3 --- /dev/null +++ b/src/app/components/auth/session.ts @@ -0,0 +1,25 @@ +import {Injectable, Inject} from "ng-forward"; + +@Injectable() +@Inject("$localStorage", "$log") +export class Session { + constructor(private $localStorage, $log) { + + } + + create = function(data) { + this.$localStorage.currentUser = data.user; + this.$log.debug('User session created.', this.$localStorage.currentUser); + return this.$localStorage.currentUser; + }; + + destroy = function() { + delete this.$localStorage.currentUser; + this.$log.debug('User session destroyed.'); + }; + + getCurrentUser = function() { + return this.$localStorage.currentUser; + }; + +} \ No newline at end of file diff --git a/src/app/components/noosfero-articles/article/article.directive.spec.ts b/src/app/components/noosfero-articles/article/article.directive.spec.ts index f45d554..98a567f 100644 --- a/src/app/components/noosfero-articles/article/article.directive.spec.ts +++ b/src/app/components/noosfero-articles/article/article.directive.spec.ts @@ -12,7 +12,8 @@ const htmlTemplate: string = ' { - + + // the karma preprocessor html2js transform the templates html into js files which put // the templates to the templateCache into the module templates // we need to load the module templates here as the template for the @@ -32,22 +33,26 @@ describe("Article Directive", () => { } // uses the TestComponentBuilder instance to initialize the component - tcb - .createAsync(ArticleContainerComponent).then(fixture => { - // and here we can inspect and run the test assertions - let myComponent: ArticleDirective = fixture.componentInstance; + tcb.createAsync(ArticleContainerComponent).then((fixture) => { + // and here we can inspect and run the test assertions + let myComponent: ArticleContainerComponent = fixture.componentInstance; + + console.log(myComponent); - // assure the article object inside the ArticleDirective matches - // the provided through the parent component - expect(myComponent.article.type).toEqual("anyArticleType"); - expect(myComponent.profile.name).toEqual("profile-name"); + // assure the article object inside the ArticleDirective matches + // the provided through the parent component + expect(myComponent.article.type).toEqual("anyArticleType"); + expect(myComponent.profile.name).toEqual("profile-name"); - // done needs to be called (it isn't really needed, as we can read in - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) - // because createAsync in ng-forward is not really async, but as the intention - // here is write tests in angular 2 ways, this is recommended + myComponent.metodoAsync(() => { done(); }); + // done needs to be called (it isn't really needed, as we can read in + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) + // because createAsync in ng-forward is not really async, but as the intention + // here is write tests in angular 2 ways, this is recommended + done(); + }); }); @@ -69,13 +74,12 @@ describe("Article Directive", () => { constructor() { } } - tcb - .createAsync(CustomArticleType).then(fixture => { - let myComponent: CustomArticleType = fixture.componentInstance; - expect(myComponent.article.type).toEqual("TinyMceArticle"); - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); - done(); - }); + tcb.createAsync(CustomArticleType).then(fixture => { + let myComponent: CustomArticleType = fixture.componentInstance; + expect(myComponent.article.type).toEqual("TinyMceArticle"); + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); + done(); + }); }); }); \ No newline at end of file diff --git a/src/app/components/noosfero-articles/article/article.directive.ts b/src/app/components/noosfero-articles/article/article.directive.ts index 212d23e..08d8d46 100644 --- a/src/app/components/noosfero-articles/article/article.directive.ts +++ b/src/app/components/noosfero-articles/article/article.directive.ts @@ -10,14 +10,11 @@ export class ArticleView { @Input() article: any; @Input() profile: any; - constructor() { - - } } @Component({ selector: 'noosfero-article', - template: '
', + template: 'not-used', directives: [ArticleView, NoosferoArticleBlog] }) @Inject("$element", "$scope", "$injector", "$compile") @@ -36,7 +33,11 @@ export class ArticleDirective { this.$element.replaceWith(this.$compile('<' + this.directiveName + ' [article]="ctrl.article" [profile]="ctrl.profile">')(this.$scope)); } - constructor(private $element: any, private $scope: ng.IScope, private $injector: ng.auto.IInjectorService, private $compile: ng.ICompileService) { + constructor( + private $element: any, + private $scope: ng.IScope, + private $injector: ng.auto.IInjectorService, + private $compile: ng.ICompileService) { } } diff --git a/src/app/index.ts b/src/app/index.ts index 1ae43d0..c45273b 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -16,7 +16,7 @@ import {Cms as noosferoCms} from "./cms/cms.component"; import {Main} from "./main/main.component"; import {bootstrap, bundle} from "ng-forward"; - +import {AUTH_EVENTS} from "./components/auth/auth_events"; declare var moment: any; @@ -28,12 +28,9 @@ let noosferoApp: any = bundle("noosferoApp", Main, ["ngAnimate", "ngCookies", "n NoosferoApp.angularModule = noosferoApp; + NoosferoApp.addConstants("moment", moment); -NoosferoApp.addConstants("AUTH_EVENTS", { - loginSuccess: "auth-login-success", - loginFailed: "auth-login-failed", - logoutSuccess: "auth-logout-success" -}); +NoosferoApp.addConstants("AUTH_EVENTS", AUTH_EVENTS); NoosferoApp.addConfig(noosferoModuleConfig); NoosferoApp.run(noosferoAngularRunBlock); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index 04203e2..0419d27 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -15,7 +15,7 @@ export class MainContent { @Component({ selector: 'main', - template: '
', + template: 'not-used', directives: [NoosferoArticleBlog, ArticleDirective, Boxes, Block] }) @StateConfig([ diff --git a/src/app/models/interfaces.ts b/src/app/models/interfaces.ts index e850fa1..5bef04b 100644 --- a/src/app/models/interfaces.ts +++ b/src/app/models/interfaces.ts @@ -21,3 +21,15 @@ export interface TynyMceArticle extends Article { export interface Blog extends Article { id: number; } + +export interface Credentials { + username: string; + password: string; +} + +export interface User { + id: number; + login: string; + email: string; + person: Person; +} -- libgit2 0.21.2