nettrine loggable neuklada username

stewestw
Člen | 42
+
0
-

zdravim,
viete mi prosim poradit, ako viem naplnit username v Gedmo\Loggable, aby nam do databazi ukladal username?
dakujem

stewestw
Člen | 42
+
+1
-

podarilo sa mi to vyriesit cez base presenter, takto, ak by to nahodou niekto potreboval:

$this->context->getByType('Gedmo\Loggable\LoggableListener')->setUsername($u);
Felix
Nette Core | 1245
+
0
-

Je to tak. Pridam k tomu i resource do dokumentace Gedmo – http://atlantic18.github.io/…oggable.html#….

Osobne bych to resil automaticky pres contributte/event-dispatcher-extra balicek. Kdy by se navesil event na application onRequest a nebo onPresenter.

Napriklad takto:

use Contributte\EventDispatcher\EventSubscriber;
use Contributte\Events\Extra\Event\Application\ApplicationEvents;
use Contributte\Events\Extra\Event\Application\RequestEvent;
use Gedmo\Loggable\LoggableListener;
use Nette\Security\User;

final class GedmoLoggableSubscriber implements EventSubscriber
{

	/** @var LoggableListener */
	private $loggableListener;

	/** @var User */
	private $user;

	/**
	 * @return mixed[]
	 */
	public static function getSubscribedEvents(): array
	{
		return [
			ApplicationEvents::ON_REQUEST => 'onRequest',
			//ApplicationEvents::ON_PRESENTER => 'onPresenter',
		];
	}

	public function onRequest(RequestEvent $event): void
	{
		if ($this->user->isLoggedIn()) {
			$this->loggableListener->setUsername($this->user->getId());
		} else {
			$this->loggableListener->setUsername('Anonymous');
		}
	}

}