<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController {
public function signup() {
if ($this->request->is('post')) {
if ($this->User->save($this->data)) {
$url = 'activate/' . $this->User->id . '/' . $this->User->getActivationHash();
$url = Router::url($url, true);
//sending Email
$email = new CakeEmail();
$email->from(array('sender@domain.com' => 'Sender'));
$email->to($this->data['Users']['email']);
$email->subject('User activiation');
$email->send($url); // メール本文に本登録用リンクを記す
$this->Session->setFlash('sent for activiation');
} else {
$this->Session->setFlash('error');
}
}
}
public function activate($user_id = null, $in_hash = null) {
$this->User->id = $user_id;
if ($this->User->exists() && $in_hash == $this->User->getActivationHash()) {
$this->User->saveField('active', 1);
$this->Session->setFlash('Your user account could be activated');
} else {
$this->Session->setFlash('Invalid link');
}
}
// ログイン
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->User->id = $this->Auth->user('id');
$this->User->saveField('logins', $this->Auth->user('logins') + 1); //通算ログイン回数
$this->User->saveField('lastlogin', date('Y-m-d H:i:s')); //最終ログイン日時
return $this->redirect($this->Auth->redirect());
} else {
$active = $this->User->field('active', array('username' => $this->data['User']['username']));
if ($active === 0) {
$this->Session->setFlash('Plesdr activate your account');
} else {
$this->Session->setFlash('Your username or password is wrong');
}
}
}
}
public function logout() {
$this->Session->setFlash('Loggedout');
return $this->redirect($this->Auth->logout());
}
public function dashboard() {
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(array('signup', 'activate', 'login'));
}
public function password() {
$id = $this->Auth->user('id');
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException('Invalid user');
}
if ($this->request->is('post') || $this->request->is('put')) {
debug($this->request->data);
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('your passworf is updated');
$this->redirect(array('action' => 'dashboard'));
} else {
$this->Session->setFlash('Your password could not be updated');
}
}
$this->request->data = $this->User->read(null, $id);
}
}
?>
コード
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。