Lithium PHP framework: User management

This is the configuration for simple user management function in Lithium php framework.

Activate user/session in app/config/bootstrap.php

require __DIR__ . '/bootstrap/session.php';
require __DIR__ . '/bootstrap/users.php';

app/config/bootstrap/session.php

<?php
use lithium\storage\Session;

Session::config(array(
	'default' => array('adapter' => 'Php')
));

use lithium\security\Auth;

Auth::config(array(
 	'default' => array(
 		'adapter' => 'Form',
 		'model' => 'Users',
 		'fields' => array('username', 'password')
 	)
));
?>

app/config/bootstrap/users.php

<?php

use app\models\Users;
use lithium\security\Password;
use lithium\util\String;

Users::applyFilter('save', function($self, $params, $chain) {
    if ($params['data']) {
        $params['entity']->set($params['data']);
        $params['data'] = array();
    }
    if (!$params['entity']->exists()) {
        $params['entity']->password = String::hash($params['entity']->password);
    }
    return $chain->next($self, $params, $chain);
});

?>

app/controllers/SessionsController.php

<?php
namespace app\controllers;

use lithium\security\Auth;

class SessionsController extends \lithium\action\Controller {

    public function add() {
        if ($this->request->data && Auth::check('default', $this->request)) {
            return $this->redirect('/');
        }
        // Handle failed authentication attempts
    }
    
    public function delete() {
    	Auth::clear('default');
    	return $this->redirect('/');
    }
}
?>

app/controllers/UsersController.php

<?php
namespace app\controllers;

use lithium\security\Auth;
use app\models\Users;

class UsersController extends \lithium\action\Controller {

    public function index() {
        if (!Auth::check('default')) {
    		return $this->redirect('Sessions::add');
    	}
    	
    	$users = Users::all();
        return compact('users');
    }

    public function add() {
    	/* uncomment this after adding users */
    	//if (!Auth::check('default')) {
    	//	return $this->redirect('Sessions::add');
    	//}
    	
        $user = Users::create($this->request->data);

        if (($this->request->data) && $user->save()) {
            return $this->redirect('Users::index');
        }
        return compact('user');
    }
}

?>

app/models/Users.php

<?php
namespace app\models;
class Users extends \lithium\data\Model {
}
?>

app/views/sessions/add.html.php

<h3>Log in</h3>
<?=$this->form->create(null); ?>
    <?=$this->form->field('username'); ?>
    <?=$this->form->field('password', array('type' => 'password')); ?>
    <?=$this->form->submit('Log in'); ?>
<?=$this->form->end(); ?>

app/views/users/add.html.php

<h3>Add user</h3>
<?=$this->form->create($user); ?>
<?=$this->form->field('username'); ?>
<?=$this->form->field('password', array('type' => 'password')); ?>
<?=$this->form->submit('Create'); ?>
<?=$this->form->end(); ?>

app/views/users/index.html.php

<h3>User List</h3>
<ul>
	<?php foreach ($users as $user) { ?>
	<li><?=$user->username; ?></li>
	<?php } ?>
</ul>

Then access
http://yourserver/users/add

Add an user then uncomment the check in Users.add()

Have fun!

Advertisements

One thought on “Lithium PHP framework: User management

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s