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

RSM*_*RSM 5 php permissions zend-framework user-permissions socialengine

我正在使用使用Zend Framework的Social Engine为站点开发模块.我是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));
Run Code Online (Sandbox Code Playgroud)

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

因此,如果用户选择"所有人",则每个人都应该能够删除和编辑该CD,依此类推.

显然我认为控制器必须有一些代码可以处理确定用户是否有权查看每个CD等.

所以扫描Poll控制器我发现这是在控制器的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在顶部的每个操作中,它们都有一些不同的授权代码,其中一个例子就是将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;
}
Run Code Online (Sandbox Code Playgroud)

同样的动作还有其他几个我不明白他们在做什么,下面是editActionPoll控制器中的随机片段:

$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);
Run Code Online (Sandbox Code Playgroud)

我的问题是,我真的不明白,如果上面的任何一个,坐了几天后谷歌搜索我的手指伤害我仍然没有真正的线索,如果我100%诚实.可以为我清除上述任何内容,帮助向我解释一下,如果可能的话,我如何将我想要的权限应用到我的模块中.

小智 7

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

SocialEngine有一些控制器动作助手类,用于动作控制器中的查询授权:

  • 应用/模块/授权/控制器/动作/助手/ RequireAuth.php
  • 应用/模块/核心/控制器/动作/助手/ RequireAbstract.php
  • 应用/模块/核心/控制器/动作/助手/ RequireAdmin.php
  • 应用/模块/核心/控制器/动作/助手/ RequireSubject.php
  • 应用/模块/核心/控制器/动作/助手/ RequireUser.php

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

  • 应用/模块/授权/控制器/动作/助手/ RequireAuth.php
  • 应用/模块/核心/控制器/动作/助手/ RequireSubject.php
  • 应用/模块/核心/控制器/动作/助手/ RequireUser.php

可以在Album_AlbumController类中找到如何使用这些帮助程序的一个很好的示例:application/modules/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;
Run Code Online (Sandbox Code Playgroud)

init函数中的代码只是设置访问页面的要求,然后在editAction函数中,对授权数据运行检查.requireSubject和requireUser助手非常简单:

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

requireAuth助手稍微不那么直截了当.为简洁起见,我将省略大部分抽象的内部工作.最后,帮助程序指向Authorization_Api_Core :: isAllowed函数:application/modules/Authorization/Core/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')
Run Code Online (Sandbox Code Playgroud)

函数所需的$ 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开箱即用创建的成员级别,以及从管理面板中的Manage> Member Levels部分创建的自定义成员级别.
  2. engine4_authorization_permissions表包含所有默认和管理员指定的权限处理,例如成员级别设置.
  3. engine4_authorization_allow包含各个对象的权限数据.例如,有关谁能够查看相册的信息将被放置在那里.是否允许engine4_authorization_allow.role_id(映射到模型的项目ID)访问engine4_authorization_allow.resource_id(映射到模型的项目ID)由engine4_authorization_allow.value列确定,该列应包含数字0-5.

应用/模块/授权/原料药/ 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;
Run Code Online (Sandbox Code Playgroud)

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

1)允许访问链接的资源

2)允许访问和调节资源(即Superadmin,Admin和主持人级别)

3-5)因不允许而被忽略.这些期望一些自定义逻辑,以便适当地处理授权.