jorge07 / symfony-6-es-cqrs-boilerplate

Symfony 6 DDD ES CQRS backend boilerplate.
MIT License
1.07k stars 187 forks source link

Retrieving user data | Traits #178

Closed abelardolg closed 4 years ago

abelardolg commented 4 years ago

Hi there, I wonder if a trail is a good choice to retrieve the user data in those controllers which need this information to process their requests.

Best regards.

jorge07 commented 4 years ago

You mean trait?

Could be an option but you'll add as requirement the session so need to add a setter (which I do not like for this kind of things as I try to avoid optional dependencies) or modify the construct. What about an abstract controller?

<?php

declare(strict_types=1);

namespace App\UI\Http\Rest\Controller;

use App\Infrastructure\Share\Bus\Command\CommandBus;
use App\Infrastructure\Share\Bus\Query\QueryBus;
use App\Infrastructure\User\Auth\Session;
use App\UI\Http\Rest\Controller\CommandController;

abstract class SessionAwareController extends CommandQueryController
{
    private Session $session;

    public function __construct(
        Session $session, 
        CommandBus $commandBus,
        QueryBus $queryBus
    )  {
        parent::__construct($commandBus);

        $this->session = $session;
    }

    /**
     * @returns [ 'uuid': string, 'username': string, 'roles': array<string>]
     * @throws App\Domain\User\Exception\InvalidCredentialsException
     */
    protected function userFromSession(): array
        return $this->session->get()
    }
}
abelardolg commented 4 years ago

Regarding your suggestion to add it inside an abstract controller: It would be a good idea to add a method to retrieve the user data inside the base controller AbstractRenderController or anything else.

Ps: Yes, I meant "trait" not "trail".🙊

jorge07 commented 4 years ago

I think this will depend of each app and needs to be implemented just when needed. Don't see a real benefit add it to the boilerplate.

I'm closing the issue ;)