Skip to content

Commit

Permalink
return all permissions for admin role
Browse files Browse the repository at this point in the history
  • Loading branch information
Mh-Asmi committed Feb 13, 2024
1 parent 55e9902 commit f6ff481
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php


use Phinx\Migration\AbstractMigration;

class AddMissingAdminPermissions extends AbstractMigration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Manage Users')");

$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Manage Posts')");

$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Manage Settings')");

$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Bulk Data Import and Export')");

$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Edit their own posts')");

$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Delete Posts')");

$this->execute("INSERT INTO `roles_permissions` (`role`, `permission`)
VALUES ('admin', 'Delete Their Own Posts')");
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Manage Users'");

$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Manage Posts'");

$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Manage Settings'");

$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Bulk Data Import and Export'");

$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Edit their own posts'");

$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Delete Posts'");

$this->execute("DELETE FROM roles_permissions
WHERE permission = 'Delete Their Own Posts'");
}
}
43 changes: 37 additions & 6 deletions src/Ushahidi/Modules/V5/Http/Controllers/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ushahidi\Modules\V5\Models\Role;
use Ushahidi\Modules\V5\Actions\Role\Queries\FetchRoleByIdQuery;
use Ushahidi\Modules\V5\Actions\Role\Queries\FetchRoleQuery;
use Ushahidi\Modules\V5\Actions\Permissions\Queries\FetchPermissionsQuery;
use Ushahidi\Modules\V5\Actions\Role\Commands\CreateRoleCommand;
use Ushahidi\Modules\V5\Actions\Role\Commands\DeleteRoleCommand;
use Ushahidi\Modules\V5\Actions\Role\Commands\UpdateRoleCommand;
Expand All @@ -30,6 +31,7 @@ public function show(int $id)
{
$role = $this->queryBus->handle(new FetchRoleByIdQuery($id));
$this->authorize('show', $role);
$role->permissions_name = $this->getPermissions($role);
return new RoleResource($role);
} //end show()

Expand Down Expand Up @@ -75,9 +77,7 @@ public function store(RoleRequest $request)
$request->input('permissions') ?? []
);
$this->commandBus->handle($command);
return new RoleResource(
$this->queryBus->handle(new FetchRoleByIdQuery($command->getId()))
);
return $this->show($command->getId());
} //end store()

/**
Expand Down Expand Up @@ -113,9 +113,7 @@ public function update(RoleRequest $request, int $id)
)
);

return new RoleResource(
$this->queryBus->handle(new FetchRoleByIdQuery($id))
);
return $this->show($id);
} //end store()


Expand All @@ -136,4 +134,37 @@ public function delete(int $id)
$this->commandBus->handle(new DeleteRoleCommand($id));
return $this->deleteResponse($id);
} //end store()

private function getPermissions(Role $role)
{
$permissions_name = [];
if ($role->name === RoleEntity::ADMIN) {
$permissions_name = $this->getAllPermissionsName();
} else {
foreach ($role->getPermission()->toArray() as $permission) {
$permissions_name[] = $permission['permission'];
}
}
return $permissions_name;
}

private function getAllPermissionsName()
{
$permissions_name = [];

$permissions = $this->queryBus->handle(
new FetchPermissionsQuery(
FetchPermissionsQuery::DEFAULT_LIMIT,
1,
FetchPermissionsQuery::DEFAULT_SORT_BY,
FetchPermissionsQuery::DEFAULT_ORDER,
['q' => false, 'name' => false]
)
);
foreach ($permissions as $permission) {
$permissions_name[] = $permission->name;
}

return $permissions_name;
}
} //end class
11 changes: 1 addition & 10 deletions src/Ushahidi/Modules/V5/Http/Resources/Role/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,11 @@ public function toArray($request)
'description' => $this->description,
'display_name'=> $this->display_name,
'protected'=> $this->protected,
'permissions' =>$this->getResourcePermissions($this->permissions),
'permissions' =>$this->permissions_name,
'allowed_privileges' => $this->getResourcePrivileges()



];
}

private function getResourcePermissions(Collection $permissions)
{
$permissions_name = [];
foreach ($permissions->all() as $permission) {
$permissions_name[] = $permission->permission;
}
return $permissions_name;
}
}

0 comments on commit f6ff481

Please sign in to comment.