Commit 17b9833a0c4d8946126a1d7a9335867918cc35dd
1 parent
bc8f0242
Exists in
master
and in
35 other branches
started refactory of auth-service
Showing
10 changed files
with
147 additions
and
120 deletions
Show diff stats
src/app/components/auth/auth.service.js
| ... | ... | @@ -1,88 +0,0 @@ |
| 1 | -(function() { | |
| 2 | - 'use strict'; | |
| 3 | - | |
| 4 | - angular | |
| 5 | - .module('noosferoApp') | |
| 6 | - .factory('Session', Session) | |
| 7 | - .factory('AuthService', AuthService); | |
| 8 | - | |
| 9 | - /** @ngInject */ | |
| 10 | - function AuthService($q, $http, $rootScope, Session, $log, AUTH_EVENTS) { | |
| 11 | - | |
| 12 | - function login (credentials) { | |
| 13 | - var url = '/api/v1/login'; | |
| 14 | - var encodedData = 'login=' + credentials.username + '&password=' + credentials.password; | |
| 15 | - return $http.post(url, encodedData).then(loginSuccessCallback, loginFailedCallback); | |
| 16 | - } | |
| 17 | - | |
| 18 | - function loginFromCookie() { | |
| 19 | - var url = '/api/v1/login_from_cookie'; | |
| 20 | - return $http.post(url).then(loginSuccessCallback, loginFailedCallback); | |
| 21 | - } | |
| 22 | - | |
| 23 | - function loginSuccessCallback(response) { | |
| 24 | - $log.debug('AuthService.login [SUCCESS] response', response); | |
| 25 | - var currentUser = Session.create(response.data); | |
| 26 | - $rootScope.currentUser = currentUser; | |
| 27 | - $rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser); | |
| 28 | - return currentUser; | |
| 29 | - } | |
| 30 | - | |
| 31 | - function loginFailedCallback(response) { | |
| 32 | - $log.debug('AuthService.login [FAIL] response', response); | |
| 33 | - $rootScope.$broadcast(AUTH_EVENTS.loginFailed); | |
| 34 | - // return $q.reject(response); | |
| 35 | - return null; | |
| 36 | - } | |
| 37 | - | |
| 38 | - function logout () { | |
| 39 | - Session.destroy(); | |
| 40 | - $rootScope.currentUser = undefined; | |
| 41 | - $rootScope.$broadcast(AUTH_EVENTS.logoutSuccess); | |
| 42 | - $http.jsonp('/account/logout'); //FIXME logout from noosfero to sync login state | |
| 43 | - } | |
| 44 | - | |
| 45 | - function isAuthenticated () { | |
| 46 | - return !!Session.userId; | |
| 47 | - } | |
| 48 | - | |
| 49 | - function isAuthorized (authorizedRoles) { | |
| 50 | - if (!angular.isArray(authorizedRoles)) { | |
| 51 | - authorizedRoles = [authorizedRoles]; | |
| 52 | - } | |
| 53 | - return (service.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1); | |
| 54 | - } | |
| 55 | - | |
| 56 | - var service = { | |
| 57 | - login: login, | |
| 58 | - loginFromCookie: loginFromCookie, | |
| 59 | - logout: logout, | |
| 60 | - isAuthenticated: isAuthenticated, | |
| 61 | - isAuthorized: isAuthorized | |
| 62 | - }; | |
| 63 | - return service; | |
| 64 | - } | |
| 65 | - | |
| 66 | - /** @ngInject */ | |
| 67 | - function Session($localStorage, $log) { | |
| 68 | - var service = {}; | |
| 69 | - | |
| 70 | - service.create = function(data) { | |
| 71 | - $localStorage.currentUser = data.user; | |
| 72 | - $log.debug('User session created.', $localStorage.currentUser); | |
| 73 | - return $localStorage.currentUser; | |
| 74 | - }; | |
| 75 | - | |
| 76 | - service.destroy = function() { | |
| 77 | - delete $localStorage.currentUser; | |
| 78 | - $log.debug('User session destroyed.'); | |
| 79 | - }; | |
| 80 | - | |
| 81 | - service.getCurrentUser = function () { | |
| 82 | - return $localStorage.currentUser; | |
| 83 | - }; | |
| 84 | - | |
| 85 | - return service; | |
| 86 | - } | |
| 87 | - | |
| 88 | -})(); |
| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | + | |
| 2 | + | |
| 3 | +export interface IAuthEvents { | |
| 4 | + loginSuccess: string; | |
| 5 | + loginFailed: string; | |
| 6 | + logoutSuccess: string; | |
| 7 | +} | |
| 8 | + | |
| 9 | +export const AUTH_EVENTS: IAuthEvents = { | |
| 10 | + loginSuccess: "auth-login-success", | |
| 11 | + loginFailed: "auth-login-failed", | |
| 12 | + logoutSuccess: "auth-logout-success" | |
| 13 | +}; | |
| 0 | 14 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,58 @@ |
| 1 | +import {Injectable, Inject} from "ng-forward"; | |
| 2 | + | |
| 3 | +import {Credentials} from "./../../models/interfaces"; | |
| 4 | +import {Session} from "./session"; | |
| 5 | +import {NoosferoRootScope} from "./noosfero_root_scope"; | |
| 6 | +import {AUTH_EVENTS, IAuthEvents} from "./auth_events"; | |
| 7 | + | |
| 8 | +@Injectable | |
| 9 | +@Inject("$q", "$http", "$rootScope", Session, "$log", "AUTH_EVENTS") | |
| 10 | +export class AuthService { | |
| 11 | + constructor(private $q: ng.IQService, | |
| 12 | + private $http: ng.IHttpService, | |
| 13 | + private $rootScope: NoosferoRootScope, | |
| 14 | + private Session: Session, | |
| 15 | + private $log: ng.ILogService, | |
| 16 | + private AUTH_EVENTS: IAuthEvents) { | |
| 17 | + | |
| 18 | + } | |
| 19 | + | |
| 20 | + private loginSuccessCallback(response) { | |
| 21 | + this.$log.debug('AuthService.login [SUCCESS] response', response); | |
| 22 | + let currentUser = this.Session.create(response.data); | |
| 23 | + this.$rootScope.currentUser = currentUser; | |
| 24 | + this.$rootScope.$broadcast(this.AUTH_EVENTS.loginSuccess, currentUser); | |
| 25 | + return currentUser; | |
| 26 | + } | |
| 27 | + | |
| 28 | + login(credentials: Credentials) { | |
| 29 | + let url = '/api/v1/login'; | |
| 30 | + let encodedData = 'login=' + credentials.username + '&password=' + credentials.password; | |
| 31 | + return this.$http.post(url, encodedData).then(this.loginSuccessCallback, this.loginFailedCallback); | |
| 32 | + } | |
| 33 | + | |
| 34 | + private loginFailedCallback(response) { | |
| 35 | + this.$log.debug('AuthService.login [FAIL] response', response); | |
| 36 | + this.$rootScope.$broadcast(AUTH_EVENTS.loginFailed); | |
| 37 | + // return $q.reject(response); | |
| 38 | + return null; | |
| 39 | + } | |
| 40 | + | |
| 41 | + public logout() { | |
| 42 | + this.Session.destroy(); | |
| 43 | + this.$rootScope.currentUser = undefined; | |
| 44 | + this.$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess); | |
| 45 | + this.$http.jsonp('/account/logout'); //FIXME logout from noosfero to sync login state | |
| 46 | + } | |
| 47 | + | |
| 48 | + public isAuthenticated() { | |
| 49 | + return !!this.Session.getCurrentUser(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public isAuthorized(authorizedRoles) { | |
| 53 | + if (!angular.isArray(authorizedRoles)) { | |
| 54 | + authorizedRoles = [authorizedRoles]; | |
| 55 | + } | |
| 56 | + return (this.isAuthenticated() && authorizedRoles.indexOf(this.Session.getCurrentUser().userRole) !== -1); | |
| 57 | + } | |
| 58 | +} | |
| 0 | 59 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,25 @@ |
| 1 | +import {Injectable, Inject} from "ng-forward"; | |
| 2 | + | |
| 3 | +@Injectable() | |
| 4 | +@Inject("$localStorage", "$log") | |
| 5 | +export class Session { | |
| 6 | + constructor(private $localStorage, $log) { | |
| 7 | + | |
| 8 | + } | |
| 9 | + | |
| 10 | + create = function(data) { | |
| 11 | + this.$localStorage.currentUser = data.user; | |
| 12 | + this.$log.debug('User session created.', this.$localStorage.currentUser); | |
| 13 | + return this.$localStorage.currentUser; | |
| 14 | + }; | |
| 15 | + | |
| 16 | + destroy = function() { | |
| 17 | + delete this.$localStorage.currentUser; | |
| 18 | + this.$log.debug('User session destroyed.'); | |
| 19 | + }; | |
| 20 | + | |
| 21 | + getCurrentUser = function() { | |
| 22 | + return this.$localStorage.currentUser; | |
| 23 | + }; | |
| 24 | + | |
| 25 | +} | |
| 0 | 26 | \ No newline at end of file | ... | ... |
src/app/components/noosfero-articles/article/article.directive.spec.ts
| ... | ... | @@ -12,7 +12,8 @@ const htmlTemplate: string = '<noosfero-article [article]="ctrl.article" [profil |
| 12 | 12 | |
| 13 | 13 | |
| 14 | 14 | describe("Article Directive", () => { |
| 15 | - | |
| 15 | + | |
| 16 | + | |
| 16 | 17 | // the karma preprocessor html2js transform the templates html into js files which put |
| 17 | 18 | // the templates to the templateCache into the module templates |
| 18 | 19 | // we need to load the module templates here as the template for the |
| ... | ... | @@ -32,22 +33,26 @@ describe("Article Directive", () => { |
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | // uses the TestComponentBuilder instance to initialize the component |
| 35 | - tcb | |
| 36 | - .createAsync(ArticleContainerComponent).then(fixture => { | |
| 37 | - // and here we can inspect and run the test assertions | |
| 38 | - let myComponent: ArticleDirective = fixture.componentInstance; | |
| 36 | + tcb.createAsync(ArticleContainerComponent).then((fixture) => { | |
| 37 | + // and here we can inspect and run the test assertions | |
| 38 | + let myComponent: ArticleContainerComponent = fixture.componentInstance; | |
| 39 | + | |
| 40 | + console.log(myComponent); | |
| 39 | 41 | |
| 40 | - // assure the article object inside the ArticleDirective matches | |
| 41 | - // the provided through the parent component | |
| 42 | - expect(myComponent.article.type).toEqual("anyArticleType"); | |
| 43 | - expect(myComponent.profile.name).toEqual("profile-name"); | |
| 42 | + // assure the article object inside the ArticleDirective matches | |
| 43 | + // the provided through the parent component | |
| 44 | + expect(myComponent.article.type).toEqual("anyArticleType"); | |
| 45 | + expect(myComponent.profile.name).toEqual("profile-name"); | |
| 44 | 46 | |
| 45 | - // done needs to be called (it isn't really needed, as we can read in | |
| 46 | - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) | |
| 47 | - // because createAsync in ng-forward is not really async, but as the intention | |
| 48 | - // here is write tests in angular 2 ways, this is recommended | |
| 47 | + myComponent.metodoAsync(() => { | |
| 49 | 48 | done(); |
| 50 | 49 | }); |
| 50 | + // done needs to be called (it isn't really needed, as we can read in | |
| 51 | + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) | |
| 52 | + // because createAsync in ng-forward is not really async, but as the intention | |
| 53 | + // here is write tests in angular 2 ways, this is recommended | |
| 54 | + done(); | |
| 55 | + }); | |
| 51 | 56 | }); |
| 52 | 57 | |
| 53 | 58 | |
| ... | ... | @@ -69,13 +74,12 @@ describe("Article Directive", () => { |
| 69 | 74 | constructor() { |
| 70 | 75 | } |
| 71 | 76 | } |
| 72 | - tcb | |
| 73 | - .createAsync(CustomArticleType).then(fixture => { | |
| 74 | - let myComponent: CustomArticleType = fixture.componentInstance; | |
| 75 | - expect(myComponent.article.type).toEqual("TinyMceArticle"); | |
| 76 | - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); | |
| 77 | - done(); | |
| 78 | - }); | |
| 77 | + tcb.createAsync(CustomArticleType).then(fixture => { | |
| 78 | + let myComponent: CustomArticleType = fixture.componentInstance; | |
| 79 | + expect(myComponent.article.type).toEqual("TinyMceArticle"); | |
| 80 | + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); | |
| 81 | + done(); | |
| 82 | + }); | |
| 79 | 83 | }); |
| 80 | 84 | |
| 81 | 85 | }); |
| 82 | 86 | \ No newline at end of file | ... | ... |
src/app/components/noosfero-articles/article/article.directive.ts
| ... | ... | @@ -10,14 +10,11 @@ export class ArticleView { |
| 10 | 10 | @Input() article: any; |
| 11 | 11 | @Input() profile: any; |
| 12 | 12 | |
| 13 | - constructor() { | |
| 14 | - | |
| 15 | - } | |
| 16 | 13 | } |
| 17 | 14 | |
| 18 | 15 | @Component({ |
| 19 | 16 | selector: 'noosfero-article', |
| 20 | - template: '<div></div>', | |
| 17 | + template: 'not-used', | |
| 21 | 18 | directives: [ArticleView, NoosferoArticleBlog] |
| 22 | 19 | }) |
| 23 | 20 | @Inject("$element", "$scope", "$injector", "$compile") |
| ... | ... | @@ -36,7 +33,11 @@ export class ArticleDirective { |
| 36 | 33 | this.$element.replaceWith(this.$compile('<' + this.directiveName + ' [article]="ctrl.article" [profile]="ctrl.profile"></' + this.directiveName + '>')(this.$scope)); |
| 37 | 34 | } |
| 38 | 35 | |
| 39 | - constructor(private $element: any, private $scope: ng.IScope, private $injector: ng.auto.IInjectorService, private $compile: ng.ICompileService) { | |
| 36 | + constructor( | |
| 37 | + private $element: any, | |
| 38 | + private $scope: ng.IScope, | |
| 39 | + private $injector: ng.auto.IInjectorService, | |
| 40 | + private $compile: ng.ICompileService) { | |
| 40 | 41 | |
| 41 | 42 | } |
| 42 | 43 | } | ... | ... |
src/app/index.ts
| ... | ... | @@ -16,7 +16,7 @@ import {Cms as noosferoCms} from "./cms/cms.component"; |
| 16 | 16 | import {Main} from "./main/main.component"; |
| 17 | 17 | import {bootstrap, bundle} from "ng-forward"; |
| 18 | 18 | |
| 19 | - | |
| 19 | +import {AUTH_EVENTS} from "./components/auth/auth_events"; | |
| 20 | 20 | |
| 21 | 21 | declare var moment: any; |
| 22 | 22 | |
| ... | ... | @@ -28,12 +28,9 @@ let noosferoApp: any = bundle("noosferoApp", Main, ["ngAnimate", "ngCookies", "n |
| 28 | 28 | |
| 29 | 29 | NoosferoApp.angularModule = noosferoApp; |
| 30 | 30 | |
| 31 | + | |
| 31 | 32 | NoosferoApp.addConstants("moment", moment); |
| 32 | -NoosferoApp.addConstants("AUTH_EVENTS", { | |
| 33 | - loginSuccess: "auth-login-success", | |
| 34 | - loginFailed: "auth-login-failed", | |
| 35 | - logoutSuccess: "auth-logout-success" | |
| 36 | -}); | |
| 33 | +NoosferoApp.addConstants("AUTH_EVENTS", AUTH_EVENTS); | |
| 37 | 34 | |
| 38 | 35 | NoosferoApp.addConfig(noosferoModuleConfig); |
| 39 | 36 | NoosferoApp.run(noosferoAngularRunBlock); | ... | ... |
src/app/main/main.component.ts
src/app/models/interfaces.ts
| ... | ... | @@ -21,3 +21,15 @@ export interface TynyMceArticle extends Article { |
| 21 | 21 | export interface Blog extends Article { |
| 22 | 22 | id: number; |
| 23 | 23 | } |
| 24 | + | |
| 25 | +export interface Credentials { | |
| 26 | + username: string; | |
| 27 | + password: string; | |
| 28 | +} | |
| 29 | + | |
| 30 | +export interface User { | |
| 31 | + id: number; | |
| 32 | + login: string; | |
| 33 | + email: string; | |
| 34 | + person: Person; | |
| 35 | +} | ... | ... |