Merge lp:~canonical-isd-hackers/drupal-teams/manual-role-persistence into lp:drupal-teams/6.x

Proposed by Stuart Metcalfe
Status: Merged
Merged at revision: not available
Proposed branch: lp:~canonical-isd-hackers/drupal-teams/manual-role-persistence
Merge into: lp:drupal-teams/6.x
Diff against target: None lines
To merge this branch: bzr merge lp:~canonical-isd-hackers/drupal-teams/manual-role-persistence
Reviewer Review Type Date Requested Status
Stuart Metcalfe (community) Approve
Łukasz Czyżykowski (community) Approve
Review via email: mp+6439@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Stuart Metcalfe (stuartmetcalfe) wrote :

Revision 7 is only diff from trunk. Fixes bug #375041. Functionality copied from Wordpress and adapted to drupal API.

Revision history for this message
Łukasz Czyżykowski (lukasz-czyzykowski) :
review: Approve
Revision history for this message
Stuart Metcalfe (stuartmetcalfe) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'openid_teams.module'
--- openid_teams.module 2009-03-16 14:18:31 +0000
+++ openid_teams.module 2009-05-11 20:26:38 +0000
@@ -94,7 +94,7 @@
94 * Implementation of hook_form_alter.94 * Implementation of hook_form_alter.
95 *95 *
96 * Add the ability to limit login/registration to members of known teams to the96 * Add the ability to limit login/registration to members of known teams to the
97 * drupal 97 * drupal
98 */98 */
99function openid_teams_form_alter(&$form, &$form_state, $form_id) {99function openid_teams_form_alter(&$form, &$form_state, $form_id) {
100 if ($form_id == 'user_admin_settings') {100 if ($form_id == 'user_admin_settings') {
@@ -457,7 +457,7 @@
457 $request['openid.lp.query_membership'] = join(',', $all_teams);457 $request['openid.lp.query_membership'] = join(',', $all_teams);
458 }458 }
459 return $request;459 return $request;
460 460
461 case 'extension':461 case 'extension':
462 return 'lp';462 return 'lp';
463 }463 }
@@ -496,14 +496,19 @@
496 module_load_include('inc', 'user', 'user.pages');496 module_load_include('inc', 'user', 'user.pages');
497 user_logout();497 user_logout();
498 }498 }
499 clear_team_roles($account);
500 $current = get_role_ids_assigned_to_user($account);
499 $assigned = array();501 $assigned = array();
500 foreach ($teams as $mid) {502 foreach ($teams as $mid) {
501 if (isset($roles_map[$mid]) && !in_array($roles_map[$mid], $assigned)) {503 if (isset($roles_map[$mid]) && should_assign_role($roles_map[$mid], $current, $assigned)) {
502 db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, '%s')",504 db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, '%s')",
503 $account->uid, $roles_map[$mid]);505 $account->uid, $roles_map[$mid]);
504 $assigned[] = $roles_map[$mid];506 $assigned[] = $roles_map[$mid];
505 }507 }
506 }508 }
509 if (sizeof($assigned) > 0) {
510 variable_set("openid_assigned_roles_{$account->uid}", join(',', $assigned));
511 }
507 module_invoke_all('openid_teams_response', $raw_teams, $server);512 module_invoke_all('openid_teams_response', $raw_teams, $server);
508 }513 }
509 elseif ($op == 'logout') {514 elseif ($op == 'logout') {
@@ -512,25 +517,44 @@
512}517}
513518
514/**519/**
515 * Clear the stored roles for the specified account520 * Clear the openid-assigned roles for the specified account
516 *
517 * Updated version of this function provided by feedback from Drupal CVS
518 * administrator feedback (AjK 13/10/08).
519 *521 *
520 * @param object $account The user object522 * @param object $account The user object
521 */523 */
522function clear_team_roles($account) {524function clear_team_roles($account) {
523 $all_rids = array();525 $assigned_roles = variable_get("openid_assigned_roles_{$account->uid}", "");
524 foreach (get_teams_roles_map() as $rids) {526 if (!empty($assigned_roles)) {
525 $all_rids = array_merge($all_rids, $rids);527 $sql = 'DELETE FROM {users_roles} WHERE uid = %d AND rid IN (%s)';
526 }528 db_query($sql, array($account->uid, $assigned_roles));
527 $sql = 'DELETE FROM {users_roles} WHERE uid = %d';529 }
528 if (count($all_rids)) {530 variable_del("openid_assigned_roles_{$account->uid}");
529 $sql .= ' AND rid IN ('.implode(',', array_fill(0, count($all_rids), '%d')).531}
530 ')';532
531 }533/**
532 array_unshift($all_rids, $account->uid);534 * Check if the role has already been assigned to the user manually or in this login session
533 db_query($sql, $all_rids);535 *
536 * @param integer $rid
537 * @param array $current_roles
538 * @param array $assigned_roles
539 * @return boolean
540 */
541function should_assign_role($rid, $current_roles, $assigned_roles) {
542 return !in_array($rid, $current_roles) && !in_array($rid, $assigned_roles);
543}
544
545/**
546 * Get the role ids already assigned to the user
547 *
548 * @param object $user
549 * @return array
550 */
551function get_role_ids_assigned_to_user($user) {
552 $query = db_query('SELECT rid FROM users_roles WHERE uid=%d', $user->uid);
553 $rids = array();
554 while ($row = db_fetch_object($query)) {
555 $rids[] = $row->rid;
556 }
557 return $rids;
534}558}
535559
536/**560/**

Subscribers

People subscribed via source and target branches

to all changes: