User Authentication

That's pretty easy with akelos.

This is how I did it. (I'am new to Akelos-Framework but I think this way to implement authentication is ok? But anyway I know pretty less about Akelos-Framework It's fun to work with it :-) )

prerequisites:

a user-model with at least the following fields (name, password and e_mail). If you don't have one create a user-model

OK, You know all your controllers extends the ApplicationController (app/application_controller.php)?

place the following function into it

<?php
 
require_once(AK_LIB_DIR.DS.'AkActionController.php');
 
/**
* This file is application-wide controller file. You can put all 
* application-wide controller-related methods here.
*
* Add your application-wide methods in the class below, your controllers 
* will inherit them.
* 
* @package ActionController
* @subpackage Base
*/
 
class ApplicationController extends AkActionController 
{
 
    private function authentication()
    {
        if (!empty($_SESSION['time'])) {
 
            if (!$_SESSION['time'] < time() - 1800)
                $_SESSION['time'];
        }
    }
 
    protected function forbiddenAction()
    {
 
        if (!empty($_SESSION['time'])) {
 
            if ($_SESSION['time'] < time() - 1800) {
 
                $_SESSION['name'] = '';
                $_SESSION['uid'] = '';
                $_SESSION['time'] = '';
                $this->redirectTo(array('controller' => 'home', 'action' => 'login'));
            }
        } else
            $this->redirectTo(array('controller' => 'home', 'action' => 'login'));
    }
}
 
?>

generate a home controller

script/generate controller Home

write the following methods

<?php
 
class HomeController extends ApplicationController
{
    var $layout = 'forum';
    var $models = 'User';
 
    public function __construct()
    {
        $this->beforeFilter('authentication', array('forbiddenAction'=>array('except'=>array('login', 'index', 'logout'))));
    }
 
    public function index()
    {
    }
 
    public function login()
    {
        if(!empty($this->params['login'])){
 
            $user = $this->User->find('first', array('conditions' => array('e_mail =?', $this->params['login']['e_mail'])));
            if ($user->password == $this->params['login']['password'])
            {
                $_SESSION['uid'] = $user->id;
                $_SESSION['name'] = $user->name;
                $_SESSION['time'] = time();
            }
 
        }
    }
 
    public function logout()
    {
        $_SESSION['uid'] = '';
        $_SESSION['name'] = '';
        $_SESSION['time'] = '';
        $this -> redirectTo(array('action' => 'index'));
    }
 
}
 
?>

now you can write a login/logout link into your template I use the layout (my layout called app/views/layouts/forum.tpl)

{?session-name}
     <?php  echo  $url_helper->link_to($text_helper->translate('Logout'), array('controller' => 'home', 'action' => 'logout'))?> {session-name}
{else}
    <?php  echo  $url_helper->link_to($text_helper->translate('Login'), array('controller' => 'home', 'action' => 'login'))?>
{end}

now you're ready :-)

don't forget place in every controller your

    public function __construct()
    {
        $this->beforeFilter('authentication', array('forbiddenAction'=>array('except'=>array('index', 'listing', 'show'))));
    }

OK, hope this help a bit for the first steps ;-)

badPussycat

 
user-authentication.txt · Last modified: 2008/08/31 18:30 by 217.7.247.2
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki