Search code examples
zend-frameworkzend-authzend-framework3

Authenticationservice in Zend framework 3


Is the authenticationservice in ZF3 still available? If not, what is the name of the new one? Does it work the same?

I get the error message:

Class 'Import\AuthenticationService' not found

I asked composer to grab it:

composer

I also searched for it at zend-framework.com and I couldn't find any information either migration topics.

Because I'm coming from ZF1 I would like to ask also if my idea is correct. I implemented the authenticationService as factory in my Module.php:

'factories' => [

                    Model\Authentication::class => function ($container){
                        $auth = new AuthenticationService();
                        $dbAdapter = $container->get(AdapterInterface::class);  // AND aktiv != 0
                        $dbTableAuthAdapter  = new DbTableAuthAdapter($dbAdapter, 't_user','accessname','password', 'SHA2(?, 512)');
                        $auth->setAdapter($dbTableAuthAdapter);
                        return $auth;
                    },

I connected the auth service to my controller (Module.php):

Controller\IndexController::class => function($container) {
                        return new Controller\IndexController(
                                $container->get(Model\UserTable::class),
                                $container->get(Model\Authentication::class),
                                $container->get(AdapterInterface::class)
                                );
                    },

My Indexcontroller looks like follows:

class IndexController extends AbstractActionController
{
    private $loginTable;
    private $authService;
    private $db;

public function __construct(UserTable $loginTable, AuthenticationService $authService, AdapterInterface $db)
{
    //$db=$this->db ;
    $this->loginTable = $loginTable;
    $this->authService  = $authService;
    $this->db=$db;
}

//Anzeigen der importierten Dateien
public function indexAction()
{
    $berechtigung = (int) $this->loginTable->getBerechtigung($this->authService->getIdentity());
    if(!$this->authService->hasIdentity() || $berechtigung > 1){ // 1 = Administrator
        return $this->redirect()->toRoute('user', ['action' => 'login']);
    }
    else {

        return $this->redirect()->toRoute('project', ['action' => 'index']);
    }

}

Is this the proper way to implement the authentication?


Solution

  • To install zend-authentication please type this in your terminal

    composer require zendframework/zend-authentication
    

    And about your error message like this

    Class 'Import\AuthenticationService' not found

    You should use it by full namespace like this

    use Zend\Authentication\AuthenticationService;