tellform/public/application.js

93 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-06-29 22:51:29 +00:00
'use strict';
//Start by defining the main module and adding the module dependencies
angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies);
// Setting HTML5 Location Mode
angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider',
function($locationProvider) {
$locationProvider.hashPrefix('!');
}
]);
//Permission Constants
angular.module(ApplicationConfiguration.applicationModuleName).constant('APP_PERMISSIONS', {
viewAdminSettings: 'viewAdminSettings',
editAdminSettings: 'editAdminSettings',
editForm: 'editForm',
2016-05-13 18:13:09 +00:00
viewPrivateForm: 'viewPrivateForm'
});
//User Role constants
angular.module(ApplicationConfiguration.applicationModuleName).constant('USER_ROLES', {
admin: 'admin',
normal: 'user',
2016-05-13 18:13:09 +00:00
superuser: 'superuser'
});
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', '$state', '$stateParams',
function($rootScope, Auth, $state, $stateParams) {
2015-07-01 23:14:39 +00:00
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
2015-07-01 23:14:39 +00:00
// add previous state property
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
$state.previous = fromState;
//console.log('toState: '+toState.name);
2015-07-07 01:56:38 +00:00
2016-05-16 22:26:10 +00:00
var statesToIgnore = ['home', 'signin', 'resendVerifyEmail', 'verify', 'signup', 'signup-success', 'forgot', 'reset-invalid', 'reset', 'reset-success'];
//Redirect to listForms if user is authenticated
2016-05-16 22:26:10 +00:00
if(statesToIgnore.indexOf(toState.name) > 0){
if(Auth.isAuthenticated()){
event.preventDefault(); // stop current execution
//console.log('go to forms');
$state.go('listForms'); // go to listForms page
}
}
2016-04-28 01:40:51 +00:00
//Redirect to 'signup' route if user is not authenticated
else if(toState.name !== 'access_denied' && !Auth.isAuthenticated() && toState.name !== 'submitForm'){
2016-05-16 22:26:10 +00:00
console.log('go to signup');
event.preventDefault(); // stop current execution
2016-04-29 02:48:02 +00:00
$state.go('listForms'); // go to listForms page
}
2016-04-29 02:48:02 +00:00
});
2015-07-01 23:14:39 +00:00
}
]);
2015-06-29 22:51:29 +00:00
//Page access/authorization logic
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', 'User', 'Authorizer', '$state', '$stateParams',
function($rootScope, Auth, User, Authorizer, $state, $stateParams) {
$rootScope.$on('$stateChangeStart', function(event, next) {
var authenticator, permissions, user;
permissions = next && next.data && next.data.permissions ? next.data.permissions : null;
Auth.ensureHasCurrentUser(User);
user = Auth.currentUser;
if(user){
authenticator = new Authorizer(user);
2016-04-10 21:54:41 +00:00
//console.log('access denied: '+!authenticator.canAccess(permissions));
//console.log(permissions);
if( (permissions != null) ){
if( !authenticator.canAccess(permissions) ){
2016-05-16 22:26:10 +00:00
event.preventDefault();
//console.log('access denied');
$state.go('access_denied');
2016-04-10 21:54:41 +00:00
}
}
}
});
}]);
2015-06-29 22:51:29 +00:00
//Then define the init function for starting up the application
angular.element(document).ready(function() {
//Fixing facebook bug with redirect
if (window.location.hash === '#_=_') window.location.hash = '#!';
//Then init the app
angular.bootstrap(document, [ApplicationConfiguration.applicationModuleName]);
2016-04-10 21:54:41 +00:00
});