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\Security; |
||||
12 | use Ffcms\Core\Helper\Type\Arr; |
||||
13 | use Ffcms\Core\Helper\Type\Str; |
||||
14 | use Symfony\Component\Console\Input\InputInterface; |
||||
15 | use Symfony\Component\Console\Input\InputOption; |
||||
16 | use Symfony\Component\Console\Output\OutputInterface; |
||||
17 | |||||
18 | /** |
||||
19 | * Class MainAdduserCommand. Add new user in database |
||||
20 | * @package Apps\Console |
||||
21 | */ |
||||
22 | class MainAdduserCommand extends Command |
||||
23 | { |
||||
24 | /** |
||||
25 | * Register adduser command |
||||
26 | */ |
||||
27 | public function configure() |
||||
28 | { |
||||
29 | $this->setName('main:adduser') |
||||
30 | ->setDescription('Add new user into database') |
||||
31 | ->addOption('login', 'login', InputOption::VALUE_OPTIONAL, 'Set user login. Should be unique!') |
||||
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 login and check validity |
||||
47 | $login = $this->optionOrAsk('login', 'User login'); |
||||
48 | if (Str::length($login) < 2) { |
||||
49 | throw new \Exception('Login is too short'); |
||||
50 | } |
||||
51 | // get email and check validity |
||||
52 | $email = $this->optionOrAsk('email', 'User email'); |
||||
53 | if (!Str::isEmail($email)) { |
||||
54 | throw new \Exception('Email syntax is wrong'); |
||||
55 | } |
||||
56 | // get password and role and check validity |
||||
57 | $password = $this->optionOrAsk('password', 'User password'); |
||||
58 | $roleId = (int)$this->optionOrAsk('role', 'RoleId', '1'); |
||||
59 | $records = Role::all()->toArray(); |
||||
60 | $roles = Arr::pluck('id', $records); |
||||
61 | if (!Arr::in($roleId, $roles)) { |
||||
62 | throw new \Exception('RoleId is not found'); |
||||
63 | } |
||||
64 | |||||
65 | // check if user is always exists |
||||
66 | if (User::isLoginExist($login) || User::isMailExist($email)) { |
||||
67 | $output->writeln('User is always exists'); |
||||
68 | return; |
||||
69 | } |
||||
70 | |||||
71 | // create new user instance in prefix_users table |
||||
72 | $salt = Console::$Properties->get('passwordSalt'); |
||||
73 | $user = new User(); |
||||
74 | $user->login = $login; |
||||
75 | $user->email = $email; |
||||
76 | $user->password = Security::password_hash($password, $salt); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
The function
Ffcms\Core\Helper\Security::password_hash() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
77 | $user->role_id = $roleId; |
||||
78 | $user->save(); |
||||
79 | |||||
80 | // crate empty user profile in prefix_profiles table |
||||
81 | $profile = new Profile(); |
||||
82 | $profile->user_id = $user->id; |
||||
83 | $profile->save(); |
||||
84 | $output->writeln('New user are successful added'); |
||||
85 | } |
||||
86 | } |