New fields for User

This commit is contained in:
Alex Solomaha
2017-03-16 22:34:33 +02:00
parent a47a3bb8f4
commit 222ff4674b
6 changed files with 79 additions and 10 deletions

View File

@@ -0,0 +1,26 @@
<?php
use yii\db\Migration;
class m170316_163700_add_user_fields extends Migration
{
public function safeUp()
{
$this->addColumn('user', 'name', $this->string()->after('email'));
$this->addColumn('user', 'avatar', $this->string()->after('name'));
$this->addColumn('user', 'room_id', $this->integer()->after('email'));
$this->createIndex('idx-user-room_id', 'user', 'room_id');
$this->addForeignKey('fk-user-room_id', 'user', 'room_id', 'room', 'id');
}
public function safeDown()
{
$this->dropForeignKey('fk-user-room_id', 'user');
$this->dropIndex('idx-user-room_id', 'user');
$this->dropColumn('user', 'name');
$this->dropColumn('user', 'avatar');
$this->dropColumn('user', 'room_id');
}
}

View File

@@ -18,6 +18,9 @@ use yii\web\IdentityInterface;
* @property string $auth_key
* @property string $auth_token
* @property string $api_key
* @property string $name
* @property string $avatar
* @property integer $room_id
* @property integer $status
* @property integer $group
* @property integer $created_at
@@ -26,13 +29,15 @@ use yii\web\IdentityInterface;
* @property boolean $isAdmin
*
* @property string $password write-only password
*
* @property Room $room
*/
class User extends ActiveRecord implements IdentityInterface
{
const SCENARIO_CREATE = 'create';
const SCENARIO_UPDATE = 'update';
const STATUS_DELETED = 0;
const STATUS_DISABLED = 0;
const STATUS_ACTIVE = 10;
const GROUP_ADMIN = 10;
@@ -65,14 +70,15 @@ class User extends ActiveRecord implements IdentityInterface
{
return [
[['username', 'email'], 'required'],
[['api_key', 'auth_token'], 'string'],
[['group'], 'integer'],
[['api_key', 'auth_token', 'name', 'avatar'], 'string'],
[['group', 'room_id'], 'integer'],
[['password'], 'required', 'on' => self::SCENARIO_CREATE],
[['password'], 'safe', 'on' => self::SCENARIO_UPDATE],
[['auth_token'], 'default', 'value' => null],
[['status'], 'default', 'value' => self::STATUS_ACTIVE],
[['status'], 'in', 'range' => self::getStatusesArray()],
[['group'], 'in', 'range' => self::getGroupsArray()],
[['room_id'], 'exist', 'skipOnError' => true, 'targetClass' => Room::className(), 'targetAttribute' => ['room_id' => 'id']],
];
}
@@ -88,6 +94,9 @@ class User extends ActiveRecord implements IdentityInterface
'email' => 'Email',
'status' => 'Статус',
'group' => 'Группа',
'name' => 'Имя',
'avatar' => 'Аватар',
'room_id' => 'Комната',
'api_key' => 'API ключ',
'auth_token' => 'Auth токен',
'created_at' => 'Дата создания',
@@ -102,7 +111,7 @@ class User extends ActiveRecord implements IdentityInterface
{
return [
self::STATUS_ACTIVE => 'Активен',
self::STATUS_DELETED => 'Удален',
self::STATUS_DISABLED => 'Не активен',
];
}
@@ -277,6 +286,14 @@ class User extends ActiveRecord implements IdentityInterface
return $this->hasMany(History::className(), ['user_id' => 'id'])->inverseOf('user');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRoom()
{
return $this->hasOne(Room::className(), ['id' => 'room_id']);
}
/**
* @return bool
*/

View File

@@ -4,7 +4,7 @@ namespace app\modules\admin\controllers;
use Yii;
use app\models\User;
use app\models\UserSearch;
use app\modules\admin\models\UserSearch;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

View File

@@ -1,6 +1,6 @@
<?php
namespace app\models;
namespace app\modules\admin\models;
use Yii;
use yii\base\Model;
@@ -18,8 +18,8 @@ class UserSearch extends User
public function rules()
{
return [
[['id', 'status', 'group', 'created_at', 'updated_at'], 'integer'],
[['username', 'email'], 'safe'],
[['id', 'status', 'group', 'created_at', 'updated_at', 'room_id'], 'integer'],
[['username', 'email', 'name'], 'safe'],
];
}
@@ -64,9 +64,12 @@ class UserSearch extends User
'group' => $this->group,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'room_id' => $this->room_id,
]);
$query->andFilterWhere(['like', 'username', $this->username])
$query
->andFilterWhere(['like', 'username', $this->username])
->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'email', $this->email]);
return $dataProvider;

View File

@@ -4,7 +4,9 @@
/* @var $model app\models\User */
/* @var $form yii\widgets\ActiveForm */
use app\models\Room;
use app\models\User;
use kartik\widgets\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@@ -26,10 +28,21 @@ if ($model->isNewRecord) {
<?= $form->field($model, 'email')->input('email') ?>
<?= $form->field($model, 'name')->textInput() ?>
<?= $form->field($model, 'avatar')->textInput() ?>
<?= $form->field($model, 'status')->dropDownList(User::getStatuses()) ?>
<?= $form->field($model, 'group')->dropDownList(User::getGroups()) ?>
<?= $form->field($model, 'room_id')->widget(Select2::className(), [
'data' => Room::getList(),
'options' => [
'placeholder' => 'Выберите комнату ...',
],
]) ?>
<?= $form->field($model, 'api_key')->textInput()->hint($randomApiKey) ?>
<div class="form-group">

View File

@@ -1,9 +1,10 @@
<?php
/* @var $this yii\web\View */
/* @var $searchModel app\models\UserSearch */
/* @var $searchModel \app\modules\admin\models\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
use app\models\Room;
use app\models\User;
use yii\helpers\Html;
use yii\widgets\Pjax;
@@ -26,6 +27,15 @@ $this->params['in-card'] = false;
'id',
'username',
'email',
'name',
[
'attribute' => 'room_id',
'filter' => Room::getList(),
'value' => function ($model) {
/** @var $model User */
return $model->room->name;
},
],
[
'filter' => User::getStatuses(),
'attribute' => 'status',