first commit

This commit is contained in:
2023-05-06 20:40:02 +03:00
commit fdf3e1e602
221 changed files with 12262 additions and 0 deletions

79
common/models/LoginForm.php Executable file
View File

@ -0,0 +1,79 @@
<?php
namespace common\models;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}

47
common/models/News.php Executable file
View File

@ -0,0 +1,47 @@
<?php
namespace common\models;
/**
* This is the model class for table "news".
*
* @property int $id
* @property string|null $title
* @property string|null $text
* @property string|null $slug
*/
class News extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'news';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['text'], 'string'],
[['title', 'slug'], 'string', 'max' => 255],
[['slug'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'text' => 'Text',
'slug' => 'Slug',
];
}
}

95
common/models/Profile.php Executable file
View File

@ -0,0 +1,95 @@
<?php
namespace common\models;
/**
* This is the model class for table "profile".
*
* @property int $id
* @property int|null $user_id
* @property string|null $created_at
* @property string|null $image
* @property int|null $activity
*
* @property Text[] $texts
* @property User $user
*/
class Profile extends \yii\db\ActiveRecord
{
public $file;
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'profile';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id', 'activity'], 'integer'],
[['created_at'], 'safe'],
[['image'], 'string', 'max' => 255],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
[['file'], 'file', 'extensions' => 'jpg, png'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'Пользователь',
'created_at' => 'Когда создан',
'image' => 'Картинка',
'activity' => 'Активность',
];
}
/**
* Gets query for [[Texts]].
*
* @return \yii\db\ActiveQuery
*/
public function getTexts()
{
return $this->hasMany(Text::class, ['profile_id' => 'id']);
}
/**
* Gets query for [[User]].
*
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
public function getStatusLabel()
{
if ($this->activity == self::STATUS_ACTIVE)
return 'Активен';
else
return 'Не активен';
}
/**
* @param string $lang
* @return \yii\db\ActiveQuery
*/
public function getTextByLanguage(string $lang)
{
return $this->hasMany(Text::class, ['profile_id' => 'id', 'language' => $lang]);
}
}

78
common/models/Text.php Executable file
View File

@ -0,0 +1,78 @@
<?php
namespace common\models;
/**
* This is the model class for table "text".
*
* @property int $id
* @property int|null $profile_id
* @property string|null $title
* @property string|null $text
* @property string|null $language
*
* @property Profile $profile
*/
class Text extends \yii\db\ActiveRecord
{
const LANG_RU = 'ru';
const LANG_EN = 'en';
const LANG_FR = 'fr';
const LANG_ES = 'es';
const LANG_IT = 'it';
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'text';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['profile_id'], 'integer'],
[['text'], 'string'],
[['title', 'language'], 'string', 'max' => 255],
[['profile_id'], 'exist', 'skipOnError' => true, 'targetClass' => Profile::class, 'targetAttribute' => ['profile_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'profile_id' => 'Profile ID',
'title' => 'Title',
'text' => 'Text',
'language' => 'Language',
];
}
public function extraFields()
{
return ['text[]', 'title[]'];
}
/**
* Gets query for [[Profile]].
*
* @return \yii\db\ActiveQuery
*/
public function getProfile()
{
return $this->hasOne(Profile::class, ['id' => 'profile_id']);
}
public static function getLanguages()
{
return [self::LANG_RU, self::LANG_IT, self::LANG_FR, self::LANG_EN, self::LANG_ES];
}
}

213
common/models/User.php Executable file
View File

@ -0,0 +1,213 @@
<?php
namespace common\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/**
* User model
*
* @property integer $id
* @property string $username
* @property string $password_hash
* @property string $password_reset_token
* @property string $verification_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
*/
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_INACTIVE = 9;
const STATUS_ACTIVE = 10;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
TimestampBehavior::class,
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_INACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, 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 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;
}
}