/** * 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); }
/** * Execute the console command. * * @return mixed */ public function fire() { $key = $this->option('api-key'); if (!is_null($key)) { // we delete a specific API key $confirmation = $this->ask("Are you sure you want to delete this API key? [y/n]"); if ($confirmation == 'y') { $apiKey = ApiKey::where('key', '=', $key)->first(); if (empty($apiKey) || $apiKey->exists == false) { $this->info("The API key you specified does not exist."); return; } $this->info("The API key {$key} was deleted."); return; } return; } $this->error("Specify an API key to delete using the --api-key option. Example: --api-key=xxxxxxxxx"); }
/** * 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; } }
protected function authenticated($request, $user) { $apiKey = \Chrisbjr\ApiGuard\Models\ApiKey::where('user_id', $user->id)->firstOrFail(); return ['key' => $apiKey->key, 'admin' => $user->admin == '1' ? true : false]; }
/** * Display api key * * @return Response */ public function apiKey(Request $request) { return ApiKey::where('user_id', $request->user()->id)->first(); }
/** * Log the user out * * @param string $apiKey * @return \Illuminate\Http\JsonResponse */ public function deauthenticate($apiKey) { $this->apiKey = ApiKey::where('key', $apiKey)->first(); if (empty($this->apiKey)) { return $this->response->errorUnauthorized("There is no such user to deauthenticate."); } $this->apiKey->delete(); return $this->response->withArray(['ok' => ['code' => 'SUCCESSFUL', 'http_code' => 200, 'message' => 'User was successfuly deauthenticated']]); }