How to get instance of Authentication Service

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get instance of Authentication Service

Khurram
This post has NOT been accepted by the mailing list yet.
I have authenticated my user using DbTable and stored the identity into the session.
Now on every action i have to get the instance of  AuthenticationService() using the code below.



$auth = new AuthenticationService();
if($auth->hasIdentity())
{
}

Is there a way i can get the Already defined instance () of it or do i have write a singleton class for it. ?

Regards to experts
Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

Marco Pivetta
Heya,

why don't you make your authenticationservice a shared service in your ServiceManager? The whole concept of Singletons was removed from ZF2 for a reason...




On 10 December 2012 10:02, Khurram [via Zend Framework Community] <[hidden email]> wrote:
I have authenticated my user using DbTable and stored the identity into the session.
Now on every action i have to get the instance of  AuthenticationService() using the code below.



$auth = new AuthenticationService();
if($auth->hasIdentity())
{
}

Is there a way i can get the Already defined instance () of it or do i have write a singleton class for it. ?

Regards to experts


If you reply to this email, your message will be added to the discussion below:
http://zend-framework-community.634137.n4.nabble.com/How-to-get-instance-of-Authentication-Service-tp4658367.html
To start a new topic under Zend Auth, email [hidden email]
To unsubscribe from Zend Framework Community, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

Khurram
HI , thanks for beautiful conceptual reply, just wondaring how would you achieve this. sorry to ask but a small snippet of code would be highly appreciated. It is my second week on zf2.
thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

samsonasik
Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

Marco Pivetta
In reply to this post by Khurram
You can handle this via the ServiceManager. You can read up the docs for it at http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.intro.html

Basically, you can check how ZfcUser does this (it's a quite complex module, but it has exactly what you probably are re-building from scratch):

https://github.com/ZF-Commons/ZfcUser/blob/master/Module.php#L97-L102

This basically defines that `zfcuser_auth_service` is a service, and the closure is the logic that should be executed when it is requested for the first time. After that, the instance is "shared" and can be accessed wherever you have access to the service manager as following:

$authService = $serviceManager->get('zfcuser_auth_service');






On 10 December 2012 10:23, Khurram [via Zend Framework Community] <[hidden email]> wrote:
HI , thanks for beautiful conceptual reply, just wondaring how would you achieve this. sorry to ask but a small snippet of code would be highly appreciated. It is my second week on zf2.
thanks


If you reply to this email, your message will be added to the discussion below:
http://zend-framework-community.634137.n4.nabble.com/How-to-get-instance-of-Authentication-Service-tp4658367p4658370.html
To start a new topic under Zend Auth, email [hidden email]
To unsubscribe from Zend Framework Community, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

Khurram
In reply to this post by samsonasik
i have the following in my Module.php

 public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Application\Config\DbAdapter' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return $dbAdapter;
                },
                 'Admin\Model\PagesTable' => function($sm){
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $pagesTable = new PagesTable(new TableGateway('pages',$dbAdapter) );
                    return $pagesTable;
                },
                'Admin\Authentication\Service' => function($sm){
                    return new AuthenticationService();
                   
                }
            ),
        );
    }

It instantiates a new instance of AuthenticationService() every time i call this i want to know a way  how to create a shared instance of it so that i can get it in my whole application for all the Modules controllers


On Mon, Dec 10, 2012 at 2:52 PM, samsonasik [via Zend Framework Community] <[hidden email]> wrote:
You can read my blog post : https://samsonasik.wordpress.com/2012/10/23/zend-framework-2-create-login-authentication-using-authenticationservice-with-rememberme/ . For sample code, clone from my github account https://github.com/samsonasik/SanAuth


If you reply to this email, your message will be added to the discussion below:
http://zend-framework-community.634137.n4.nabble.com/How-to-get-instance-of-Authentication-Service-tp4658367p4658371.html
To unsubscribe from How to get instance of Authentication Service, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

samsonasik
This post has NOT been accepted by the mailing list yet.
if you're not inject of class with other class. simplify with invokables.

'invokables' => array(
    'Admin\Authentication\Service' => 'Admin\Authentication\AuthenticationService'
)  
Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

Perosurac
This post was updated on .
Hi there,

How can we have the same result if we want to pass some parameters in the constructor of this class ?

Thanks in advance.

Perosurac

"We must become the change we want to see in the world." (Gandhi)

http://www.thezeitgeistmovement.com/
Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

samsonasik
This post has NOT been accepted by the mailing list yet.
via factories key
Reply | Threaded
Open this post in threaded view
|

Re: How to get instance of Authentication Service

Khurram
Thanks a lot samsonasik,

i successfully grabbed it from very top level onBootstrap function

$path = $e->getRequest()->getUri()->getPath();
       
        if(strripos($path,'Dashboard')){
            $authService = $serviceManager->get('Admin\Authentication\Service');
            if (!$authService->hasIdentity()) {
                return $e->getResponse()
                             ->setStatusCode(401)
                             ->getHeaders()
                             ->addHeaderLine('location', '/zfcms/public/Admin');
            }
           
        }


That solved my problem and Route Dashboard controller doensn't need to check Auth any more inside its everyAction() ..
Regards,

On Fri, Dec 14, 2012 at 11:26 AM, samsonasik [via Zend Framework Community] <[hidden email]> wrote:
via factories key


If you reply to this email, your message will be added to the discussion below:
http://zend-framework-community.634137.n4.nabble.com/How-to-get-instance-of-Authentication-Service-tp4658367p4658482.html
To unsubscribe from How to get instance of Authentication Service, click here.
NAML