refactor(auth): change contributor's role logic to have it included in the users_podcasts table

- update myth-auth and codeigniter to latest develop changes
- improve permission check: remove all
dynamic permissions per podcast and overwrite myth-auth services and permission filter
- remove
unnecessary code because of myth-auth upgrade
- refactor some controller code for better clarity
-
add remaining seeders in docs

closes #19, #20
This commit is contained in:
Yassine Doghri 2020-07-31 16:05:10 +00:00
commit 58364bfed1
39 changed files with 1197 additions and 685 deletions

View file

@ -8,8 +8,6 @@
namespace App\Models;
use CodeIgniter\Model;
use Myth\Auth\Authorization\GroupModel;
use Myth\Auth\Config\Services;
class PodcastModel extends Model
{
@ -58,7 +56,7 @@ class PodcastModel extends Model
];
protected $validationMessages = [];
protected $afterInsert = ['clearCache', 'createPodcastPermissions'];
protected $afterInsert = ['clearCache'];
protected $afterUpdate = ['clearCache'];
protected $beforeDelete = ['clearCache'];
@ -77,17 +75,29 @@ class PodcastModel extends Model
->findAll();
}
public function addContributorToPodcast($user_id, $podcast_id)
public function addPodcastContributor($user_id, $podcast_id, $group_id)
{
$data = [
'user_id' => (int) $user_id,
'podcast_id' => (int) $podcast_id,
'group_id' => (int) $group_id,
];
return $this->db->table('users_podcasts')->insert($data);
}
public function removeContributorFromPodcast($user_id, $podcast_id)
public function updatePodcastContributor($user_id, $podcast_id, $group_id)
{
return $this->db
->table('users_podcasts')
->where([
'user_id' => (int) $user_id,
'podcast_id' => (int) $podcast_id,
])
->update(['group_id' => $group_id]);
}
public function removePodcastContributor($user_id, $podcast_id)
{
return $this->db
->table('users_podcasts')
@ -98,6 +108,24 @@ class PodcastModel extends Model
->delete();
}
public function getContributorGroupId($user_id, $podcast_id)
{
// TODO: return only the group id
$user_podcast = $this->db
->table('users_podcasts')
->select('group_id')
->where([
'user_id' => $user_id,
'podcast_id' => $podcast_id,
])
->get()
->getResultObject();
return (int) count($user_podcast) > 0
? $user_podcast[0]->group_id
: false;
}
protected function clearCache(array $data)
{
$podcast = $this->find(
@ -109,101 +137,11 @@ class PodcastModel extends Model
cache()->delete(md5($podcast->link));
// TODO: clear cache for every podcast's episode page?
// foreach ($podcast->episodes as $episode) {
// $cache->delete(md5($episode->link));
// cache()->delete(md5($episode->link));
// }
$data['podcast'] = $podcast;
return $data;
}
protected function createPodcastPermissions(array $data)
{
$authorize = Services::authorization();
$podcast = $data['podcast'];
$podcast_permissions = [
'podcasts:' . $podcast->id => [
[
'name' => 'View',
'description' => "View the $podcast->name podcast",
],
[
'name' => 'edit',
'description' => "Edit the $podcast->name podcast",
],
[
'name' => 'delete',
'description' => "Delete the $podcast->name podcast without removing it from the database",
],
[
'name' => 'delete_permanently',
'description' => "Delete the $podcast->name podcast from the database",
],
[
'name' => 'manage_contributors',
'description' => "Add / remove contributors to the $podcast->name podcast and edit their roles",
],
[
'name' => 'manage_publication',
'description' => "Publish / unpublish $podcast->name",
],
],
'podcasts:' . $podcast->id . ':episodes' => [
[
'name' => 'list',
'description' => "List all episodes of the $podcast->name podcast",
],
[
'name' => 'create',
'description' => "Add new episodes for the $podcast->name podcast",
],
[
'name' => 'edit',
'description' => "Edit an episode of the $podcast->name podcast",
],
[
'name' => 'delete',
'description' => "Delete an episode of the $podcast->name podcast without removing it from the database",
],
[
'name' => 'delete_permanently',
'description' => "Delete all occurrences of an episode of the $podcast->name podcast from the database",
],
[
'name' => 'manage_publications',
'description' => "Publish / unpublish episodes of the $podcast->name podcast",
],
],
];
$group_model = new GroupModel();
$owner_group_id = $group_model->insert(
[
'name' => "podcasts:$podcast->id" . '_owner',
'description' => "The owner of the $podcast->name podcast",
],
true
);
// add podcast owner to owner group
$authorize->addUserToGroup($podcast->owner_id, $owner_group_id);
foreach ($podcast_permissions as $context => $actions) {
foreach ($actions as $action) {
$permission_id = $authorize->createPermission(
get_permission($context, $action['name']),
$action['description']
);
$authorize->addPermissionToGroup(
$permission_id,
$owner_group_id
);
}
}
return $data;
}
}