php - 了解用户权限以及如何应用它

标签 php zend-framework permissions user-permissions socialengine

我正在使用 Social Engine 为站点开发模块,它使用 Zend 框架。我是 Zend Framework 和 Social Engine 的新手,但在 OOP 和 MVC 架构方面有经验,因此可以相对快速地掌握基础知识。

这是我正在开发的一个测试模块,所以刚刚构建了一个简单的模块,用户可以在其中创建、编辑或删除 CD 信息。然后有一个小部件,可以显示在他们喜欢的地方,显示 CD 信息。

我现在正处于需要设置人们可以看到哪些 CD 等权限的地步。所以我研究了其他模块,发现 Poll 模块是一个具体的例子。

查看其他模块后,我意识到当您创建某些内容时,它们会让用户手动设置权限。

因此将此代码添加到我的表单中以创建具有相关权限的选择框:

$auth = Engine_Api::_()->authorization()->context;
$user = Engine_Api::_()->user()->getViewer();
$viewOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('ryan', $user, 'auth_view');
$viewOptions = array_intersect_key($availableLabels, array_flip($viewOptions));

$privacy = null;

if( !empty($viewOptions) && count($viewOptions) >= 1 ) {
    // Make a hidden field
    if(count($viewOptions) == 1) {
        //$this->addElement('hidden', 'auth_view', array('value' => key($viewOptions)));
        $privacy  = new Zend_Form_Element_Hidden('auth_view');
        $privacy->setValue(key($viewOptions));
        // Make select box
    } else {
        $privacy = new Zend_Form_Element_Select('auth_view');
        $privacy->setLabel('Privacy')
                ->setDescription('Who may see this CD?')
                ->setMultiOptions($viewOptions)
                ->setValue(key($viewOptions));
        /*$this->addElement('Select', 'auth_view', array(
            'label' => 'Privacy',
            'description' => 'Who may see this CD?',
            'multiOptions' => $viewOptions,
            'value' => key($viewOptions),
        ));*/
    }
}

$this->addElements(array($artist, $title, $privacy, $submit));

老实说,除了显然创建一个选择框并用指定的值填充它之外,我不完全确定这段代码的作用。

因此,如果用户选择“每个人”,每个人都应该能够删除和编辑该 CD,等等。

显然我认为 Controller 必须有一些代码来处理确定用户是否有权查看每张 cd 等。

所以扫描 Poll Controller 我发现这是在 Controller 的 init 函数中:

public function init() {
    // Get subject
    $poll = null;
    if( null !== ($pollIdentity = $this->_getParam('poll_id')) ) {
        $poll = Engine_Api::_()->getItem('poll', $pollIdentity);
        if( null !== $poll ) {
            Engine_Api::_()->core()->setSubject($poll);
        }
    }

    // Get viewer
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    $this->view->viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity();

    // only show polls if authorized
    $resource = ( $poll ? $poll : 'poll' );
    $viewer = ( $viewer && $viewer->getIdentity() ? $viewer : null );
    if( !$this->_helper->requireAuth()->setAuthParams($resource, $viewer, 'view')->isValid() ) {
        return;
    }
}

在顶部的每个操作中,它们都有一些不同的授权代码,一个这样的例子是 editAction,它在顶部有这个代码:

// Check auth
if( !$this->_helper->requireUser()->isValid() ) {
    return;
}
if( !$this->_helper->requireSubject()->isValid() ) {
    return;
}
if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) {
    return;
}

在同一个 Action 中还有其他几个我不明白他们在做什么,下面是来自轮询 Controller 中 editAction 的随机片段:

$auth = Engine_Api::_()->authorization()->context;
$roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');

// Populate form with current settings
$form->search->setValue($poll->search);
foreach( $roles as $role ) {
    if( 1 === $auth->isAllowed($poll, $role, 'view') ) {
        $form->auth_view->setValue($role);
    }
    if( 1 === $auth->isAllowed($poll, $role, 'comment') ) {
        $form->auth_comment->setValue($role);
    }
}

// CREATE AUTH STUFF HERE
if( empty($values['auth_view']) ) {
    $values['auth_view'] = array('everyone');
}
if( empty($values['auth_comment']) ) {
    $values['auth_comment'] = array('everyone');
}

$viewMax = array_search($values['auth_view'], $roles);
$commentMax = array_search($values['auth_comment'], $roles);

我的问题是,如果我 100% 诚实,我真的不太了解以上任何一项,在坐了几天并用谷歌搜索我的手指受伤后,我仍然不知道我是否真的有任何线索。是否可以为我解决以上任何问题,帮助向我解释事情,如果可能的话,我如何将我想要的权限应用到我的模块。

最佳答案

