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) »
  • Is there a way to find Drupal role in the $session object?
Pages: [1]

Author Topic: Is there a way to find Drupal role in the $session object?  (Read 2841 times)

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Is there a way to find Drupal role in the $session object?
August 06, 2010, 07:15:57 pm
Searched the forums, searched the docs, didn't find anything.  Searched for "civicrm session object", etc etc.  Maybe I looked in the wrong place?   Nothing.  

&smartyDebug=1 doesn't show me the contents of the $session object, only that it exists.   {$session|@debug_print_var} in the .tpl gives me only the name of the session object, no contents...   I couldn't find a map of what's in the $session object or how to access it anywhere in Civi docs.  This page tells me I can find the userID in the $session, but no other examples nor anything else about the session: http://en.flossmanuals.net/CiviCRM/DevelopTemplates

Do these docs exist?

Is there a way to find the users role(s) in the session object just as we find their userID?  
$session->get('userID')

If this is not possible, how do I access the current user role(s) in the .php files using CiviCRMish language?

but ....
$session->get('roles');
$session->get('role');
$session->get('userRole')
$session->get('userRoles')
all produce NULL.

Am I missing the obvious?  Where are the docs describing the contents of $session?  After spending 30 minutes just trying to find this information I decided to post here.

PS. woot!  500th post
« Last Edit: August 06, 2010, 07:22:05 pm by Stoob »
Try CiviTeacher: the online video tutorial CiviCRM learning library.

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: Is there a way to find Drupal role in the $session object?
August 06, 2010, 08:00:47 pm

i assume by user roles, u mean drupal user roles and not civicrm acl roles.

for drupal roles, u'll have to use the drupal api (dont know the function name, something like user_roles() ?) to get the roles of the current user. we dont use that internally within civicrm

note that in 3.2 the session should be significantly smaller than prior versions (most of the sessions contents are now stored in a cache table)

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

demeritcowboy

  • Ask me questions
  • ****
  • Posts: 570
  • Karma: 42
  • CiviCRM version: Always the latest!
  • CMS version: Drupal 6 mostly, still evaluating 7.
  • MySQL version: Mix of 5.0 / 5.1 / 5.5
  • PHP version: 5.3, usually on Windows
Re: Is there a way to find Drupal role in the $session object?
August 07, 2010, 02:30:21 am
Code: [Select]
global $user;
$roles_array = $user->roles;

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Is there a way to find Drupal role in the $session object?
August 09, 2010, 01:38:11 am
Thanks guys.  So Drupal roles are not in the Civi session is basically what you're saying?

Ok, I can work with that.



Basically the reason I'm doing this is I want Drupal authenticated users to be able to use auto-complete fields on contribution forms, while logged in of course.  This ability only comes with the "Access civicrm" drupal permission.  This permission has the unwanted side effect of putting the black Civi menu at the top of their page, so I wrote this code and just included it in the Drupal theme page.tpl.php.  Hides the menu for certain roles.  Code is written long hand for ease of reading.  Experienced programmers can condense it as needed.  This was my code.

Code: [Select]
 <?php 
       
//this hides the civicrm menu unless user has specific role(s)
//add role key # (in my case it's '3') if you want to add more roles
  
global $user;
  
$roles_array = $user->roles;
  
if (array_key_exists(3,$roles_array)) {  
//don't hide the menu
} else {
?>

<!-- this hides the CiviCRM menu for certain roles in the page.tpl.php -->
<style type="text/css">
 #civicrm-menu { display: none; }
</style>
<?php 
}
  
?>
Try CiviTeacher: the online video tutorial CiviCRM learning library.

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Is there a way to find Drupal role in the $session object?
August 11, 2010, 09:37:29 pm
Quote from: Stoob on August 09, 2010, 01:38:11 am
Thanks guys.  So Drupal roles are not in the Civi session is basically what you're saying?

Ok, I can work with that.

Yep.  Nor should you expect it to be there.

Your code should be pretty robust, since the standard way of getting the current user's roles is exactly as you're doing it here.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Is there a way to find Drupal role in the $session object?
August 14, 2010, 04:18:49 pm
Beware that it meant that your users can add manually /civicrm and have access to civicrm.

If they are hackerish enough, they are able to use the ajax api and retrieve contacts and what else (or modify them).

ie. "CiviCRM access" is your first, and best line of defence. If you put it down, you might have bad surprises.

I would suggest you to create a ajax rest interface clone mostly a copy of the the civicrm one but that don't need CiviCRM access right, and that you can limit to exactly the urls you want (on your own module).

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

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Is there a way to find Drupal role in the $session object?
September 02, 2010, 11:38:07 pm
Thanks, yes I realize the risk.  I don't have the time or skill right now to clone rest.js and modify it to my goals.  I might like to hire you to do it.  Are you available in October?
Try CiviTeacher: the online video tutorial CiviCRM learning library.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Is there a way to find Drupal role in the $session object?

This forum was archived on 2017-11-26.