di User model di tambahkan baris seperti ini.
function validLogin($data)
{$user = $this->find(array('username' => $data['User']['username'], 'password' => ($data['User']['password'])), array('id', 'username', 'password'));
if(!empty($user)){
$this->user = $user['User'];
return TRUE;
}
else {
return FALSE;
}}
Di atas user_controller.php di tambahkan
var $components = array('Auth','Session');
var $layout = "login";
dan di bawah index tambahkan prosedur login.
function login()
{
//user already logged in?
//checking if session has been written
$user_id = $this->Auth->user('id');
if (!empty($user_id) && $this->Session->valid())
{
$this->Session->setFlash('You are already logged in');
$this->redirect(array('action'=>''), null, true);
}
else
{
if(!empty($this->data))
{
//calling login validation validLogin() in model
if($this->User->validLogin($this->data))
{
if($this->Auth->login($this->User->user))
{
$this->Session->setFlash('You have successfully logged in');
$this->Session->write('LoggedIn',$this->Auth->user());$this->redirect('/projects');
}} else {
$this->Session->setFlash('Invalid login, try again!');
$this->set('auth_msg', 'Invalid login, try again!');
$this->set('password', null);
}
}
else
{
$this->Session->setFlash('Please enter your username and password!');
$this->set('auth_msg', 'Please enter your username and password');
$this->set('password', null);
}
}}
function logout()
{
$this->Session->destroy('user');
$this->Session->setFlash('You\'ve successfully logged out.');
$this->redirect('/users/login');
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid User.', true));
$this->redirect(array('action'=>'index'));
}
$this->set('user', $this->User->read(null, $id));
}
Pada view tambahkan satu file lagi. login.ctp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<?php __('Please Log-In'); ?>
<?php echo $title_for_layout;?>
</title>
<?php
echo $html->charset();
echo $html->meta('icon');echo $html->css('cake.generic');
echo $scripts_for_layout;
?>
</head>
<body>
<div id="container">
<div id="header">
<h1><?php echo $html->link(__('Please Log-In', true), '');?></h1>
</div>
<div id="content">
<?php
if ($session->check('Message.flash')):
$session->flash();
endif;
?><?php echo $content_for_layout;?>
</div>
<div id="footer">
<?php echo $html->link(
$html->image('cake.power.gif', array('alt'=> __("CakePHP: the rapid development php framework", true), 'border'=>"0")),
'http://www.cakephp.org/',
array('target'=>'_new'), null, false
);
?>
</div>
</div>
<?php echo $cakeDebug?>
</body>
</html>
file ini di modifikasi dari default.ctp pada cake template skel
pada app_controller.php ada prosedur-prosedur yang membantu proses login. saya masukkan juga yang lainnya terutama untuk hal-hal yang membantu dalam penanganan session.
class AppController extends Controller {
var $helper = array('Form','Session','Ajax','Javascript','Html');
var $components = array('Auth','Session');function beforeFilter()
{
//actions we allow without authentication, you can also put them in the app_controller.php
$this->Auth->allow('register', 'login', 'logout','add');
$this->Session->write('params',$this->params);
$this->set('mysession',$this->Session->read()); //this is for the ACL phpGACL
$this->set('username',$this->Auth->user('username'));
$this->set('controller_action',$this->params['controller'].'_'.$this->params['action']) ;
$this->set('refer',Controller::referer()); //referer$username = $this->Auth->user('username');
$controller_action = $this->params['controller'].'_'.$this->params['action'];
}
}
0 komentar:
Posting Komentar