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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to check current user's authority inside a hook
Pages: [1]

Author Topic: How to check current user's authority inside a hook  (Read 4061 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
How to check current user's authority inside a hook
March 02, 2011, 07:43:36 am
I am working on a hook that adds an additional tab to the contact summary screen.  The hook is working fine. But I would like to add authority restrictions that check if the active user has authority to "civicontribute" and "civipledge"  and only display the tab to authorized users.

Within my hook, how can I get the current user?

Thanks,
Sarah
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: How to check current user's authority inside a hook
March 02, 2011, 12:24:45 pm
To get CMS user from contact ID (using v3 api)

http://svn.civicrm.org/civicrm/trunk/api/v3/examples/UFMatchGet.php

Or you can use the Drupal function to get logged in user
off -the top of my head
global $user;
$userid = $user->uid;
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: How to check current user's authority inside a hook
March 03, 2011, 12:41:30 am
If the 'url' member of the array of the tab in hook_civicrm_tabs points to a custom URL that you created in the module, then you should be able to simply use

Code: [Select]
'access arguments' => array('civicontribute','civipledge'),
in the array for that menu item.

Please let me know if this fails.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: How to check current user's authority inside a hook
March 21, 2011, 06:11:13 pm
I added the following to the menu-related XML:
<access_arguments>access CiviContribute</access_arguments>

This works in the sense that the user cannot see the financial data when they do not have enough authority.   However, they still see the tab on the contact record.

The current user experience is that they see my custom tab, and can click on it. They do not see any information after clicking because they are not authorized.    This is confusing to the user.   I would prefer that they do not see the tab at all if they are not authorized.


Thanks,

Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: How to check current user's authority inside a hook
March 22, 2011, 03:45:43 am
I don't quite follow the issue here. If you are using hook_civicrm_tabs, why don't you just check the user permissions there?
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: How to check current user's authority inside a hook
March 23, 2011, 03:36:42 am
I would like to check the user permissions in my hook, but I am struggling to find the right API function to call to determine if the current user is authorized to CiviContribute. 

I can figure out the current user id, as well as their current roles, but that is not helping me determine if they have permission to use CiviContribute or not. 
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: How to check current user's authority inside a hook
March 23, 2011, 04:02:27 am
http://api.drupal.org/api/drupal/modules--user--user.module/function/user_access/6

That's the top page in Google for "drupal check user permissions"
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: How to check current user's authority inside a hook
March 23, 2011, 04:36:33 am
That did the trick in my Drupal+CiviCRM install.    Any ideas on making this CMS-neutral? Or at least making sure this does not blow up running under Joomla+CiviCRM?

Thanks,
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: How to check current user's authority inside a hook
March 23, 2011, 04:39:17 am
This will generate an error in Joomla but I have no idea how to check user permissions in Joomla. I would suggest you start a new thread what that specific question.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

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: How to check current user's authority inside a hook
March 23, 2011, 07:06:13 am

in civi 3.x and prior, we ignore joomla permissions

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to check current user's authority inside a hook

This forum was archived on 2017-11-26.