CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Permission check in Smarty?
Pages: [1]

Author Topic: Permission check in Smarty?  (Read 861 times)

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Permission check in Smarty?
November 06, 2013, 04:48:35 pm
I recently saw this bit of Smarty code (from https://github.com/civicrm/civihr/pull/204/files ):

Code: [Select]
{if call_user_func(array('CRM_Core_Permission','check'), 'administer CiviCRM') }
 <a href="{crmURL p='civicrm/admin/optionValue' q='reset=1&gid='}{$result.id}" target="_blank"><span class="batch-edit"></span></a>
{/if}

It seems pretty minimal/effective. Not-with-standing the fact that it uses call_user_func() and references a specific PHP function, does this flow seem OK? For example, it could be made a little more maintainable with a new Smarty helper:

Code: [Select]
{crmPermission check="administer CiviCRM"}
  <a href="{crmURL p='civicrm/admin/optionValue' q='reset=1&gid='}{$result.id}" target="_blank"><span class="batch-edit">
{/crmPermission}

But that doesn't change the basic structure of the logic. There's a general attitude that templates should have minimal logic, but I'm not sure what the better alternative would be in this case (short of a big project to build a client-side permission API).

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Permission check in Smarty?
November 06, 2013, 06:00:42 pm

IMHO doing something like {crmPermission...} is just so much nicer and cleaner. I'm not sure why we ever went down the call_user.. route

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Permission check in Smarty?
November 06, 2013, 07:17:12 pm
In its defense, call_user_func can be used in a boolean operators (!, &&, etc) and can work with {else}/{elseif} statements. Grepping on master, there are 43 spots using the call_user_func technique. All but 10 of them could be easily rewritten with a new {crmPermission} sketched before.

I think this would be almost as pretty as {crmPermission} and as flexible as call_user_func:

Code: [Select]
// Example Smarty notation
{if $crmPermission->check('administer CiviCRM')}

...

// Helper class
class CRM_Core_Smarty_PermissionAdapter {
  // non-static adapter for CRM_Core_Permission::check
  function check($perm) {
    return CRM_Core_Permission::check($perm);
  }
}

...

// CRM_Core_Smarty initialization
$smarty->assign('crmPermission', new CRM_Core_Smarty_PermissionAdapter());

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Permission check in Smarty?
November 11, 2013, 02:52:44 pm
Submitted above as https://github.com/civicrm/civicrm-core/pull/1955

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Permission check in Smarty?
November 11, 2013, 11:10:19 pm
Nice. now let's get the news out.

Should we have a changelog like for the api or rename the api one to cover more ground?

To get more devs, we need to be clear about what are the public stuff (functions/variables) you can build on vs. the ones that might change without notice.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Permission check in Smarty?

This forum was archived on 2017-11-26.