app controller:
$this->Auth->fields = array('username' => 'username', 'password' =>
'password');
view:
<?=$this->Form->input('name',array('label'=>'Username'));?>
On 21/06/2011 14:54, prabha wrote:
<?php
class AppController extends Controller {
var $components = array('Auth','Email','Session','SeoUrl');
function beforeFilter(){
//reads the site-wide config values from the DB and
puts them
through the Configure::write method
$this->Setting->getcfg();
$this->Auth->fields = array('username' => 'username',
'password' =>
'password');
$this->Auth->allow('*');
$this->Auth->authorize = 'controller';
$this->Auth->userScope = array('User.account_status' =>
'active','User.email_status' => 'active');
$this->set('loggedIn', $this->Auth->user('id'));
print_r($this->Auth->user('id'));
$this->Cookie->name = 'famousQuotes';
}
function isAuthorized() {
return true;
}
}
?>
<?php
class UsersController extends AppController {
var $name = 'Users';
function signup(){
if (!empty($this->data)) {
$this->data['User']['password2hashed'] = $this->Auth-
password($this->data['User']['password2']);
$this->User->set($this->data);
if ($this->User->validates()) {
if(isset($this->data['User']['password2']))
$this->data['User']['email_validation_key'] = String::uuid();
$this->data['User']['userip'] =
$_SERVER['REMOTE_ADDR'];
$this->User->create();
if ($this->User->save($this->data)) {
$this->Email->to =
$this->data['User']['email'];
$this->Email->subject = Confirmation';
$this->Email->replyTo =
'[email protected]';
$this->Email->from =
Site<[email protected]>';
$this->Email->sendAs = 'html';
$this->Email->template = 'confirmation';
$this->set('name',
$this->data['User']['username']);
$this->set('server_name',
$_SERVER['SERVER_NAME']);
$this->set('id',
$this->User->getLastInsertID());
$this->set('code',
$this->data['User']['email_validation_key']);
if ($this->Email->send()) {
$this->set('confirm_mail','Confirmation mail sent. Please check
your inbox');
} else {
//$this->User->delete($this->User->getLastInsertID());
$this->set('error_message','There was a problem sending the
confirmation mail. Please try again');
}
} else {
$this->set('error_message','There was
an error signing up.
Please, try again.');
$this->data = null;
}
}
else
{
$User = $this->User->invalidFields();
$data = compact('User');
$this->set('errors', compact('message',
'data'));
}
$this->render('user_signup_ajax','ajax');
}
}
function confirm($user_id=null, $code=null) {
if(empty($user_id) || empty($code)) {
$this->set('email_status', 'inactive');
$this->set('account_status', 'inactive');
$this->render();
}
$user = $this->User->read(null, $user_id);
if(empty($user)) {
$this->set('email_status', 'inactive');
$this->set('account_status', 'inactive');
$this->render();
}
if($user['User']['email_validation_key'] == $code){
$this->User->id = $user_id;
$this->User->saveField('email_status', 'active');
$this->User->saveField('account_status', 'active');
$this->set('account_status', 'active');
$this->set('email_status', 'active');
} else {
$this->set('email_status', 'inactive');
$this->set('account_status', 'inactive');
}
}
function login() {
$this->Auth->login($this->data);
}
function logout() {
$this->Session->setFlash('Logout');
$this->redirect($this->Auth->logout());
}
}
?>
Login View
<div class="post">
<h2>Login</h2>
<?=$this->Form->create('User',array('class'=>'form'));?>
<fieldset>
<?=$this->Form->input('name',array('label'=>'Username'));?>
<?=$this->Form-
input('password',array('type'=>'password','label'=>'Password'));?>
<div class="clear"></div>
</fieldset>
<?php echo $this->Form->end('Login'); ?>
</div>
db has
users table
username => 'user1'
password => '0cda0f6d0b0d6bb2d13866f09b6456c9ad4d0ddd'
account_status => 'active'
email_status => 'active'
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php