| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
class User extends ActiveRecord |
|---|
| 37 |
{ |
|---|
| 38 |
var $belongs_to = array( |
|---|
| 39 |
'creator' => array('class_name' => 'User', 'foreign_key' => 'created_by') |
|---|
| 40 |
); |
|---|
| 41 |
|
|---|
| 42 |
var $habtm = array('roles' => array('unique'=>true)); |
|---|
| 43 |
|
|---|
| 44 |
var $has_many = 'system_messages'; |
|---|
| 45 |
|
|---|
| 46 |
var $salt = 'A thing of beauty is a joy forever'; |
|---|
| 47 |
|
|---|
| 48 |
function validate() |
|---|
| 49 |
{ |
|---|
| 50 |
$this->validatesUniquenessOf('login', array('message'=>$this->t('login %login already in use', array('%login'=>$this->get('login'))))); |
|---|
| 51 |
$this->needsPasswordConfirmation() ? $this->validatesConfirmationOf('password', $this->t('Must match confirmation')) : null; |
|---|
| 52 |
|
|---|
| 53 |
$this->validatesPresenceOf(array('login','name')); |
|---|
| 54 |
$this->isNewRecord() ? $this->validatesPresenceOf(array('password','password_confirmation')) : null; |
|---|
| 55 |
$this->needsEmailValidation() ? $this->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION, $this->t('Invalid email address')) : null; |
|---|
| 56 |
$this->validatesLengthOf('login', array('in'=>array(3, 40), 'too_long' => $this->t('pick a shorter login'), 'too_short' => $this->t('pick a longer name'))); |
|---|
| 57 |
$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; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
function needsPasswordLengthValidation() |
|---|
| 61 |
{ |
|---|
| 62 |
return $this->isNewRecord() || !empty($this->password); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
function needsPasswordConfirmation() |
|---|
| 66 |
{ |
|---|
| 67 |
return $this->isNewRecord(); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
function needsEmailValidation() |
|---|
| 71 |
{ |
|---|
| 72 |
return empty($this->_byspass_email_validation); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
function beforeCreate() |
|---|
| 76 |
{ |
|---|
| 77 |
$this->encryptPassword(); |
|---|
| 78 |
return true; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
function beforeUpdate() |
|---|
| 82 |
{ |
|---|
| 83 |
$this->_encryptPasswordUnlessEmptyOrUnchanged(); |
|---|
| 84 |
return true; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
function encryptPassword() |
|---|
| 88 |
{ |
|---|
| 89 |
$this->set('password', $this->sha1($this->get('password'))); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
function sha1($phrase) |
|---|
| 93 |
{ |
|---|
| 94 |
return sha1('--'.$this->salt.'--'.$phrase.'--'); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
function _encryptPasswordUnlessEmptyOrUnchanged() |
|---|
| 98 |
{ |
|---|
| 99 |
$User =& $this->find($this->id); |
|---|
| 100 |
switch ($this->get('password')) { |
|---|
| 101 |
case '': |
|---|
| 102 |
$this->password = $User->password; |
|---|
| 103 |
break; |
|---|
| 104 |
case $User->password: |
|---|
| 105 |
break; |
|---|
| 106 |
default: |
|---|
| 107 |
$this->encryptPassword(); |
|---|
| 108 |
break; |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
?> |
|---|
| 115 |
|
|---|