[right]لقد أنشأت الجدول التالي[/right]
CREATE TABLE `sitetb_users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`auth_key` varchar(100) NOT NULL DEFAULT '',
`access_token` varchar(100) NOT NULL DEFAULT '',
`email` varchar(25) NOT NULL,
`phone_number` varchar(20) DEFAULT NULL,
`user_type` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
)
ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
[right]ثم قمت بتنفيذ الكود التالي[/right]
<?php
$id=1;
$username='khaled';
$password='retyppp';
$auth_key=md5($id);
$access_token=sha1($username.':'.$password);
$email='khaled@ket.com';
$phone_number='20235634253';
$user_type='owner';
mysql_query(
"insert into sitetb_users values(
$id,
'$username',
'$password',
'$auth_key',
'$access_token',
'$email',
'$phone_number',
'$user_type'
)"
);
$id=2;
$username='muhammad';
$password='poestmp';
$auth_key=md5($id);
$access_token=sha1($username.':'.$password);
$email='muhammad@ket.com';
$phone_number='20235634253';
$user_type='forman';
mysql_query(
"insert into sitetb_users values(
$id,
'$username',
'$password',
'$auth_key',
'$access_token',
'$email',
'$phone_number',
'$user_type'
)"
);
[right]في ملف الإعدادات فمت بالتعديل التالي[/right]
'user' => [
'identityClass' => 'app\models\SitetbUsers',
'enableAutoLogin' => false,
],
[right]ثم أنشات الموديل التالي[/right]
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
class SitetbUsers extends \yii\db\ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return '{{%users}}';
}
public function rules()
{
return [
[['username', 'password', 'email'], 'required'],
[['username', 'password', 'phone_number'], 'string', 'max' => 20],
[['auth_key', 'access_token'], 'string', 'max' => 100],
[['email'], 'string', 'max' => 25],
[['user_type'], 'string', 'max' => 10],
[['username'], 'unique'],
[['email'], 'unique']
];
}
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'username' => Yii::t('app', 'Username'),
'password' => Yii::t('app', 'Password'),
'auth_key' => Yii::t('app', 'Auth Key'),
'access_token' => Yii::t('app', 'Access Token'),
'email' => Yii::t('app', 'Email'),
'phone_number' => Yii::t('app', 'Phone Number'),
'user_type' => Yii::t('app', 'User Type'),
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public static function findIdentityByAccessToken($token)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
public function getId()
{
return $this->getPrimaryKey();
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
public function validatePassword($password)
{
return $this->password === $password;
}
}
[right]موديل اللوجن فورم عندي كما يلي[/right]
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
public function rules()
{
return [
[['username', 'password'], 'required'],
['rememberMe', 'boolean'],
['password', 'validatePassword'],
];
}
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
} else {
return false;
}
}
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
[right]أكشن تسجيل الدخول والخروج عندي كما يلي[/right]
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
[right]المشكلة هي أنه عند تسجيل الدخول تظهر لي الرسالة التالية
"Incorrect username or password."[/right]