2

how could i getState from zfcUser

in view/index.phtml i get it from $this->zfcUserIdentity()->getState();

but now i need to get this value ( state for this user who is logged in ), in other module /controller (this is my costum module controller)

so i need to get State from: zfcUser/Entity/User to myModule/Controller

i watch this https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in but this solutons is not helpful

5er_dev
  • 91
  • 5

3 Answers3

7

and this helps too, for me:

$sm = $this->getServiceLocator();
$auth = $sm->get('zfcuserauthservice');
if ($auth->hasIdentity()) {
    $user_edit = $auth->getIdentity()->getPrem();
}
personman
  • 65
  • 9
5er_dev
  • 91
  • 5
1

follow this code, I had same problem then I have manage how to use identity of logged in user via zfcUser

in other modules controller at topside,

  use Zend\EventManager\EventManagerInterface;

then create this two function in the sameclass,

public function setEventManager(EventManagerInterface $events)
{
     parent::setEventManager($events);

    $controller = $this;
    $events->attach('dispatch', function ($e) use ($controller) {

        if (is_callable(array($controller, 'checkUserIdentity')))
        {
            call_user_func(array($controller, 'checkUserIdentity'));
        }
    }, 100);
}

public function checkUserIdentity()
{

    if ($this->zfcUserAuthentication()->hasIdentity()) {
    echo "<pre>"; print_r($this->zfcUserAuthentication()->getIdentity());die;

        }

}

it will give this kind of output

Admin\Entity\User Object
(
[id:protected] => 2
[username:protected] => 
[email:protected] => rajat.modi@softwebsolutions.com
[displayName:protected] => 
[password:protected] => $2y$14$2WxYLE0DV0mH7txIRm7GkeVJB3mhD4FmnHmxmrkOXtUFL7S9PqWy6
[state:protected] => 
)

That's it you will automatically get Identity whether user is logged in if not then it will redirect to login page.

Hope this will helps

Rajat Modi
  • 1,343
  • 14
  • 38
  • sorry no, i need just getState form user who is logged in, if is not, then it give me NULL – 5er_dev Dec 24 '12 at 07:55
  • maybe must i use BjyAuthorize ? – 5er_dev Dec 24 '12 at 08:13
  • i try put this in other controller : use ZfcUser\Entity; an then in function this: $state = $this->getState(); but it give me just error, help please – 5er_dev Dec 24 '12 at 09:14
  • I updated my code please review it and let me know it works or not... it is tested by me.... – Rajat Modi Dec 24 '12 at 10:15
  • and yes state shows blank because in database it is empty column that'z why it is empty string. insert some data it will show in this field. hope this makes sense... – Rajat Modi Dec 24 '12 at 10:21
1

The state is a property from the user itself. So if you get the user throught the identification service, you can grab the state from there.

public function myFooAction()
{
    if ($this->zfcUserAuthentication()->hasIdentity()) {
        $user  = $this->zfcUserAuthentication()->getIdentity();
        $state = $user->getState();
    }
}

Mind that when the user is not logged in, the if condition is false. Also the state can be null or any arbitrary value, so do not expect that every user returns a valid state (in other words, check the returned value!)

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99