J
J
j0ker132020-10-31 13:31:57
Slim Framework
j0ker13, 2020-10-31 13:31:57

How to organize the project structure to separate user rights?

We only have installed slim3 (+doctrine).
we add: services are injected into the controllers, repositories are injected into the services.
correctly I imagine DI?

//... dependencies.php
$container[Services\PostService::class] = function ($c) {
    $postRepo = $c->get('em')->getRepository('Entities\Post');

    return new Services\PostService($postRepo);
};

$container[Controllers\PostController::class] = function ($c) {
    $postService = $c->get('Services\PostService');

    return new Controllers\PostController($postService);
};

//... routes.php
$app->get('/posts', 'Controllers\PostController:getAll');

//... PostController.php
namespace Controllers;

use Services\CourierService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class PostController extends Controller
{
    protected $postService;

    public function __construct(PostService $postService)
    {
        $this->postService = $postService;
    }

    public function getAll(ServerRequestInterface $request, ResponseInterface $response)
    {
        try {
            $post = $this->postService->getAll();

            return $this->respondWithData($response, $$post, 200);
        } catch (\Exception $e) {
            return $this->respondWithData($response, [], 400, $e->getMessage());
        }
    }
}

//...PostService.php
namespace Services;

use Entities\Post;
use Controllers\Controller;
use Repositories\PostRepository;

class PostService extends Service
{
    protected $postRepo;

    public function __construct(PostRepository $postRepo)
    {
        $this->postRepo = $postRepo;
    }

    public function getAll()
    {
        return $this->postRepo->findAll();
    }
}

now it is necessary to separate the rights of users. for example, the admin sees all the posts, the writer only sees his own.
and so on will be: the administrator sees all the stories, the writer only sees his own. It seems silly to me to block the check of rights in the service and give out only the necessary data
. I see the idea that the admin has his own service, the writer has his own:
WriterPostService extends PostService
AdminPostService extends WriterPostService

is this right? if correct how to implement it with DI? where to separate services: in the controller or in dependencies.php?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question