crisu83 / yii-auth
Yii PHP框架授权管理器的Web用户界面。
Requires
- crisu83/yiistrap: >=1.0.0
Requires (Dev)
- yiisoft/yii: >=1.1.0
This package is auto-updated.
Last update: 2024-08-29 04:08:14 UTC
README
Auth是Yii PHP框架的一个模块,提供了一个Web用户界面来管理Yii内置的授权管理器(CAuthManager)。您可以在框架文档的认证和授权部分了解更多关于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.*代替actionId来覆盖控制器中的所有操作,或使用module.*代替controllerId来覆盖模块中的所有控制器。
public function filters() { return array( array('auth.filters.AuthFilter'), ); }
有关过滤器的更多信息,请参阅框架文档中的控制器。
国际化
您希望为Auth提供翻译吗?如果是,请为它发起一个pull request。翻译应放置在其区域名称命名的文件夹(例如:en_us)下的messages文件夹中。
注意
注意:版本1.0.6-wip使用和需要yiistrap!!yiistrap是下一代yii-bootsrap。