Skip to content

Instantly share code, notes, and snippets.

@jbroadway
Last active December 12, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbroadway/4732539 to your computer and use it in GitHub Desktop.
Save jbroadway/4732539 to your computer and use it in GitHub Desktop.
Poor man's admin ACL for Elefant CMS.

This will show/hide links in the Tools admin menu based on a custom user role field. Note that it doesn't prevent users from browsing to those links through other means.

To add it to your site:

  1. Log into Elefant and go to Tools > Users > Custom Fields. Add a Role drop down field and list all admin roles here.
  2. Save the tools.php file to apps/acl/handlers/tools.php.
  3. Edit the tools.html file to add your own roles and the links they can see, then save it to apps/acl/views/tools.html.
  4. Add this tag directly below the {! admin/head !} tag in each layout template, including layouts/admin.html:
{! acl/tools !}

Now when you assign your admin users to each role, they will see different options in the Tools menu depending on their role.

<!-- apps/admin/views/tools.html -->
<script>
$(function () {
var user_role = '{{role}}',
tools_updated = false;
function update_tools_list () {
if (tools_updated) {
return;
}
if ($('#admin-tools-list li').length === 0) {
setTimeout (update_tools_list, 50);
return;
}
switch (user_role) {
case 'editor':
$('#admin-tools-list').html (
'<li><a href="/admin/pages">{"All Pages"}</a></li>' +
'<li><a href="/blocks/admin">{"Blocks"}</a></li>' +
'<li><a href="/blog/admin">{"Blog Posts"}</a></li>'
// etc.
);
break;
case 'manager':
$('#admin-tools-list').html (
'<li><a href="/admin/pages">{"All Pages"}</a></li>' +
'<li><a href="/blocks/admin">{"Blocks"}</a></li>' +
'<li><a href="/filemanager/index">{"Files"}</a></li>'
// etc.
);
break;
case 'all':
default:
break;
}
tools_updated = true;
}
setTimeout (update_tools_list, 50);
});
</script>
<?php // apps/acl/handlers/tools.php
if (! User::is_valid ()) {
return;
}
echo View::render (
'acl/tools',
array (
'role' => User::$user->ext ('role')
)
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment