behance / nbd.php-gatekeeper
Behance Gatekeeper
Installs: 5 911
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 5
Forks: 5
Open Issues: 1
Requires
- php: >=5.6
Requires (Dev)
- behance/php-sniffs: ~2.1
- phpunit/phpunit: ~5.0
README
Gatekeeper provides a set of rules that are used to control access to defined features. Dependancies have been kept minimal for easy setup and use, requiring no database storage while still making it easy to configure from a database, json file, etc. if you'd like to.
Right now it comes with the following rules (though it's easy to add your own too):
BinaryRule | Can be set to on/off and grants access if set to on. |
IdentifierRule | Identifiers (ex. user ids) that are in the given list provided by config are granted access. |
AuthenticatedPercentageRule | Primarily for a/b testing, allows a feature to be turned on for x percentage of users, which is consistently determined automatically by the given user identifier. For this rule, "authenticated" is meant to represent that the rule should only be applied to an identifier that is marked as "authenticated" (see "Basic Usage" example below.) |
AnonymousPercentageRule | Exactly the same as AuthenticatedPercentageRule, except this rule type only applies to identifiers that are marked as "anonymous." |
RandomPercentageRule | This rule creates a new, random identifier every time access to the rule is checked. This rule can be used for action you want to occur x% of the time, not based on any actual identifier. |
BetweenTimesRule | grants access if the current date/time is between two specified date/times |
StartTimeRule | grants access beginning at a specified (in the rule config) date/time by using the current time |
EndTimeRule | grants access up until a specified (in the rule config) end date/time by using the current time |
BetweenTimesIdentifierRule | grants access if a specific time (supplied as time identifier to canAccess) is between two specified (in the rule config) date/times |
StartTimeIdentifierRule | grants access if a specified time (supplied as time identifier to canAccess) is after a specified (in the rule config) date/time |
EndTimeIdentifierRule | grants access if a specified time (supplied as time identifier to canAccess) is before a specified (in the rule config) date/time |
IpRule | grants access if a specified ip address (supplied as ip identifier to canAccess) is specified (in the rule config) as an allowed IP |
IpRangeRule | grants access if a specified ip address (supplied as ip identifier to canAccess) is within a specified (in the rule config) IP range |
$rule_config = [ 'website_overhaul' => [ // turn the site overhaul on for a few specific users [ 'type' => \Behance\NBD\Gatekeeper\Rules\IdentifierRule::RULE_NAME, 'params' => [ 'valid_identifiers' => [ 123, // admin 1 user id 456, // admin 2 user id ] ] ], // roll out the new site to 10% of users (users who see it will remain consistent) [ 'type' => \Behance\NBD\Gatekeeper\Rules\AuthenticatedPercentageRule::RULE_NAME, 'params' => [ 'percentage' => 10 ] ], ], 'welcome_text' => [ // allow everyone to see this feature [ 'type' => \Behance\NBD\Gatekeeper\Rules\BinaryRule::RULE_NAME, 'params' => [ 'on' => true ] ], ] ]; $ruleset_provider = new \Behance\NBD\Gatekeeper\RulesetProviders\ConfigRulesetProvider( $rule_config ); $gatekeeper = new \Behance\NBD\Gatekeeper\Gatekeeper( $ruleset_provider ); // This can be any kind of identifier. the percentage rule hashes it consistently // so the same user identifiers are always allowed/disallowed into the test. // Here we use an "authenticated" identifier because we're dealing with a user id. $identifier = [ 'authenticated' => 456 ]; if ( $gatekeeper->canAccess( 'welcome_text', $identifier ) ) { echo "<p>Welcome to the website.</p>"; } if ( $gatekeeper->canAccess( 'website_overhaul', $identifier ) ) { echo "<p>Congrats! You get to see the awesome new site!</p>"; } else { echo "<p>Looks like you're stuck with the old stuff...</p>"; }
More Advanced Usage
A StartTimeRule combined with an IpRangeRule:
$rule_config = [ 'special_secret_stuff' => [ // turn the stuff for the admin users [ 'type' => \Behance\NBD\Gatekeeper\Rules\IdentifierRule::RULE_NAME, 'params' => [ 'valid_identifiers' => [ 123, // admin 1 user id 456, // admin 2 user id ] ] ], // turn on the stuff right away for a range of IPs (ex. your office) [ 'type' => \Behance\NBD\Gatekeeper\Rules\IpRangeRule::RULE_NAME, 'params' => [ 'start_ip' => '192.168.56.101', 'end_ip' => '192.168.56.105', ] ], // let everyone see the stuff starting in 2017 [ 'type' => \Behance\NBD\Gatekeeper\Rules\StartTimeRule::RULE_NAME, 'params' => [ 'start' => new DateTimeImmutable('2017-01-01') ] ], ] ]; $ruleset_provider = new \Behance\NBD\Gatekeeper\RulesetProviders\ConfigRulesetProvider( $rule_config ); $gatekeeper = new \Behance\NBD\Gatekeeper\Gatekeeper( $ruleset_provider ); $identifier = [ 'ip' => $_SERVER['REMOTE_ADDR'], 'authenticated' => 678, // not an admin, so has to be in IP range or it must be 2017 for access to be granted ]; if ( $gatekeeper->canAccess( 'special_secret_stuff', $identifier ) ) { echo "<p>You get to see the secret stuff!</p>"; } else { echo "<p>There's nothing here to see...</p>"; }
License
nbd.php-gatekeeper is licensed under the MIT license. See License File for more information.
Contributing
See CONTRIBUTING.md for information.