1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MultiTenantLaravel\App\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Faker\Generator as Faker; |
7
|
|
|
|
8
|
|
|
class CreateUser extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'tenant:create-user {--fake}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Create a new user with the option to assign to tenants'; |
23
|
|
|
|
24
|
|
|
protected $faker; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new command instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct(Faker $faker) |
32
|
|
|
{ |
33
|
|
|
$this->faker = $faker; |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function handle() |
44
|
|
|
{ |
45
|
|
|
$count = (int) $this->ask('How many would you like to create?'); |
46
|
|
|
|
47
|
|
|
while ($count > 0) { |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
if (!$this->option('fake')) { |
51
|
|
|
|
52
|
|
|
$this->createNewUser(); |
53
|
|
|
} else { |
54
|
|
|
$this->createFakeUser(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$count--; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Setup a fake user using faker |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
private function createFakeUser() |
68
|
|
|
{ |
69
|
|
|
$name = $this->faker->name; |
70
|
|
|
|
71
|
|
|
$user = config('multi-tenant.user_class')::create([ |
72
|
|
|
'name' => $name, |
73
|
|
|
'email' => $this->faker->unique()->safeEmail, |
74
|
|
|
'password' => bcrypt('tester'), |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$add_to_tenant = $this->anticipate('Would you like to assign the user to a tenant?', ['Yes', 'No'], 'Yes'); |
78
|
|
|
|
79
|
|
View Code Duplication |
if ($add_to_tenant == 'Yes') { |
|
|
|
|
80
|
|
|
$headers = ['Name', 'ID']; |
81
|
|
|
$tenants = config('multi-tenant.tenant_class')::all('name', 'id'); |
82
|
|
|
|
83
|
|
|
if($tenants->count() <= 0) { |
84
|
|
|
$this->comment($user->email . ' with the password `tester` was created without any tenants'); |
85
|
|
|
} else { |
86
|
|
|
$this->table($headers, $tenants->toArray()); |
87
|
|
|
|
88
|
|
|
$tenant_id = (int) $this->ask('Please enter the id of the desired tenant.'); |
89
|
|
|
|
90
|
|
|
$tenant = config('multi-tenant.tenant_class')::findOrFail($tenant_id); |
91
|
|
|
|
92
|
|
|
$tenant->update(['owner_id' => $user->id]); |
93
|
|
|
|
94
|
|
|
$this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create a new user with user supplied input for the users settings |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
private function createNewUser() |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
$name = (string) $this->ask('Name'); |
108
|
|
|
$email = (string) $this->ask('E-Mail'); |
109
|
|
|
$user = config('multi-tenant.user_class')::create([ |
110
|
|
|
'name' => $name, |
111
|
|
|
'email' => $email, |
112
|
|
|
'password' => bcrypt('tester'), |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$add_to_tenant = $this->anticipate('Would you like to assign the user to a tenant?', ['Yes', 'No'], 'Yes'); |
116
|
|
|
|
117
|
|
View Code Duplication |
if ($add_to_tenant == 'Yes') { |
|
|
|
|
118
|
|
|
$headers = ['Name', 'ID']; |
119
|
|
|
$tenants = config('multi-tenant.tenant_class')::all('name', 'id'); |
120
|
|
|
|
121
|
|
|
if($tenants->count() <= 0) { |
122
|
|
|
$this->comment($user->email . ' with the password `tester` was created without any tenants'); |
123
|
|
|
} else { |
124
|
|
|
$this->table($headers, $tenants->toArray()); |
125
|
|
|
|
126
|
|
|
$tenant_id = (int) $this->ask('Please enter the id of the desired tenant.'); |
127
|
|
|
|
128
|
|
|
$tenant = config('multi-tenant.tenant_class')::findOrFail($tenant_id); |
129
|
|
|
|
130
|
|
|
$tenant->update(['owner_id' => $user->id]); |
131
|
|
|
|
132
|
|
|
$this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
else{ |
136
|
|
|
$this->comment($user->email . ' with the password `tester` was created without any tenants'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.