add user CRUD

This commit is contained in:
2015-09-19 08:29:47 +02:00
parent 271835ae6b
commit 5163f1a9a9
15 changed files with 653 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace backend\models;
use Yii;
use common\models\User;
class UserCreate extends User{
public $password_plain;
public $password_repeat;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username','email','password_plain','password_repeat'], 'required' ],
['email' ,'email' ],
['email' ,'unique' ],
['username' ,'unique' ],
[['password_plain' ,'password_repeat'] ,'string','min' =>6 ],
[['password_repeat'] ,'validatePasswordRepeat' ]
];
}
public function validatePasswordRepeat($attribute,$params){
if ( !$this->hasErrors()){
if ( $this->password_plain != $this->password_repeat ){
$this->addError($attribute, Yii::t('app', 'Jelszó és jelszó újra nem egyezik!') );
}
}
}
public function attributeLabels(){
return [
'email' =>'E-mail',
'username' =>'Felhasználónév',
'created_at' =>'Létrehozás dátuma',
'password_plain' => Yii::t('app','Jelszó'),
'password_repeat' => Yii::t('app','Jelszó újra'),
];
}
public function beforeSave($insert){
if ( parent::beforeSave($insert)){
if ( $insert ){
$this->setPassword($this->password_plain);
$this->generateAuthKey();
return true;
}
}else{
return false;
}
}
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
// $am = Yii::$app->authManager;
// $role = $am->getRole('admin');
// Yii::$app->authManager->assign($role, $this->id);
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\User;
/**
* UserSearch represents the model behind the search form about `common\models\User`.
*/
class UserSearch extends User
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'status', 'created_at', 'updated_at'], 'integer'],
[['username', 'auth_key', 'password_hash', 'password_reset_token', 'email'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'username', $this->username])
->andFilterWhere(['like', 'auth_key', $this->auth_key])
->andFilterWhere(['like', 'password_hash', $this->password_hash])
->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
->andFilterWhere(['like', 'email', $this->email]);
return $dataProvider;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace backend\models;
use Yii;
use common\models\User;
class UserUpdate extends User {
public $password_plain;
public $password_repeat;
/**
* @inheritdoc
* @formatter:off
*/
public function rules()
{
return [
[['username','email'], 'required' ],
['email' ,'email' ],
['email' ,'unique' , 'targetClass' => User::className(), 'targetAttribute' => 'email'],
['username' ,'unique', 'targetClass' => User::className(), 'targetAttribute' => 'username'],
[['password_plain' ,'password_repeat'] ,'string','min' =>6 ],
[['password_repeat'] ,'validatePasswordRepeat' ]
];
}
/**
* @formatter:on
*/
public function validatePasswordRepeat($attribute, $params) {
if (! $this->hasErrors ()) {
if ( !empty($this->password_plain) || !empty($this->password_repeat) ){
if ($this->password_plain != $this->password_repeat) {
$this->addError ( $attribute, Yii::t ( 'app', 'Jelszó és jelszó újra nem egyezik!' ) );
}
}
}
}
public function attributeLabels() {
return [
'email' => 'E-mail',
'username' => 'Felhasználónév',
'created_at' => 'Létrehozás dátuma',
'password_plain' => Yii::t ( 'app', 'Jelszó' ),
'password_repeat' => Yii::t ( 'app', 'Jelszó újra' )
]
;
}
public function beforeSave($insert) {
if (parent::beforeSave ( $insert )) {
if (! $insert) {
if ( !empty( $this->password_plain ) ) {
$this->setPassword($this->password_plain);
return true;
}
}
return true;
} else {
return false;
}
}
public function afterSave($insert, $changedAttributes) {
parent::afterSave ( $insert, $changedAttributes );
// $am = Yii::$app->authManager;
// $role = $am->getRole('admin');
// Yii::$app->authManager->assign($role, $this->id);
}
}