[php] New user model
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- <?php
- namespace common\models;
- use common\models\query\UserQuery;
- use Yii;
- use yii\base\NotSupportedException;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- use yii\helpers\FileHelper;
- use yii\imagine\Image;
- use yii\web\IdentityInterface;
- /**
- * This is the model class for table "user".
- *
- * @property int $id
- * @property string $username
- * @property string|null $fullname
- * @property string|null $description
- * @property string|null $age
- * @property string|null $website
- * @property string|null $phone
- * @property string|null $city
- * @property string|null $degree
- * @property int|null $is_freelance
- * @property string|null $picture
- * @property string $auth_key
- * @property string $password_hash
- * @property string|null $password_reset_token
- * @property string $email
- * @property int $status
- * @property int $created_at
- * @property int $updated_at
- * @property string|null $verification_token
- */
- class User extends ActiveRecord implements IdentityInterface {
- const STATUS_DELETED = 0;
- const STATUS_INACTIVE = 9;
- const STATUS_ACTIVE = 10;
- const FREELANCE_AVAILABLE = 'Available';
- const FREELANCE_AWAY = 'Away';
- public $profilePicture; //i.e. if we don't want to create a separate DB table field for saving/keeping picture record. Then we can do this by creating a class attribute for keeping the picture record
- /**
- * {@inheritdoc}
- */
- public static function tableName() {
- return 'user';
- }
- /**
- * {@inheritdoc}
- */
- public function behaviors() {
- return [
- TimestampBehavior::className(),
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function rules() {
- return [
- // [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
- [['description'], 'string'],
- // [['age'], 'date', 'format' => 'dd-MM-yyyy'],
- [['status', 'created_at', 'updated_at'], 'integer'],
- [['username', 'password_hash', 'password_reset_token', 'email', 'verification_token'], 'string', 'max' => 255],
- [['fullname', 'facebook', 'twitter', 'instagram', 'skype', 'linkedin' /*, 'picture'*/], 'string', 'max' => 64],
- [['is_freelance', 'website', 'city', 'auth_key', 'phone', 'age', 'degree'], 'string', 'max' => 32],
- ['status', 'default', 'value' => self::STATUS_INACTIVE],
- ['is_freelance', 'default', 'value' => self::FREELANCE_AWAY],
- ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
- ['picture', 'image', /*'extensions' => 'jpg',*/ 'minWidth' => '1280'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels() {
- return [
- 'id' => 'ID',
- 'username' => 'Username',
- 'fullname' => 'Fullname',
- 'description' => 'Description',
- 'age' => 'Age',
- 'website' => 'Website',
- 'phone' => 'Phone',
- 'city' => 'City',
- 'degree' => 'Degree',
- 'is_freelance' => 'Is Freelance',
- 'picture' => 'Picture',
- 'auth_key' => 'Auth Key',
- 'password_hash' => 'Password Hash',
- 'password_reset_token' => 'Password Reset Token',
- 'email' => 'Email',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'verification_token' => 'Verification Token',
- 'facebook' => 'Facebook ID',
- 'twitter' => 'Twitter ID',
- 'instagram' => 'Instagram ID',
- 'skype' => 'Skype ID',
- 'linkedin' => 'LinkedIn ID',
- ];
- }
- public function getStatus() {
- return [
- self::STATUS_ACTIVE => 'Active',
- self::STATUS_INACTIVE => 'Inactive',
- self::STATUS_DELETED => 'Deleted',
- ];
- }
- public function getFreelancestatus() {
- return [
- self::FREELANCE_AVAILABLE => 'Available',
- self::FREELANCE_AWAY => 'Away',
- ];
- }
- /**
- * {@inheritdoc}
- */
- public static function findIdentity($id) {
- return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
- }
- /**
- * {@inheritdoc}
- */
- public static function findIdentityByAccessToken($token, $type = null) {
- throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
- }
- /**
- * Finds user by username
- *
- * @param string $username
- * @return static|null
- */
- public static function findByUsername($username) {
- return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
- }
- /**
- * Finds user by password reset token
- *
- * @param string $token password reset token
- * @return static|null
- */
- public static function findByPasswordResetToken($token) {
- if (!static::isPasswordResetTokenValid($token)) {
- return null;
- }
- return static::findOne([
- 'password_reset_token' => $token,
- 'status' => self::STATUS_ACTIVE,
- ]);
- }
- /**
- * Finds user by verification email token
- *
- * @param string $token verify email token
- * @return static|null
- */
- public static function findByVerificationToken($token) {
- return static::findOne([
- 'verification_token' => $token,
- 'status' => self::STATUS_INACTIVE
- ]);
- }
- /**
- * Finds out if password reset token is valid
- *
- * @param string $token password reset token
- * @return bool
- */
- public static function isPasswordResetTokenValid($token) {
- if (empty($token)) {
- return false;
- }
- $timestamp = (int) substr($token, strrpos($token, '_') + 1);
- $expire = Yii::$app->params['user.passwordResetTokenExpire'];
- return $timestamp + $expire >= time();
- }
- /**
- * {@inheritdoc}
- */
- public function getId() {
- return $this->getPrimaryKey();
- }
- /**
- * {@inheritdoc}
- */
- public function getAuthKey() {
- return $this->auth_key;
- }
- /**
- * {@inheritdoc}
- */
- public function validateAuthKey($authKey) {
- return $this->getAuthKey() === $authKey;
- }
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return bool if password provided is valid for current user
- */
- public function validatePassword($password) {
- return Yii::$app->security->validatePassword($password, $this->password_hash);
- }
- /**
- * Generates password hash from password and sets it to the model
- *
- * @param string $password
- */
- public function setPassword($password) {
- $this->password_hash = Yii::$app->security->generatePasswordHash($password);
- }
- /**
- * Generates "remember me" authentication key
- */
- public function generateAuthKey() {
- $this->auth_key = Yii::$app->security->generateRandomString();
- }
- /**
- * Generates new password reset token
- */
- public function generatePasswordResetToken() {
- $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
- /**
- * Generates new token for email verification
- */
- public function generateEmailVerificationToken() {
- $this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
- /**
- * Removes password reset token
- */
- public function removePasswordResetToken() {
- $this->password_reset_token = null;
- }
- /**
- * {@inheritdoc}
- * @return UserQuery the active query used by this AR class.
- */
- public static function find() {
- return new UserQuery(get_called_class());
- }
- }
Editor
You can edit this paste and save as new: