Skip to content

Instantly share code, notes, and snippets.

@kvnm
Created May 30, 2016 21:32
Show Gist options
  • Save kvnm/73fe8cc8ac463ed053ca58610a831abb to your computer and use it in GitHub Desktop.
Save kvnm/73fe8cc8ac463ed053ca58610a831abb to your computer and use it in GitHub Desktop.
Restrict IMCE directory access based on a taxonomy term value associated with workbench_access taxonomy access control. DIRECTORY_FIELD constant is a new text field on the term entity used for control.
/**
* Hijack IMCE Profile Loading to add a check for directory access coming from
* a field on the access vocabulary term.
*
* @param $imce
* @param $user
*/
function custom_workbench_imce_alter_profiles(&$imce, $user) {
// Find access rules/terms for current user.
$access_tree = workbench_access_get_user_tree($user);
$tids = array();
foreach ($access_tree as $tid => $access_item) {
$tids[] = $tid;
// If the user has access to the full list of available terms, don't
// restrict the directories at all at this point.
if ($tid == $access_item['access_type_id']) {
return;
}
}
// Provide default directory settings, just in case.
$directory_base_settings = array(
'subnav' => 1,
'browse' => 1,
'upload' => 1,
'thumb' => 1,
'delete' => 1,
'resize' => 1,
'crop' => 1,
'mkdir' => 1,
'rmdir' => 1,
'rename' => 1,
'rename_folder' => 1,
);
foreach ($imce['directories'] as $key => $directory) {
if ($directory['name'] == BASE_DIRECTORY) {
// Override default base settings if main directory has different config.
$directory_base_settings = $directory;
// Unset the main directory, since we're about to override if possible.
unset($imce['directories'][$key]);
}
}
// Load all terms for the user's tree.
$terms = taxonomy_term_load_multiple($tids);
// No terms loaded (no uploads access granted for this user), so bail.
if (empty($terms)) {
return;
}
// Assign any menus associated with terms that are accessible to the user.
foreach ($terms as $term) {
if (isset($term->{DIRECTORY_FIELD}[LANGUAGE_NONE])) {
// Assign all associated directories to the IMCE Profile.
foreach ($term->{DIRECTORY_FIELD}[LANGUAGE_NONE] as $directory_name) {
$dir = trim($directory_name['value']);
$directory_base_settings['name'] = BASE_DIRECTORY . '/' . $dir;
$imce['directories'][] = $directory_base_settings;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment