User.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class User extends CActiveRecord
  3. {
  4. /**
  5. * The followings are the available columns in table 'tbl_user':
  6. * @var integer $id
  7. * @var string $username
  8. * @var string $password
  9. * @var string $email
  10. * @var string $profile
  11. */
  12. /**
  13. * Returns the static model of the specified AR class.
  14. * @return CActiveRecord the static model class
  15. */
  16. public static function model($className=__CLASS__)
  17. {
  18. return parent::model($className);
  19. }
  20. /**
  21. * @return string the associated database table name
  22. */
  23. public function tableName()
  24. {
  25. return '{{user}}';
  26. }
  27. /**
  28. * @return array validation rules for model attributes.
  29. */
  30. public function rules()
  31. {
  32. // NOTE: you should only define rules for those attributes that
  33. // will receive user inputs.
  34. return array(
  35. array('username, password, email', 'required'),
  36. array('username, password, email', 'length', 'max'=>128),
  37. array('profile', 'safe'),
  38. );
  39. }
  40. /**
  41. * @return array relational rules.
  42. */
  43. public function relations()
  44. {
  45. // NOTE: you may need to adjust the relation name and the related
  46. // class name for the relations automatically generated below.
  47. return array(
  48. 'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
  49. );
  50. }
  51. /**
  52. * @return array customized attribute labels (name=>label)
  53. */
  54. public function attributeLabels()
  55. {
  56. return array(
  57. 'id' => 'Id',
  58. 'username' => 'Username',
  59. 'password' => 'Password',
  60. 'email' => 'Email',
  61. 'profile' => 'Profile',
  62. );
  63. }
  64. /**
  65. * Checks if the given password is correct.
  66. * @param string the password to be validated
  67. * @return boolean whether the password is valid
  68. */
  69. public function validatePassword($password)
  70. {
  71. return CPasswordHelper::verifyPassword($password,$this->password);
  72. }
  73. /**
  74. * Generates the password hash.
  75. * @param string password
  76. * @return string hash
  77. */
  78. public function hashPassword($password)
  79. {
  80. return CPasswordHelper::hashPassword($password);
  81. }
  82. }