Initial import
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace app\services;
|
||||
|
||||
use app\controllers\PublicController;
|
||||
use yii\db\ActiveRecord;
|
||||
|
||||
class Validate
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $uc
|
||||
* @param $cb
|
||||
* @return mixed|null
|
||||
*/
|
||||
public static function login(string $uc, $cb)
|
||||
{
|
||||
$p = self::getPost();
|
||||
if (!$p) return null;
|
||||
$email_login = $p('email_login');
|
||||
$password = $p('password');
|
||||
if (!$email_login)
|
||||
self::error('email_login', 'This field is required');
|
||||
if (!$password)
|
||||
self::error('password', 'This field is required');
|
||||
/** @var \app\models\_Base $uc */
|
||||
$user = $uc::find()->where(['email_login' => $email_login])->one();
|
||||
if (!$user)
|
||||
self::error('email_login', 'Unknown email');
|
||||
if (!$user->verifyPassword($password))
|
||||
self::error('password', 'Wrong password');
|
||||
|
||||
if($user->is_active != 1)
|
||||
self::error('email_login', 'This user is not active');
|
||||
|
||||
return $cb($user);
|
||||
}
|
||||
|
||||
public static function defaultValidation($validation = null) {
|
||||
if($validation) {
|
||||
$key = array_keys($validation)[0];
|
||||
self::error($key, $validation[$key], $validation[0] ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
private static function error($field, $message, $params = [])
|
||||
{
|
||||
header('Content-type: application/json');
|
||||
echo json_encode([
|
||||
'error' => true,
|
||||
'field' => $field,
|
||||
'message' => \Yii::t('cms', $message, $params)
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
private static function getPost()
|
||||
{
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'POST')
|
||||
return function ($param) {
|
||||
if (isset($_POST[$param]))
|
||||
return $_POST[$param];
|
||||
return null;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user