array('class_name' => 'User', 'foreign_key' => 'created_by') ); var $habtm = array('roles' => array('unique'=>true)); var $has_many = 'system_messages'; var $salt = 'A thing of beauty is a joy forever'; function validate() { $this->validatesUniquenessOf('login', array('message'=>$this->t('login %login already in use', array('%login'=>$this->get('login'))))); $this->needsPasswordConfirmation() ? $this->validatesConfirmationOf('password', $this->t('Must match confirmation')) : null; $this->validatesPresenceOf(array('login','name')); $this->isNewRecord() ? $this->validatesPresenceOf(array('password','password_confirmation')) : null; $this->needsEmailValidation() ? $this->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION, $this->t('Invalid email address')) : null; $this->validatesLengthOf('login', array('in'=>array(3, 40), 'too_long' => $this->t('pick a shorter login'), 'too_short' => $this->t('pick a longer name'))); $this->needsPasswordLengthValidation() ? $this->validatesLengthOf('password', array('in'=>array(4, 40), 'too_long' => $this->t('pick a shorter password'), 'too_short' => $this->t('pick a longer password'))) : null; } function needsPasswordLengthValidation() { return $this->isNewRecord() || !empty($this->password); } function needsPasswordConfirmation() { return $this->isNewRecord(); } function needsEmailValidation() { return empty($this->_byspass_email_validation); } function beforeCreate() { $this->encryptPassword(); return true; } function beforeUpdate() { $this->_encryptPasswordUnlessEmptyOrUnchanged(); return true; } function encryptPassword() { $this->set('password', $this->sha1($this->get('password'))); } function sha1($phrase) { return sha1('--'.$this->salt.'--'.$phrase.'--'); } function _encryptPasswordUnlessEmptyOrUnchanged() { $User =& $this->find($this->id); switch ($this->get('password')) { case '': $this->password = $User->password; break; case $User->password: break; default: $this->encryptPassword(); break; } } } ?>