P
P
Petr Ponomarev2016-11-23 13:29:07
Yii
Petr Ponomarev, 2016-11-23 13:29:07

I use Yii2 and sha1, in the database the password is written as NULL How to fix it?

<?
namespace app\models;
use Yii;
use yii\base\Model;
class RegForm extends Model
{
public $login;
public $email;
public $password;
public $password_repeat;
public $wmr;
public $skype;
public $status;
public $created_at;
public $updated_at;
public function rules(){
return[
, ]; } public function attributeLabels() { return[ 'login' => 'Enter Login',
'password' => 'Password',
'password_repeat' => 'Repeat password',
'email' => 'Enter Email',
'wmr' => 'Enter WMR Wallet',
'skype' => 'Enter your skype' ,
];
}
public function reg()
{
$user = new User;
$user->login = $this->login;
$user->email = $this->email;
$user->wmr = $this->wmr;
$user->skype = $this->skype;
$user->status = $this->status;
$user->setPassword($this->password);
$user->generateAuthKey();
return $user->save();
}
}
User.
php <?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\behaviors\TimestampBehavior;
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_NOT_ACTIVE = 1;
const STATUS_ACTIVE = 10;
public static function tableName()
{
return 'user';
}
public function behaviors()
{
return
[
TimestampBehavior::className()
];
}
public static function findByUsername($login)
{
return static::findOne([
'login' => $login
]);
}
public function setPassword($password)
{
$this->password = Yii::$app->getSecurity()->generatePasswordHash($password);
}
public function generateAuthKey(){
$this->auth_key = Yii::$app->security->generateRandomString();
}
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
/*User authentication*/
public static function findIdentity($id)
{
return static::findOne([
'id' => $id,
'status' => self::
);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
}
SiteController
public function actionReg()
{
$model = new RegForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()):
if ($model->reg()):
return $this->goHome( );
endif;
endif;
return $this->render(
'reg',
['model' => $model]
);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-11-23
@Petrdis

sha1 is no longer secure for password encryption. Check it out

P
Petr Ponomarev, 2016-11-23
@Petrdis

Everything worked
$user->setPassword($this->password);
public function setPassword($password)
{
$this->password = Yii::$app->getSecurity()->generatePasswordHash($password);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question