simple capabilities ecosystem

This commit is contained in:
Jacson Passold 2017-12-20 22:13:33 -02:00
parent 5d2f121159
commit b2e4639e43
5 changed files with 76 additions and 2 deletions

View File

@ -77,7 +77,7 @@ class Collection extends Entity {
'rewrite' => [
'slug' => $this->get_slug()
],
'capability_type' => 'post',
'capability_type' => $this->get_db_identifier(),
'supports' => [
'title',
'editor',

View File

@ -134,7 +134,7 @@ class Collections extends Repository {
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'capability_type' => Entities\Collection::get_post_type(),
'supports' => [
'title',
'editor',

View File

@ -37,6 +37,11 @@ class Items extends Repository {
]);
}
/**
* Register each Item post_type
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::register_post_type()
*/
public function register_post_type() {
global $Tainacan_Collections, $Tainacan_Taxonomies;

View File

@ -16,6 +16,8 @@ abstract class Repository {
*/
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
//add_action('admin_init', array(&$this, 'init_caps'));
add_action('init', array(&$this, 'init_caps'));
add_filter('tainacan-get-map-'.$this->get_name(), array($this, 'get_default_properties'));
}
@ -337,6 +339,61 @@ abstract class Repository {
return false;
}
/**
* Update post_type caps using WordPress basic roles
* @param string $name //capability name
*/
public function init_caps($name = '') {
if( empty($name) ) {
$name = $this->entities_type::get_post_type();
}
if($name) {
$wp_append_roles = array(
'administrator' => array(
'delete_'.$name.'s',
'delete_private_'.$name.'s',
'edit_'.$name,
'edit_'.$name.'s',
'edit_private_'.$name.'s',
'publish_'.$name.'s',
'read_'.$name,
'read_private_'.$name.'s',
'delete_published_'.$name.'s',
'edit_published_'.$name.'s',
'edit_published_'.$name,
'edit_others_'.$name.'s',
'edit_others_'.$name,
'delete_others_'.$name.'s',
'delete_others_'.$name,
),
'contributor' => array(
'read_'.$name,
),
'subscriber' => array(
'read_'.$name,
),
'author' => array(
'read_'.$name,
),
'editor' => array(
'read_'.$name,
)
);
// append new capabilities to WordPress default roles
foreach ($wp_append_roles as $role_name => $caps) {
$role = get_role($role_name);
if(!is_object($role)) {
throw new \Exception(sprintf('Capability "%s" not found', $role_name));
}
foreach ($caps as $cap) {
$role->add_cap($cap);
}
}
}
}
/**
* @param $object
* @return mixed

View File

@ -13,6 +13,18 @@ namespace Tainacan\Tests;
*/
class Collections extends TAINACAN_UnitTestCase {
/**
* @group permissions
*/
function test_permissions () {
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
wp_set_current_user($new_user);
$user_id = get_current_user_id();
$this->assertEquals($new_user, $user_id);
$this->assertTrue(user_can($user_id, 'read_'.\Tainacan\Entities\Collection::get_post_type()), 'User cannot read Collections');
$this->assertFalse(user_can($user_id, 'edit_'.\Tainacan\Entities\Collection::get_post_type()), 'A subscriber User can edit a Collections?');
}
/**
* A single example test.
*/