Row-level filtering of models via bitmasks.
- PHP5.2+ (Fully featured with 5.3+)
- CakePHP 1.3+
- Download this: http://github.com/jmcneese/bitmasked/zipball/master
- Unzip that download.
- Copy the resulting folder to app/plugins
- Rename the folder you just copied to
From your repository root directory type:
git submodule add git://github.com/jmcneese/bitmasked.git app/plugins/bitmasked git submodule update --init
In your plugin directory type
git clone git://github.com/jmcneese/bitmasked.git
- Import the SQL in config/schema/bitmasked_bits.sql into your application’s database
- Activate the behavior in whichever model(s) you desire, via:
public $actsAs = array( 'Bitmasked.Bitmasked' );
There are several configurable options that can be passed into the behavior:
- disabled — If you want the behavior to start as disabled, i.e. not automatically filter rows based on bitmask, set this to true. Default: false
- default — This is the default bit that is saved for new records for this model, unless otherwise specified in save data. Default: 1
- bits — An array of flags (string) that map to bits (integer) to use for this particular model. Default: array(‘ALL’ => 1)
mask — The bitmask to use when finding records on this model. There are several possible settings here:
- Implicit (integer). This is when the bitmask is static across all finds.
- Flag (string). This is a string representation of a flag you have defined in the ‘bits’ option.
- Flags (array). An array of flags in string form.
- Callback (mixed). Either a string referring to a function name in global scope, a method in the attached model or an anonymous function (PHP5.3 only).
Lets say we run a grocery store and want to easily query all the items that a user is allowed to even view. For this example we’ll use items that have age restrictions.
First off, lets section off our groups into common restrictions. This is easy to do by adjusting our
bitmask setting on the behavior for our model.
public $actsAs = array( 'Bitmasked.Bitmasked' => array( 'bits' => array( 'ALL' => 1, '18+' => 2, '21+' => 4, '55+' => 8 ) ) );
We’ve given each group an easy to remember name that we can reference later on in our queries. With that out of the way we can go ahead and create our items.
// Item for everyone. $this->Item->create(); $this->Item->save(array( 'name' => 'Ice Cream', 'bits' => array( 'ALL', '18+', '21+', '55+' ) )); // Item for 18+ year olds $this->Item->create(); $this->Item->save(array( 'name' => 'Cigarettes', 'bits' => array( '18+', '21+', '55+' ) )); // Item for 21+ year olds $this->Item->create(); $this->Item->save(array( 'name' => 'Alcohol', 'bits' => array( '21+', '55+' ) )); // Item for 55+ year olds $this->Item->create(); $this->Item->save(array( 'name' => 'Lawn', 'bits' => array( '55+' ) ));
Notice how for the items we saved the lowest matching group and all the above ones. Just because you’re 28 doesn’t mean you don’t want ice cream, right?
With those rows created we can easily get at only the rows that are applicable to be shown. Here’s an example of finding everything that can be seen by an 18 year old.
$items = $this->Item->find( 'all', array( 'conditions' => array(), 'bitmask' => array( '18+' ) ) );
$items array we should have the “Ice Cream” and “Cigarettes” records available. Notice how we made use of the name of the group (
18+) instead of the actual bitmask value.
Instead of saving the items with all of their available groups, you can save them with their lowest group and instead query for all groups that the person is allowed; the same example would look like this
// Item for everyone. Note we only save "ALL" $this->Item->create(); $this->Item->save(array( 'name' => 'Ice Cream', 'bits' => array( 'ALL' ) )); // Query for an 18yr old. Notice how we include "ALL" $items = $this->Item->find( 'all', array( 'conditions' => array(), 'bitmask' => array( 'ALL', '18+' ) ) );
- Add behavior method to reconfigure, e.g. change configuration options.
Copyright © 2009-2011 Joshua M. McNeese, Curtis J. Beeson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN