Last active
August 29, 2015 14:05
-
-
Save PierrickP/ded0b39ca3ac519e5868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.run(['$rootScope', '$state', '$stateParams', 'Config', 'Notification', function ($rootScope, $state, $stateParams, Config, Notification) { | |
$rootScope.lkConfig = Config.get; | |
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { | |
$rootScope.$stateDestination = toState; | |
$rootScope.$state = $state; | |
$rootScope.$stateParams = $stateParams; | |
}); | |
$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error){ | |
if ((error === 'need_auth' && toState.name != 'login') || error.status === 401){ | |
$state.go('login'); | |
} | |
}); | |
}]) | |
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider', function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) { | |
$locationProvider.html5Mode(true); | |
$urlRouterProvider | |
.otherwise('/login'); | |
$stateProvider | |
.state('app', { | |
abstract: true, | |
template: '<div ui-view class="wrapper"></div>', | |
resolve: { | |
lkConfig: function ($rootScope, $q, $http, $state, Config, Auth) { | |
$http.get('/api/config').success(function (data) { | |
/* | |
Recupere si l'autentification est necessaire | |
*/ | |
Config.setSettings(data.config); | |
/* | |
si necessaire et pas deja a destination de login alors on reject | |
*/ | |
if (Config.get('rights').auth_required && !Auth.isLoggedIn() && $rootScope.$stateDestination.name !== 'login') { | |
configPromise.reject('need_auth'); | |
} else { | |
configPromise.resolve(Config.get('rights')); | |
} | |
}); | |
return configPromise.promise; | |
} | |
} | |
}) | |
.state('dashboard', { | |
parent: 'app', | |
url: '/dashboard?path', | |
templateUrl: '/partials/dashboard.html', | |
controller: 'DashboardCtrl', | |
resolve: { | |
visualizations: function ($state, Visualization, lkConfig) { | |
/* | |
Dependence a lkConfig pour attendre le resolve parent mais sinon useless | |
*/ | |
return Visualization.getTree(); | |
}, | |
} | |
}) | |
/* | |
A besoin de la conf | |
*/ | |
.state('login', { | |
parent: 'app', | |
url: '/login', | |
templateUrl: '/partials/login.html', | |
resolve: { | |
noNeedAuth: function ($q, $rootScope, $state, lkConfig) { | |
var configPromise = $q.defer(); | |
if ($rootScope.lkConfig('rights').auth_required === false) { | |
$state.go('dashboard'); | |
configPromise.reject('no_need_auth'); | |
} else { | |
configPromise.resolve(); | |
} | |
return configPromise.promise; | |
} | |
}, | |
controller: ['$rootScope', , function ($rootScope) { | |
// blabla | |
}] | |
}) | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment