Creating a Task Permission
It's easy to create custom permissions from within a package. Why might you need to do this? Well, let's say your package contains a theme with some custom settings, and you'd like a global task permission to cover changing those theme's settings. Rather than piggy-backing on an existing permission, just add a custom permission from within your theme. To create a custom task permission in a package controller, add a permission key during installation:
public function install()
{
$pkg = parent::install();
\Concrete\Core\Permission\Key\Key::add('admin', 'change_theme_settings', 'Change Theme Settings', '', false, false, $pkg);
}
The first argument in the Key::add()
method is the permission category handle. "admin" is one of the task permission key categories (marketplace_newsflow and sitemap) are the other two. Then, we specify the handle of the permission key, which is the identifier by which we usually grab the object. Next, we specify a name for our permission. The third parameter is an extended description of the permission. Then we have two parameters for whether the permission can trigger workflow, and whether the permission uses a custom class (note: this will only be the case for certain kinds of special permissions that deal with more granular permissions.) Finally, we pass the $pkg object in as the last parameter.
Once the task permission is installed, it automatically shows up on the Task Permissions Dashboard page:
Creating a Page, File or User Permission
While the above code adds a task permission, that's only because the Admin Permission Category doesn't work on a particular permission object. You can easily add a custom permission that interacts with existing permisson objects by using a different permission category:
public function install()
{
$pkg = parent::install();
\Concrete\Core\Permission\Key\Key::add('page', 'change_custom_theme_settings', 'Change Theme Settings', '', false, false, $pkg);
}
In this example we're adding our permission as a custom page permission.