Yammer import API keys can now be overridden by the admin.

This commit is contained in:
Brion Vibber 2010-09-27 16:56:48 -07:00
parent 585c7f35ca
commit f528cc5548
7 changed files with 209 additions and 78 deletions

View File

@ -119,6 +119,7 @@ class YammerImportPlugin extends Plugin
case 'sn_yammerclient':
case 'yammerimporter':
case 'yammerrunner':
case 'yammerapikeyform':
case 'yammerauthinitform':
case 'yammerauthverifyform':
case 'yammerprogressform':

View File

@ -52,15 +52,18 @@ class YammeradminpanelAction extends AdminPanelAction
*/
function getInstructions()
{
return _m('Yammer import tool');
return _m('This Yammer import tool is still undergoing testing, ' .
'and is incomplete in some areas. ' .
'Currently user subscriptions and group memberships are not ' .
'transferred; in the future this may be supported for ' .
'imports done by verified administrators on the Yammer side.');
}
function prepare($args)
{
$ok = parent::prepare($args);
$this->init_auth = $this->trimmed('init_auth');
$this->verify_token = $this->trimmed('verify_token');
$this->subaction = $this->trimmed('subaction');
$this->runner = YammerRunner::init();
return $ok;
@ -68,26 +71,48 @@ class YammeradminpanelAction extends AdminPanelAction
function handle($args)
{
// @fixme move this to saveSettings and friends?
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->checkSessionToken();
if ($this->init_auth) {
if ($this->subaction == 'change-apikey') {
$form = new YammerApiKeyForm($this);
} else if ($this->subaction == 'apikey') {
if ($this->saveKeys()) {
$form = new YammerAuthInitForm($this, $this->runner);
} else {
$form = new YammerApiKeyForm($this);
}
} else if ($this->subaction == 'authinit') {
// hack
if ($this->arg('change-apikey')) {
$form = new YammerApiKeyForm($this);
} else {
$url = $this->runner->requestAuth();
$form = new YammerAuthVerifyForm($this, $this->runner);
return $this->showAjaxForm($form);
} else if ($this->verify_token) {
$this->runner->saveAuthToken($this->verify_token);
}
} else if ($this->subaction == 'authverify') {
$this->runner->saveAuthToken($this->trimmed('verify_token'));
// Haho! Now we can make THE FUN HAPPEN
$this->runner->startBackgroundImport();
$form = new YammerProgressForm($this, $this->runner);
return $this->showAjaxForm($form);
} else {
throw new ClientException('Invalid POST');
}
} else {
return $this->showAjaxForm($form);
}
return parent::handle($args);
}
function saveKeys()
{
$key = $this->trimmed('consumer_key');
$secret = $this->trimmed('consumer_secret');
Config::save('yammer', 'consumer_key', $key);
Config::save('yammer', 'consumer_secret', $secret);
return !empty($key) && !empty($secret);
}
function showAjaxForm($form)
@ -102,6 +127,27 @@ class YammeradminpanelAction extends AdminPanelAction
$this->elementEnd('html');
}
/**
* Fetch the appropriate form for our current state.
* @return Form
*/
function statusForm()
{
if (!(common_config('yammer', 'consumer_key'))
|| !(common_config('yammer', 'consumer_secret'))) {
return new YammerApiKeyForm($this);
}
switch($this->runner->state())
{
case 'init':
return new YammerAuthInitForm($this, $this->runner);
case 'requesting-auth':
return new YammerAuthVerifyForm($this, $this->runner);
default:
return new YammerProgressForm($this, $this->runner);
}
}
/**
* Show the Yammer admin panel form
*
@ -110,20 +156,7 @@ class YammeradminpanelAction extends AdminPanelAction
function showForm()
{
$this->elementStart('fieldset');
switch($this->runner->state())
{
case 'init':
$form = new YammerAuthInitForm($this, $this->runner);
break;
case 'requesting-auth':
$form = new YammerAuthVerifyForm($this, $this->runner);
break;
default:
$form = new YammerProgressForm($this, $this->runner);
}
$form->show();
$this->statusForm()->show();
$this->elementEnd('fieldset');
}

View File

@ -49,3 +49,7 @@
/* override */
background: none !important;
}
.magiclink {
margin-left: 40px;
}

View File

