Permissions API
This is a technical summary of what is offered by our Permissions API and how to utilize custom Cornerstone Permissions.
Handled through our Settings dashboard, permissions offer a great way to enable or disable certain features for certain user roles.
Functions
cs_permission_user_can($type, $userID = null)
This function will check if a permission is enabled for a given user or the current user using the second argument.
// Has any layout ability
$hasLayoutCapability = cs_permission_user_can('layout');
// Can create layouts
$canCreateLayouts = cs_permission_user_can('layout.create');Controls
The filter for adding and changing controls is cs_permissions_controls. The only argument is an array of all controls. The required keys are id, title, and options. Options are a string list of all available permissions in your group. You will then utilize the permissions as ID.OPTION_NAME. So if your ID was my_id and your option name was my_option your permission string to use would be `my_id.my_option.
The following samples are from CSAI.
// Controls for AI
add_filter('cs_permissions_controls', function($controls) {
$controls[] = [
'id' => 'ai',
'title' => __('AI', CS_LOCALIZE),
'options' => [
'settings',
'actions',
]
];
return $controls;
});To create a label for your permission option utilize the filter cs_i18n_group_admin. Simply preface with admin.permission..
// Strings for usage in UI
add_filter('cs_i18n_group_admin', function($strings) {
$strings['admin.permissions.settings'] = __('Settings', CS_LOCALIZE);
$strings['admin.permissions.actions'] = __('Actions', CS_LOCALIZE);
return $strings;
});Setting Default Values
You can utilize the filter cs_app_permission_defaults to change any permission default value. This array is keyed by a given user role or the special key _default which will be the default for all roles. You can set the default value for a group as well as individual group permissions.
In our example we utilize the options we setup earlier. We are going to enable our permission group for the administrator and editor roles to be enabled by default. The _default permission for all other roles will be to disable this permission group.
// Permission defaults
add_filter('cs_app_permission_defaults', function($permissions) {
$permissions['administrator']['ai'] = true;
$permissions['editor']['ai'] = true;
$permissions['_default']['ai'] = false;
$permissions['_default']['ai.settings'] = true;
$permissions['_default']['ai.actions'] = true;
return $permissions;
});See something inaccurate? Let us know