wesleyalmeida / sentry
简单的Laravel资源访问控制
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-28 15:44:33 UTC
README
#Sentry
简单的Laravel资源访问控制
用法
Sentry是一个简单的Laravel资源访问控制插件,无需指定资源即可使用。sentry_user_roles数据库表存储了用户与其角色之间的关系。角色可以是组织选择的任意字符串。由于该系统不关心资源,开发者只需通过运行Sentry::hasRole("my_role")的检查来验证用户的角色,即可在任何时间进行验证。开发者的脚本可以根据该检查的布尔结果继续或暂停。
Sentry在有效之前需要了解用户的角色。在您的应用程序中,最佳加载Sentry的位置是在用户授权之后;通常是在登录后。
示例
public function doLogin() {
$credentials = [
'username' => 'foo',
'password' => 'bar',
]
if(Auth::attempt($credentials)) {
// Retrieve SentryUserRoles from storage
// Below is the Query way, but you can use
// any other database driver.
$table = DB::table('sentry_user_roles');
$query = $table->where('user_id', "=", $user_id);
$user_roles = $query->lists('role');
// Add user roles to Sentry
Sentry::setUserRoles($user_roles);
// Success Authentication
return Redirect::intended('/');
} else {
// Fail Authentication
return Redirect::route('login');
}
}
一旦开发者完成了加载Sentry用户角色的操作,就不需要再次执行此步骤。
验证很简单。开发者可以在任何地方执行此操作,但最常见的情况可能是在一个Controller的动作中。
示例
class HomeController extends BaseController {
public function myAdminAction() {
// Sentry::requireRole accepts a string, or an array
// String usage is below
$isAllowed = Sentry::requireRole('admin');
if($isAllowed) {
dd("Success, I'm allowed to do this!");
}
dd("Bummer, I am not allowed to do this...");
}
public function myPowerUserAction() {
// Sentry::requireRole accepts a string, or an array
// Array usage is below
$isAllowed = Sentry::requireRole(['sales', 'sales_admin', 'sales_intern']);
if($isAllowed) {
dd("Success, I'm allowed to do this!");
}
dd("Bummer, I am not allowed to do this...");
}
}
开发者可以使用Sentry::allowFooRole魔术方法来允许角色,而不是将字符串或数组传递给Sentry::requireRole()。允许角色的第三种方式是使用Sentry::allow("foo_role")。如果开发者选择这种方法,那么他或她可以调用Sentry::requireRole()而不带任何参数。
示例
Sentry::allowUser();
Sentry::allowGuest();
$isAllowed = Sentry::requireRole();
等同于
$isAllowed = Sentry::requireRole(['user', 'guest']);
等同于
Sentry::allow('user');
Sentry::allow('guest');
$isAllowed = Sentry::requireRole();
此外,此包的配置文件包括参数super_admin。分配给此键的角色将在调用Sentry::requireRole()时始终被允许。换句话说,对于角色中包含与super_admin中值匹配的值的用户,Sentry::requireRole()将返回TRUE。
示例
// config/packages/wesleyalmeida/sentry/config.php
'super_admin' => 'admin',
// login action
// User Roles
$user_roles = ['user', 'sales', 'admin']
// Add user roles to Sentry
Sentry::setUserRoles($user_roles);
// someAction()
$isAllowed = Sentry::requireRole(); // returns true
最终注意
-
用户角色不区分大小写。一旦开发者将它们提供给Sentry,所有用户角色都将被转换为小写。下划线不会转换为camelCase。因此,salesAdmin与salesadmin相同,但它们都不与sales_admin相同。
-
Sentry使用Laravel的Session来存储用户角色。如果您想将用户角色存储在Auth::user()对象中,可以在您UserProvider类要求的User对象中添加以下方法。如果用户角色在Session中过期,Sentry将抛出SentryKeyNotFoundException。捕获此异常,并使用以下方法重新设置用户角色:
Sentry::setUserRoles($user_roles);
示例
// Eloquent User
public function roles() {
$this->hasMany('SentryUserRoles', 'user_id', 'id); // SentryUserRoles must also be an Eloquent model
}
// Using QueryBuilder
public function roles() {
$table = DB::table('sentry_user_roles');
$query = $table->where('user_id', "=", $user_id);
return $query->lists('role');
}
安装
Composer
"require": {
"wesleyalmeida/sentry": "dev-master"
},
"repositories": [
{ "type": "vcs", "url": "git@github.com:wesleyalmeida/sentry.git" }
],
配置文件
php artisan config:publish wesleyalmeida/sentry"
数据库表
php artisan migrate --package="wesleyalmeida/sentry"