Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

CreateAdmin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 75
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 6
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