I am working on a mean stack web application which contains differential access based upon the logged user's role. there are few different roles such as admin, govt, volunteer etc.
1. how to manage the front end based upon the role?
currently i am setting global flags such as isAdmin, isVolunteer, isGovt etc for each role and based upon their value, i am able to show different UI using data-ng-if = "Global.isAdmin"
Is this aproach is correct. if not please suggest the correct way to handle UI.
2. how to manage the back-end and redirect the route if the user did'n have authorization?
currently i am trying to use angular-permission and rbac but still unable to use these successfully in my application. can anyone tell me the best practice for role based access control for mean stack web app.
while trying to use the angular-permission, i was unable to link the created roles to route file.
this is role module.
angular.module('mean.users', ['permission'])
.run(['permission', 'Global', '$q',
function(Permission, Global, $q) {
console.log('not anonumous');
Permission
.defineRole('anonymous', function(stateParams) {
var deferred = $q.defer();
if (Global.user) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise;
})
.defineRole('admin', function(stateParams) {
if (Global.isAdmin) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise;
})
.defineRole('govt', function(stateParams) {
if (Global.isGovt) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise;
})
.defineRole('volunteer', function(stateParams) {
if (Global.isVolunteer) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise;
});
}
]);
how to link above configoured roles in route file
$stateProvider
.state('create user', {
url: '/users/create',
templateUrl: 'users/views/create.html',
resolve: {
loggedin: checkLoggedin
}
})
.state('all users', {
url: '/users/list',
templateUrl: 'users/views/list.html',
data: {
permissions: {
only: ['admin'],
redirectTo: 'home'
}
}
})
.state('show user', {
url: '/users/:userId/view',
templateUrl: 'users/views/view.html',
resolve: {
loggedin: checkLoggedin
}
})
.state('edit user', {
url: '/users/:userId/edit',
templateUrl: 'users/views/edit.html',
data: {
permissions: {
only: ['admin']
}
}
})
.state('myprofile', {
url: '/users/:userId/me',
templateUrl: 'users/views/myprofile.html',
resolve: {
loggedin: checkLoggedin
}
});