1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\App\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Artisan; |
6
|
|
|
use Thinktomorrow\Chief\Authorization\AuthorizationDefaults; |
7
|
|
|
|
8
|
|
|
class CreateAdmin extends BaseCommand |
9
|
|
|
{ |
10
|
|
|
protected $signature = 'chief:admin {--dev}'; |
11
|
|
|
protected $description = 'Create a new chief admin user'; |
12
|
|
|
|
13
|
|
|
public function handle() |
14
|
|
|
{ |
15
|
|
|
$this->settingPermissionsAndRoles(); |
16
|
|
|
|
17
|
|
|
$anticipations = $this->getAnticipations(); |
18
|
|
|
$anticipatedLastname = null; |
19
|
|
|
$firstname = null; |
20
|
|
|
$lastname = null; |
21
|
|
|
|
22
|
|
|
while (!$firstname) { |
23
|
|
|
$firstname = $this->anticipate('firstname', array_pluck($anticipations, 'firstname')); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
while (!$lastname) { |
27
|
|
|
$lastname = $this->anticipate('lastname', array_pluck($anticipations, 'lastname'), $anticipatedLastname); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$email = $this->ask('email', str_slug($firstname).'@thinktomorrow.be'); |
31
|
|
|
|
32
|
|
|
$password = $this->askPassword(); |
33
|
|
|
|
34
|
|
|
if ($this->option('dev')) { |
35
|
|
|
$role = 'developer'; |
36
|
|
|
} else { |
37
|
|
|
$role = 'admin'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->createUser($firstname, $lastname, $email, $password, [$role]); |
41
|
|
|
|
42
|
|
|
$this->info($firstname.' '.$lastname. ' succesfully added as admin user.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function settingPermissionsAndRoles() |
46
|
|
|
{ |
47
|
|
|
AuthorizationDefaults::permissions()->each(function ($permissionName) { |
48
|
|
|
Artisan::call('chief:permission', ['name' => $permissionName]); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
AuthorizationDefaults::roles()->each(function ($defaultPermissions, $roleName) { |
52
|
|
|
Artisan::call('chief:role', ['name' => $roleName, '--permissions' => implode(',', $defaultPermissions)]); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$this->info('Default permissions and roles'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* We assume we are creating users for ourselves so we make this a bit easier to do |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
private function getAnticipations() |
63
|
|
|
{ |
64
|
|
|
$anticipations = [ |
65
|
|
|
[ |
66
|
|
|
'firstname' => 'Ben', |
67
|
|
|
'lastname' => 'Cavens', |
68
|
|
|
'email' => '[email protected]', |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
'firstname' => 'Philippe', |
72
|
|
|
'lastname' => 'Damen', |
73
|
|
|
'email' => '[email protected]', |
74
|
|
|
], |
75
|
|
|
[ |
76
|
|
|
'firstname' => 'Johnny', |
77
|
|
|
'lastname' => 'Berkmans', |
78
|
|
|
'email' => '[email protected]', |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
return $anticipations; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|