I am developing a module for a site using the Social Engine , which uses the Zend Framework. I am new to both the Zend Framework and the Social Engine, but I have experience in OOP and MVC architecture, so you can quickly learn the basics.
His test module, which I am developing, just created a simple module where the user can create, edit or delete information about the CD. Then there is a widget that can be displayed wherever they like, which displays information about the CD.
Now I am at the point where I need to set permissions for what CD people can see. So I studied other modules and found that the polling module is a concrete example.
Looking at other modules, I realized that when you create something, they allow the user to set their permissions manually.
So added this code to my form to create a selection box with the appropriate permissions:
$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 ) {
if(count($viewOptions) == 1) {
$privacy = new Zend_Form_Element_Hidden('auth_view');
$privacy->setValue(key($viewOptions));
} else {
$privacy = new Zend_Form_Element_Select('auth_view');
$privacy->setLabel('Privacy')
->setDescription('Who may see this CD?')
->setMultiOptions($viewOptions)
->setValue(key($viewOptions));
}
}
$this->addElements(array($artist, $title, $privacy, $submit));
Honestly, I'm not quite sure what this code does, except that it explicitly creates a selection field and populates it with the specified values.
So, if the user selects "Everyone", everyone should be able to delete and edit this cd, etc.
Obviously, I thought that the controller should have code that could affect the determination of whether the user has rights to view each CD, etc.
So, scanning the polling controller, I found that it is in the controller's init function:
public function init() {
$poll = null;
if( null !== ($pollIdentity = $this->_getParam('poll_id')) ) {
$poll = Engine_Api::_()->getItem('poll', $pollIdentity);
if( null !== $poll ) {
Engine_Api::_()->core()->setSubject($poll);
}
}
$this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
$this->view->viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity();
$resource = ( $poll ? $poll : 'poll' );
$viewer = ( $viewer && $viewer->getIdentity() ? $viewer : null );
if( !$this->_helper->requireAuth()->setAuthParams($resource, $viewer, 'view')->isValid() ) {
return;
}
}
, - editAction, :
if( !$this->_helper->requireUser()->isValid() ) {
return;
}
if( !$this->_helper->requireSubject()->isValid() ) {
return;
}
if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) {
return;
}
, , , editAction :
$auth = Engine_Api::_()->authorization()->context;
$roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
$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);
}
}
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% . - , , , , .