behance / nbd.php-gatekeeper

此软件包已被废弃且不再维护。没有推荐替代软件包。

Behance Gatekeeper

1.4.1 2016-09-12 21:16 UTC

This package is not auto-updated.

Last update: 2018-09-29 21:47:21 UTC


README

Build Status

Gatekeeper提供了一组规则,用于控制对定义功能的访问。为了便于设置和使用,已将依赖项保持到最低,无需数据库存储,同时仍然可以从数据库、json文件等轻松配置。

目前它包含以下规则(尽管也很容易添加您自己的规则)

BinaryRule可以设置为开启/关闭,设置为开启时允许访问。
IdentifierRule在配置提供的列表中的标识符(例如用户ID)被授予访问权限。
AuthenticatedPercentageRule主要用于A/B测试,允许将功能对x%的用户打开,这是通过给定的用户标识符自动一致确定的。对于此规则,“认证的”意味着此规则应仅应用于标记为“认证的”标识符(请参阅以下“基本用法”示例)。
AnonymousPercentageRule与AuthenticatedPercentageRule完全相同,但此规则类型仅适用于标记为“匿名”的标识符。
RandomPercentageRule每次检查规则的访问权限时都会创建一个新的、随机的标识符。此规则可用于您希望x%的时间发生操作,而不是基于任何实际标识符。
BetweenTimesRule如果当前日期/时间在两个指定的日期/时间之间,则授予访问权限。
StartTimeRule通过使用当前时间从指定的(在规则配置中)日期/时间开始授予访问权限。
EndTimeRule通过使用当前时间直到指定的(在规则配置中)结束日期/时间授予访问权限。
BetweenTimesIdentifierRule如果特定的日期(作为时间标识符提供给canAccess)在两个指定的(在规则配置中)日期/时间之间,则授予访问权限。
StartTimeIdentifierRule如果特定的日期(作为时间标识符提供给canAccess)在指定的(在规则配置中)日期/时间之后,则授予访问权限。
EndTimeIdentifierRule如果特定的日期(作为时间标识符提供给canAccess)在指定的(在规则配置中)日期/时间之前,则授予访问权限。
IpRule如果指定的IP地址(作为IP标识符提供给canAccess)在规则配置中指定为允许的IP,则授予访问权限。
IpRangeRule如果指定的IP地址(作为IP标识符提供给canAccess)在指定的(在规则配置中)IP范围内,则授予访问权限。
## 基本用法
$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>";

}

更高级用法

StartTimeRule与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>";

}

许可证

nbd.php-gatekeeper采用MIT许可证。有关更多信息,请参阅许可证文件

贡献

有关信息,请参阅CONTRIBUTING.md