我将提供有关如何使用授权的简要说明,但需要通过查看 SocialEngine 的代码来推断更详细的信息。请注意,虽然我们不为 SocialEngine 编译文档,但我们的开发人员在我们的代码中使用了 PHPDocumentor 风格的语法,您可以使用像 Neatbeans (http://netbeans.org/) 这样的 IDE 来快速访问该信息。

SocialEngine 有一些 Controller Action 助手类,用于 Action Controller 中的查询授权:

  • application/modules/Authorization/Controller/Action/Helper/RequireAuth.php
  • application/modules/Core/Controller/Action/Helper/RequireAbstract.php
  • application/modules/Core/Controller/Action/Helper/RequireAdmin.php
  • application/modules/Core/Controller/Action/Helper/RequireSubject.php
  • application/modules/Core/Controller/Action/Helper/RequireUser.php

在大多数情况下,您唯一关心的是这些:

  • application/modules/Authorization/Controller/Action/Helper/RequireAuth.php
  • application/modules/Core/Controller/Action/Helper/RequireSubject.php
  • application/modules/Core/Controller/Action/Helper/RequireUser.php

可以在 Album_AlbumController 类中找到如何使用这些助手的一个很好的例子: 应用程序/模块/Album/controllers/AlbumController.php

public function init()
{
if( !$this->_helper->requireAuth()->setAuthParams('album', null, 'view')->isValid() ) return;

if( 0 !== ($photo_id = (int) $this->_getParam('photo_id')) &&
null !== ($photo = Engine_Api::_()->getItem('album_photo', $photo_id)) )
{
Engine_Api::_()->core()->setSubject($photo);
}

else if( 0 !== ($album_id = (int) $this->_getParam('album_id')) &&
null !== ($album = Engine_Api::_()->getItem('album', $album_id)) )
{
Engine_Api::_()->core()->setSubject($album);
}
}

public function editAction()
{
if( !$this->_helper->requireUser()->isValid() ) return;
if( !$this->_helper->requireSubject('album')->isValid() ) return;
if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) return;

init 函数中的代码简单地设置了访问页面的要求,然后在 editAction 函数中,检查授权数据。 requireSubject 和 requireUser 助手非常简单:

  1. requireSubject 期望页面的主题设置在 上面的例子在 init 函数中完成
  2. requireUser 检查查看者是否是登录用户

requireAuth 帮助程序不太直接。为了简洁起见,我将省略大部分抽象的内部工作。最后,helper 指向 Authorization_Api_Core::isAllowed 函数: 应用程序/模块/授权/核心/Api.php

/**
* Gets the specified permission for the context
*
* @param Core_Model_Item_Abstract|string $resource The resource type or object that is being accessed
* @param Core_Model_Item_Abstract $role The item (user) performing the action
* @param string $action The name of the action being performed
* @return mixed 0/1 for allowed, or data for settings
*/
public function isAllowed($resource, $role, $action = 'view')

函数期望的 $resource 和 $role 对象是 Zend_Db_Table_Row 的实例,它在 SocialEngine 中被称为 Models 并且期望位于模块的 Models 目录中。调用 isAllowed 函数时,授权 API 将根据 engine4_authorization_allow、engine4_authorization_levels 和 engine4_authorization_permissions 表查询数据库。

  1. engine4_authorization_levels 表包含成员级别 由开箱即用的 SocialEngine 创建,以及自定义成员 从管理中的“管理”>“成员(member)级别”部分创建的级别 面板。
  2. engine4_authorization_permissions 表包含所有 default和admin指定的权限处理,比如member 电平设置。
  3. engine4_authorization_allow 包含 单个对象的权限数据。例如资料 关于谁能够查看相册的信息将放在那里。 engine4_authorization_allow.role_id 是否(映射到项目 模型的 ID)被允许访问 engine4_authorization_allow.resource_id(映射到项目 ID 型号)由 engine4_authorization_allow.value 列确定 其中应包含数字 0-5。

应用程序/模块/授权/Api/Core.php

class Authorization_Api_Core extends Core_Api_Abstract
{
/**
* Constants
*/
const LEVEL_DISALLOW = 0;
const LEVEL_ALLOW = 1;
const LEVEL_MODERATE = 2;
const LEVEL_NONBOOLEAN = 3;
const LEVEL_IGNORE = 4;
const LEVEL_SERIALIZED = 5;

0) 不允许访问链接的资源。这与允许表中不存在的行相同

1) 也允许访问链接的资源

2) 允许访问和管理资源(即 super 管理员、管理员和版主成员级别)

3-5) 因不允许而被忽略。这些需要一些自定义逻辑才能适本地处理授权。

关于php - 了解用户权限以及如何应用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11520448/

相关文章:

javascript - jQuery 表单提交后返回空白页

php - 要解决 "assignment in condition"使用 !==false 减慢脚本。为什么?

php - Zend_Soap - 错误解析 WSDL : Start tag expected, '<' 未找到

permissions - Gitlab:我可以创建一个仅对某些开发人员可见的分支吗?

java - android java权限拒绝manifest.xml

java - AndroidManifest 中的多个权限?

javascript - 即使浏览器窗口关闭后仍继续使用 setTimeout 执行 AJAX

php - 将字母字符交换为特殊字符的最佳方法是什么? (a = !, b = ", c = £, ...)

php - 将输入与掩码匹配

php - 如何在调用类的成员函数时自动调用函数?