1

If I put this (or any JQuery) in an .extra tpl file it works

{literal}
<script>
if (!CRM.checkPerm("administer CiviCRM")) {
CRM.$( function( $ ) {
    $( document ).ajaxComplete(function( event, request, settings ) {
   //do stuff
 })
 })
 }
{/literal}
</script>

If I switch the permission away from administer CiviCRM, it does not work and executes the code for all users. Carrying over from here: Removing elements for non-admin users.

2

If you want to use CRM.checkPerm() JS function than you will need to add permission using hook and than it will be catched in CRM.checkPerm() js function. eg: In hook

function extname_civicrm_buildForm($formName, &$form) {
  if ('Some_FormName' == $formName) {
    CRM_Core_Resources::singleton()->addPermissions('administer CiviCRM');
  }
}

and than in tpl

{literal}
<script>
  CRM.$( function( $ ) {
    if (!CRM.checkPerm("administer CiviCRM")) {
      $( document ).ajaxComplete(function(event, request, settings) {
       //do stuff
      });
    }
  });
{/literal}
</script>

An other alternate could be

{if !call_user_func(array('CRM_Core_Permission','check'), 'administer CiviCRM')}
  {literal}
  <script>
    CRM.$( function( $ ) {
      $( document ).ajaxComplete(function(event, request, settings) {
        //do stuff
      })
    });
  {/literal}
  </script>
{/if}
| improve this answer | |
  • Wonderful. I tested a couple core permissions and it works. FWIW, I also tried keying off a permissioning created by your custom permissions ext but that apparently isn't included in the core permission check. – Andy Burns Mar 24 at 17:10
  • Have updated my answer how you can use CRM.checkPerm() functions – Pradeep Nayak Mar 24 at 17:13
  • The custom permission extension does work, depends upon how you using it. You need to check against the value of the options rather label. If you can add some example than it would be good. – Pradeep Nayak Mar 24 at 17:23
  • Can you mark this as answered if it has fixed the problem? – Pradeep Nayak Mar 24 at 17:27
  • So if a custom permission has a option value of 1, what does that change? – Andy Burns Mar 24 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.