|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\User\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Da\User\Query\UserQuery; |
|
15
|
|
|
use Throwable; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\base\Module; |
|
18
|
|
|
use yii\console\Controller; |
|
19
|
|
|
use yii\db\StaleObjectException; |
|
20
|
|
|
use yii\helpers\Console; |
|
21
|
|
|
|
|
22
|
|
|
class DeleteController extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
protected $userQuery; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct($id, Module $module, UserQuery $userQuery, array $config = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->userQuery = $userQuery; |
|
29
|
|
|
parent::__construct($id, $module, $config); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* This command deletes a user. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $usernameOrEmail Email or username of the user to delete |
|
36
|
|
|
* |
|
37
|
|
|
* |
|
38
|
|
|
* @throws Throwable |
|
39
|
|
|
* @throws StaleObjectException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function actionIndex($usernameOrEmail) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->confirm(Yii::t('usuario', 'Are you sure? Deleted user can not be restored'))) { |
|
44
|
|
|
$user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one(); |
|
45
|
|
|
if ($user === null) { |
|
46
|
|
|
$this->stdout(Yii::t('usuario', 'User is not found') . "\n", Console::FG_RED); |
|
47
|
|
|
} else { |
|
48
|
|
|
if ($user->delete()) { |
|
49
|
|
|
$this->stdout(Yii::t('usuario', 'User has been deleted') . "\n", Console::FG_GREEN); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->stdout(Yii::t('usuario', 'Error occurred while deleting user') . "\n", Console::FG_RED); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|