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
1=== modified file 'openid_teams.module'
2--- openid_teams.module 2009-03-16 14:18:31 +0000
3+++ openid_teams.module 2009-05-11 20:26:38 +0000
4@@ -94,7 +94,7 @@
5 * Implementation of hook_form_alter.
6 *
7 * Add the ability to limit login/registration to members of known teams to the
8- * drupal
9+ * drupal
10 */
11 function openid_teams_form_alter(&$form, &$form_state, $form_id) {
12 if ($form_id == 'user_admin_settings') {
13@@ -457,7 +457,7 @@
14 $request['openid.lp.query_membership'] = join(',', $all_teams);
15 }
16 return $request;
17-
18+
19 case 'extension':
20 return 'lp';
21 }
22@@ -496,14 +496,19 @@
23 module_load_include('inc', 'user', 'user.pages');
24 user_logout();
25 }
26+ clear_team_roles($account);
27+ $current = get_role_ids_assigned_to_user($account);
28 $assigned = array();
29 foreach ($teams as $mid) {
30- if (isset($roles_map[$mid]) && !in_array($roles_map[$mid], $assigned)) {
31+ if (isset($roles_map[$mid]) && should_assign_role($roles_map[$mid], $current, $assigned)) {
32 db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, '%s')",
33 $account->uid, $roles_map[$mid]);
34 $assigned[] = $roles_map[$mid];
35 }
36 }
37+ if (sizeof($assigned) > 0) {
38+ variable_set("openid_assigned_roles_{$account->uid}", join(',', $assigned));
39+ }
40 module_invoke_all('openid_teams_response', $raw_teams, $server);
41 }
42 elseif ($op == 'logout') {
43@@ -512,25 +517,44 @@
44 }
45
46 /**
47- * Clear the stored roles for the specified account
48- *
49- * Updated version of this function provided by feedback from Drupal CVS
50- * administrator feedback (AjK 13/10/08).
51+ * Clear the openid-assigned roles for the specified account
52 *
53 * @param object $account The user object
54 */
55 function clear_team_roles($account) {
56- $all_rids = array();
57- foreach (get_teams_roles_map() as $rids) {
58- $all_rids = array_merge($all_rids, $rids);
59- }
60- $sql = 'DELETE FROM {users_roles} WHERE uid = %d';
61- if (count($all_rids)) {
62- $sql .= ' AND rid IN ('.implode(',', array_fill(0, count($all_rids), '%d')).
63- ')';
64- }
65- array_unshift($all_rids, $account->uid);
66- db_query($sql, $all_rids);
67+ $assigned_roles = variable_get("openid_assigned_roles_{$account->uid}", "");
68+ if (!empty($assigned_roles)) {
69+ $sql = 'DELETE FROM {users_roles} WHERE uid = %d AND rid IN (%s)';
70+ db_query($sql, array($account->uid, $assigned_roles));
71+ }
72+ variable_del("openid_assigned_roles_{$account->uid}");
73+}
74+
75+/**
76+ * Check if the role has already been assigned to the user manually or in this login session
77+ *
78+ * @param integer $rid
79+ * @param array $current_roles
80+ * @param array $assigned_roles
81+ * @return boolean
82+ */
83+function should_assign_role($rid, $current_roles, $assigned_roles) {
84+ return !in_array($rid, $current_roles) && !in_array($rid, $assigned_roles);
85+}
86+
87+/**
88+ * Get the role ids already assigned to the user
89+ *
90+ * @param object $user
91+ * @return array
92+ */
93+function get_role_ids_assigned_to_user($user) {
94+ $query = db_query('SELECT rid FROM users_roles WHERE uid=%d', $user->uid);
95+ $rids = array();
96+ while ($row = db_fetch_object($query)) {
97+ $rids[] = $row->rid;
98+ }
99+ return $rids;
100 }
101
102 /**

Subscribers

People subscribed via source and target branches

to all changes: