Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 2 - ACL check permission based on 'separate' roles
    primarykey
    data
    text
    <p>Let's say we have 3 main roles that are directly bound to the database table <code>user</code>: <code>ROLE_USER</code>, <code>ROLE_MODERATOR</code> and <code>ROLE_ADMIN</code>.</p> <p>BUT, we also got some other roles, which are used for the <code>Crews</code> component (see UML below). I use the following roles for actions peformed in a <code>Crew</code>: <code>ROLE_CREW_BOSS</code>, <code>ROLE_CREW_LEFTHAND</code>, <code>ROLE_CREW_RIGHTHAND</code>, <code>ROLE_CREW_MEMBER</code>.</p> <pre> +----------------+ +------------------+ | users | | crews | |----------------| |------------------| | id | | id | | username &lt;---+ | name | | password | | +---&gt; cash | | roles | | +-------------------+ | | ... | | ... | | | crew_members | | | | | | | |-------------------| | | | +----------------+ | | crew_id +--------------+ | | +----+ user_id | +--------^---------+ | roles | | | ... | +------------+ | | | | | | +------------------+ | | | | forum_topics | | | | |------------------| | | | | id | +-------------------+ +---+ crew_id | | title | | description | | ... | | | | | | | +------------------+</pre> <p>That is the base structure, I hope that part is clear. Now comes the problem...</p> <p><strong>The problem</strong></p> <p>Every user with the role <code>ROLE_MODERATOR</code> can create <code>ForumTopic</code> objects, but not the one where <code>crew_id</code> is set, because that one is private for a specific crew. Also, only crew members (which are also users) that have the role <code>ROLE_CREW_BOSS</code>, <code>ROLE_CREW_LEFTHAND</code> or <code>ROLE_CREW_RIGHTHAND</code> can edit the forum topics of their crew. How do I check those kind of complexity? With a <code>Voter</code> maybe?</p> <p><strong>UPDATE 1</strong></p> <p>I have solved the problem for 50%, but it's not solid. I've created a voter specific for the object <code>Entity\\ForumTopic</code>.</p> <pre><code>public function vote(TokenInterface $token, $object, array $attributes) { if ($object instanceof ObjectIdentityInterface) { if ($object-&gt;getType() == 'Entity\\ForumTopic') { /** * @var Member $member */ $member = $token-&gt;getUser(); $userTable = new UserTable(); $user = $userTable-&gt;getByMember($member); $userInCrewTable = new UserInCrewTable(); $crewMember = $userInCrewTable-&gt;getByUser($user); if ($crewMember &amp;&amp; in_array($crewMember-&gt;getRole(), array('boss', 'lefthand', 'righthand'))) { return self::ACCESS_GRANTED; } } } return self::ACCESS_ABSTAIN; } </code></pre> <p>The only problem here is that I don't use the respective roles, so I can't use the role hierarchy functionality for example. Anyone got a better solution or a improvement on my current solution?</p> <p>Thanks!</p> <p>Steffen</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.
 

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