working with session on zf2 (recover container)

manou

PRECEDENTS:

  • using a custom hack OF ZfcUserLdap to authenticate against a LDAP server (include zfcUser too as dependency)
  • the hack is due the Ldap server uses a ldapc wrapper, so the bind and search process doesn't belong to Ldap standards but through a ldapc library
  • the login/password box works great against the Ldap server by modifying the bind and findbyuser methods

NEED:

  • add country selection at login step
  • check if the user has the permission to work with this country (so to have the country here has sense, don't need ACL, it will be check through LDAP user groups)
  • store the selected country to use along the whole application

WORK IN PROGRESS:

  • add SELECT dropdown with available countries to login box [OK]
  • get the country selected at the login form [OK]

    -> at authenticate method on ZfcUserLdap\Authentication\Adapter\Ldap.php class I get correctly the country set at the form

PROBLEM:

  • how to store the country into a session variable,

    -> since zfcUser has an Storage defined and the country is defined at the login step, I would like to use that Storage

I will appreciate any kind of clarification or tips to accomplish this task.

SOLUTION:

The logic is more at zfcUserLdap module, since the auth is against an LDAP Server. I added to the Entity extended at zfcUserLdap a new property, country that is set to the Entity object along the findByUsername method.

public function findByUsername($username, $country = null)
{
    $pUser = $this->ldap->findByUsername($username);

    if (isObjectNotNull($pUser))
    {
        $this->entity->setDisplayName(getLdapUserFirstName($pUser) . ' ' . getLdapUserLastName($pUser));
        $this->entity->setEmail(getLdapUserMail($pUser));
        $this->entity->setId(getLdapUserUid($pUser));
        $this->entity->setUsername(getLdapUserUid($pUser));
        $this->entity->setLdapcObject($pUser);
        $this->entity->setUserCountry($country);

        return $this->entity;
    }
    else {
        return null;
    }
}

To have the country here will be useful because the authentication process might check if the username has permission to work within that country. I'll need to add that check later.

Like this, the country is part of the entity object, so I can get the country at the same way I was able to get the username.

For now, I have create a View Helper very similar to ZfcUserDisplayName. I just update the get metohd to get the country property.

 $countryName = $user->getUserCountry();

I plan to create a Controller Plugin to get the country from any Controller.

Saeven

ZFCUser has an authenticate event that you should leverage for this. IN your Module's main bootstrap:

$sm = $e->getApplication()->getServiceManager();
$zfcAuthEvents = $e->getApplication()->getServiceManager()->get('ZfcUser\Authentication\Adapter\AdapterChain')->getEventManager();

$zfcAuthEvents->attach( 'authenticate', function( $authEvent ) use( $sm ){
    try
    {
        // use $authEvent->getIdentity() to get country and stick it in a session

        return true;
    }
    catch( \Exception $x )
    {
        // handle it
    }
});

How you store in session is up to you, there's 400 ways to skin that cat.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ZF2 get global session container

From Dev

ZF2 deleteAction not working

From Dev

Translation is not working with ZF2

From Dev

ZF2 setCookie not working

From Dev

zf2 session validation failed

From Dev

ZF2 translator is not working in controller?

From Dev

ZF2 transalation is not working in form class

From Dev

ZF2 segment child route not working

From Dev

ZF2 route with prefix in not working for default

From Dev

ZF2 : Filedset validation is not working

From Dev

ZF2 - How to check if session remember me is still valid?

From Dev

zf2 navigation, how to hide some items based on session

From Dev

Passing session variable to external url in zf2

From Dev

Losing ZF2 Session after Paypal redirection

From Dev

ZF2 redirect() not working while header location does

From Dev

ZF2 form HTML5 Number element is not working

From Dev

ZF2 Annotation Validators NotEmpty and Int not working?

From Dev

recover ssh session connection

From Dev

ZF2, exception was raised while creating a listener & Session validation failed

From Dev

How to recover a 'lost' screen session?

From Dev

Doctrine2 with MS Server pagination and order by not working under ZF2

From Dev

Session timeout not working in Cakephp 2

From Dev

ZF2, I18n and dynamic locale not working anymore after update

From Dev

Why did javascript stop working when I implemented ZF2 ACL?

From Dev

ZF2, I18n and dynamic locale not working anymore after update

From Dev

Zf2 child route not working when it has one more child.

From Dev

@Recover is not working if @Retryable returns a value

From Dev

Can you recover ~/.ssh contents on AWS EC2 with an open session and keys?

From Dev

Session are not working

Related Related

HotTag

Archive