New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bitmask-flags

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitmask-flags

A utility for dealing with flags and permissions using bitmasks.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
160
decreased by-19.19%
Maintainers
1
Weekly downloads
 
Created
Source

bitmask-flags

A utility for working with bitmasks to do flags and permissions. Supports flag inheritance.

While the examples in this documentation will show usage for a permission system, you could of course use it for any kind of flags.

License

WTFPL or CC0, whichever you prefer.

Contributing

Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the .coffee files, not the .js files.

As this module could potentially deal with authentication, tests are needed; a pull request for those would be especially welcome.

Build tool of choice is gulp; simply run gulp while developing, and it will watch for changes.

Usage

Create a new instance

var bitmaskFlags = require("bitmask-flags");

var flagHandler = bitmaskFlags({
	view_announcements: 1,
	create_announcements: {
		value: 2,
		inheritedFlags: ["edit_announcements"]
	},
	edit_announcements: 4,
	delete_announcements: 8
});

Why you need to explicitly specify the values for each flag? Because if they were assigned automatically in order, it'd be too easy to accidentally mess up the values of your flags during later editing or rearranging. Especially in a permission system, that could have disastrous consequences.

bitmask-flags will do a sanity check on instantiating to ensure that all of your flags are a power of 2. If that's not the case, it will throw an Error and bail out.

Regular flags

The user object is a hypothetical ORM model/object of some sort. It hypothetically supports plain attributes and has a save() method, for the sake of illustrating how this module works.

var permissions = flagHandler.create(); // This is how you create a new value, starting at 0 (no flags).
// or
var permissions = flagHandler.create(user.permissions); // This is how you use an existing starting value (eg. from the database).
// or
var permissions = flagHandler.create(user.permissions, user.originalPermissions); // If you want to use flag inheritance (explained later).

permissions.add("view_announcements"); // This permission is now set.
permissions.add("delete_announcements"); // The value now has `view_announcements` and `delete_announcements` flags.

permissions.remove("view_announcements"); // Now only `delete_announcements` is left.

console.log(permissions.has("delete_announcements")); // true

user.permissions = permissions.getValue();
user.save(); // Done!

Flag inheritance

The really interesting thing, though, is flag inheritance. Sometimes one flag should automatically grant another flag, but the other flag can also be granted separately. bitmask-flags makes this easy.

Note that you will have to store two values in your database for this to work, rather than one - the second value indicates which flags were explicitly set. This way, when you've explicitly set a child flag, unsetting the parent flag won't change the state of the child flag. That also means you can explicitly set a flag that was already set through inheritance, and have it persist.

For these examples, we will assume that the section above never happened, and the user starts out with no permissions.

var permissions = flagHandler.create(user.permissions, user.originalPermissions); // Like we saw before...

permissions.add("create_announcements"); // The user now has `create_announcements` AND `edit_anouncements`.

console.log(permissions.has("edit_announcements")); // true

permissions.remove("create_announcements"); // The user now has no permissions.

console.log(permissions.has("edit_announcements")); // false

/* Now let's try that again, but this time explicitly setting `edit_announcements`. */

permissions.add("create_announcements"); // The user now has `create_announcements` and `edit_anouncements`.
permissions.add("edit_announcements"); // The user *still* has `create_announcements` and `edit_anouncements`.

permissions.remove("create_announcements");

console.log(permissions.has("edit_announcements")); // true - Because we set `edit_announcements` explicitly, it wasn't unset along with `create_announcements`. Magic!

user.permissions = permissions.getValue();
user.originalPermissions = permissions.getOriginalValue();
user.save(); // Done! The inheritance structure will persist, even through database loads and saves.

Keywords

FAQs

Package last updated on 12 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc