Skip to content

Instantly share code, notes, and snippets.

@basvandorst
Created May 31, 2012 11:12
Show Gist options
  • Save basvandorst/2842698 to your computer and use it in GitHub Desktop.
Save basvandorst/2842698 to your computer and use it in GitHub Desktop.
@maurice; Check this example of RegisterController.php with activateAction
<?php
class RegisterController extends Zend_Controller_Action {
public function init ()
{
$this->view->title = 'Register';
$this->view->menu = 'home';
}
public function indexAction() {
$formRegister = new Application_Forms_Register();
if ($this->_request->isPost()) {
if ($formRegister->isValid($this->_request->getPost())) {
$data = $formRegister->getValues();
// insert company
$company = new Application_Model_Company();
$id_company = $company->insert(array(
'name' => $data['company'],
'id_group' => 3
));
// insert default company in addressbook
$address = new Application_Model_Addressbook();
$address->insert(array(
'name' => $data['company'],
'id_company' => $id_company
),true);
$hash = md5(mt_rand().uniqid());
// insert user
$user = new Application_Model_User();
$user->insert(array(
'name' => $data['name'],
'email' => $data['email'],
'password' => md5(SALT.$data['password']),
'active' => '0',
'language' => 'NL',
'id_company' => $id_company,
'hash_activate' => $hash
));
// send e-mail to user (with activation link)
$url = 'http://'.$_SERVER['SERVER_NAME'].'/register/activate/?hash='.$hash;
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH. '/views/scripts/register/');
$html->assign("hash",$url);
$html->assign("name",$data['name']);
$body = $html->render('email.phtml');
$mail = new Zend_Mail();
$mail->setFrom('[email protected]', 'Application')
->addTo($data['email'], $data['email'])
->setSubject('Welkom bij Application')
->setBodyHtml($body)
->send();
$this->render("thanks");
} else {
$errors = $formRegister->getErrors();
$this->view->errors = $errors;
}
}
$this->view->form = $formRegister;
}
public function activateAction() {
// activate account + automatic login
Zend_Auth::getInstance()->clearIdentity();
$hash = $this->_getParam("hash","xxx");
$model = new Application_Model_User();
$user = $model->getByHash($hash);
if(isset($user) && count($user) > 0) {
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('user')
->setIdentityColumn('email')
->setCredentialColumn('hash_activate');
$authAdapter->setIdentity($user['email'])
->setCredential($hash);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid()) {
$userInfo = $authAdapter->getResultRowObject(null, 'password');
$user = new Application_Model_User();
$user->update(array(
'id'=> $userInfo->id,
'active' => 1,
'hash_activate' => null,
'lastlogin' => new Zend_Db_Expr('NOW()')
));
$company = new Application_Model_Company();
$companyInfo = $company->findById($userInfo->id_company);
$userInfo->company = $companyInfo[0];
$authStorage = $auth->getStorage();
$authStorage->write($userInfo);
$this->_redirect('/');
}
} else {
// no valid activation link, go to normal view modus...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment