pascalbrewing / yii-auth-module
Yii Web 编程认证模块
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 04:50:43 UTC
README
Auth 是一个为 Yii PHP 框架提供的认证管理器(CAuthManager)的 Web 用户界面的模块。您可以在框架文档的 认证和授权 部分了解更多有关 Yii 认证管理器的信息。
Auth 是为了提供现代和响应式的用户界面来管理 Yii 项目的用户权限而开发的。为了实现其目标,它使用了流行的 Twitter Bootstrap 扩展。
Auth 严格按照 Yii 的约定编写,并遵循 关注点分离 原则,因此不需要您从其类扩展。相反,它通过单个行为为授权管理器提供额外的功能。
演示
您可以在这里尝试实时演示 此处。
要求
- Yii 的 Twitter Bootstrap 扩展版本 2.0.0 或更高版本
使用
设置
从 Yii 扩展下载最新版本。
解压模块到 protected/modules/auth,并将以下内容添加到您的应用程序配置中
return array(
'modules' => array(
'auth',
),
'components' => array(
'authManager' => array(
.....
'behaviors' => array(
'auth' => array(
'class' => 'auth.components.AuthBehavior',
),
),
),
'user' => array(
'class' => 'auth.components.AuthWebUser',
'admins' => array('admin', 'foo', 'bar'), // users with full access
),
),
);
protected/config/main.php
请注意,虽然该模块不需要您使用数据库,但如果您想使用 CDbAuthManager,则需要其模式(可以在框架的 web/auth 下找到)。
配置
根据您的需求配置模块。以下是可以用的配置列表(带默认值)。
'auth' => array(
'strictMode' => true, // when enabled authorization items cannot be assigned children of the same type.
'userClass' => 'User', // the name of the user model class.
'userIdColumn' => 'id', // the name of the user id column.
'userNameColumn' => 'name', // the name of the user name column.
'defaultLayout' => 'application.views.layouts.main', // the layout used by the module.
'viewDir' => null, // the path to view files to use with this module.
),
启用缓存
要为 CDbAuthManager 启用缓存,您可以使用提供访问检查缓存的 CachedDbAuthManager。以下是该组件的配置示例
'authManager'=>array(
'class'=>'auth.components.CachedDbAuthManager',
'cachingDuration'=>3600,
),
检查访问
当您希望检查当前用户是否具有某种权限时,可以使用 CWebUser::checkAccess() 方法,该方法可以通过 Yii::app() 在应用程序的任何地方访问,如下所示
if (Yii::app()->user->checkAccess('itemName')) // itemName = name of the operation
{
// access is allowed.
}
为了保持您的权限动态,您不应该检查特定的角色或任务,而应该始终检查操作。有关 Yii 认证管理器的更多信息,请参阅框架文档中的 认证和授权。
使用过滤器检查访问
您还可以使用过滤器在控制器操作被调用之前自动检查访问。此过滤器使用的操作必须命名为以下格式 (moduleId.)controllerId.actionId,其中 moduleId 是可选的。您还可以使用通配符 controllerId. 覆盖控制器中的所有操作,或者使用通配符 module.** 覆盖模块中的所有控制器。
public function filters()
{
return array(
array('auth.filters.AuthFilter'),
),
}
有关过滤器如何工作的更多信息,请参阅框架文档中的 控制器。
国际化
您想为 Auth 提供翻译吗?如果是这样,请提交一个拉取请求。翻译应放在消息文件夹中,文件夹名称按照其区域设置命名(例如 en_us)。
注意
注意:版本 1.0.6-wip 使用和需要 yiistrap!! yiistrap 是下一代 yii-bootsrap