Nicolas FROIDURE
Web Platform Architect
Rights are simply defined by an URI pattern:
/users/:user_id/notifications\?(.*)
And a set of methods:
HEAD, OPTIONS, GET, PUT, POST, DELETE
You can refer to deep object properties:
/places/:user.home.id/
Wildcards allow to match several values. In this case, the access will be granted for each matched values:
/places/:user.places.*.id
app.use(function(req, res, next) {
getUserInfosFromTheDB().then(function(user) {
req._rights = user.rights;
req._rightsTemplateValues = [user];
next();
});
});
var reaccess = require('express-reaccess');
app.use(reaccess({
rightsProps: ['_rights'],
valuesProps: ['_rightsTemplatesValues'],
accessErrorMessage: 'UNAUTHORIZED'
));
app.use(function(err, req, res, next) {
if("UNAUTHORIZED" == err.message) {
res.send(401, req._user);
}
next(err);
});
angular.module('myApp', [
'simplifield.reaccess'
]).config(['sfReaccessServiceProvider',
function(sfReaccessServiceProvider) {
// Debugging rights (if you wish)
$logProvider.debugEnabled(true);
sfReaccessServiceProvider.debug(true);
// Setting templated rights
sfReaccessServiceProvider.setPredefinedRights({
'USER_ADD': {
path: '/api/users',
methods: ['POST']
},
'USER_EDIT':{
path: '/api/users/:id',
methods: ['PUT', 'PATCH']
},
'USER_DELETE': {
path: '/api/users/:id',
methods: ['DELETE']
}
});
}]);
// Retrieving current user informations
$http.get('/profile').then(function(response) {
// Saving rights
sfReaccessService.setRights(response.data.rights);
// Setting values to fill templated rights
sfReaccessService.setValues([{
_username: response.data.username
}]);
});
<!-- Display button if user can add users -->
<a href="#/beers/create" class="btn btn-primary"
ng-show="'USER_ADD' | sfReaccess">
Add a user
</a>
In your templates:
if($sfReaccessService.test('USER_ADD')) {
// Yay, i can add users
}
In your code:
@nfroidure on GitHub and Twitter
working at @SimpliField
Projects repos:
express-reaccess - angular-reaccess
By Nicolas FROIDURE
End to end approach for user access in modern applications with reaccess.