add authentication and controle of authentication before routing
authorrougeronj
Thu, 02 Apr 2015 18:28:31 +0200
changeset 57 388b29d38b44
parent 56 e38e9d5114ad
child 58 e71b49ff4a23
add authentication and controle of authentication before routing
client/app/Auth/auth.html
client/app/Auth/login_controller_test.js
client/app/app.js
client/app/books/books_controller.js
client/app/home/home.html
client/app/index.html
client/app/slideshow/slideshow_controller.js
--- a/client/app/Auth/auth.html	Thu Apr 02 18:26:43 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-Authentication Page
\ No newline at end of file
--- a/client/app/Auth/login_controller_test.js	Thu Apr 02 18:26:43 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-'use strict';
-
-describe("auth_controller_test", function(){
-    it("should assert something",function(){
-        expect(true).toBe(true);
-    })
-})
--- a/client/app/app.js	Thu Apr 02 18:26:43 2015 +0200
+++ b/client/app/app.js	Thu Apr 02 18:28:31 2015 +0200
@@ -1,110 +1,116 @@
 (function(){
-  'use strict';
+	'use strict';
 
-  angular.module('ammico', [ 'ngRoute','ammicoHome', 'ammicoBooks', 'ammicoSlides', 'ammicoSlideshow', 'ammicoSearch', 'ammicoAuth', 'templates' ])
-    .config(function($routeProvider) {
-      $routeProvider.
-        when('/', {
-        controller: 'homeCtrl',
-        templateUrl: 'home/home.html'
-      }).
-      when('/books', {
-          controller: 'booksCtrl',
-          templateUrl: 'books/books.html'
-      }).
-      when('/books/:idBook', {
-        controller: 'bookCtrl',
-        templateUrl: 'books/book.html'
-      }).
-      when('/books/:idBook/add/:searched', {
-    	  controller: 'booksCtrl',
-    	  templateUrl: 'books/book.html'
-      }).
-      when('/slide/:idSlide', {
-          controller: 'slidesCtrl',
-          templateUrl: 'slides/slides.html'
-       }).
-      when('/slideshow/', {
-          controller: 'slideshowCtrl',
-          templateUrl: 'slideshow/slideshow.html'
-      }).
-      when('/slideshow/:iSlide', {
-          controller: 'slideshowCtrl',
-          templateUrl: 'slideshow/slideshow.html'
-      }).
-      when('/search/:q', {
-          controller: 'searchCtrl',
-          templateUrl: 'search/search.html'
-      }).
-      when('/login', {
-          controller: 'authCtrl',
-          templateUrl: 'auth/auth.html'
-      }).
-      otherwise({
-        redirectTo: '/'
-      });
-    })
-    // .config(function ($routeProvider) {
-    //   $routeProvider
-    //     .otherwise({
-    //       redirectTo: '/'
-    //   });
-    // });
-    .config(function($logProvider){
-      $logProvider.debugEnabled(true);
-    })
-    .service('searchApi', function($resource, context) {
-        this.searchResource = function(params){
-            return $resource(context.urls.searchUrl,  
-                {
-                    callback: 'JSON_CALLBACK'
-                },
-                {
-                    getJsonp: {
-                        method: 'JSONP',
-                        params: params,
-                        isArray: false,
-                        transformResponse: function(data){
-                            // Transform meta list into meta dict
-                            var nb = data.hits.length;
-                            for(var i=0;i<nb;i++){
-                                var nb_metas = data.hits[i].metas.length;
-                                data.hits[i].metas_dict = {};
-                                for(var j=0;j<nb_metas;j++){
-                                    if(typeof data.hits[i].metas[j].images==='undefined'){
-                                        data.hits[i].metas_dict[data.hits[i].metas[j].name] = data.hits[i].metas[j].value;
-                                    }
-                                    else{
-                                        data.hits[i].metas_dict.images = data.hits[i].metas[j].images[0].value;
-                                    }
-                                }
-                            }
-                            return data;
-                        }
-                    }
-                });
-        };
-    })
-    .service('ammicoApi', function($resource, context, $sce) {
-        this.listBooks = $resource(context.urls.ammicoUrl+'/books');
-        this.book = $resource(context.urls.ammicoUrl+'/books/:idBook', {idBook:'@idBook'});
-        this.order = $resource(context.urls.ammicoUrl+'/books/:idBook/order', {idBook:'@idBook'});
-        this.booksSlides = $resource(context.urls.ammicoUrl+'/books/:idBook/slides', {idBook:'@idBook'});
-        this.listSlides= $resource(context.urls.ammicoUrl+'/slides');
-        this.slide = $resource(context.urls.ammicoUrl+'/slides/:idSlide', {idSlide:'@idSlide'});
-        this.sanitizeUrls = function(data){
-        	data.details.audio = $sce.trustAsResourceUrl(data.details.audio);
-        	data.details.video = $sce.trustAsResourceUrl(data.details.video);
-        	data.audio = $sce.trustAsResourceUrl(data.audio);
-        	data.video = $sce.trustAsResourceUrl(data.video);
-        	return data;
-        };
-    })
-  .service('authApi', function($resource, context) {
-	  this.login = $resource(context.urls.ammicoUrl+'/auth/api-token-auth');
-	  this.logout = $resource(context.urls.ammicoUrl+'/auth/logout');
-	  this.user = $resource(context.urls.ammicoUrl+'/auth/user');
-	  this.test = $resource(context.urls.ammicoUrl+'/auth/auth');
-  });
-
+	angular.module('ammico', [ 'ngRoute','ammicoHome', 'ammicoBooks', 'ammicoSlides', 'ammicoSlideshow', 'ammicoSearch', 'ammicoAuth', 'templates' ])
+	.config(function($routeProvider) {
+		$routeProvider.
+		when('/', {
+			controller: 'homeCtrl',
+			templateUrl: 'home/home.html',
+			authRequired: false
+		}).
+		when('/books', {
+			controller: 'booksCtrl',
+			templateUrl: 'books/books.html',
+			authRequired: true
+		}).
+		when('/books/:idBook', {
+			controller: 'bookCtrl',
+			templateUrl: 'books/book.html',
+			authRequired: true
+		}).
+		when('/slide/:idSlide', {
+			controller: 'slidesCtrl',
+			templateUrl: 'slides/slides.html',
+			authRequired: true
+		}).
+		when('/slideshow/', {
+			controller: 'slideshowCtrl',
+			templateUrl: 'slideshow/slideshow.html',
+			authRequired: true
+		}).
+		when('/slideshow/:iSlide', {
+			controller: 'slideshowCtrl',
+			templateUrl: 'slideshow/slideshow.html',
+			authRequired: true
+		}).
+		when('/search/:q', {
+			controller: 'searchCtrl',
+			templateUrl: 'search/search.html',
+			authRequired: true
+		}).
+		when('/auth/:action', {
+			controller: 'authCtrl',
+			templateUrl: 'home/home.html',
+			authRequired: false
+		}).
+		otherwise({
+			redirectTo: '/'
+		});
+	})
+	.run( function($rootScope, $location, $window, $http) {
+		$rootScope.globals = {};
+		if ($window.sessionStorage.token) {
+			$rootScope.globals.userLogged = true;
+			$http.defaults.headers.common.Authorization = 'Token ' + $window.sessionStorage.token;
+		}
+		$rootScope.$on( '$routeChangeStart', function(event, next) {
+			if (next.authRequired && !$rootScope.globals.userLogged) {
+				$location.path( '/auth/login' );
+			}
+		});
+	})
+	.service('searchApi', function($resource, context) {
+		this.searchResource = function(params){
+			return $resource(context.urls.searchUrl,  
+					{
+				callback: 'JSON_CALLBACK'
+					},
+					{
+						getJsonp: {
+							method: 'JSONP',
+							params: params,
+							isArray: false,
+							transformResponse: function(data){
+								// Transform meta list into meta dict
+								var nb = data.hits.length;
+								for(var i=0;i<nb;i++){
+									var nb_metas = data.hits[i].metas.length;
+									data.hits[i].metas_dict = {};
+									for(var j=0;j<nb_metas;j++){
+										if(typeof data.hits[i].metas[j].images==='undefined'){
+											data.hits[i].metas_dict[data.hits[i].metas[j].name] = data.hits[i].metas[j].value;
+										}
+										else{
+											data.hits[i].metas_dict.images = data.hits[i].metas[j].images[0].value;
+										}
+									}
+								}
+								return data;
+							}
+						}
+					});
+		};
+	})
+	.service('ammicoApi', function($resource, context, $sce) {
+		this.listBooks = $resource(context.urls.ammicoUrl+'/books');
+		this.book = $resource(context.urls.ammicoUrl+'/books/:idBook', {idBook:'@idBook'});
+		this.order = $resource(context.urls.ammicoUrl+'/books/:idBook/order', {idBook:'@idBook'});
+		this.booksSlides = $resource(context.urls.ammicoUrl+'/books/:idBook/slides', {idBook:'@idBook'});
+		this.listSlides= $resource(context.urls.ammicoUrl+'/slides');
+		this.slide = $resource(context.urls.ammicoUrl+'/slides/:idSlide', {idSlide:'@idSlide'});
+		this.sanitizeUrls = function(data){
+			data.details.audio = $sce.trustAsResourceUrl(data.details.audio);
+			data.details.video = $sce.trustAsResourceUrl(data.details.video);
+			data.audio = $sce.trustAsResourceUrl(data.audio);
+			data.video = $sce.trustAsResourceUrl(data.video);
+			return data;
+		};
+	})
+	.service('authApi', function($resource, context) {
+		this.login = $resource(context.urls.ammicoUrl+'/auth/api-token-auth');
+		this.logout = $resource(context.urls.ammicoUrl+'/auth/logout');
+		this.user = $resource(context.urls.ammicoUrl+'/auth/user');
+		this.test = $resource(context.urls.ammicoUrl+'/auth/auth');
+	});
 })();
