1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Console; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Apps\ActiveRecord\Profile; |
7
|
|
|
use Apps\ActiveRecord\Role; |
8
|
|
|
use Apps\ActiveRecord\User; |
9
|
|
|
use Ffcms\Console\Command; |
10
|
|
|
use Ffcms\Console\Console; |
11
|
|
|
use Ffcms\Core\Helper\Crypt; |
12
|
|
|
use Ffcms\Core\Helper\Security; |
13
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
14
|
|
|
use Ffcms\Core\Helper\Type\Str; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class MainAdduserCommand. Add new user in database |
21
|
|
|
* @package Apps\Console |
22
|
|
|
*/ |
23
|
|
|
class MainAdduserCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Register adduser command |
27
|
|
|
*/ |
28
|
|
|
public function configure() |
29
|
|
|
{ |
30
|
|
|
$this->setName('main:adduser') |
31
|
|
|
->setDescription('Add new user into database') |
32
|
|
|
->addOption('email', 'email', InputOption::VALUE_OPTIONAL, 'Set user email. Should be unique!') |
33
|
|
|
->addOption('password', 'password', InputOption::VALUE_OPTIONAL, 'Set user password') |
34
|
|
|
->addOption('role', 'role', InputOption::VALUE_OPTIONAL, 'Define user role_id. Should be integer (see prefix_roles table). By default: 1=guest, 2=user, 3=moder, 4=admin'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add new user in database by passed params |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
* @throws \Exception |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
// get email and check validity |
47
|
|
|
$email = $this->optionOrAsk('email', 'User email'); |
48
|
|
|
if (!Str::isEmail($email)) { |
49
|
|
|
throw new \Exception('Email syntax is wrong'); |
50
|
|
|
} |
51
|
|
|
// get password and role and check validity |
52
|
|
|
$password = $this->optionOrAsk('password', 'User password'); |
53
|
|
|
$roleId = (int)$this->optionOrAsk('role', 'RoleId', '1'); |
54
|
|
|
$records = Role::all()->toArray(); |
55
|
|
|
$roles = Arr::pluck('id', $records); |
56
|
|
|
if (!Arr::in($roleId, $roles)) { |
57
|
|
|
throw new \Exception('RoleId is not found'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// check if user is always exists |
61
|
|
|
if (User::isMailExist($email)) { |
62
|
|
|
$output->writeln('User is always exists'); |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// create new user instance in prefix_users table |
67
|
|
|
$user = new User(); |
68
|
|
|
$user->email = $email; |
69
|
|
|
$user->password = Crypt::passwordHash($password); |
70
|
|
|
$user->role_id = $roleId; |
71
|
|
|
$user->save(); |
72
|
|
|
|
73
|
|
|
// crate empty user profile in prefix_profiles table |
74
|
|
|
$profile = new Profile(); |
75
|
|
|
$profile->user_id = $user->id; |
76
|
|
|
$profile->save(); |
77
|
|
|
$output->writeln('New user are successful added'); |
78
|
|
|
} |
79
|
|
|
} |