4 * gallery.module : gallery_roles.inc
5 * Group/Role Functions (sync groups, ...)
9 * Sync Drupal roles and Gallery groups for a specific user
11 // Check for remove user.
12 function gallery_sync_groups_for_user($user) {
13 // Take this opportunity to sync the Drupal roles and Gallery groups
14 gallery_sync_groups();
15 // Find the Ids for the 2 special Drupal roles (from user.module code)
16 $authenticated_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'"));
17 $anonymous_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'anonymous user'"));
19 // Find the Ids for the special G2 groups
20 list ($ret, $g2_gid_everybodyGroup) = GalleryCoreApi
::getPluginParameter('module', 'core',
23 $msg = t('Error retrieving Gallery group Id for \'Everybody\' group');
24 gallery_error($msg, $ret);
27 // Get the Gallery groups for this user
28 // First get the G2 Id from the Drupal uid
29 list ($ret, $g2_user) = GalleryCoreApi
::loadEntityByExternalId($user->uid
, 'GalleryUser');
31 $msg = t('Error getting Gallery User info from Drupal Id');
32 $msg .
= ' ' .
t('Drupal User Id: ') .
$user->uid
;
33 gallery_error($msg, $ret);
35 // Then get the groups for this user currently set in G2
36 list ($ret, $g2_cur_groups) = GalleryCoreApi
::fetchGroupsForUser($g2_user->getId());
38 $msg = t('Error getting Gallery group info for user');
39 $msg .
= ' ' .
t('Drupal User Id: ') .
$user->uid
;
40 gallery_error($msg, $ret);
42 // Now convert the new Drupal role Ids into Gallery Group Ids(for comparison)
43 foreach ($user->roles as
$rid=>$role_name) {
44 list ($ret, $g2_group) = GalleryCoreApi
::loadEntityByExternalId($rid, 'GalleryGroup');
46 $msg = t('Error getting Gallery Group Id from Drupal Role Id');
47 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid;
48 gallery_error($msg, $ret);
50 $g2_rid[$rid] = $g2_group->getId();
52 // Find if the user needs to be deleted from any G2 groups (only mapped groups)
53 $g2_mapped_groups = gallery_get_mapped_groups();
54 foreach ($g2_cur_groups as
$gid=>$gname) {
55 if (!in_array($gid, $g2_rid) && ($gid != $g2_gid_everybodyGroup) && in_array($gid, $g2_mapped_groups)) {
56 $delete_list[] = $gid;
57 $ret = GalleryCoreApi
::removeUserFromGroup($g2_user->getId(), $gid);
59 $msg = t('Error removing user from Gallery group');
60 $msg .
= ' ' .
t('Gallery Group Id: ') .
$gid .
' ' .
t('Gallery Group Name: ') .
$gname;
61 gallery_error($msg, $ret);
65 // Find if the user needs to be added to any G2 groups
66 foreach ($g2_rid as
$rid=>$gid) {
67 if (!isset($g2_cur_groups[$gid])) {
69 $ret = GalleryCoreApi
::addUserToGroup($g2_user->getId(), $gid);
71 $msg = t('Error adding user to Gallery group');
72 $msg .
= ' ' .
t('Gallery Group Id: ') .
$gid;
73 gallery_error($msg, $ret);
80 * Sync Drupal roles and Gallery groups. This will add any mappings that are required
81 * (eg on first install, or if a group is added). It will also delete groups in Gallery
82 * that have been deleted from Drupal.
84 function gallery_sync_groups() {
85 // Check if the Drupal role <-> G2 group mapping exists
86 $roles = user_roles();
87 // Find the Ids for the 2 special Drupal groups (from user.module code)
88 $authenticated_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'"));
89 $anonymous_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'anonymous user'"));
90 // Go through each role and add or delete the gallery group if needed
91 foreach ($roles as
$rid => $role_name) {
92 // Add Drupal<->G2 mapping if needed
93 $ret = GalleryEmbed
::isExternalIdMapped($rid, 'GalleryGroup');
94 if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT
)) {
95 // Need to add the mapping
97 // Add mapping for Anonymous and get the G2 group Id
99 list ($ret, $g2_gid) = GalleryCoreApi
::getPluginParameter('module', 'core', 'id.everybodyGroup');
101 $msg = t('Error retrieving Gallery group Id for \'Everybody\' group');
102 gallery_error($msg, $ret);
104 $ret = GalleryEmbed
::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup');
106 $msg = t('Error creating new Drupal role <-> Gallery group mapping (for \'anonymous user\' role)');
107 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid .
' ' .
t('Gallery Group Id: ') .
$g2_gid;
108 gallery_error($msg, $ret);
111 // Add mapping for authenticated users role and get the G2 group Id
112 case
$authenticated_role:
113 list ($ret, $g2_gid) = GalleryCoreApi
::getPluginParameter('module', 'core', 'id.allUserGroup');
115 $msg = t('Error retrieving Gallery group Id for \'Registered Users\' group');
116 gallery_error($msg, $ret);
118 $ret = GalleryEmbed
::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup');
120 $msg = t('Error creating new Drupal role <-> Gallery group mapping (for \'authenticated user\' role)');
121 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid .
' ' .
t('Gallery Group Id: ') .
$g2_gid;
122 gallery_error($msg, $ret);
126 // Is there already a group by this name? If so, map to it.
127 list ($ret, $g2_group) = GalleryCoreApi
::fetchGroupByGroupName($role_name);
129 $g2_gid = $g2_group->getId();
130 $ret = GalleryEmbed
::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup');
132 $msg = t('Error creating new Drupal role <-> Gallery group mapping (by name)');
133 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid .
' ' .
t('Gallery Group Id: ') .
$g2_gid;
134 gallery_error($msg, $ret);
137 // If not, create a new group
138 $ret = GalleryEmbed
::createGroup($rid, $role_name);
140 $msg = t('Error creating new Gallery group');
141 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid .
' ' .
t('Drupal Role Name: ') .
$role_name;
142 gallery_error($msg, $ret);
148 // Update group name if needed (not for $authenticated_role or $anonymous_role)
149 list($ret, $g2_group) = GalleryCoreApi
::loadEntityByExternalId($rid, 'GalleryGroup');
150 // In some cases the ExternalId may be present, but the user may have been deleted
152 $msg = t('Error retrieving Gallery Group Id from Drupal Role Id');
153 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid;
154 gallery_error($msg, $ret);
156 if (($rid != $authenticated_role) && ($rid != $anonymous_role) && ($role_name != $g2_group->getGroupName())) {
157 $ret = GalleryEmbed
::updateGroup($rid, array('groupname'=>$role_name));
159 $msg = t('Error updating Gallery group');
160 $msg .
= ' ' .
t('Drupal Role Id: ') .
$rid .
' ' .
t('Drupal Role Name: ') .
$role_name;
161 gallery_error($msg, $ret);
166 // Now check for any deleted Drupal roles. Only delete those G2 groups that were mapped to Drupal roles
167 // (just in case other groups have been defined which are not meant to be sync'd with Drupal)
168 list ($ret, $g2_map) = GalleryEmbed
::getExternalIdMap('entityId');
170 $msg = t('Error retrieving all Drupal<->Gallery Map Ids');
171 gallery_error($msg, $ret);
173 $g2_mapped_groups = gallery_get_mapped_groups();
174 foreach ($g2_mapped_groups as
$rid=>$g2_gid) {
176 if (!isset($roles[$rid])) {
177 $msg = t('Deleting G2 group') .
' (' .
t('Gallery Group Id: ') .
$g2_gid .
')';
178 $ret = GalleryEmbed
::deleteGroup($rid);
180 $msg = t('Error deleting Gallery group');
181 $msg .
= ' ' .
t('Gallery Group Id: ') .
$g2_gid;
182 gallery_error($msg, $ret);
189 * Get G2 Groups that have been mapped to Drupal Roles
191 function gallery_get_mapped_groups() {
192 list ($ret, $g2_map) = GalleryEmbed
::getExternalIdMap('entityId');
194 $msg = t('Error retrieving all Drupal<->Gallery Map Ids');
195 gallery_error($msg, $ret);
198 * getExternalIdMap returns groups and user mappings.
199 * Cannot use 'externalId' as key as is not unique between users & groups
201 foreach ($g2_map as
$g2_gid => $g2_data) {
202 if ($g2_data['entityType'] == 'GalleryGroup') {
203 $g2_mapped_groups[$g2_data['externalId']] = $g2_gid;
206 return $g2_mapped_groups;