--- a/client/app/books/books_controller.js	Thu Apr 02 18:26:43 2015 +0200
+++ b/client/app/books/books_controller.js	Thu Apr 02 18:28:31 2015 +0200
@@ -2,7 +2,7 @@
 	'use strict';
 
 	angular.module('ammicoBooks',['ngRoute', 'ui.sortable'])
-	.controller('booksCtrl', function($scope, $location, booksModel, $modal, ammicoApi){
+	.controller('booksCtrl', function($scope, $location, $modal, ammicoApi){
 		$scope.books = ammicoApi.listBooks.query({format:'json'});
 		$scope.searchSubmit = function(){
 			$location.path('/search/' + $scope.q);
--- a/client/app/home/home.html	Thu Apr 02 18:26:43 2015 +0200
+++ b/client/app/home/home.html	Thu Apr 02 18:28:31 2015 +0200
@@ -1,1 +1,12 @@
-<h1>Ammico Application</h1>
\ No newline at end of file
+<h1>Ammico Application</h1>
+
+<p>
+	Le Lorem Ipsum est simplement du faux texte employé dans la composition 
+	et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de 
+	l'imprimerie depuis les années 1500, quand un peintre anonyme assembla ensemble des 
+	morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait 
+	que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans 
+	que son contenu n'en soit modifié. Il a été popularisé dans les années 1960 grâce à la 
+	vente de feuilles Letraset contenant des passages du Lorem Ipsum, et, plus récemment,
+	par son inclusion dans des applications de mise en page de texte, comme Aldus PageMaker.
+</p>
\ No newline at end of file
--- a/client/app/index.html	Thu Apr 02 18:26:43 2015 +0200
+++ b/client/app/index.html	Thu Apr 02 18:28:31 2015 +0200
@@ -25,8 +25,8 @@
             <li ng-class="{active:isActive('/slideshow')}"><a href="#/slideshow">Diaporama</a></li>
           </ul>
           <ul class="nav navbar-nav navbar-right" ng-controller="routeClassCtrl">
-            <li ng-hide="globals.is_authenticated" ng-class="{active:isActive('/login')}"><a href="#/login">Login</a></li>
-            <li ng-show="globals.is_authenticated"><a href="#/logout">Logout</a></li>
+            <li ng-hide="globals.userLogged" ng-class="{active:isActive('/login')}"><a href="#/auth/login">Login</a></li>
+            <li ng-show="globals.userLogged"><a href="#/auth/logout">Logout</a></li>
           </ul>
           <form class="navbar-form navbar-right" role="search" ng-submit="searchSubmit()">
             <div class="form-group">
--- a/client/app/slideshow/slideshow_controller.js	Thu Apr 02 18:26:43 2015 +0200
+++ b/client/app/slideshow/slideshow_controller.js	Thu Apr 02 18:28:31 2015 +0200
@@ -14,8 +14,5 @@
     		data = ammicoApi.sanitizeUrls(data);
     	});
     	$scope.book = ammicoApi.book.get({idBook:'12', format:'json'});
-    	$scope.showSlide = function(index){
-    		console.log(index);
-    	};
     });
 })();