psecio/verify

框架无关的授权和认证

dev-master 2015-12-21 22:27 UTC

This package is auto-updated.

Last update: 2024-09-15 09:59:33 UTC


README

Travis-CI Build Status

Verify 库的目的是提供一个结构,用于使用您自己的数据源来实现良好的授权和认证实践。 Verify 提供了接口,您可以在其中编程并注入自己的对象和数据以进行评估。

示例代码

首先,我们将基于我们已有的对象创建一个用户。在大多数情况下,用户的值(模型或其他)被引用为属性。为了简化这个创建过程,Verify 提供了一个可用的 Simple 主体类。您将在下面的示例中看到这一点

<?php

$user = (object)[
  'username' => 'ccornutt',
  'password' => password_hash('test1234', PASSWORD_DEFAULT),
  'permissions' => ['test1', 'test2', 'edit']
];
$subject = new \Psecio\Verify\Subject\Simple($user);

// Now we'll set up our Gateway to work with our user and run some checks
$gate = new Gateway($subject);

// We can see if the password they entered matches
echo 'Password match? '.var_export($gate->authorize($_POST['password']), true);

// And we can check their permissions with the "can" and "cannot" checks
if ($gate->can('edit') && $gate->cannot('delete')) {
    echo "We're here!";
}

// Or we can make it a bit more complex and include multiple
if ($gate->can(['edit', 'test1']) && $gate->cannot(['bar', 'test2'])) {
    /* Won't get here, the user has "test" so it fails */
} else {
    echo "This one fails!";
}

// Or, if you'd like to build up more of a policy:
$gate->allow('edit')->deny('test1234');
if ($gate->evaluate() === true) {
    echo 'Pass with flying colors!';
}

?>

在幕后,Verify 库使用了 PropAuth 库。这个库比这里使用的功能强大得多。如果您有更多的“功能”需求,请查看它。