Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Last active February 1, 2019 14:35
Show Gist options
  • Save SethVandebrooke/af7ae9a828e91291c57538cb7372f326 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/af7ae9a828e91291c57538cb7372f326 to your computer and use it in GitHub Desktop.
Handling CRUD Permissions via bitwise operations
// Crud Permissions
function CRUDPermissions () {
var self = this;
self.CREATE = 8,
self.READ = 4,
self.UPDATE = 2,
self.DELETE = 1,
self.perm = 0;
self.readPerm = action => (self.perm & action) == action;
self.givePerm = action => self.perm = (self.perm | action);
self.removePerm = action => self.perm = (self.perm & ~action);
}
/*
var t = new CRUDPermissions();
t.givePerm(t.CREATE); // 8
t.readPerm(t.CREATE); // true
t.removePerm(t.CREATE); // 0
t.readPerm(t.CREATE); // false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment