public function updateCMSFields(FieldList $fields) { $redirectUrl = HailProvider::getRedirectUri(); $redirectField = new ReadonlyField('RedirectURL', 'Redirect URL', $redirectUrl); // Twitter setup $fields->addFieldsToTab('Root.Hail', array(new TextField('HailClientID', 'Client ID', null, 255), new TextField('HailClientSecret', 'Client Secret', null, 255), $redirectField)); $siteconfig = SiteConfig::current_site_config(); if (HailProvider::isReadyToAuthorised()) { $provider = new HailProvider(); $link = HailProvider::isAuthorised() ? 'Reauthorise SilverStripe to Access Hail' : 'Authorise SilverStripe to Access Hail'; $auth = $provider->getAuthorizationUrl(); $fields->addFieldsToTab('Root.Hail', new LiteralField('Go', "<a href='{$auth}'>{$link}</a>")); } try { if (HailProvider::isAuthorised()) { $orgs = HailApi::getOrganisationList(); $orgs[''] = ''; $orgField = DropdownField::create('HailOrgID', 'Hail Organisation', $orgs); $fields->addFieldsToTab('Root.Hail', $orgField); } } catch (HailApiException $ex) { $fields->addFieldsToTab('Root.Hail', new LiteralField('Retry', 'You Have to Re-Authorise SilverStripe to Access Hail')); } $holderField = DropdownField::create('PrimaryHailHolderID', 'Primary Hail Holder', HailHolder::get()->map('ID', 'Title')); $holderField->setEmptyString('(None)'); $fields->addFieldsToTab('Root.Hail', $holderField); }
public function index() { $siteconfig = SiteConfig::current_site_config(); if ($siteconfig->canEdit()) { $siteconfig->HailRedirectCode = $_GET['code']; $provider = new HailProvider(); try { $token = $provider->getAccessToken('authorization_code', ['code' => $siteconfig->HailRedirectCode]); } catch (Exception $ex) { die($ex->getMessage()); } $siteconfig->HailAccessToken = $token->accessToken; $siteconfig->HailAccessTokenExpire = $token->expires; $siteconfig->HailRefreshToken = $token->refreshToken; $siteconfig->write(); // Refresh site config and save the user id $user = HailApi::getUser(); $siteconfig = SiteConfig::current_site_config(); $siteconfig->HailUserID = $user->id; $siteconfig->write(); } $this->redirect('admin/settings'); }
/** * Send a GET request to the Hail API for a specific URI and returns * the results. Extra paremeters can be specified via $request. * * @param string $uri ressource to get * @param StdClass $request Body of the request to send to the Hail API. * @return StdClass Reply from Hail * @throws HailApiException */ protected static function get($uri, $request = false) { // Initialise request $response = Request::get(static::config()->Url . $uri)->addHeader('Authorization', 'Bearer ' . HailProvider::getHailAccessToken())->timeoutIn(static::config()->Timeout); // If we have a request body if ($request) { $response->sendsJson()->body(json_encode($request)); } // Send the request and catch any Comms Exception try { $reply = $response->send(); } catch (ConnectionErrorException $ex) { throw new HailApiException($ex->getMessage(), 0, $ex); } // Validate the response we get from Hail if (!empty($reply->body->error->message)) { throw new HailApiException($reply->body->error->message, $reply->code); } return $reply->body; }