Answers.Body: <p>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</p>
  
  <pre><code>function extname_civicrm_buildForm($formName, &amp;$form) {
    if ('Some_FormName' == $formName) {
      CRM_Core_Resources::singleton()-&gt;addPermissions('administer CiviCRM');
    }
  }
  </code></pre>
  
  <p>and than in tpl</p>
  
  <pre><code>{literal}
  &lt;script&gt;
    CRM.$( function( $ ) {
      if (!CRM.checkPerm("administer CiviCRM")) {
        $( document ).ajaxComplete(function(event, request, settings) {
         //do stuff
        });
      }
    });
  {/literal}
  &lt;/script&gt;
  </code></pre>
  
  <p>An other alternate could be</p>
  
  <pre><code>{if !call_user_func(array('CRM_Core_Permission','check'), 'administer CiviCRM')}
    {literal}
    &lt;script&gt;
      CRM.$( function( $ ) {
        $( document ).ajaxComplete(function(event, request, settings) {
          //do stuff
        })
      });
    {/literal}
    &lt;/script&gt;
  {/if}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T17:10:40.237Z
Answers.Text: 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.
Answers.UserDisplayName: Andy Burns
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T17:13:31.647Z
Answers.Text: Have updated my answer how you can use CRM.checkPerm() functions
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T17:23:20.470Z
Answers.Text: 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.
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T17:27:10.700Z
Answers.Text: Can you mark this as answered if it has fixed the problem?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T17:33:59.770Z
Answers.Text: So if a custom permission has a option value of 1, what does that change?
Answers.UserDisplayName: Andy Burns
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T17:41:01.963Z
Answers.Text: You shouldn't have numeric as value, for reference see https://lab.civicrm.org/extensions/custompermissions/-/blob/master/images/CustomPermission.gif
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T18:30:42.993Z
Answers.Text: In your gif it shows value of 1
Answers.UserDisplayName: Andy Burns
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T18:56:24.707Z
Answers.Text: Ohh yeah sorry, it should be the name i.e civicrm_option_value.name field
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T19:24:22.860Z
Answers.Text: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/105919/discussion-between-andy-burns-and-pradeep-nayak).
Answers.UserDisplayName: Andy Burns
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T16:48:44.220Z
Answers.LastActivityDate: 2020-03-24T17:12:59.077Z
Answers.LastEditDate: 2020-03-24T17:12:59.077Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>If I put this (or any JQuery) in an .extra tpl file it works</p>
  
  <pre><code>{literal}
  &lt;script&gt;
  if (!CRM.checkPerm("administer CiviCRM")) {
  CRM.$( function( $ ) {
      $( document ).ajaxComplete(function( event, request, settings ) {
     //do stuff
   })
   })
   }
  {/literal}
  &lt;/script&gt;
  </code></pre>
  
  <p>If I switch the permission away from <strong>administer CiviCRM</strong>, it does not work and executes the code for all users. Carrying over from here: <a href="https://civicrm.stackexchange.com/questions/33464/removing-elements-for-non-admin-users#comment38397_33465">Removing elements for non-admin users</a>.</p>
  
Tags: <permissions><templates>
Title: Only !CRM.checkPerm("administer CiviCRM")) conditional works
ThreadText: # Only !CRM.checkPerm("administer CiviCRM")) conditional works
  
  (Asked by: username_0 on 2020-03-24)
  
  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](https://civicrm.stackexchange.com/questions/33464/removing-elements-for-non-admin-users#comment38397_33465).
  
  **Tags:** permissions, templates
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-24. Score: 2 | Accepted answer
  
  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}
  
  ```
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_1**: Have updated my answer how you can use CRM.checkPerm() functions  
  > **username_1**: 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.  
  > **username_1**: Can you mark this as answered if it has fixed the problem?  
  > **username_0**: So if a custom permission has a option value of 1, what does that change?  
  > **username_1**: You shouldn't have numeric as value, for reference see https://lab.civicrm.org/extensions/custompermissions/-/blob/master/images/CustomPermission.gif  
  > **username_0**: In your gif it shows value of 1  
  > **username_1**: Ohh yeah sorry, it should be the name i.e civicrm_option_value.name field  
  > **username_0**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/105919/discussion-between-username_0-username_0-and-username_1).  
  
  ---
  
  