Jalle19 /
xbmc-video-server
| 1 | <?php |
||
| 2 | |||
| 3 | use Hautelook\Phpass\PasswordHash; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * This is the model class for table "user". |
||
| 7 | * |
||
| 8 | * The followings are the available columns in table 'user': |
||
| 9 | * @property integer $id |
||
| 10 | * @property string $role |
||
| 11 | * @property string $username |
||
| 12 | * @property string $password |
||
| 13 | * @property string $language |
||
| 14 | * @property string $start_page |
||
| 15 | * |
||
| 16 | * @author Sam Stenvall <[email protected]> |
||
| 17 | * @copyright Copyright © Sam Stenvall 2013- |
||
| 18 | * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0 |
||
| 19 | */ |
||
| 20 | class User extends CActiveRecord |
||
| 21 | { |
||
| 22 | |||
| 23 | const ROLE_ADMIN = 'admin'; |
||
| 24 | const ROLE_USER = 'user'; |
||
| 25 | const ROLE_SPECTATOR = 'spectator'; |
||
| 26 | const ROLE_NONE = ''; |
||
| 27 | |||
| 28 | const START_PAGE_MOVIES_BROWSE = 'movie/index'; |
||
| 29 | const START_PAGE_MOVIES_RECENTLY_ADDED = 'movie/recentlyAdded'; |
||
| 30 | const START_PAGE_TVSHOWS_BROWSE = 'tvShow/index'; |
||
| 31 | const START_PAGE_TVSHOWS_RECENTLY_ADDED = 'tvShow/recentlyAdded'; |
||
| 32 | |||
| 33 | const DEFAULT_START_PAGE = self::START_PAGE_MOVIES_BROWSE; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The base-2 logarithm of the iteration count used for password stretching |
||
| 37 | */ |
||
| 38 | const PHPASS_ITERATIONS = 8; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var boolean whether to hash the password before saving the model. |
||
| 42 | * Defaults to true. |
||
| 43 | */ |
||
| 44 | private $_hashPasswordBeforeSave = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Returns the static model of the specified AR class. |
||
| 48 | * @param string $className active record class name. |
||
| 49 | * @return User the static model class |
||
| 50 | */ |
||
| 51 | public static function model($className = __CLASS__) |
||
| 52 | { |
||
| 53 | return parent::model($className); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string the associated database table name |
||
| 58 | */ |
||
| 59 | public function tableName() |
||
| 60 | { |
||
| 61 | return 'user'; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return array validation rules for model attributes. |
||
| 66 | */ |
||
| 67 | public function rules() |
||
| 68 | { |
||
| 69 | return array( |
||
| 70 | array('role, username, language', 'required'), |
||
| 71 | // password is only required when creating new users |
||
| 72 | array('password', 'required', 'on'=>'insert'), |
||
| 73 | array('username', 'unique'), |
||
| 74 | // recommended by phpass |
||
| 75 | array('password', 'length', 'max'=>72), |
||
| 76 | array('role', 'in', 'range'=>array_keys($this->getRoles())), |
||
| 77 | array('language', 'in', 'range'=>array_keys(LanguageManager::getAvailableLanguages())), |
||
| 78 | array('role', 'validateRole', 'on'=>'update'), |
||
| 79 | array('start_page', 'in', 'range'=>array_keys($this->getStartPages())), |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Checks that there is at least one administrator configured |
||
| 85 | * @param string $attribute |
||
| 86 | */ |
||
| 87 | public function validateRole($attribute) |
||
| 88 | { |
||
| 89 | $role = $this->{$attribute}; |
||
| 90 | |||
| 91 | if ($role != self::ROLE_ADMIN) |
||
| 92 | { |
||
| 93 | $administrators = $this->findAll('role = :role AND id != :id', array( |
||
| 94 | ':role'=>self::ROLE_ADMIN, |
||
| 95 | ':id'=>$this->id)); |
||
| 96 | |||
| 97 | if (count($administrators) === 0) |
||
| 98 | $this->addError($attribute, Yii::t('User', 'There must be at least one administrator')); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return array customized attribute labels (name=>label) |
||
| 104 | */ |
||
| 105 | public function attributeLabels() |
||
| 106 | { |
||
| 107 | return array( |
||
| 108 | 'role'=>Yii::t('User', 'Role'), |
||
| 109 | 'username'=>Yii::t('User', 'Username'), |
||
| 110 | 'password'=>Yii::t('User', 'Password'), |
||
| 111 | 'start_page'=>Yii::t('User', 'Start page'), |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Hashes the password before saving the model, unless hashing has been |
||
| 117 | * inhibited |
||
| 118 | */ |
||
| 119 | protected function beforeSave() |
||
| 120 | { |
||
| 121 | if ($this->_hashPasswordBeforeSave) |
||
| 122 | $this->password = $this->hashPassword($this->password); |
||
| 123 | |||
| 124 | return parent::beforeSave(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Inhibits the automatic hashing of the password on save |
||
| 129 | */ |
||
| 130 | public function inhibitPasswordHash() |
||
| 131 | { |
||
| 132 | $this->_hashPasswordBeforeSave = false; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return User the model for the currently logged in user |
||
| 137 | */ |
||
| 138 | public function findCurrent() |
||
| 139 | { |
||
| 140 | return $this->findByPk(Yii::app()->user->id); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the possible roles |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function getRoles() |
||
| 148 | { |
||
| 149 | return array( |
||
| 150 | self::ROLE_ADMIN=>Yii::t('UserRole', 'Administrator'), |
||
| 151 | self::ROLE_USER=>Yii::t('UserRole', 'User'), |
||
| 152 | self::ROLE_SPECTATOR=>Yii::t('UserRole', 'Spectator'), |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the name of the user's role |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getRoleName() |
||
| 161 | { |
||
| 162 | $roles = $this->getRoles(); |
||
| 163 | |||
| 164 | return $roles[$this->role]; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array the available start pages and their descriptions |
||
| 169 | */ |
||
| 170 | public function getStartPages() |
||
| 171 | { |
||
| 172 | return array( |
||
| 173 | self::START_PAGE_MOVIES_BROWSE => Yii::t('StartPage', 'Movies - Browse'), |
||
| 174 | self::START_PAGE_MOVIES_RECENTLY_ADDED => Yii::t('StartPage', 'Movies - Recently added'), |
||
| 175 | self::START_PAGE_TVSHOWS_BROWSE => Yii::t('StartPage', 'TV shows - Browse'), |
||
| 176 | self::START_PAGE_TVSHOWS_RECENTLY_ADDED => Yii::t('StartPage', 'TV shows - Recently added'), |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return array the route to the currently set start page, or the default one if |
||
| 182 | * none has been set |
||
| 183 | */ |
||
| 184 | public function getStartPage() |
||
| 185 | { |
||
| 186 | $startPages = $this->getStartPages(); |
||
| 187 | $startPage = $this->start_page ? $this->start_page : self::DEFAULT_START_PAGE; |
||
| 188 | |||
| 189 | return $startPages[$startPage]; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @return array the route to the start page |
||
| 195 | */ |
||
| 196 | public function getStartPageRoute() |
||
| 197 | { |
||
| 198 | return $this->start_page ? [$this->start_page] : [self::DEFAULT_START_PAGE]; |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns a data provider for this model |
||
| 204 | * @return \CActiveDataProvider |
||
| 205 | */ |
||
| 206 | public function getDataProvider() |
||
| 207 | { |
||
| 208 | return new CActiveDataProvider(__CLASS__, array( |
||
| 209 | 'pagination'=>false |
||
| 210 | )); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns the hash for the specified password |
||
| 215 | * @param string $password |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public static function hashPassword($password) |
||
| 219 | { |
||
| 220 | return self::getPasswordHasher()->hashPassword($password); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Checks whether the given password matches the given hash |
||
| 225 | * @param string $password |
||
| 226 | * @param string $hash |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | public static function checkPassword($password, $hash) |
||
| 230 | { |
||
| 231 | return self::getPasswordHasher()->checkPassword($password, $hash); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the password hasher |
||
| 236 | * @return \Hautelook\Phpass\PasswordHash |
||
| 237 | */ |
||
| 238 | private static function getPasswordHasher() |
||
| 239 | { |
||
| 240 | return new PasswordHash(self::PHPASS_ITERATIONS, false); |
||
| 241 | } |
||
| 242 | |||
| 243 | } |
||
| 244 | |||
| 245 |