@ -0,0 +1,112 @@
<?php
class YammerApikeyForm extends Form
{
private $runner;
function __construct($out)
{
parent::__construct($out);
$this->runner = $runner;
}
/**
* ID of the form
*
* @return int ID of the form
*/
function id()
{
return 'yammer-apikey-form';
}
/**
* class of the form
*
* @return string of the form class
*/
function formClass()
{
return 'form_yammer_apikey form_settings';
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('yammeradminpanel');
}
/**
* Legend of the Form
*
* @return void
*/
function formLegend()
{
$this->out->element('legend', null, _m('Yammer API registration'));
}
/**
* Data elements of the form
*
* @return void
*/
function formData()
{
$this->out->hidden('subaction', 'apikey');
$this->out->elementStart('fieldset');
$this->out->elementStart('p');
$this->out->text(_m('Before we can connect to your Yammer network, ' .
'you will need to register the importer as an ' .
'application authorized to pull data on your behalf. ' .
'This registration will work only for your own network. ' .
'Follow this link to register the app at Yammer; ' .
'you will be prompted to log in if necessary:'));
$this->out->elementEnd('p');
$this->out->elementStart('p', array('class' => 'magiclink'));
$this->out->element('a',
array('href' => 'https://www.yammer.com/client_applications/new',
'target' => '_blank'),
_m('Open Yammer application registration form'));
$this->out->elementEnd('p');
$this->out->element('p', array(), _m('Copy the consumer key and secret you are given into the form below:'));
$this->out->elementStart('ul', array('class' => 'form_data'));
$this->out->elementStart('li');
$this->out->input('consumer_key', _m('Consumer key:'), common_config('yammer', 'consumer_key'));
$this->out->elementEnd('li');
$this->out->elementStart('li');
$this->out->input('consumer_secret', _m('Consumer secret:'), common_config('yammer', 'consumer_secret'));
$this->out->elementEnd('li');
$this->out->elementEnd('ul');
$this->out->submit('submit', _m('Save'), 'submit', null, _m('Save these consumer keys'));
$this->out->elementEnd('fieldset');
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
}
}

View File

@ -22,7 +22,7 @@ class YammerAuthInitForm extends Form
function formClass()
{
return 'form_yammer_auth_init';
return 'form_yammer_auth_init form_settings';
}
@ -56,7 +56,12 @@ class YammerAuthInitForm extends Form
function formData()
{
$this->out->hidden('init_auth', '1');
$this->out->hidden('subaction', 'authinit');
$this->out->elementStart('fieldset');
$this->out->submit('submit', _m('Start authentication'), 'submit', null, _m('Request authorization to connect to Yammer account'));
$this->out->submit('change-apikey', _m('Change API key'));
$this->out->elementEnd('fieldset');
}
/**
@ -67,6 +72,5 @@ class YammerAuthInitForm extends Form
function formActions()
{
$this->out->submit('submit', _m('Connect to Yammer'), 'submit', null, _m('Request authorization to connect to Yammer account'));
}
}

View File

@ -30,7 +30,7 @@ class YammerAuthVerifyForm extends Form
function formClass()
{
return 'form_yammer_auth_verify';
return 'form_yammer_auth_verify form_settings';
}
@ -64,27 +64,39 @@ class YammerAuthVerifyForm extends Form
function formData()
{
$this->out->hidden('subaction', 'authverify');
$this->out->elementStart('fieldset');
$this->out->elementStart('p');
$this->out->text(_m('Follow this link to confirm authorization at Yammer; you will be prompted to log in if necessary:'));
$this->out->elementEnd('p');
$this->out->elementStart('blockquote');
$this->out->element('a',
array('href' => $this->runner->getAuthUrl(),
'target' => '_blank'),
_m('Open Yammer authentication window'));
$this->out->elementEnd('blockquote');
$this->out->element('p', array(), _m('Copy the verification code you are given into the form below:'));
$this->out->input('verify_token', _m('Verification code:'));
// iframe would be nice to avoid leaving -- since they don't seem to have callback url O_O
/*
$this->out->element('iframe', array('id' => 'yammer-oauth',
'src' => $this->runner->getAuthUrl()));
*/
// yeah, it ignores the callback_url
// soo... crappy link. :(
$this->out->elementStart('p', array('class' => 'magiclink'));
$this->out->element('a',
array('href' => $this->runner->getAuthUrl(),
'target' => '_blank'),
_m('Open Yammer authentication window'));
$this->out->elementEnd('p');
$this->out->element('p', array(), _m('Copy the verification code you are given below:'));
$this->out->elementStart('ul', array('class' => 'form_data'));
$this->out->elementStart('li');
$this->out->input('verify_token', _m('Verification code:'));
$this->out->elementEnd('li');
$this->out->elementEnd('ul');
$this->out->submit('submit', _m('Continue'), 'submit', null, _m('Save code and begin import'));
$this->out->elementEnd('fieldset');
}
/**
@ -95,6 +107,5 @@ class YammerAuthVerifyForm extends Form
function formActions()
{
$this->out->submit('submit', _m('Verify code'), 'submit', null, _m('Verification code'));
}
}

View File

@ -1,34 +0,0 @@
<?php
if (php_sapi_name() != 'cli') {
die('no');
}
define('INSTALLDIR', dirname(dirname(dirname(dirname(__FILE__)))));
require INSTALLDIR . "/scripts/commandline.inc";
// temp stuff
require 'yam-config.php';
$yam = new SN_YammerClient($consumerKey, $consumerSecret, $token, $tokenSecret);
$imp = new YammerImporter($yam);
/*
$data = $yam->users();
var_dump($data);
// @fixme follow paging
foreach ($data as $item) {
$user = $imp->prepUser($item);
var_dump($user);
}
*/
$data = $yam->messages(array('newer_than' => 1));
var_dump($data);
// @fixme follow paging
$messages = $data['messages'];
$messages = array_reverse($messages);
foreach ($messages as $message) {
$notice = $imp->prepNotice($message);
var_dump($notice);
}