Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding user permissions and how to apply it
    primarykey
    data
    text
    <p>I am developing a module for a site using <a href="http://www.socialengine.com/" rel="noreferrer">Social Engine</a>, which uses Zend Framework. I am new to both Zend Framework and Social Engine but have experience in OOP and MVC architecture so could get to grips with basics relatively quickly.</p> <p>Its a test module I'm developing so have just built a simple module where the user can create, edit or delete CD information. Then there is a widget which can be displayed where they like which shows there CD information. </p> <p>I am now at the point where I need to set permissions of what CDs people can see etc. So I studied other modules and found the Poll module to be a concrete example.</p> <p>Looking at other modules I realised that when you create something, they let the user set their permissions manually.</p> <p>So added this code to my form to create a select box with relevant permissions:</p> <pre><code>$auth = Engine_Api::_()-&gt;authorization()-&gt;context; $user = Engine_Api::_()-&gt;user()-&gt;getViewer(); $viewOptions = (array) Engine_Api::_()-&gt;authorization()-&gt;getAdapter('levels')-&gt;getAllowed('ryan', $user, 'auth_view'); $viewOptions = array_intersect_key($availableLabels, array_flip($viewOptions)); $privacy = null; if( !empty($viewOptions) &amp;&amp; count($viewOptions) &gt;= 1 ) { // Make a hidden field if(count($viewOptions) == 1) { //$this-&gt;addElement('hidden', 'auth_view', array('value' =&gt; key($viewOptions))); $privacy = new Zend_Form_Element_Hidden('auth_view'); $privacy-&gt;setValue(key($viewOptions)); // Make select box } else { $privacy = new Zend_Form_Element_Select('auth_view'); $privacy-&gt;setLabel('Privacy') -&gt;setDescription('Who may see this CD?') -&gt;setMultiOptions($viewOptions) -&gt;setValue(key($viewOptions)); /*$this-&gt;addElement('Select', 'auth_view', array( 'label' =&gt; 'Privacy', 'description' =&gt; 'Who may see this CD?', 'multiOptions' =&gt; $viewOptions, 'value' =&gt; key($viewOptions), ));*/ } } $this-&gt;addElements(array($artist, $title, $privacy, $submit)); </code></pre> <p>To be honest I'm not entirely sure what this code does apart from obviously create a select box and fill it with values specified. </p> <p>So if the user selects 'Everyone' everyone should be able to delete and edit that cd, and so on.</p> <p>Obviously I thought controller must have some code that might deal with determining whether the user has the rights to view each cd etc. </p> <p>So scanning the Poll controller I found this is in the init function of the controller:</p> <pre><code>public function init() { // Get subject $poll = null; if( null !== ($pollIdentity = $this-&gt;_getParam('poll_id')) ) { $poll = Engine_Api::_()-&gt;getItem('poll', $pollIdentity); if( null !== $poll ) { Engine_Api::_()-&gt;core()-&gt;setSubject($poll); } } // Get viewer $this-&gt;view-&gt;viewer = $viewer = Engine_Api::_()-&gt;user()-&gt;getViewer(); $this-&gt;view-&gt;viewer_id = Engine_Api::_()-&gt;user()-&gt;getViewer()-&gt;getIdentity(); // only show polls if authorized $resource = ( $poll ? $poll : 'poll' ); $viewer = ( $viewer &amp;&amp; $viewer-&gt;getIdentity() ? $viewer : null ); if( !$this-&gt;_helper-&gt;requireAuth()-&gt;setAuthParams($resource, $viewer, 'view')-&gt;isValid() ) { return; } } </code></pre> <p>And in each action at the top they have some different authorization code, one such example is the <code>editAction</code> which has this code right at the top:</p> <pre><code>// Check auth if( !$this-&gt;_helper-&gt;requireUser()-&gt;isValid() ) { return; } if( !$this-&gt;_helper-&gt;requireSubject()-&gt;isValid() ) { return; } if( !$this-&gt;_helper-&gt;requireAuth()-&gt;setAuthParams(null, null, 'edit')-&gt;isValid() ) { return; } </code></pre> <p>also in the same action is has several other bits i don't understand what they are doing, below is random snippets from the <code>editAction</code> in the Poll controller:</p> <pre><code>$auth = Engine_Api::_()-&gt;authorization()-&gt;context; $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone'); // Populate form with current settings $form-&gt;search-&gt;setValue($poll-&gt;search); foreach( $roles as $role ) { if( 1 === $auth-&gt;isAllowed($poll, $role, 'view') ) { $form-&gt;auth_view-&gt;setValue($role); } if( 1 === $auth-&gt;isAllowed($poll, $role, 'comment') ) { $form-&gt;auth_comment-&gt;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); </code></pre> <p>My problem is I really don't understand much if any of the above and after sitting on it for a couple of days and googling to my fingers hurt I still don't really have a clue if I am 100% honest. Can any of the above be cleared up for me, help explain things to me, and if possible how can i apply the permissions I want to my module. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload