mirror of
https://github.com/CyanoFresh/SmartHomePHP.git
synced 2026-03-03 08:34:02 +01:00
72 lines
1.5 KiB
PHP
72 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace app\commands;
|
|
|
|
use app\models\User;
|
|
use yii\console\Controller;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function actionRegister($username, $password, $email)
|
|
{
|
|
$user = new User([
|
|
'scenario' => 'create',
|
|
]);
|
|
|
|
$user->username = $username;
|
|
$user->email = $email;
|
|
$user->generateAuthKey();
|
|
$user->setPassword($password);
|
|
|
|
if ($user->save()) {
|
|
echo 'Success' . PHP_EOL;
|
|
return 1;
|
|
}
|
|
|
|
echo 'Errors:' . PHP_EOL;
|
|
var_dump($user->errors);
|
|
return 0;
|
|
}
|
|
|
|
public function actionChangePassword($id, $newPassword)
|
|
{
|
|
$user = User::findOne($id);
|
|
|
|
if (!$user) {
|
|
echo 'User was not found' . PHP_EOL;
|
|
return 0;
|
|
}
|
|
|
|
$user->setPassword($newPassword);
|
|
$user->generateAuthKey();
|
|
|
|
if ($user->save()) {
|
|
echo 'Password successfully changed' . PHP_EOL;
|
|
|
|
return 1;
|
|
}
|
|
|
|
echo 'Errors:' . PHP_EOL;
|
|
var_dump($user->errors);
|
|
|
|
return 0;
|
|
}
|
|
|
|
public function actionCheckPassword($id, $password)
|
|
{
|
|
$user = User::findOne($id);
|
|
|
|
if (!$user) {
|
|
echo 'User was not found' . PHP_EOL;
|
|
return 0;
|
|
}
|
|
|
|
if ($user->validatePassword($password)) {
|
|
echo 'Password is valid' . PHP_EOL;
|
|
return 1;
|
|
}
|
|
|
|
echo 'Password is invalid' . PHP_EOL;
|
|
return 0;
|
|
}
|
|
} |