<p>After some digging I came up with some answers myself. For future reference, here they are:</p>

<p>Is the event triggered by the CP: </p>

<p><code>if(craft()-&gt;request-&gt;isCpRequest()){}</code></p>

<p>Check the logged in user's permissions:</p>

<pre><code>$userPermissions = craft()-&gt;userPermissions-&gt;getPermissionsByUserId($user-&gt;id);
</code></pre>

<p>or use a permission directly:</p>

<pre><code>if($user-&gt;can('permissionName')){}
</code></pre>

<p>Add an extra param to the criteria that filters to correct region:</p>

<pre><code>$query = $event-&gt;params['query'];
$params = array( ':regio' =&gt; '["us","eu"]' );
$query-&gt;andWhere('content.field_regio LIKE :regio', $params);
</code></pre>

<p>Send the changed ElementCriterioModel (or query) to wherever it needs to go:</p>

<pre><code>After setting that query, the function just executes and filters the list.
</code></pre>

<p>If you use something like this, make sure you filter out the correct section to check on (because the <code>onBuildElementsQuery</code> is fired on almost everything in the CP). But that's where I stopped my search as eventually, we will not be needing it in this project.</p>

CC BY-SA 3.0
2017-03-23T13:26:05.163Z
2017-03-23T13:26:05.163Z
Tom De Smet
<p>We started a new project, a website that lists products based on regions (for example US and Europe). We have all products in 1 channel (to make it dynamic to add more regions later on) and have a region field added to those entries (a checkbox field). We now want to hide products for specific users in the CP.</p>

<p>Through this <a href="https://craftcms.stackexchange.com/questions/14627/restrict-entry-editing-to-category-topic/14632#14632">thread</a> I was able to add custom permissions via my plugin and it pointed me towards this event <a href="https://craftcms.com/docs/plugins/events-reference#elements-onBeforeBuildElementsQuery" rel="nofollow noreferrer">onBeforeBuildElementsQuery</a> that I could use to add my custom filtering.
I think I may need to add a custom param with the value of what the regio field needs to be to the ElementCriteriaModel.</p>

<p>But I have no idea how to build the next steps. I don't really understand what that ElementCriteriaModel returns. It's just a model with all basic attributes, no custom fields or anything. No clear section it targets...</p>

<p>The next steps should be:</p>

<ol>
<li>Is the event triggered by the CP?</li>
<li>If so, check the logged in user's permissions and decide what regio should be filtered</li>
<li>Add an extra param to the criteria that filters to correct region</li>
<li>Send the changed ElementCriterioModel (or Query) to wherever it needs to go</li>
</ol>

<p>So, how do I insert a custom parameter into that ElementCriteriaModel and will it alter the entries that are visible in the CP? Am I on the right track here?</p>

<plugin-development><control-panel><elementcriteriamodel>
Filter entries in the CP based on custom permission
# Filter entries in the CP based on custom permission

(Asked by: username_0 on 2017-03-22)

We started a new project, a website that lists products based on regions (for example US and Europe). We have all products in 1 channel (to make it dynamic to add more regions later on) and have a region field added to those entries (a checkbox field). We now want to hide products for specific users in the CP.

Through this [thread](https://craftcms.stackexchange.com/questions/14627/restrict-entry-editing-to-category-topic/14632#14632) I was able to add custom permissions via my plugin and it pointed me towards this event [onBeforeBuildElementsQuery](https://craftcms.com/docs/plugins/events-reference#elements-onBeforeBuildElementsQuery) that I could use to add my custom filtering.
I think I may need to add a custom param with the value of what the regio field needs to be to the ElementCriteriaModel.

But I have no idea how to build the next steps. I don't really understand what that ElementCriteriaModel returns. It's just a model with all basic attributes, no custom fields or anything. No clear section it targets...

The next steps should be:

1. Is the event triggered by the CP?
2. If so, check the logged in user's permissions and decide what regio should be filtered
3. Add an extra param to the criteria that filters to correct region
4. Send the changed ElementCriterioModel (or Query) to wherever it needs to go

So, how do I insert a custom parameter into that ElementCriteriaModel and will it alter the entries that are visible in the CP? Am I on the right track here?

**Tags:** plugin-development, control-panel, elementcriteriamodel

---

## 1 answers

### Answer by username_0 on 2017-03-23. Score: 4 | Accepted answer

After some digging I came up with some answers myself. For future reference, here they are:
Is the event triggered by the CP: 
if(craft()->request->isCpRequest()){}
Check the logged in user's permissions:
$userPermissions = craft()->userPermissions->getPermissionsByUserId($user->id);

or use a permission directly:
if($user->can('permissionName')){}

Add an extra param to the criteria that filters to correct region:
$query = $event->params['query'];
$params = array( ':regio' => '["us","eu"]' );
$query->andWhere('content.field_regio LIKE :regio', $params);

Send the changed ElementCriterioModel (or query) to wherever it needs to go:
After setting that query, the function just executes and filters the list.

If you use something like this, make sure you filter out the correct section to check on (because the onBuildElementsQuery is fired on almost everything in the CP). But that's where I stopped my search as eventually, we will not be needing it in this project.


---

