mozarcik/yii-auth

Yii PHP框架授权管理器的Web界面。

1.4.1 2015-04-20 10:27 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:08:04 UTC


README

Auth是Yii PHP框架的一个模块,提供了对Yii内置授权管理器(CAuthManager)的Web用户界面。有关Yii授权管理器的更多信息,请参阅框架文档中的身份验证和授权部分。

Auth的开发旨在为Yii项目提供现代且响应式的用户界面来管理用户权限。为了实现这一目标,它使用了我的流行Twitter Bootstrap扩展

Auth遵循Yii的约定,并遵循关注点分离原则,因此不需要你从其类扩展。相反,它通过单个行为为授权管理器提供额外的功能。

演示

你可以在这里尝试实时演示。

要求

使用方法

设置

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 提供翻译吗?如果是,请为其发起一个拉取请求。翻译应放置在以其区域设置命名的文件夹下的 messages 文件夹中(例如 en_us)。

注意

注意:版本 1.0.6-wip 使用和需要 yiistrap!!yiistrap 是下一代 yii-bootsrap。