public static function withCredentials() { $usernames = static::all()->toArray(); if ($usernames) { foreach ($usernames as $key => $user) { $username = static::first($user['id'])->toArray(); $userProfile = UsersProfilesModel::first(array('user_id' => $user['id'])); if ($userProfile) { $userProfile = $userProfile->toArray(); } $credentials = UsersCredentialsModel::first(array('user_id' => $user['id'])); if ($credentials) { $credentials = $credentials->toArray(); } $services = ServicesModel::forUser(array('id' => $user['id'])); $usernames[$key] = compact('username', 'userProfile', 'credentials', 'services'); } } return $usernames; }
public static function oAuthRefreshToken($user = array(), $updateSession = false, $force = false) { $expires = time() + 1000; if (isset($user['credentials']['expires_at'])) { $expires = strtotime($user['credentials']['expires_at']); } if ($expires < time() || $force) { $payload = array('client_id' => OAUTH_CLIENT_ID, 'client_secret' => OAUTH_CLIENT_SECRET, 'grant_type' => 'refresh_token', 'refresh_token' => $user['credentials']['refresh_token']); $refreshedToken = Rest::post('https://accounts.google.com/o/oauth2/token', $payload); if ($refreshedToken) { // Credentials $credentials = UsersCredentialsModel::first(array('user_id' => $user['username']['id'])); $credentials->access_token = $refreshedToken['access_token']; $credentials->token_type = $refreshedToken['token_type']; $credentials->expires_at = date(DATE_TIME, time() + $refreshedToken['expires_in']); $credentials->save(); // Refresh session data if ($updateSession) { $_SESSION['usernames'][$user['username']['id']]['credentials']['access_token'] = $credentials->access_token; $_SESSION['usernames'][$user['username']['id']]['credentials']['expires_at'] = $credentials->expires_at; if ($_SESSION['current']['username']['id'] == $user['username']['id']) { $_SESSION['current'] = $_SESSION['usernames'][$user['username']['id']]; } } $user['credentials']['access_token'] = $credentials->access_token; $user['credentials']['expires_at'] = $credentials->expires_at; } return $refreshedToken; } return $user; }