2015-12-18 11:20:17 +01:00

156 lines
5.4 KiB
JavaScript

/*
This file is part of Accountant.
Accountant is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Accountant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
*/
var accountantApp = angular.module("accountantApp", [
'ngResource', 'ngRoute',
"mgcrea.ngStrap",
"highcharts-ng",
"http-auth-interceptor",
"cgNotify",
"Storage.Service",
"xeditable"
])
.factory("sessionInjector", ["$storage", function($storage) {
var sessionInjector = {
request : function(config) {
var token = $storage.get('token');
if(token) {
var token_type = $storage.get('token_type');
var authorization = token_type + " " + token;
config.headers["Authorization"] = authorization;
}
return config;
}
};
return sessionInjector;
}])
.config(["$httpProvider", "$routeProvider", "$locationProvider", "$storageProvider",
function($httpProvider, $routeProvider, $locationProvider, $storageProvider) {
// Define interceptors.
$httpProvider.interceptors.push('sessionInjector');
// Defining template and controller in function of route.
$routeProvider.when('/account/:accountId/operations', {
templateUrl: 'static/templates/operations.html',
controller: 'OperationController'
}).when('/account/:accountId/scheduler', {
templateUrl: 'static/templates/scheduler.html',
controller: 'SchedulerController'
}).when('/accounts', {
templateUrl: 'static/templates/accounts.html',
controller: 'AccountController'
}).when('/', {
redirectTo: 'accounts'
});
// Enable HTML5 mode.
$locationProvider.html5Mode(true);
// Configure storage
// Set global prefix for stored keys
$storageProvider.setPrefix("accountant");
// Change the default storage engine
// Defaults to "local"
$storageProvider.setDefaultStorageEngine("session");
// Change the enabled storage engines
// Defaults to ['memory', 'cookie', 'session', 'local']
$storageProvider.setEnabledStorageEngines(['local', 'session']);
}])
.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
})
.controller("MainController", [
"$scope", "$rootScope", "$http", "authService", "notify", "$storage",
function($scope, $rootScope, $http, authService, notify, $storage) {
$scope.dialogShown = false;
$scope.showLoginForm = function(e) {
// First, if there are registered credentials, use them
if($scope.dialogShown) {
return;
}
$scope.dialogShown = true;
bootbox.dialog({
title: "Authentification requise",
message: '<form class="form-horizontal">' +
' <div class="form-group">' +
' <label for="email" class="col-sm-4 control-label">Adresse email</label>' +
' <div class="col-sm-8">' +
' <input type="text" class="form-control" id="email" ng-model="email" placeholder="Nom d\'utilisateur">' +
' </div>' +
' </div>' +
' <div class="form-group">' +
' <label for="password" class="col-sm-4 control-label">Mot de passe</label>' +
' <div class="col-sm-8">' +
' <input type="password" class="form-control" id="password" ng-model="password" placeholder="Mot de passe">' +
' </div>' +
' </div>' +
' </div>' +
'</form>',
buttons: {
login: {
label: "Login",
className: "btn-primary",
callback: function(result) {
$scope.dialogShown = false;
var email = $('#email').val();
var password = $('#password').val();
$http.post(
"/api/users/login",
{
"email": email,
"password": password
}
).success(function(result) {
// TODO Alexis Lahouze 2015-08-28 Handle callback.
// Call to /api/login to retrieve the token
$storage.set('token_type', result.token_type);
$storage.set('token', result.token);
$storage.set('expiration_date', result.expiration_date);
authService.loginConfirmed();
});
}
},
cancel: {
label: "Annuler",
className: "btn-default",
callback: function(result) {
authService.loginCancelled(null, "Login cancelled by user action.");
$scope.dialogShown = false;
}
}
}
});
};
$rootScope.$on("event:auth-loginRequired", $scope.showLoginForm);
}])
;