You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

7 years ago
<?php
/**
* Created by PhpStorm.
* User: Egorka
* Date: 11.08.2017
* Time: 15:07
*/
namespace console\controllers;
use core\forms\user\UserForm;
3 years ago
use yii\base\Exception;
7 years ago
use yii\helpers\ArrayHelper;
use core\services\user\UserManageService;
use yii\console\Controller;
use Yii;
class UserController extends Controller
{
3 years ago
private UserManageService $service;
7 years ago
public function __construct($id, $module, UserManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
7 years ago
}
3 years ago
/**
* @throws Exception
* @throws \yii\db\Exception
*/
7 years ago
public function actionAdd()
{
6 years ago
$username = $this->prompt('Username:', ['required' => true]);
$email = $this->prompt('Email:', ['required' => true]);
7 years ago
$password = $this->prompt('Password:', ['required' => true]);
6 years ago
$role = $this->select('Role:', ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description'));
7 years ago
6 years ago
$form = new UserForm();
7 years ago
$form->username = $username;
6 years ago
$form->email = $email;
7 years ago
$form->password = $password;
6 years ago
$form->role = $role;
7 years ago
6 years ago
if ($form->validate()) {
$this->service->create($form);
7 years ago
$this->stdout('Done!' . PHP_EOL);
6 years ago
7 years ago
return;
}
$this->stdout('Errors found!' . PHP_EOL);
6 years ago
foreach ($form->errors as $error) {
7 years ago
$this->stdout(is_string($error) ? $error : $error[0] . PHP_EOL);
}
}
3 years ago
/**
* @throws Exception
* @throws \yii\db\Exception
*/
6 years ago
public function actionAddAdmin($username, $email, $password)
{
$form = new UserForm();
$form->username = $username;
$form->email = $email;
$form->password = $password;
$form->role = 'admin';
if ($form->validate()) {
$this->service->create($form);
6 years ago
}
}
}