config v kazdem modulu, nenajde se IAuthenticator

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
vosy
Člen | 532
+
0
-

ahojte,
chtel bych v kazdem modulu (Front, Admin) mit vlastni config.neon.

v bootstrap.php mam:

$configurator->addConfig(__DIR__ . '/AdminModule/config/config.neon');

pak mam strukturu:

  • AdminModule
    • config
      • config.neon

v config.neon mam:

common:
    parameters:

    php:
        date.timezone: Europe/Prague
        # zlib.output_compression: yes

    nette:
        application:
            errorPresenter: Error

        database:
            dsn: 'mysql:host=127.0.0.1;dbname=zk'
            user:
            password:

        session:
            authenticator: AdminModule\model\AdminsModel:getAuthenticatorService
            autoStart: smart
            expiration: +30 days

ve slozce

  • AdminModule
    • model
      • AdminsModel.php
      • Authenticator.php

v AdminsModel mam:

namespace AdminModule;

use \Nette\Object;

class AdminsModel extends Object
{
    public $db;

    public function __construct($connection)
    {
        $this->db = $connection;
    }

    public function getAuthenticatorService()
    {
        return new Authenticator($this->db->table(\Glb::$NAZEV_WEBU.'_admins'));
    }
}

a v Authenticator.php
<?php
namespace AdminModule;

use \Nette\Object;
use \Nette\Security as NS;

class Authenticator extends \Nette\Object implements NS\IAuthenticator
{
private $admins;

function __construct(\Nette\Database\Table\Selection $admins)
{
$this->admins = $admins;
 }

public function authenticate(array $opt)
{
list($login, $password) = $opt;
// $row = $
throw new NS\AuthenticationException(‚The password is incorrect.‘, self::INVALID_CREDENTIAL);
 }
}

a v AdminModule/presentersLoginBoardPresenters.php
mam

<?php

namespace AdminModule;

use \Nette\Application\UI\Form;
use \Nette\Http\Request;
use \Nette\Config\Configurator;
use \Nette\Security as NS;

class LoginBoardPresenter extends BasePresenter
{

    /* component factoriew */
    protected function createComponentSignInForm()
    {
        $form = new Form;

        $form->addText('username', 'Login:')
                ->setRequired('Vyplňte prosím Login.')
                ->setAttribute('placeholder', 'Vaše přihlašovací jméno');

        $form->addPassword('password', 'Heslo:')
                ->setRequired('Vyplňte prosím heslo.')
                ->setAttribute('placeholder', 'Vaše heslo');

        $form->addSubmit('send', 'Přihlásit se')
                ->setAttribute('class', 'btn btn-large btn-warning fS140p');

        $form->addCheckbox('remember', 'Zůstat přihlášen');

        $form->onSuccess[] = callback($this, 'signInFormSubmitted');

        $form->addProtection('Vypršel časový limit, odešlete formulář znovu');

        return $form;
    }

    public function signInFormSubmitted($form)
    {
        try
        {
            $user = $this->getUser();
            $values = $form->getValues();

            if($values->remember)
            {
                $user->setExpiration('+30 days', FALSE);
            }

            $password = \Hash::create('sha1', $values->password, \Glb::$SALT);
            $user->login($values->username, $password);
            $this->flashMessage('Přihlášení proběhlo úspěšně', 'success');


        }catch(NS\AuthenticationException $e)
        {
            $form->addError($e->getMessage());
        }
    }
}

Formular se vykresli spravne ale kdyz zmacknu Prihlasit se tak se zobrazi chyba.

Nette\DI\MissingServiceException

Service of type Nette\Security\IAuthenticator not found.

.../libs/Nette/Security/User.php:165	 source ►  Nette\DI\Container->	 getByType (arguments ►)

.../libs/Nette/Security/User.php:92	 source ►  Nette\Security\User->	 getAuthenticator ()

.../app/AdminModule/presenters/LoginBoardPresenter.php:51	 source ►  Nette\Security\User->	 login (arguments ▼)

$id
"root" (4)
$password
"a372beadcccaa4745e340713509dd4d11d71bb0b" (40)
41:            {
42:                $user = $this->getUser();
43:                $values = $form->getValues();
44:
45:                if($values->remember)
46:                {
47:                    $user->setExpiration('+30 days', FALSE);
48:                }
49:
50:                $password = \Hash::create('sha1', $values->password, \Glb::$SALT);
51:                $user->login($values->username, $password);
52:                $this->flashMessage('Přihlášení proběhlo úspěšně', 'success');
53:
54:
55:            }catch(NS\AuthenticationException $e)
inner-code	 AdminModule\LoginBoardPresenter->	 signInFormSubmitted (arguments ►)

.../libs/Nette/common/Callback.php:100	 source ►  call_user_func_array (arguments ►)

.../libs/Nette/common/ObjectMixin.php:59	 source ►  Nette\Callback->	 invokeArgs (arguments ►)

.../libs/Nette/common/Object.php:79	 source ►  Nette\ObjectMixin::	 call (arguments ►)

.../libs/Nette/Forms/Form.php:405	 source ►  Nette\Object->	 __call (arguments ►)

.../libs/Nette/Forms/Form.php:405	 source ►  Nette\Application\UI\Form->	 onSuccess (arguments ►)

.../libs/Nette/Application/UI/Form.php:131	 source ►  Nette\Forms\Form->	 fireEvents ()

.../libs/Nette/Application/UI/Presenter.php:302	 source ►  Nette\Application\UI\Form->	 signalReceived (arguments ►)

