jorge07/symfony-6-es-cqrs-boilerplate

Retrieving user data | Traits

abelardolg opened this issue · 3 comments

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.

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()
    }
}

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".🙊

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 ;)