-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
AdminCommand.php
150 lines (124 loc) · 3.4 KB
/
AdminCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace TCG\Voyager\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\Console\Input\InputOption;
use TCG\Voyager\Facades\Voyager;
class AdminCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'voyager:admin';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make sure there is a user with the admin role that has all of the necessary permissions.';
/**
* Get user options.
*/
protected function getOptions()
{
return [
['create', null, InputOption::VALUE_NONE, 'Create an admin user', null],
];
}
public function fire()
{
return $this->handle();
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
// Get or create user
$user = $this->getUser(
$this->option('create')
);
// the user not returned
if (!$user) {
exit;
}
// Get or create role
$role = $this->getAdministratorRole();
// Get all permissions
$permissions = Voyager::model('Permission')->all();
// Assign all permissions to the admin role
$role->permissions()->sync(
$permissions->pluck('id')->all()
);
// Ensure that the user is admin
$user->role_id = $role->id;
$user->save();
$this->info('The user now has full access to your site.');
}
/**
* Get command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['email', InputOption::VALUE_REQUIRED, 'The email of the user.', null],
];
}
/**
* Get the administrator role, create it if it does not exists.
*
* @return mixed
*/
protected function getAdministratorRole()
{
$role = Voyager::model('Role')->firstOrNew([
'name' => 'admin',
]);
if (!$role->exists) {
$role->fill([
'display_name' => 'Administrator',
])->save();
}
return $role;
}
/**
* Get or create user.
*
* @param bool $create
*
* @return \App\User
*/
protected function getUser($create = false)
{
$email = $this->argument('email');
$model = config('voyager.user.namespace', 'App\\User');
// If we need to create a new user go ahead and create it
if ($create) {
$name = $this->ask('Enter the admin name');
$password = $this->secret('Enter admin password');
$confirmPassword = $this->secret('Confirm Password');
// Ask for email if there wasnt set one
if (!$email) {
$email = $this->ask('Enter the admin email');
}
// Passwords don't match
if ($password != $confirmPassword) {
$this->info("Passwords don't match");
return;
}
$this->info('Creating admin account');
return $model::create([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
]);
}
return $model::where('email', $email)->firstOrFail();
}
}