<?php | |
namespace SystemSolution\Manager\Models; | |
use Phalcon\Mvc\Model; | |
use Phalcon\Mvc\Model\Validator\Uniqueness; | |
class Users extends Model | |
{ | |
/** | |
* | |
* @var integer | |
*/ | |
public $id; | |
/** | |
* | |
* @var string | |
*/ | |
public $name; | |
/** | |
* | |
* @var string | |
*/ | |
public $notyficationId; | |
/** | |
* | |
* @var string | |
*/ | |
public $email; | |
/** | |
* | |
* @var string | |
*/ | |
public $password; | |
/** | |
* | |
* @var string | |
*/ | |
public $mustChangePassword; | |
/** | |
* | |
* @var string | |
*/ | |
public $profilesId; | |
/** | |
* | |
* @var string | |
*/ | |
public $banned; | |
/** | |
* | |
* @var string | |
*/ | |
public $suspended; | |
/** | |
* | |
* @var string | |
*/ | |
public $active; | |
public $companyId; | |
public function beforeValidationOnCreate() { | |
if(empty($this->notyficationId)) { | |
$this->notyficationId = 0; | |
} | |
if (empty($this->password)) { | |
$tempPassword = preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(openssl_random_pseudo_bytes(12))); | |
$this->mustChangePassword = 'Y'; | |
$this->password = $this->getDI() | |
->getSecurity() | |
->hash($tempPassword); | |
} else { | |
$this->mustChangePassword = 'N'; | |
} | |
} | |
/** | |
* Send a confirmation e-mail to the user if the account is not active | |
*/ | |
public function afterSave() | |
{ | |
if ($this->active == 'N') { | |
$emailConfirmation = new EmailConfirmations(); | |
$emailConfirmation->usersId = $this->id; | |
if ($emailConfirmation->save()) { | |
$this->getDI() | |
->getFlash() | |
->notice('A confirmation mail has been sent to ' . $this->email); | |
} | |
} | |
} | |
/** | |
* Validate that emails are unique across users | |
*/ | |
public function validation() | |
{ | |
$this->validate(new Uniqueness(array( | |
"field" => "email", | |
"message" => "The email is already registered" | |
))); | |
return $this->validationHasFailed() != true; | |
} | |
public function initialize() | |
{ | |
$this->belongsTo('profilesId', __NAMESPACE__ . '\Profiles', 'id', array( | |
'alias' => 'profile', | |
'reusable' => true | |
)); | |
$this->hasOne('id', __NAMESPACE__ . '\Avatars', 'usersId', array( | |
'alias' => 'avatar', | |
'reusable' => true | |
)); | |
$this->hasOne('id', __NAMESPACE__ . '\Customers', 'usersId', array( | |
'alias' => 'customers', | |
'reusable' => true | |
)); | |
$this->belongsTo('companyId', __NAMESPACE__ . '\Company', 'id', array( | |
'alias' => 'company', | |
'reusable' => true | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\SuccessLogins', 'usersId', array( | |
'alias' => 'successLogins' | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\RememberTokens', 'usersId', array( | |
'alias' => 'rememberTokens' | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\ResetPasswords', 'usersId', array( | |
'alias' => 'resetPasswords' | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\PasswordChanges', 'usersId', array( | |
'alias' => 'passwordChanges' | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\FailedLogins', 'usersId', array( | |
'alias' => 'failedLogins' | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\ EmailConfirmations', 'usersId', array( | |
'alias' => ' emailConfirmations' | |
)); | |
$this->hasMany('id', __NAMESPACE__ . '\Activity', 'userId', array( | |
'alias' => 'activity' | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment