|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Profile; |
|
6
|
|
|
use Apps\ActiveRecord\User; |
|
7
|
|
|
use Ffcms\Console\App; |
|
8
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
9
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
|
10
|
|
|
use Ffcms\Core\Helper\Security; |
|
11
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
12
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
13
|
|
|
|
|
14
|
|
|
class Db |
|
15
|
|
|
{ |
|
16
|
|
|
public function actionImport($activeRecord) |
|
17
|
|
|
{ |
|
18
|
|
|
$importFile = root . '/Private/Database/Tables/' . ucfirst(strtolower($activeRecord)) . '.php'; |
|
19
|
|
|
if (!File::exist($importFile)) { |
|
20
|
|
|
return App::$Output->write('Database model table not founded: ' . $activeRecord); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
@include($importFile); |
|
|
|
|
|
|
24
|
|
|
return App::$Output->write('Database table import runned: ' . $activeRecord); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function actionImportAll($connectName = 'default') |
|
28
|
|
|
{ |
|
29
|
|
|
$importFile = root . '/Private/Database/install.php'; |
|
30
|
|
|
if (!File::exist($importFile)) { |
|
31
|
|
|
return App::$Output->write('Import file is not exist: ' . $importFile); |
|
32
|
|
|
} |
|
33
|
|
|
@include($importFile); |
|
|
|
|
|
|
34
|
|
|
return App::$Output->write('All database tables was imported!'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function actionAdduser() |
|
38
|
|
|
{ |
|
39
|
|
|
echo "Login:"; |
|
40
|
|
|
$login = App::$Input->read(); |
|
41
|
|
|
if (Str::length($login) < 2) { |
|
42
|
|
|
throw new NativeException('Login is bad'); |
|
43
|
|
|
} |
|
44
|
|
|
echo "Email:"; |
|
45
|
|
|
$email = App::$Input->read(); |
|
46
|
|
|
if (!Str::isEmail($email)) { |
|
47
|
|
|
throw new NativeException('Email is bad'); |
|
48
|
|
|
} |
|
49
|
|
|
echo "Password:"; |
|
50
|
|
|
$pass = App::$Input->read(); |
|
51
|
|
|
if (Str::length($pass) < 2) { |
|
52
|
|
|
throw new NativeException('Password is bad'); |
|
53
|
|
|
} |
|
54
|
|
|
echo "RoleId (1 = user, 3 = admin):"; |
|
55
|
|
|
$role = (int)App::$Input->read(); |
|
56
|
|
|
if (!Arr::in($role, [1,2,3])) { |
|
57
|
|
|
$role = 1; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (User::isMailExist($email) || User::isLoginExist($login)) { |
|
61
|
|
|
throw new NativeException('User with this email or login is always exist'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$salt = App::$Properties->get('passwordSalt'); |
|
65
|
|
|
|
|
66
|
|
|
$user = new User(); |
|
67
|
|
|
$user->login = $login; |
|
|
|
|
|
|
68
|
|
|
$user->email = $email; |
|
|
|
|
|
|
69
|
|
|
$user->password = Security::password_hash($pass, $salt); |
|
|
|
|
|
|
70
|
|
|
$user->role_id = $role; |
|
|
|
|
|
|
71
|
|
|
$user->save(); |
|
72
|
|
|
|
|
73
|
|
|
$profile = new Profile(); |
|
74
|
|
|
$profile->user_id = $user->id; |
|
|
|
|
|
|
75
|
|
|
$profile->save(); |
|
76
|
|
|
|
|
77
|
|
|
return App::$Output->write('User was successful added to database!'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: