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