Question

How can I show or hide html elements based on a modx user's group membership?

For example I have an edit button and a view button on a given page, I want users who have permission to access the edit resource to be able to see the button while it should be hidden from all other users.

UPDATE I wound up writing a little thing to get the job done:

call snippet getButton

[[!getButton? &rid =`20` &btn=`ButtonEditTpl` &qs=`contract_id=[[!+contract_id]]`]]

The snippet

<?php
    $btn = isset($btn) ? $btn : null;

    $qs = isset($qs) ? $qs : 'this=that';

    $rid = isset($rid) ? $rid : 0;

    $output = '';

    $resource = $modx->getObject('modResource',$rid);

    if($resource){

      $url = $modx->makeUrl($rid, '', $qs);

      $output = $modx->getChunk($btn,array('url' => $url));

    }

    return $output;
?>
Was it helpful?

Solution

Just use Personalize snippet http://bobsguides.com/personalize-tutorial.html

[[!Personalize?
    &yesChunk=`@CODE:<p>Place for your button</p>`
    &noChunk=`@CODE:<p>You are not logged in</p>`
    &allowedGroups=`MembersOnly,Managers`
]]

OTHER TIPS

There are two right ways to solve this problem in MODX style: resource and context permissions.

  • First of all you should create new “Policy template” (Security » Access Controls) for your users with new custom permission. For ex.: “can_edit_my_resources_permission”. Policies template

Permissions at “Admin template group” will be valid in context for all users in any usergroup which has accesses to the context with you custom policy. Permissions at “Resource template group” are valid in context for all resources in specified resource group.

  • You should create new “Access policy” based on you custom template. Your permission must be checked.
  • On that step you should decide: what is you style?

If you have a smidgen of resources where user should have new permissions you can choose resource groups. Also at that step you have to create new resource group.

If there are a lot of resources or will be in close future your have to choose user group with your custom context policy:

So we are close to the end.

  • Let's flush permissions and clear all the cache.
  • After all you can check permissions via MODX api.

For context: $modx->hasPermission('can_edit_my_resources_permission');

For resource: $modx->resource->checkPolicy('can_edit_my_resources_permission');

Anyway you will get true for users who are your user group's members which has access to the context with you Access policy or has access to your resource group with the same Access policy.

P.S.: All roles must be the same. Other way that won't work :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top