From 8195fc24005c73b97f21ea1eaa89d50a6ab865d8 Mon Sep 17 00:00:00 2001 From: yarik Date: Mon, 26 Dec 2016 18:38:37 +0200 Subject: [PATCH] Models --- common/models/User.php | 410 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- frontend/controllers/CabinetController.php | 19 ++++++++++++++++++- frontend/models/CreativeRole.php | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/IntellectualProperty.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/Report.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/UserData.php | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/UserPassport.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 597 insertions(+), 186 deletions(-) create mode 100644 frontend/models/CreativeRole.php create mode 100644 frontend/models/IntellectualProperty.php create mode 100644 frontend/models/Report.php create mode 100644 frontend/models/UserData.php create mode 100644 frontend/models/UserPassport.php diff --git a/common/models/User.php b/common/models/User.php index 2f4508f..2aacddf 100644 --- a/common/models/User.php +++ b/common/models/User.php @@ -1,189 +1,229 @@ self::STATUS_ACTIVE], - ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], - ]; - } - - /** - * @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 + namespace common\models; + + use frontend\models\UserData; + use frontend\models\UserPassport; + use Yii; + use yii\base\NotSupportedException; + use yii\behaviors\TimestampBehavior; + use yii\db\ActiveRecord; + use yii\web\IdentityInterface; + + /** + * User model * - * @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 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; + * @property integer $id + * @property string $username + * @property string $password_hash + * @property string $password_reset_token + * @property string $email + * @property string $auth_key + * @property integer $status + * @property integer $created_at + * @property integer $updated_at + * @property string $password write-only password + * @property UserData $userData + * @property UserPassport $userPassport + */ + class User extends ActiveRecord implements IdentityInterface + { + const STATUS_DELETED = 0; + const STATUS_ACTIVE = 10; + + /** + * @inheritdoc + */ + public static function tableName() + { + return '{{%user}}'; + } + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + TimestampBehavior::className(), + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + 'status', + 'default', + 'value' => self::STATUS_ACTIVE, + ], + [ + 'status', + 'in', + 'range' => [ + self::STATUS_ACTIVE, + self::STATUS_DELETED, + ], + ], + ]; + } + + /** + * @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 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(); + } + + /** + * Removes password reset token + */ + public function removePasswordResetToken() + { + $this->password_reset_token = null; + } + + public function getUserData() + { + return $this->hasOne(UserData::className(), [ 'user_id' => 'id' ]); + } + + public function getUserPassport() + { + return $this->hasOne(UserPassport::className(), [ 'user_id' => 'id' ]); } - - $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(); - } - - /** - * Removes password reset token - */ - public function removePasswordResetToken() - { - $this->password_reset_token = null; } -} diff --git a/frontend/controllers/CabinetController.php b/frontend/controllers/CabinetController.php index 2b543b5..dbe1611 100644 --- a/frontend/controllers/CabinetController.php +++ b/frontend/controllers/CabinetController.php @@ -1,6 +1,9 @@ render('index'); + \Yii::$app->user->login(User::findOne(1)); + /** + * @var User $user + */ + $user = \Yii::$app->user->identity; + if(!$userData = $user->userData) { + $userData = new UserData(); + } + if(!$userPassport = $user->userPassport) { + $userPassport = new UserPassport(); + } + return $this->render('index', [ + 'userData' => $userData, + 'userPassport' => $userPassport, + ]); } } diff --git a/frontend/models/CreativeRole.php b/frontend/models/CreativeRole.php new file mode 100644 index 0000000..31eee76 --- /dev/null +++ b/frontend/models/CreativeRole.php @@ -0,0 +1,66 @@ + 255], + [['intellectual_property_id'], 'exist', 'skipOnError' => true, 'targetClass' => IntellectualProperty::className(), 'targetAttribute' => ['intellectual_property_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'intellectual_property_id' => 'Intellectual Property ID', + 'title' => 'Title', + 'name' => 'Name', + 'part' => 'Part', + 'code' => 'Code', + 'iri' => 'Iri', + 'society' => 'Society', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getIntellectualProperty() + { + return $this->hasOne(IntellectualProperty::className(), ['id' => 'intellectual_property_id']); + } +} diff --git a/frontend/models/IntellectualProperty.php b/frontend/models/IntellectualProperty.php new file mode 100644 index 0000000..381b6cc --- /dev/null +++ b/frontend/models/IntellectualProperty.php @@ -0,0 +1,87 @@ + 255], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'user_id' => 'User ID', + 'title' => 'Title', + 'creation_date' => 'Creation Date', + 'code' => 'Code', + 'genre' => 'Genre', + 'registration_date' => 'Registration Date', + 'contract' => 'Contract', + 'type' => 'Type', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCreativeRoles() + { + return $this->hasMany(CreativeRole::className(), ['intellectual_property_id' => 'id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), ['id' => 'user_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getReports() + { + return $this->hasMany(Report::className(), ['intellectual_property_id' => 'id']); + } +} diff --git a/frontend/models/Report.php b/frontend/models/Report.php new file mode 100644 index 0000000..49567d6 --- /dev/null +++ b/frontend/models/Report.php @@ -0,0 +1,70 @@ + 255], + [['intellectual_property_id'], 'exist', 'skipOnError' => true, 'targetClass' => IntellectualProperty::className(), 'targetAttribute' => ['intellectual_property_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'intellectual_property_id' => 'Intellectual Property ID', + 'title' => 'Title', + 'artist' => 'Artist', + 'music_author' => 'Music Author', + 'text_author' => 'Text Author', + 'time' => 'Time', + 'total_time' => 'Total Time', + 'royalty' => 'Royalty', + 'count' => 'Count', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getIntellectualProperty() + { + return $this->hasOne(IntellectualProperty::className(), ['id' => 'intellectual_property_id']); + } +} diff --git a/frontend/models/UserData.php b/frontend/models/UserData.php new file mode 100644 index 0000000..5a1abae --- /dev/null +++ b/frontend/models/UserData.php @@ -0,0 +1,67 @@ + 255], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'user_id' => 'User ID', + 'name' => 'Name', + 'surname' => 'Surname', + 'patronymic' => 'Patronymic', + 'phone' => 'Phone', + 'email' => 'Email', + 'inn' => 'Inn', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), ['id' => 'user_id']); + } +} diff --git a/frontend/models/UserPassport.php b/frontend/models/UserPassport.php new file mode 100644 index 0000000..f1692cd --- /dev/null +++ b/frontend/models/UserPassport.php @@ -0,0 +1,64 @@ + 255], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'user_id' => 'User ID', + 'series' => 'Series', + 'number' => 'Number', + 'birthday' => 'Birthday', + 'given_by' => 'Given By', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), ['id' => 'user_id']); + } +} -- libgit2 0.21.4