1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-25 03:43:33 +01:00

Merge pull request #520 from cloudron-io/user-create-args

Add commandline arguments to freescout:create-user
This commit is contained in:
FreeScout 2020-03-09 16:49:33 +05:45 committed by GitHub
commit 2c4d41f8da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,7 @@ class CreateUser extends Command
*
* @var string
*/
protected $signature = 'freescout:create-user';
protected $signature = 'freescout:create-user {--role=} {--firstName=} {--lastName=} {--email=} {--password=}';
/**
* The console command description.
@ -46,39 +46,56 @@ class CreateUser extends Command
).'.model'
);
$user = new $class();
$fillables = ['role', 'first_name', 'last_name', 'email', 'password'];
foreach ($fillables as $key => $fillable) {
if ($fillable == 'password') {
$user->$fillable = \Hash::make($this->secret(($key + 1).'/'.count($fillables)." User $fillable"));
} elseif ($fillable == 'role') {
$user->$fillable = $this->ask(($key + 1).'/'.count($fillables)." User $fillable (admin/user)", 'admin');
if (!$user->$fillable) {
$user->$fillable = 'admin';
}
while (!in_array($user->$fillable, User::$roles)) {
$this->error('Incorrect role');
$user->$fillable = $this->ask('Please enter valid role');
$user->role = $this->option('role');
if ($user->role ) {
if (!in_array($user->role , User::$roles)) {
$this->error('Invalid role');
return false;
}
$user->$fillable = array_flip(User::$roles)[$user->$fillable];
} else {
$user->$fillable = $this->ask(($key + 1).'/'.count($fillables)." User $fillable");
$user->role = $this->ask('User role (admin/user)', 'admin');
while (!in_array($user->role, User::$roles)) {
$this->error('Invalid role');
$user->role = $this->ask('Please enter valid role');
}
}
$user->role = array_flip(User::$roles)[$user->role];
if ($fillable == 'email') {
while (!filter_var($user->$fillable, FILTER_VALIDATE_EMAIL)) {
$user->first_name = $this->option('firstName') ? $this->option('firstName') : $this->ask('User first name');
$user->last_name = $this->option('lastName') ? $this->option('lastName') : $this->ask('User last name');
$user->email = $this->option('email');
if ($user->email) {
if (!filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
$this->error('Invalid email address');
return false;
}
} else {
$user->email = $this->ask('User email address');
while (!filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
$this->error('Incorrect email address');
$user->$fillable = $this->ask('Please enter valid email address');
}
}
$user->email = $this->ask('Please enter valid email address');
}
}
$user->password = \Hash::make($this->option('password') ? $this->option('password') : $this->secret('User password'));
if ($this->confirm('Do you want to create the user?', true)) {
if ($user->isAdmin()) {
$user->invite_state = User::INVITE_STATE_ACTIVATED;
}
try {
$user->save();
$this->info("User created (id: {$user->id})");
} catch (\Exception $e) {
$this->line($e->getMessage());
$this->error('User already exists.');
return false;
}
}
$this->info('User created with id: '.$user->id);
return true;
}