2 actions in my controller must be access for only logged users

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

2 actions in my controller must be access for only logged users

aoohralex
My login action is in controller AuthController in action LoginAction().

If I use Zend_Auth_Storage_Session() to store username logged persons and I would like to create that only logged users can have access to action addBookAction() and removeBookAction() in controller LibraryController - should I add to these actions:

    $storage = new Zend_Auth_Storage_Session();
    $data = $storage->read();
    if($data == null) $this->_redirect('auth/login');
So they look:

        public function addBookAction()
        {
                $storage = new Zend_Auth_Storage_Session();
                $data = $storage->read();
                if($data == null) $this->_redirect('auth/login');

                .........
        }

        public function removeBookAction()
        {
                $storage = new Zend_Auth_Storage_Session();
                $data = $storage->read();
                if($data == null) $this->_redirect('auth/login');

                .........
        }


Is it the best method ? Or maybe is better method ? Or maybe this method is the best ?
Reply | Threaded
Open this post in threaded view
|

Re: 2 actions my controller must be access for only loged users

Hector Virgen
Instead of going directly to Zend_Auth's storage (which may change), you can query Zend_Auth itself:

$auth = Zend_Auth::getInstance();
if (!$auth->hasIdentity()) {
    // do redirect
}

--
Hector


On Mon, Sep 21, 2009 at 4:39 AM, aoohralex <[hidden email]> wrote:

My login action is in controller AuthController in action LoginAction().

If I use Zend_Auth_Storage_Session() to store username logged persons and I
would like to create that only logged users can go to action addBookAction()
and removeBookAction() in controller LibraryController - should I add to
these actions:
   $storage = new Zend_Auth_Storage_Session();
   $data = $storage->read();
   if($data == null) $this->_redirect('auth/login');
So they look:

       public function addBookAction()
       {
               $storage = new Zend_Auth_Storage_Session();
               $data = $storage->read();
               if($data == null) $this->_redirect('auth/login');

               .........
       }

       public function removeBookAction()
       {
               $storage = new Zend_Auth_Storage_Session();
               $data = $storage->read();
               if($data == null) $this->_redirect('auth/login');

               .........
       }


Is it the best method ? Or maybe is better method ? Or maybe this method is
the best ?
--
View this message in context: http://www.nabble.com/2-actions-my-controller-must-be-access-for-only-loged-users-tp25530295p25530295.html
Sent from the Zend Framework mailing list archive at Nabble.com.


--
Hector Virgen