diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 0d03705..5d27d89 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -4,9 +4,9 @@ namespace App\Controller; use App\Controller\ControllerBase, App\Form\UserSettings as UserSettingsForm, - App\Model\Data\ActivityLog, - App\Model\Data\PasswordLink, - App\Model\Data\User; + App\Model\Data\User, + App\Model\Data\UserActivation, + App\Model\Data\ActivityLog; class UserController extends ControllerBase { @@ -39,15 +39,15 @@ class UserController extends ControllerBase } // Else we create a password link and email. else { - $link = new PasswordLink(); - $link->setUserId($user->getId()) + $activation = new UserActivation(); + $activation->setUserId($user->getId()) ->setPassword($hash) ->save(); // Render the email content. $tpl = $this->di->get('template'); $content = $tpl->render('mail/password_activation', [ - 'link' => $link->getPublicId() + 'link' => $activation->getActivationKey() ]); // Send the email. @@ -113,7 +113,7 @@ class UserController extends ControllerBase */ public function activationLinkAction($id) { - $link = PasswordLink::findFirst(['public_id = ?0', 'bind' => [ $id ]]); + $link = UserActivation::findFirst(['activation_key = ?0', 'bind' => [ $id ]]); if ($link) { if ($link->isValid()) { diff --git a/app/models/Data/PasswordLink.php b/app/models/Data/UserActivation.php similarity index 74% rename from app/models/Data/PasswordLink.php rename to app/models/Data/UserActivation.php index 9f834c4..1703298 100644 --- a/app/models/Data/PasswordLink.php +++ b/app/models/Data/UserActivation.php @@ -6,7 +6,7 @@ use Httpcb\Mvc\Model\Behavior\RandomId as RandomIdBehavior; use Phalcon\Forms\Element\Date; use Phalcon\Mvc\Model\Behavior\SoftDelete; -class PasswordLink extends Base +class UserActivation extends Base { /** * @var int @@ -16,13 +16,18 @@ class PasswordLink extends Base /** * @var string */ - protected $public_id; + protected $activation_key; /** * @var int */ protected $user_id; + /** + * @var int + */ + protected $used; + /** * @var string */ @@ -38,7 +43,7 @@ class PasswordLink extends Base */ public function initialize() { - $this->setSource('password_link'); + $this->setSource('user_activation'); $this->useDynamicUpdate(true); // Relationships @@ -46,15 +51,20 @@ class PasswordLink extends Base // Behaviour $this->addBehavior(new RandomIdBehavior(array( - 'field' => 'public_id', - 'length' => 12, - 'expression' => '(password IS NULL OR HOUR(TIMEDIFF(date, NOW())) = 0)' + 'field' => 'activation_key', + 'length' => 40, + 'expression' => '(used = 0 OR HOUR(TIMEDIFF(date, NOW())) = 0)' ))); $this->addBehavior(new SoftDelete([ 'field' => 'password', 'value' => null ])); + + $this->addBehavior(new SoftDelete([ + 'field' => 'used', + 'value' => 1 + ])); } /** @@ -69,7 +79,7 @@ class PasswordLink extends Base * @param int $id * @return PasswordLink */ - public function setId(int $id) : PasswordLink + public function setId(int $id) : UserActivation { $this->id = $id; return $this; @@ -78,18 +88,18 @@ class PasswordLink extends Base /** * @return string */ - public function getPublicId() + public function getActivationKey() { - return $this->public_id; + return $this->activation_key; } /** * @param string $public_id - * @return PasswordLink + * @return UserActivation */ - public function setPublicId(string $public_id) : PasswordLink + public function setActivationKey(string $key) : UserActivation { - $this->public_id = $public_id; + $this->activation_key = $key; return $this; } @@ -103,9 +113,9 @@ class PasswordLink extends Base /** * @param int $user_id - * @return PasswordLink + * @return UserActivation */ - public function setUserId(int $user_id) : PasswordLink + public function setUserId(int $user_id) : UserActivation { $this->user_id = $user_id; return $this; @@ -121,9 +131,9 @@ class PasswordLink extends Base /** * @param string $password - * @return PasswordLink + * @return UserActivation */ - public function setPassword(string $password) : PasswordLink + public function setPassword(string $password) : UserActivation { $this->password = $password; return $this; @@ -136,7 +146,7 @@ class PasswordLink extends Base */ public function isUsed() : bool { - return strlen($this->password) < 1; + return (bool) $this->used; } /**