.../libs/Nette/Application/UI/Presenter.php:185	 source ►  Nette\Application\UI\Presenter->	 processSignal ()

.../libs/Nette/Application/Application.php:128	 source ►  Nette\Application\UI\Presenter->	 run (arguments ►)

.../www/index.php:9	 source ►  Nette\Application\Application->	 run ()
studna
Člen | 181
+
0
-

Definici služby musíš přesunout pod sekci services. Takhle:

common:
    parameters:

    php:
        date.timezone: Europe/Prague
        # zlib.output_compression: yes

    nette:
        application:
            errorPresenter: Error

        database:
            dsn: 'mysql:host=127.0.0.1;dbname=zk'
            user:
            password:

        session:
            autoStart: smart
            expiration: +30 days

    services:
        authenticator: AdminModule\model\AdminsModel::getAuthenticatorService
vosy
Člen | 532
+
0
-

Dix opraveno

services:
authenticator: AdminModule\AdminsModel::getAuthenticatorService
routerFactory: RouterFactory
router: @routerFactory::createRouter

ale furt stejna chyba

Jan Mikeš
Člen | 771
+
0
-

ukaz definici tridy tvojeho authenticatoru, asi neimplementuje pozadovane rozhrani..

David Matějka
Moderator | 6445
+
+1
-

vubec tam nevidim registrovany AdminsModel jako sluzbu + factory authenticatoru musis pres sluzbu AdminsModelu

services:
	adminsModel: AdminModule\model\AdminsModel
	authenticator: @adminsModel::getAuthenticatorService()

a je nutno specifikovat, co se vytvari, jelikoz to nette z factory nepozna
to muzes bud anotaci @return nad getAuthenticatorService nebo timhle v neonu:

services:
	authenticator:
		create: @adminsModel::getAuthenticatorService()
		class: Nette\Security\IAuthenticator

v adminsmodel dopln do konstruktoru typehint pro connection, jinak ti nepude autowiring

public function __construct(Nette\Database\Connection $connection)

Editoval matej21 (13. 9. 2013 15:19)

vosy
Člen | 532
+
0
-

Omlovam se za nedokonalosti, s nete zacinam tak se trochu peru.

Authenticator.php

<?php
namespace AdminModule;

use \Nette\Object;
use \Nette\Security as NS;

class Authenticator extends Object implements NS\IAuthenticator
{
    private $admins;

    function __construct(\Nette\Database\Table\Selection $admins)
    {
        $this->admins = $admins;
    }

    public function authenticate(array $opt)
    {
        list($login, $password) = $opt;
        // $row = $
        throw new NS\AuthenticationException('The password is incorrect XXD .', self::INVALID_CREDENTIAL);
    }
}
vosy
Člen | 532
+
0
-

tak ted mam uplne bilo stranku :)

config.neon

#
# SECURITY WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
#
# If you don't protect this directory from direct web access, anybody will be able to see your passwords.
# https://nette.org/en/security-warning
#
common:
    parameters:

    php:
        date.timezone: Europe/Prague
        # zlib.output_compression: yes

    nette:
        application:
            errorPresenter: Error

        database:
            dsn: 'mysql:host=127.0.0.1;dbname=zk'
            user:
            password:

        session:
            autoStart: smart
            expiration: +30 days


    services:
        adminsModel: AdminModule\AdminsModel
        authenticator: @adminsModel::getAuthenticatorService

        routerFactory: RouterFactory
        router: @routerFactory::createRouter


    factories:


production < common:

development < common:
<?php

namespace AdminModule;

use \Nette\Object;

class AdminsModel extends Object
{
    public $db;

    public function __construct(\Nette\Database\Connection $connection)
    {
        $this->db = $connection;
    }

    public function getAuthenticatorService()
    {
        return new Authenticator($this->db->table(\Glb::$NAZEV_WEBU.'_admins'));
    }
}
<?php
namespace AdminModule;

use \Nette\Object;
use \Nette\Security as NS;

class Authenticator extends Object implements NS\IAuthenticator
{
    private $admins;

    function __construct(\Nette\Database\Table\Selection $admins)
    {
        $this->admins = $admins;
    }

    public function authenticate(array $opt)
    {
        list($login, $password) = $opt;
        // $row = $
        throw new NS\AuthenticationException('The password is incorrect XXD .', self::INVALID_CREDENTIAL);
    }
}
vosy
Člen | 532
+
0
-

vypada to ze je vse vporadku :) vecer budu pokracovat, ted mi to uz nemysli :)

buksy
Člen | 22
+
0
-

Ahojte, a keby som chcel len v kazdom module pouzit iny authenticator? Ako to nastavim v configu aby vedel pre ktory modul ma ktory authenticator volat? Mam dva moduly, „Web“ a „App“

common:
	parameters:

	php:
		date.timezone: Europe/Prague
		# zlib.output_compression: yes

	nette:
		application:
			errorPresenter: Error

		database:
			dsn: 'mysql:unix_socket=/tmp/mysql51.sock;dbname=asdf'
			user: asdf
			password: asdf
			charset: utf8
		session:
			expiration: 14 days


	services:
		database: @Nette\Database\Connection
		authenticator: MyAuthenticator(@nette.database.default)
		routerFactory: RouterFactory
		router: @routerFactory::createRouter


	factories:


production < common:

development < common:
Majkl578
Moderator | 1364
+
0
-

Podívej se na addon Multi Authenticator od Vojty Dobeše.