ask('For which site would you like to create a user ?'); $site = Site::where('name', 'LIKE', "%$sitename%")->first(); if (!$site) { $this->error(sprintf('Site %s not found cancelled', $sitename)); return 0; } if (!$this->confirm(sprintf('Would you like to use %s ? ', $site->name))) { $this->info('Command cancelled'); return 0; } $name = null; while (is_null($name)) { try { $tmp = $this->ask('What is user the name?'); $validator = Validator::make(['nom' => $tmp], [ 'nom' => 'required|min:5,max:255' ]); $validator->validate(); $name = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid name %s', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } } $email = null; while (is_null($email)) { try { $tmp = $this->ask('What is the email?'); $validator = Validator::make(['email' => $tmp], [ 'email' => 'required|unique:users,email|email:rfc,dns' ]); $validator->validate(); $email = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid email %s', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } } $role = null; while (is_null($role)) { try { $num = $this->ask(sprintf( 'What is the user role %s ?', implode(',', collect(Role::toArray())->map(function ($role) { return sprintf('(%s) %d', $role->label, $role->value); })->all()) )); if (!\is_numeric($num)) { $this->error('Please provide a valid role.'); continue; } $role = Role::make(intval($num))->value; } catch (Exception $e) { $this->error('Please provide a valid role.'); continue; } } $password = $this->secret('What is the password?'); $user = new User([ 'name' => $name, 'email' => $email, 'role' => $role, 'password' => $password ]); $user->saveOrFail(); if ($user->role->equals(Role::admin())) { $user->sites()->sync([$site->id]); } $this->info('User created.'); return 0; } }