Skip to content

Commit

Permalink
refactor(authorization)
Browse files Browse the repository at this point in the history
refactor authorization container
  • Loading branch information
Mohammad-Alavi committed Oct 14, 2022
1 parent 10dde1f commit 00cac50
Show file tree
Hide file tree
Showing 36 changed files with 272 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace App\Containers\AppSection\Authorization\Actions;

use App\Containers\AppSection\Authorization\Tasks\AttachPermissionsToUserTask;
use App\Containers\AppSection\Authorization\Tasks\FindPermissionTask;
use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\Authorization\UI\API\Requests\AttachPermissionsToUserRequest;
use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\User\Tasks\FindUserByIdTask;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action as ParentAction;

class AttachPermissionsToUserAction extends ParentAction
{
/**
* @param AttachPermissionsToUserRequest $request
* @return User
* @throws NotFoundException
*/
public function run(AttachPermissionsToUserRequest $request): User
{
Expand All @@ -25,6 +26,6 @@ public function run(AttachPermissionsToUserRequest $request): User
return app(FindPermissionTask::class)->run($permissionId);
}, $permissionIds);

return app(AttachPermissionsToUserTask::class)->run($user, $permissions);
return $user->givePermissionTo($permissions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@
use App\Containers\AppSection\Authorization\Tasks\DetachPermissionsFromUserTask;
use App\Containers\AppSection\Authorization\Tasks\FindPermissionTask;
use App\Containers\AppSection\Authorization\UI\API\Requests\DetachPermissionsFromUserRequest;
use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\User\Tasks\FindUserByIdTask;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action as ParentAction;

class DetachPermissionsFromUserAction extends ParentAction
{
/**
* @param DetachPermissionsFromUserRequest $request
* @return \App\Containers\AppSection\User\Models\User
* @return User
* @throws NotFoundException
*/
public function run(DetachPermissionsFromUserRequest $request)
public function run(DetachPermissionsFromUserRequest $request): User
{
$role = app(FindUserByIdTask::class)->run($request->id);
$user = app(FindUserByIdTask::class)->run($request->id);

$permissions = array_map(static function ($permissionId) {
return app(FindPermissionTask::class)->run($permissionId);
}, $request->permissions_ids);

return app(DetachPermissionsFromUserTask::class)->run($role, $permissions);
return app(DetachPermissionsFromUserTask::class)->run($user, $permissions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

namespace App\Containers\AppSection\Authorization\Actions;

use Apiato\Core\Exceptions\CoreInternalErrorException;
use App\Containers\AppSection\Authorization\Tasks\FindRoleTask;
use App\Containers\AppSection\Authorization\Tasks\GetAllRolesTask;
use App\Containers\AppSection\Authorization\Tasks\GetRolePermissionsTask;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetAllRolesRequest;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetRolePermissionsRequest;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action as ParentAction;
use Prettus\Repository\Exceptions\RepositoryException;

class GetRolePermissionsAction extends ParentAction
{
/**
* @param GetRolePermissionsRequest $request
* @return mixed
* @throws NotFoundException
*/

public function run(GetRolePermissionsRequest $request): mixed
{
$role = app(FindRoleTask::class)->run($request->id);

return $role->permissions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

namespace App\Containers\AppSection\Authorization\Actions;

use Apiato\Core\Exceptions\CoreInternalErrorException;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetUserPermissionsRequest;
use App\Containers\AppSection\User\Tasks\FindUserByIdTask;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action as ParentAction;
use Prettus\Repository\Exceptions\RepositoryException;

class GetUserPermissionsAction extends ParentAction
{
/**
* @param GetUserPermissionsRequest $request
* @return mixed
* @throws NotFoundException
*/
public function run(GetUserPermissionsRequest $request): mixed
{
$user = app(FindUserByIdTask::class)->run($request->id);

return $user->permissions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

namespace App\Containers\AppSection\Authorization\Actions;

use Apiato\Core\Exceptions\CoreInternalErrorException;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetUserRolesRequest;
use App\Containers\AppSection\User\Tasks\FindUserByIdTask;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action as ParentAction;
use Prettus\Repository\Exceptions\RepositoryException;

class GetUserRolesAction extends ParentAction
{
/**
* @throws CoreInternalErrorException
* @throws RepositoryException
* @param GetUserRolesRequest $request
* @return mixed
* @throws NotFoundException
*/
public function run(GetUserRolesRequest $request): mixed
{
$user = app(FindUserByIdTask::class)->run($request->id);

return $user->roles;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace App\Containers\AppSection\Authorization\Tasks;

use App\Containers\AppSection\Authorization\Models\Permission;
use App\Containers\AppSection\User\Models\User;
use App\Ship\Parents\Tasks\Task as ParentTask;

class DetachPermissionsFromUserTask extends ParentTask
{
/**
* @param User $user
* @param array $permissions
* @param Permission[] $permissions
* @return User
*/
public function run(User $user, array $permissions): User
{
array_map(static function ($permission) use($user){
array_map(static function ($permission) use ($user) {
$user->revokePermissionTo($permission);
}, $permissions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace App\Containers\AppSection\Authorization\UI\API\Controllers;

use Apiato\Core\Exceptions\InvalidTransformerException;
use App\Containers\AppSection\Authorization\Actions\AttachPermissionsToUserAction;
use App\Containers\AppSection\Authorization\UI\API\Requests\AttachPermissionsToUserRequest;
use App\Containers\AppSection\User\UI\API\Transformers\UserTransformer;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Controllers\ApiController;

class AttachPermissionsToUserController extends ApiController
{
/**
* @param AttachPermissionsToUserRequest $request
* @return \App\Containers\AppSection\User\Models\User
* @return array
* @throws InvalidTransformerException
* @throws NotFoundException
*/
public function attachPermissionsToUser(AttachPermissionsToUserRequest $request)
public function attachPermissionsToUser(AttachPermissionsToUserRequest $request): array
{
$user = app(AttachPermissionsToUserAction::class)->run($request);

return $this->transform($user, UserTransformer::class, ['permissions']);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace App\Containers\AppSection\Authorization\UI\API\Controllers;

use Apiato\Core\Exceptions\InvalidTransformerException;
use App\Containers\AppSection\Authorization\Actions\DetachPermissionsFromUserAction;
use App\Containers\AppSection\Authorization\UI\API\Requests\DetachPermissionsFromUserRequest;
use App\Containers\AppSection\User\UI\API\Transformers\UserTransformer;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Controllers\ApiController;

class DetachPermissionsFromUserController extends ApiController
{
/**
* @param DetachPermissionsFromUserRequest $request
* @return \App\Containers\AppSection\User\Models\User
* @return array
* @throws InvalidTransformerException
* @throws NotFoundException
*/
public function detachPermissionFromUser(DetachPermissionsFromUserRequest $request)
public function detachPermissionFromUser(DetachPermissionsFromUserRequest $request): array
{
$user = app(DetachPermissionsFromUserAction::class)->run($request);

return $this->transform($user, UserTransformer::class, ['permissions']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace App\Containers\AppSection\Authorization\UI\API\Controllers;

use Apiato\Core\Exceptions\InvalidTransformerException;
use App\Containers\AppSection\Authorization\Actions\GetRolePermissionsAction;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetRolePermissionsRequest;
use App\Containers\AppSection\Authorization\UI\API\Transformers\PermissionTransformer;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Controllers\ApiController;

class GetRolePermissionsController extends ApiController
{
/**
* @param GetRolePermissionsRequest $request
* @return mixed
* @throws \Apiato\Core\Exceptions\CoreInternalErrorException
* @throws \Prettus\Repository\Exceptions\RepositoryException
* @return array
* @throws InvalidTransformerException
* @throws NotFoundException
*/
public function getRolePermissions(GetRolePermissionsRequest $request)
public function getRolePermissions(GetRolePermissionsRequest $request): array
{
$permission = app(GetRolePermissionsAction::class)->run($request);

return $this->transform($permission, PermissionTransformer::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace App\Containers\AppSection\Authorization\UI\API\Controllers;

use Apiato\Core\Exceptions\InvalidTransformerException;
use App\Containers\AppSection\Authorization\Actions\GetUserPermissionsAction;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetUserPermissionsRequest;
use App\Containers\AppSection\Authorization\UI\API\Transformers\PermissionTransformer;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Controllers\ApiController;

class GetUserPermissionsController extends ApiController
{
/**
* @param GetUserPermissionsRequest $request
* @return mixed
* @throws \Apiato\Core\Exceptions\CoreInternalErrorException
* @throws \Prettus\Repository\Exceptions\RepositoryException
* @return array
* @throws InvalidTransformerException
* @throws NotFoundException
*/
public function getUserPermissions(GetUserPermissionsRequest $request)
public function getUserPermissions(GetUserPermissionsRequest $request): array
{
$permission = app(GetUserPermissionsAction::class)->run($request);

return $this->transform($permission, PermissionTransformer::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

namespace App\Containers\AppSection\Authorization\UI\API\Controllers;

use Apiato\Core\Exceptions\InvalidTransformerException;
use App\Containers\AppSection\Authorization\Actions\GetUserRolesAction;
use App\Containers\AppSection\Authorization\UI\API\Requests\GetUserRolesRequest;
use App\Containers\AppSection\Authorization\UI\API\Transformers\RoleTransformer;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Controllers\ApiController;

class GetUserRolesController extends ApiController
{
public function getUserRoles(GetUserRolesRequest $request)
/**
* @param GetUserRolesRequest $request
* @return array
* @throws InvalidTransformerException
* @throws NotFoundException
*/
public function getUserRoles(GetUserRolesRequest $request): array
{
$roles = app(GetUserRolesAction::class)->run($request);

return $this->transform($roles, RoleTransformer::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function rules(): array
{
return [
'roles_ids' => 'array|required',
'user_id' => 'required',
'roles_ids.*' => 'exists:roles,id',
'user_id' => 'required|exists:users,id',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class AttachPermissionsToRoleRequest extends ParentRequest
public function rules(): array
{
return [
'permissions_ids' => 'required',
'role_id' => 'required',
'permissions_ids' => 'array|required',
'permissions_ids.*' => 'exists:permissions,id',
'role_id' => 'required|exists:roles,id',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class AttachPermissionsToUserRequest extends ParentRequest
public function rules(): array
{
return [
'permissions_ids' => 'required'
'permissions_ids' => 'array|required',
'permissions_ids.*' => 'exists:permissions,id',
'id' => 'required|exists:users,id',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class DetachPermissionsFromRoleRequest extends ParentRequest
public function rules(): array
{
return [
'role_id' => 'required',
'permissions_ids' => 'required',
'role_id' => 'required|exists:roles,id',
'permissions_ids' => 'array|required',
'permissions_ids.*' => 'exists:permissions,id',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class DetachPermissionsFromUserRequest extends ParentRequest
public function rules(): array
{
return [
'permissions_ids' => 'required',
'permissions_ids' => 'array|required',
'permissions_ids.*' => 'exists:permissions,id',
'id' => 'required|exists:users,id',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GetRolePermissionsRequest extends ParentRequest
public function rules(): array
{
return [
'id' => 'required',

];
}

Expand Down
Loading

0 comments on commit 00cac50

Please sign in to comment.