$this->getUser()->isAllowed() from within a Model?

6 months ago

ShellyMurs
Member | 2
+
0
-

hello I was just wondering if there is any way to grab the $user from within my model?

thank you

6 months ago

nightfish
Member | 152
+
+2
-

ShellyMurs wrote:

hello I was just wondering if there is any way to grab the $user from within my model?

thank you

If Nette\Security\User is a dependency of your model class, you should pass it through the constructor.

class MyModel
{
    /** @var \Nette\Security\User */
    private $user;

    public function __construct(\Nette\Security\User $user) {
        $this->user = $user;
    }

    public function doSomething() {
        if ($this->user->isAllowed()) {
            // do stuff
        }
    }
}

Last edited by nightfish (2019-04-20 22:42)

6 months ago

ShellyMurs
Member | 2
+
0
-

thank you for the prompt reply! that works perfectly!