Advertisement
Guest User

shib_auth.module snippet

a guest
Dec 26th, 2012
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /**
  2. * This function processes role assignment rules
  3. *
  4. * The function matches rule regular expressions with defined server variables
  5. * If there is a match, it assigns roles to the user logged in
  6. *
  7. * @param integer rule
  8. * The id of the rule currently processed.
  9. *
  10. * @return
  11. * 1 if profile changed and 0 if not.
  12. */
  13. function shib_auth_process_rule($rule) {
  14. global $user;
  15. $profile_changed = 0; // is a constant 0 when the rule is not a sticky one
  16. $fieldname = $rule['field'];
  17. $expression = '/' . urldecode($rule['regexpression']) . '/';
  18.  
  19. // if the given server field exists
  20. if (isset($_SERVER[$fieldname])) {
  21. foreach (explode(';', $_SERVER[$fieldname]) as $value) {
  22. //check if the RegEx fits to one of the value of the server field
  23. if (preg_match($expression, trim($value))) {
  24. $roles = unserialize($rule['role']);
  25. // there is a match, so give this user the specified role(s)
  26. if (empty($roles)) {
  27. return NULL;
  28. }
  29.  
  30. foreach ($roles as $role_id) {
  31. if (!$role_id)
  32. continue; // Zero is not allowed as a role_id
  33.  
  34. $role_name = shib_auth_get_rolename($role_id);
  35. if (!empty ($user->roles[$role_id]) && $user->roles[$role_id] == $role_name) {
  36. continue; // NOP if the user already has the given role
  37. }
  38.  
  39. $user->roles[$role_id] = $role_name;
  40.  
  41. if ($rule['sticky']) { // Sticky rules change the profile
  42. $profile_changed = 1;
  43.  
  44. if (!isset($_SESSION['shib_auth_rolelog'])) {
  45. watchdog('shib_grant_stick', 'Role "@id" has been permanently granted',
  46. array('@id' => $role_name), WATCHDOG_NOTICE);
  47. }
  48. }
  49. else {
  50. if (!isset($_SESSION['shib_auth_rolelog'])) {
  51. watchdog('shib_grant_role', 'Role "@id" has been granted',
  52. array('@id' => $role_name), WATCHDOG_NOTICE);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59.  
  60. return $profile_changed;
  61. } //function shib_auth_process_rule()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement