public function fromSteam($steamId64) { $steamUser = $this->steamUser->getUser($steamId64); // Create new user if they are not found in the database if (!($user = User::where('steam_id_64', '=', $steamId64)->first())) { $user = new User(); } $user->username = $steamUser->username; $user->steam_id_64 = $steamUser->id; $user->steam_visibility = $steamUser->visibility; $user->visible = true; // imported users visible by default $user->avatar = $steamUser->avatar_url; $user->ip = Request::server('REMOTE_ADDR'); if ($user->save()) { Event::fire('lanager.users.store', $user); // todo: use service return $user; } throw new UserImportException($user->errors()); // Cast to string as JSON }
/** * Perform actions after new user has been stored * @param BaseModel $user User that has just been stored */ public function onStore($user) { // Make the first user SuperAdmin if (count(User::all()) == 1 && !$user->hasRole('Super Admin')) { $user->roles()->attach(Role::where('name', '=', 'Super Admin')->firstOrFail()); Log::debug(e($user->username) . ': Assigning "Super Admin" role as first user to log in', [$user->api_key]); } // Generate an API key if the user does not have one if (empty($user->api_key)) { $user->api_key = md5(str_random(32)); $user->save(); Log::debug(e($user->username) . ': Generating API key', [$user->api_key]); } }
/** * Authenticate a user's request using their API key * @param Request $request Request to the app * @param Route $route Route into the app * @return BaseModel User object if successfully authenticated * @throws BadHttpException When API key format incorrect * @throws UnauthorizedHttpException When no user found with given API key */ public function authenticate(Request $request, Route $route) { // verify that Authorization header is for this provider $this->validateAuthorizationHeader($request); // extract API key from header $apikey = trim(strstr($request->header('authorization'), ' ')); // verify that api key is 32 char hexadecimal string if (strlen($apikey) != 32) { throw new UnauthorizedHttpException(null, 401); } if (!ctype_xdigit($apikey)) { throw new UnauthorizedHttpException(null, 401); } // attempt to find user in database with given api key try { $user = User::where('api_key', $apikey)->firstOrFail(); // Log the user in once for this request without making a session so that authority lib can work Auth::onceUsingId($user->id); return $user; } catch (ModelNotFoundException $e) { // reject auth if no user found with given key throw new UnauthorizedHttpException(null, 401); } }
/** * Execute the console command. * */ public function fire() { $users = User::where('visible', 1)->get()->lists('steam_id_64'); if (count($users) == 0) { $this->info('No users in database'); return; } $this->info('Requesting current status of ' . count($users) . ' users from Steam'); $steamUserInterface = $this->steamUserInterface->getUsers($users); if (count($steamUserInterface) < count($users)) { $this->error('Steam responded with ' . (count($users) - count($steamUserInterface)) . ' fewer users than requested'); } $successCount = 0; $failureCount = 0; $missingApps = []; foreach ($steamUserInterface as $steamUser) { $currentApplication = NULL; $currentServer = NULL; try { // Find the user to which the state belongs to $user = User::where('steam_id_64', $steamUser->id)->first(); // Update user details with steam details if they have changed if ($user->username != $steamUser->username) { $user->username = $steamUser->username; } if ($user->avatar != $steamUser->avatar_url) { $user->avatar = $steamUser->avatar_url; } if ($user->steam_visibility != $steamUser->visibility) { $user->steam_visibility = $steamUser->visibility; } $user->save(); // If the user is currently running an app if (is_numeric($steamUser->current_app_id)) { // Find database application ID $currentApplication = Application::where('steam_app_id', $steamUser->current_app_id)->first(); if (!count($currentApplication)) { $missingApps[] = $steamUser->current_app_id; $currentApplication = NULL; } } // If the user is currently running an app and connected to a server if ($currentApplication && isset($steamUser->current_server_ip)) { // Find database server ID $currentServer = Server::where('address', $steamUser->current_server_ip)->where('port', $steamUser->current_server_port)->first(); // Create server if it does not already exist if (!count($currentServer)) { $currentServer = new Server(); $currentServer->application_id = $currentApplication->id; $currentServer->address = $steamUser->current_server_ip; $currentServer->port = $steamUser->current_server_port; $currentServer->save(); } } // Create a new state $state = new State(); $state->user_id = $user->id; $state->status = $steamUser->status; if ($currentApplication) { $state->application_id = $currentApplication->id; } if ($currentServer) { $state->server_id = $currentServer->id; } $state->save(); $successCount++; // Only incremented if no exceptions above } catch (\Exception $e) { $this->error('Unable to insert user state for user [' . $steamUser->id . ']: ' . $e->getMessage()); $failureCount++; } } // Provide info on results if ($successCount > 0) { $this->info($successCount . ' Steam user states successfully imported'); } if (count($missingApps) > 0) { $this->error(count($missingApps) . ' ' . str_plural('application', count($missingApps)) . ' missing from local database - Please run "steam:import-apps"'); } }
/** * Transform resource into standard output format with correct typing * @param object BaseModel Resource being transformed * @return array Transformed object array ready for output */ public function transform(User $user) { return ['id' => (int) $user->id, 'username' => $user->username, 'steam_id_64' => $user->steam_id_64, 'ip' => $user->ip, 'avatar' => $user->avatar, 'avatar_small' => $user->present()->avatarSmall(), 'avatar_medium' => $user->present()->avatarMedium(), 'avatar_large' => $user->present()->avatarLarge(), 'links' => [['rel' => 'self', 'uri' => url() . '/users/' . $user->id]]]; }