namespace app\controllers;
//use yii\rest\ActiveController;
use yii\web\Response;
use Yii;
use app\models\Users;
use app\models\UsersSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
class UsersController extends Controller
{
//public $modelClass = 'app\models\Users';
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionIndex()
{
\Yii::$app->response->format = Response::FORMAT_JSON;
$searchModel = new UsersSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Users model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
\Yii::$app->response->format = Response::FORMAT_JSON;
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
}
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
UPDATE users SET token = '' WHERE token = :token;
UPDATE users SET token = :token, device = :device WHERE phone = :phone
if($act == 'auth'){
$phone = isset($_POST['phone']) ? ($_POST['phone']) : '';
$password = isset($_POST['password']) ? ($_POST['password']) : '';
$token = isset($_POST['token']) ? ($_POST['token']) : '';
$device = isset($_POST['device']) ? ($_POST['device']) : '';
if($device == 'android'){
$device = 0;
}
else if($device == 'ios'){
$device = 1;
}
else {
$device = 0;
}
echo $device;
if ($phone == '') {
$errors[] = 'Введите номер';
}
if( !$set->validatePhone($phone) ){
$errors[] = 'Не верный формат номер телефон';
}
if ($password == '') {
$errors[] = 'Введите пароль';
}
if (empty($errors)) {
$success = $set->auth($phone,$password);
if($success) {
$set->change_token($phone,$token,$device);
}
}
echo $set->result($object, $success, $errors);
}
public function auth($phone,$password){
if($phone == '' || $password ==''){
return false;
}
$client = new DB();
if($phone){
$q = $client->pdo->prepare("SELECT id,count(*) FROM users WHERE phone = :phone");
$res = $q->execute(array('phone' => $phone));
}
if($password){
$q = $client->pdo->prepare("SELECT id,count(*) FROM users WHERE password = :password");
$res = $q->execute(array('password' => self::hashit($password)));
}
$res = $q->fetchAll();
print_r($res);
if($res['count(*)'] == 1){
$_SESSION['id'] = $res['id'];
return true;
}
else{
return false;
}
}
public function change_token($phone = '', $token = '', $device = '') {
$client = new DB();
$q = $client->pdo->prepare("UPDATE users SET token = '' WHERE token = :token");
$res = $q->execute(array('token' => $token));
$q = $client->pdo->prepare("UPDATE users SET token = :token, device = :device WHERE phone = :phone");
$res = $q->execute(array(
'token' => $token,
'device' => $device,
'login' => $phone
));
if ($res) {
return true;
}
else{
return false;
}
}
public function checktoken($token, $phone) {
$salt = '123';
if(md5($salt.$phone.$salt) == $token) return true;
else return false;
}
public function registration($name,$email,$password,$user_type,$phone){
$client = new DB();
if(!self::validateEmailPhone($email,$phone)){
return false;
}
else{
$stmt = $client->pdo->prepare("INSERT INTO users(name,email,password,user_type,phone)
VALUES(:name,:email,:password,:user_type,:phone) ");
$result = $stmt->execute(array(
'name' => $name,
'email' => $email,
'password' => self::hashit($password),
'user_type' => $user_type,
'phone' => $phone
));
}
if(!$result){
//var_dump($errors);
return false;
}
else {
return true;
}
}
public function validateEmailPhone($email,$phone){
$client = new DB();
$query = $client->pdo->prepare("SELECT count(*) FROM users WHERE email = :email OR phone = :phone ");
$res = $query->execute(array('email' => $email, 'phone' => $phone));
$res = $query->fetchAll();
if($res[0]['count(*)'] !== 0){
// echo $this->errors[] = 'Такой E-mail или номер уже есть';
return true;
}
else{
return false;
}
}
else if(!$set->validateEmailPhone('phone',$phone)){
$errors[] = 'Такой номер уже есть! Наберите другой номер телефона';
}
else if(!$set->validateEmailPhone('email',$email)){
$errors[] = 'Такой E-mail уже есть! Подумайте другой E-mail';
}