/** * @param \Seat\Web\Validation\CsvImport $request * * @return \Illuminate\Http\RedirectResponse */ public function postCsv(CsvImport $request) { // Grab the Data out of the CSV $file = $request->file('csv'); $data = explode("\n", $file->openFile()->fread($file->getSize())); // Keep tabs on the amount of keys that have // been inserted, and how many have been // considered erroneous $updated = 0; $errored = 0; // Loop the CSV, validating the lines // and inserting into the database foreach ($data as $k => $key) { $parts = explode(",", $key); // Ensure we have 2 entries in the $parts array if (count($parts) != 2) { $errored++; continue; } // Assign the $parts to readable names $key_id = $parts[0]; $v_code = $parts[1]; $validator = Validator::make(['key_id' => $key_id, 'v_code' => $v_code], ['key_id' => 'required|numeric|unique:eve_api_keys,key_id', 'v_code' => 'required|size:64|alpha_num']); // Ensure the format was ok if ($validator->fails()) { $errored++; continue; } // Add the API Key ApiKey::create(['key_id' => $key_id, 'v_code' => $v_code, 'user_id' => auth()->user()->id, 'enabled' => true]); $updated++; } return redirect()->back()->with('success', 'Import complete! Success: ' . $updated . '. Error: ' . $errored); }
/** * @param \Seat\Web\Http\Validation\CsvImport $request * * @return \Illuminate\Http\RedirectResponse */ public function postCsv(CsvImport $request) { $csv = Reader::createFromFileObject($request->file('csv')->openFile()); // Keep tabs on the amount of keys that have // been inserted, and how many have been // considered erroneous $updated = 0; $errored = 0; // Loop the CSV, validating the lines // and inserting into the database foreach ($csv as $k => $data) { // Assign the $data to readable names $key_id = $data[0]; $v_code = $data[1]; // Validate the keys. We check that we dont // already have this key in the database to ensure // that we dont mess up the ownership by accident. $validator = Validator::make(['key_id' => $key_id, 'v_code' => $v_code], ['key_id' => 'required|numeric|unique:eve_api_keys,key_id', 'v_code' => 'required|size:64|alpha_num']); // Ensure the format was ok if ($validator->fails()) { $errored++; continue; } // Add the API Key ApiKey::create(['key_id' => $key_id, 'v_code' => $v_code, 'user_id' => auth()->user()->id, 'enabled' => true]); $updated++; } return redirect()->back()->with('success', 'Import complete! Success: ' . $updated . '. Error: ' . $errored); }
/** * Return an array with character information. * This includes the key info as well as the * extended information such as type/expiry etc. * * @return array */ public function all_with_info() { $response = []; foreach (EveApiKeyModel::all() as $key) { $response[$key->key_id] = ['enabled' => $key->enabled, 'user_id' => $key->user_id, 'key_id' => $key->key_id, 'v_code' => str_limit($key->v_code, 15), 'access_mask' => $key->info ? $key->info->accessMask : null, 'type' => $key->info ? $key->info->type : null, 'expires' => $key->info ? $key->info->expires : null, 'last_error' => $key->last_error, 'characters' => count($key->characters) > 0 ? implode(', ', $key->characters->lists('characterName')->all()) : null]; } return $response; }
/** * Get all of the API keys that are not part * of a specific people group * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function getPeopleUnaffiliatedUserKeys() { $keys = ApiKey::with('characters')->whereNotIn('key_id', function ($query) { $query->select('key_id')->from('person_members'); }); if (!auth()->user()->has('apikey.list', false)) { $keys = $keys->where('user_id', auth()->user()->id); } return $keys->get(); }
/** * Execute the console command. * * @param \Seat\Eveapi\Helpers\JobPayloadContainer $job * * @return mixed */ public function handle(JobPayloadContainer $job) { // Query the API Keys from the database // and queue jobs for them 10 at a time. $key = ApiKey::findOrFail($this->argument('key_id')); $job->scope = 'Key'; $job->api = 'Scheduler'; $job->owner_id = $key->key_id; $job->eve_api_key = $key; $job_id = $this->addUniqueJob(CheckAndQueueKey::class, $job); $this->info('Job ' . $job_id . ' dispatched!'); // Analytics dispatch((new Analytics((new AnalyticsContainer())->set('type', 'event')->set('ec', 'queues')->set('ea', 'queue_key')->set('el', 'console')))->onQueue('medium')); }
public function call() { // call the parent call method in order to load the Slack Api Token parent::call(); // get all Api Key owned by the user $keys = ApiKey::where('user_id', $this->user->id)->get(); // get the Slack Api User $slackUser = SlackUser::where('user_id', $this->user->id)->where('invited', true)->whereNotNull('slack_id')->first(); if ($slackUser != null) { // get channels into which current user is already member $channels = $this->getSlackApi()->member($slackUser->slack_id, false); $groups = $this->getSlackApi()->member($slackUser->slack_id, true); // if key are not valid OR account no longer paid // kick the user from all channels to which he's member if ($this->isEnabledKey($keys) == false || $this->isActive($keys) == false) { if (!empty($channels)) { $this->processChannelsKick($slackUser, $channels); $this->logEvent('kick', $channels); } if (!empty($groups)) { $this->processGroupsKick($slackUser, $groups); $this->logEvent('kick', $groups); } return; } // in other way, compute the gap and kick only the user // to channel from which he's no longer granted to be in $allowedChannels = $this->allowedChannels($slackUser, false); $extraChannels = array_diff($channels, $allowedChannels); // remove channels in which user is already in from all granted channels and invite him if (!empty($extraChannels)) { $this->processChannelsKick($slackUser, $extraChannels); $this->logEvent('kick', $extraChannels); } // remove granted channels from channels in which user is already in and kick him $allowedGroups = $this->allowedChannels($slackUser, true); $extraGroups = array_diff($groups, $allowedGroups); if (!empty($extraGroups)) { $this->processGroupsKick($slackUser, array_diff($groups, $extraGroups)); $this->logEvent('kick', $extraGroups); } } return; }
/** * Execute the console command. * * @param \Seat\Eveapi\Helpers\JobPayloadContainer $job * * @return mixed */ public function handle(JobPayloadContainer $job) { // Counter for the number of keys queued $queued_keys = 0; // Query the API Keys from the database // and queue jobs for them 10 at a time. ApiKey::where('enabled', 1)->chunk(10, function ($keys) use($job, &$queued_keys) { foreach ($keys as $key) { $job->scope = 'Key'; $job->api = 'Scheduler'; $job->owner_id = $key->key_id; $job->eve_api_key = $key; $job_id = $this->addUniqueJob(CheckAndQueueKey::class, $job); $this->info('Job ' . $job_id . ' dispatched!'); $queued_keys++; } }); // Analytics dispatch((new Analytics((new AnalyticsContainer())->set('type', 'event')->set('ec', 'queues')->set('ea', 'queue_keys')->set('el', 'console')->set('ev', $queued_keys)))->onQueue('medium')); }
public function call() { // call the parent call method in order to load the Slack Api Token parent::call(); // get all Api Key owned by the user $keys = ApiKey::where('user_id', $this->user->id)->get(); // invite user only if both account are subscribed and keys active if ($this->isEnabledKey($keys) && $this->isActive($keys)) { // if the user is not yet invited, invite him to team if ($this->isInvited($this->user) == false) { $this->processMemberInvitation($this->user); return; } // in other case, invite him to channels and groups // get the attached slack user $slackUser = SlackUser::where('user_id', $this->user->id)->first(); // control that we already know it's slack ID (mean that he creates his account) if ($slackUser->slack_id != null) { $allowedChannels = $this->allowedChannels($slackUser, false); $memberOfChannels = $this->getSlackApi()->member($slackUser->slack_id, false); $missingChannels = array_diff($allowedChannels, $memberOfChannels); if (!empty($missingChannels)) { $this->processChannelsInvitation($slackUser, $missingChannels); $this->logEvent('invite', $missingChannels); } $allowedGroups = $this->allowedChannels($slackUser, true); $memberOfGroups = $this->getSlackApi()->member($slackUser->slack_id, true); $missingGroups = array_diff($allowedGroups, $memberOfGroups); if (!empty($missingGroups)) { $this->processGroupsInvitation($slackUser, $missingGroups); $this->logEvent('invite', $missingGroups); } } } return; }
/** * @param \Seat\Web\Http\Validation\WorkerConstraint $request * * @return \Illuminate\Http\RedirectResponse */ public function postUpdateWorkerConstraint(WorkerConstraint $request) { $key = ApiKeyModel::findOrFail($request->input('key_id')); // Build a new constraints array from the input data $constraints = ['character' => $request->input('character'), 'corporation' => $request->input('corporation')]; $key->api_call_constraints = $constraints; $key->save(); // Redirect back with new values. return redirect()->back()->with('success', 'Constraints Updated'); }
/** * The required method to handle the Alert. * * @return mixed */ protected function getData() : Collection { return ApiKey::with('owner')->get(); }
/** * @param \Illuminate\Http\Request $request * @param $key_id * * @return \Illuminate\Http\RedirectResponse */ public function transfer(Request $request, $key_id) { $key = ApiKeyModel::findOrFail($key_id); $user = User::findOrFail($request->user_id); $key->user_id = $user->id; $key->save(); return redirect()->back()->with('success', 'Key successfully transferred to ' . $user->name); }
/** * Transfer an EVE API Key to a User * * @param $key_id * @param $user_id * * @return \Illuminate\Http\JsonResponse */ public function transfer($key_id, $user_id) { $key = ApiKey::findOrFail($key_id); User::findOrFail($user_id); $key->user_id = $user_id; $key->save(); return response()->json(['ok']); }
/** * Attempt to take the appropriate action based on the * EVE API Exception. Returns a boolean indicating * if the calling job should continue or not. * * @param \Seat\Eveapi\Models\JobTracking $job_tracker * @param \Seat\Eveapi\Models\Eve\ApiKey $api_key * @param \Exception $exception * * @return bool * @throws \Exception */ public function handleApiException(JobTracking $job_tracker, ApiKey $api_key, $exception) { // Start by allowing the parent job to continue. $should_continue = true; // No matter what the error, we will increment the // Api Error Counter. $this->incrementApiErrorCount(); // Errors from the EVE API should be treated seriously. If // these are ignored, one may risk having the calling IP // banned entirely. We don't want that, so lets check // and act accordingly based on the error code. We also rely // entirely on PhealNG to pass us the proper error codes. switch ($exception->getCode()) { // Invalid contractID something. Probably the // most annoying freaking response code that // CCP has! case 135: break; // "API key authentication failure." // "API key authentication failure." case 202: // "Authentication failure." // "Authentication failure." case 203: case 204: // "Authentication failure." // "Authentication failure." case 205: // "Authentication failure." // "Authentication failure." case 210: // "Authentication failure (final pass)." // "Authentication failure (final pass)." case 212: $api_key->update(['enabled' => false, 'last_error' => $exception->getCode() . ':' . $exception->getMessage()]); $should_continue = false; break; // "Invalid Corporation Key. Key owner does not fullfill role // requirements anymore." // "Invalid Corporation Key. Key owner does not fullfill role // requirements anymore." case 220: $api_key->update(['enabled' => false, 'last_error' => $exception->getCode() . ':' . $exception->getMessage()]); $should_continue = false; break; // "Illegal page request! Please verify the access granted by the key you are using!." // "Illegal page request! Please verify the access granted by the key you are using!." case 221: // Not 100% sure how to handle this one. This call has no // access mask requirement... $api_key->update(['last_error' => $exception->getCode() . ':' . $exception->getMessage()]); break; // "Key has expired. Contact key owner for access renewal." // "Key has expired. Contact key owner for access renewal." case 222: $api_key->update(['enabled' => false, 'last_error' => $exception->getCode() . ':' . $exception->getMessage()]); $should_continue = false; break; // "Authentication failure. Legacy API keys can no longer be // used. Please create a new key on support.eveonline.com // and make sure your application supports Customizable // API Keys." // "Authentication failure. Legacy API keys can no longer be // used. Please create a new key on support.eveonline.com // and make sure your application supports Customizable // API Keys." case 223: // The API we are working with is waaaaaay too old. $api_key->update(['enabled' => false, 'last_error' => $exception->getCode() . ':' . $exception->getMessage()]); $should_continue = false; break; // "Web site database temporarily disabled." // "Web site database temporarily disabled." case 901: $this->markEveApiDown(); $should_continue = false; break; // "EVE backend database temporarily disabled."" // "EVE backend database temporarily disabled."" case 902: $this->markEveApiDown(); $should_continue = false; break; // "Your IP address has been temporarily blocked because it // is causing too many errors. See the cacheUntil // timestamp for when it will be opened again. // IPs that continually cause a lot of errors // in the API will be permanently banned, // please take measures to minimize // problematic API calls from your // application." // "Your IP address has been temporarily blocked because it // is causing too many errors. See the cacheUntil // timestamp for when it will be opened again. // IPs that continually cause a lot of errors // in the API will be permanently banned, // please take measures to minimize // problematic API calls from your // application." case 904: // Get time of IP ban in minutes, rounded up to the next whole minute $time = round(($exception->cached_until_unixtime - $exception->request_time_unixtime) / 60, 0, PHP_ROUND_HALF_UP); $this->markEveApiDown($time); $should_continue = false; break; // We got a problem we don't know what to do with, so log // and throw the exception so that the can debug it. // We got a problem we don't know what to do with, so log // and throw the exception so that the can debug it. default: throw $exception; break; } // Update the Job itself with the error information $this->reportJobError($job_tracker, $exception); return $should_continue; }
/** * @param string $filter * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function doSearchApiKey(string $filter) { $keys = ApiKey::with('info'); $keys->where(function ($query) use($filter) { $query->where('key_id', 'like', '%' . $filter . '%')->orWhere('enabled', 'like', '%' . $filter . '%')->orWhereHas('info', function ($sub_filter) use($filter) { $sub_filter->where('type', 'like', '%' . $filter . '%')->orWhere('expires', 'like', '%' . $filter . '%'); }); }); if (!auth()->user()->has('apikey.list', false)) { $keys = $keys->where('user_id', auth()->user()->id); } return $keys->get(); }
/** * Determine all channels in which an user is allowed to be * * @param SlackUser $slackUser * @param boolean $private Determine if channels should be private (group) or public (channel) * @return array */ protected function allowedChannels(SlackUser $slackUser, $private) { $channels = []; $rows = User::join('slack_channel_users', 'slack_channel_users.user_id', '=', 'users.id')->join('slack_channels', 'slack_channel_users.channel_id', '=', 'slack_channels.id')->select('channel_id')->where('users.id', $slackUser->user_id)->where('slack_channels.is_group', (int) $private)->where('slack_channels.is_general', (int) false)->union(DB::table('role_user')->join('slack_channel_roles', 'slack_channel_roles.role_id', '=', 'role_user.role_id')->join('slack_channels', 'slack_channel_roles.channel_id', '=', 'slack_channels.id')->where('role_user.user_id', $slackUser->user_id)->where('slack_channels.is_group', (int) $private)->where('slack_channels.is_general', (int) false)->select('channel_id'))->union(ApiKey::join('account_api_key_info_characters', 'account_api_key_info_characters.keyID', '=', 'eve_api_keys.key_id')->join('slack_channel_corporations', 'slack_channel_corporations.corporation_id', '=', 'account_api_key_info_characters.corporationID')->join('slack_channels', 'slack_channel_corporations.channel_id', '=', 'slack_channels.id')->where('eve_api_keys.user_id', $slackUser->user_id)->where('slack_channels.is_group', (int) $private)->where('slack_channels.is_general', (int) false)->select('channel_id'))->union(CharacterSheet::join('slack_channel_alliances', 'slack_channel_alliances.alliance_id', '=', 'character_character_sheets.allianceID')->join('slack_channels', 'slack_channel_alliances.channel_id', '=', 'slack_channels.id')->join('account_api_key_info_characters', 'account_api_key_info_characters.characterID', '=', 'character_character_sheets.characterID')->join('eve_api_keys', 'eve_api_keys.key_id', '=', 'account_api_key_info_characters.keyID')->where('eve_api_keys.user_id', $slackUser->user_id)->where('slack_channels.is_group', (int) $private)->where('slack_channels.is_general', (int) false)->select('channel_id'))->union(SlackChannelPublic::join('slack_channels', 'slack_channel_public.channel_id', '=', 'slack_channels.id')->where('slack_channels.is_group', (int) $private)->where('slack_channels.is_general', (int) false)->select('channel_id'))->get(); foreach ($rows as $row) { $channels[] = $row->channel_id; } return $channels; }