/** * {@inheritdoc}. */ protected function getValidator($model) { $arr = $model->toArray(); $arr['password'] = $this->password; $arr['password_confirmation'] = $this->password; return Validator::make($arr, User::createRules()); }
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { switch ($this->method) { case 'GET': break; case 'DELETE': break; case 'POST': return User::createRules(); case 'PUT': break; case 'PATCH': return User::updateRules($this); default: break; } return []; }
/** * Execute the console command. * * @return boolean */ public function handle() { $generatedPassword = substr(md5(rand()), 0, 8); if (empty($this->option('password'))) { $this->info("Using auto generated password: {$generatedPassword}"); } if (empty($this->option('account'))) { $account = Account::where('name', '=', 'default')->first(); } else { $account = Account::where('name', '=', $this->option('account'))->first(); if (!is_object($account)) { $this->error("The account named {$this->option('account')} was not found"); return false; } } $user = new User(); $user->email = empty($this->option('email')) ? false : $this->option('email'); $user->password = empty($this->option('password')) ? $generatedPassword : $this->option('password'); $user->first_name = $this->option('firstname'); $user->last_name = $this->option('lastname'); $user->locale = $this->option('language'); $user->account_id = $account->id; $user->disabled = $this->option('disabled'); $validation = Validator::make($user->toArray(), User::createRules($user)); if ($validation->fails()) { foreach ($validation->messages()->all() as $message) { $this->warn($message); } $this->error('Failed to create the user due to validation warnings'); return false; } if (!$user->save()) { $this->error('Failed to save the user into the database'); return false; } $this->info("The user {$this->option('email')} has been created"); return true; }