cakephp authentication

cakephp authentication

appconrtoller:

class AppController extends Controller {
/**
* Helpers will be used
*
* @var array
*/
public $helpers = array(‘Html’, ‘Form’, ‘Session’, ‘Text’, ‘Js’); //, ‘GeneralFunctions’
/**
* Components will be used
*
* @var array
*/
public $components = array(‘Session’, ‘RequestHandler’, ‘Paginator’, ‘Auth’, ‘Cookie’, ‘Email’);

function beforeFilter() {
parent::beforeFilter();

$this->Auth->authenticate = array(‘all’ => array(‘scope’ => array(‘User.user_type’ => array(‘user’), ‘User.is_blocked’ => 0,’User.status’ => 1)), ‘Form’ => array(‘fields’ => array(‘username’ => ’email’, ‘password’ => ‘password’)));
$this->Auth->loginAction =  array(‘plugin’ => false, ‘controller’ => ‘users’, ‘action’ => ‘login’);
$this->sessiondata = ‘Auth.User’;
$loginRedirect =  ‘dashboard’;
$this->Auth->loginRedirect = array(‘plugin’ => false, ‘controller’ => ‘users’, ‘action’ => ‘dashboard’);

}

 

}

user conroller

class UsersController extends UsersAppController {

public $uses = array(‘Users.User’);
public $helpers = array(‘Html’, ‘Form’);
public $components = array(‘Auth’);

function beforeFilter() {
parent::beforeFilter();
$this->set(‘model’, $this->modelClass);

$this->Auth->allow(‘register’, ‘login’,’admin_login’);

}

function login() {

if($this->Auth->loggedIn()){

$this->redirect(array(‘plugin’=>’users’,’controller’=>’users’,’action’=>’dashboard’));

}

if (!empty($this->data)) {

$this->{$this->modelClass}->set($this->data);
if ($this->{$this->modelClass}->loginvalidation()) {

if ($this->Auth->login()) {

return $this->redirect($this->Auth->redirect());
}
} else {

}
} else {

}
}

function dashboard(){

}

function logout(){
return $this->redirect($this->Auth->logout());

}

}

 

Usser model

 

<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* CakePHP User
* @author vijayraj
*/
App::uses(‘AppModel’, ‘Model’);

class User extends AppModel {

public $uses = ‘User’;

//validation for login form

function loginvalidation() {
$validation1 = array(
’email’ => array(
’empty’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => ‘error_name’
),
‘valid’=>array(
‘rule’=>’email’,
‘message’=>’please enter valid email’

),

),

‘password’ => array(
’empty’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => ‘error_name’
)
),

);
$this->validate = $validation1;
return $this->validates();
}

}

 

login.ctp

 
<div class=”row”>

<?php echo $this->form->create($model,array(‘class’=>’form-horizontal’)); ?>
<div class=”form-group”>

<?php echo $this->Form->label($model.’.email’,__d(‘default’,’email’),array(‘class’=>’col-sm-2 control-label’));  ?>
<div class=”col-sm-10″>

<?php echo  $this->Form->text($model.’.email’,array(‘class’=>’form-control’,’label’=>false,’required’=>false,’error’=>false)); ?>
<?php  echo $this->form->error($model.’.email’); ?>
</div>
</div>
<div class=”form-group”>
<?php echo $this->Form->label($model.’.password’,__d(‘default’,’password’),array(‘class’=>’col-sm-2 control-label’));  ?>
<div class=”col-sm-10″>

<?php echo  $this->Form->password($model.’.password’,array(‘class’=>’form-control’,’label’=>false,’required’=>false,’error’=>false)); ?>
<?php echo $this->form->error($model.’.password’); ?>
</div>
</div>

<div class=”form-group”>
<div class=”col-sm-offset-2 col-sm-10″>

<?php echo  $this->Form->submit(‘Sign in’,array(‘class’=>’btn btn-default’)); ?>
</div>
</div>
<?php echo  $this->Form->end(); ?>

</div>

 

 

encryption decryption method in php

class Encryption {
var $skey = “d7ceb4c8102153ba-%^&*#@!#@%$#^&^$%%@$$$544d30008ed7e738″; // you can change it

public  function safe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array(‘+’,’/’,’=’),array(‘-‘,’_’,”),$data);
return $data;
}

public function safe_b64decode($string) {
$data = str_replace(array(‘-‘,’_’),array(‘+’,’/’),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr(‘====’, $mod4);
}
return base64_decode($data);
}

public  function encode($value){
if(!$value){return false;}
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);
return trim($this->safe_b64encode($crypttext));
}

public function decode($value){
if(!$value){return false;}
$crypttext = $this->safe_b64decode($value);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}
}
$str = “vijayraj”;

$converter = new Encryption;
$encoded = $converter->encode($str );
$decoded = $converter->decode($encoded);

echo “$encoded<p>$decoded”;

Remove “index.php” in codeigniter’s path

If you are using Apache place a .htaccess file in your root web directory containing the following:

RewriteEngine On
RewriteBase /code_work/

RewriteRule ^code_work/?$ index.php/admin/ [L]
RewriteRule ^code_work/([^.]+)/?$ index.php/admin/$1 [L]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
 for admin penal:-

in rout.php

$route[‘admin’]=”admin/home”;
$route[“admin/(:any)”]=”admin/$1″;