我正在应用本教程:Activating User Account via Email
当我提交邮件时,会按以下方式发送:
嘿,艾哈迈德,我们会让您立刻启动并运行,但首先我们只需要您通过点击以下链接确认您的用户帐户:
的http://本地主机/ cakenews /用户/激活/ 24 / 8877ae4a
问题是我想创建文件users / active.ctp,以便在用户点击链接时接收激活码。但我不知道该文件写给活跃用户的内容
<?php
# /controllers/users_controller.php
# please note that not all code is shown...
uses('sanitize');
class UsersController extends AppController {
var $name = 'Users';
// Include the Email Component so we can send some out :)
var $components = array('Email', 'Auth');
// Allow users to access the following action when not logged in
function beforeFilter() {
$this->Auth->allow('register', 'confirm', 'logout');
$this->Auth->autoRedirect = false;
}
// Allows a user to sign up for a new account
function register() {
if (!empty($this->data)) {
// See my previous post if this is forgien to you
$this->data['User']['password'] = $this->Auth->password($this->data['User']['passwrd']);
$this->User->data = Sanitize::clean($this->data);
// Successfully created account - send activation email
if ($this->User->save()) {
$this->__sendActivationEmail($this->User->getLastInsertID());
// this view is not show / listed - use your imagination and inform
// users that an activation email has been sent out to them.
$this->redirect('/users/register');
}
// Failed, clear password field
else {
$this->data['User']['passwrd'] = null;
}
}
}
/**
* Send out an activation email to the user.id specified by $user_id
* @param Int $user_id User to send activation email to
* @return Boolean indicates success
*/
function __sendActivationEmail($user_id) {
$user = $this->User->find(array('User.id' => $user_id), array('User.id','User.email', 'User.username'), null, false);
if ($user === false) {
debug(__METHOD__." failed to retrieve User data for user.id: {$user_id}");
return false;
}
// Set data for the "view" of the Email
$this->set('activate_url', 'http://' . env('SERVER_NAME') . '/cakenews/users/activate/' . $user['User']['id'] . '/' . $this->User->getActivationHash());
$this->set('username', $this->data['User']['username']);
$this->Email->to = $user['User']['email'];
$this->Email->subject = env('SERVER_NAME') . ' - Please confirm your email address';
$this->Email->from = 'email@gmail.com';
$this->Email->template = 'user_confirm';
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'email@gmail.com',
'password'=>'password',
);
$this->Email->sendAs = 'text'; // you probably want to use both :)
return $this->Email->send();
}
}
/**
* Activates a user account from an incoming link
*
* @param Int $user_id User.id to activate
* @param String $in_hash Incoming Activation Hash from the email
*/
function activate($user_id = null, $in_hash = null) {
$this->User->id = $user_id;
if ($this->User->exists() && ($in_hash == $this->User->getActivationHash()))
{
// Update the active flag in the database
$this->User->saveField('active', 1);
// Let the user know they can now log in!
$this->Session->setFlash('Your account has been activated, please log in below');
$this->redirect('login');
}
// Activation failed, render '/views/user/activate.ctp' which should tell the user.
}
?>
,模型是
/**
* Creates an activation hash for the current user.
*
* @param Void
* @return String activation hash.
*/
function getActivationHash()
{
if (!isset($this->id)) {
return false;
}
return substr(Security::hash(Configure::read('Security.salt') . $this->field('created') . date('Ymd')), 0, 8);
}
}
?>
视图是cakenews \ app \ views \ users \ register.ctp
<fieldset>
<legend>Add a article</legend>
<?php
echo $form->create('User');
echo $form->input('username');
echo $form->input('password');
echo $form->input('email');
?>
</fieldset>
<?php echo $form->end('Post project'); ?>
答案 0 :(得分:3)
在您的控制器中,操作activate
正在处理用户激活。如果用户已经过验证,则会重定向到登录操作,否则会尝试呈现activate.ctp
文件。因此,在这种情况下,您只需向用户显示无法完成注册的消息。请再试一次。要为视图设置一些数据(activate.ctp),您可以使用控制器的set方法。
如果您不想渲染activate.ctp文件,请将用户重定向到其他地方,告知注册无法完成。