/** * Execute the console command. * * @return mixed */ public function handle() { $email = $this->argument('email'); if (is_null($email)) { $this->error('No e-email address given! Aborting'); return false; } while (empty($name)) { $name = $this->ask('What is your name?'); } $password = $this->secret('Enter a password'); $confirm = $this->secret('Confirm the password'); if ($password !== $confirm) { $this->error('The passwords do not match. Aborting'); return false; } $admin = $this->confirm('Is this user an admin? [y/N]'); //Actually create the user $user = User::create(['name' => $name, 'email' => $email, 'password' => bcrypt($password), 'admin' => $admin]); $apiKey = new ApiKey(); $apiKey->key = $apiKey->generateKey(); $apiKey->user_id = $user->id; $apiKey->save(); $this->info("User successfully created with ID {$user->id}"); }
/** * Authenticate the login * * @return \Illuminate\Http\JsonResponse|\Illuminate\Contracts\Routing\ResponseFactory */ public function authenticate() { $credentials = ['username' => Input::get('username'), 'password' => Input::get('password')]; $validator = Validator::make(['username' => $credentials['username'], 'password' => $credentials['password']], ['username' => 'required|max:255', 'password' => 'required|max:255']); if ($validator->fails()) { return $this->response->errorWrongArgsValidator($validator); } try { $user = with(new User())->authenticate($credentials['username'], $credentials['password'])->first(); $credentials['email'] = $user->email; } catch (\ErrorException $e) { return $this->response->errorUnauthorized("Your username or password is incorrect"); } // User validated, now assign an api key for this session $apiKey = ApiKey::where('user_id', '=', $user->id)->first(); if (!isset($apiKey)) { $apiKey = new ApiKey(); $apiKey->user_id = $user->id; $apiKey->key = $apiKey->generateKey(); $apiKey->level = 5; $apiKey->ignore_limits = 0; } else { $apiKey->generateKey(); } if (!$apiKey->save()) { return $this->response->errorInternalError("Failed to create an API key. Please try again."); } // return api key return $this->response->withItem($user, new \App\LaravelRestCms\User\UserTransformer()); }
/** * Execute the console command. * * @return mixed */ public function fire() { $userId = $this->getOption('user-id', 0); if (!empty($userId)) { // check whether this user already has an API key $apiKey = ApiKey::where('user_id', '=', $userId)->first(); if (!empty($apiKey) || $apiKey->exists) { $overwrite = $this->ask("This user already has an existing API key. Do you want to overwrite it? [y/n]"); if ($overwrite == 'n') { return; } } } $apiKey = new ApiKey(); $apiKey->key = $apiKey->generateKey(); $apiKey->user_id = $this->getOption('user-id', 0); $apiKey->level = $this->getOption('level', 10); $apiKey->ignore_limits = $this->getOption('ignore-limits', 1); if ($apiKey->save() === false) { $this->error("Failed to save API key to the database."); return; } if (empty($apiKey->user_id)) { $this->info("You have successfully generated an API key:"); } else { $this->info("You have successfully generated an API key for user ID#{$apiKey->user_id}:"); } $this->info($apiKey->key); }
/** * Get the api key that belongs to user * * @return ApiKey */ public function apiKey() { $apiKey = ApiKey::where('user_id', '=', $this->getKey())->first(); if (isset($apiKey)) { return $apiKey; } else { $apiKey = new ApiKey(); $apiKey->key = $apiKey->generateKey(); $apiKey->user_id = $this->getKey(); $apiKey->level = 10; $apiKey->ignore_limits = 0; //False $apiKey->save(); return $apiKey; } }