Browse Source

First commit

Frederic G. MARAND 10 years ago
commit
5d293c6b6d
4 changed files with 305 additions and 0 deletions
  1. 9 0
      janrain_views.info
  2. 77 0
      janrain_views.module
  3. 136 0
      lib/Drupal/janrain_views/Api.php
  4. 83 0
      lib/Drupal/janrain_views/EntityType.php

+ 9 - 0
janrain_views.info

@@ -0,0 +1,9 @@
+name = Janrain Views
+description = "Implements Janrain API with Views"
+package = SSO
+
+dependencies[] = xautoload
+dependencies[] = janrain_capture
+dependencies[] = views
+core = 7.x
+php = 5.3

+ 77 - 0
janrain_views.module

@@ -0,0 +1,77 @@
+<?php
+use Drupal\janrain_views\Api;
+
+use Drupal\janrain_views\EntityType;
+
+/**
+ * @file
+ * Empty file needed for Drupal to register the module.
+ */
+
+function janrain_views_menu() {
+  $items = array();
+
+  $items['jv/entityType'] = array(
+    'title' => 'Entity types',
+    'page callback' => 'janrain_views_page_entityType_list',
+    'access arguments' => array('administer site configuration'),
+  );
+  $items['jv/entityType/%jv_entity_type'] = array(
+    'title' => 'Entity type info',
+    'page callback' => 'janrain_views_page_entityType',
+    'page arguments' => array(2),
+    'access arguments' => array('administer site configuration'),
+  );
+  $items['jv/entityType/%jv_entity_type/access'] = array(
+    'title' => 'Entity type access info',
+    'page callback' => 'janrain_views_page_entityType_accessSchema',
+    'page arguments' => array(2),
+    'access arguments' => array('administer site configuration'),
+  );
+  $items['jv/settings/keys'] = array(
+    'title' => 'Settings/keys',
+    'page callback' => 'janrain_views_page_settings_keys',
+    'access arguments' => array('administer site configuration'),
+  );
+
+  return $items;
+}
+
+function janrain_views_admin_pages() {
+  $ret = array(
+    'jv/*' => TRUE,
+  );
+  return $ret;
+}
+
+function janrain_views_page_entityType_list() {
+  EntityType::bind(new Api());
+  $ret = EntityType::getList();
+  dsm($ret);
+  return "fo";
+}
+
+function jv_entity_type_load($name) {
+  return filter_xss($name);
+}
+
+function janrain_views_page_entityType($type_name) {
+  EntityType::bind(new Api());
+  $ret = EntityType::getEntityType($type_name);
+  dsm($ret);
+  return $ret;
+}
+
+function janrain_views_page_entityType_accessSchema($type_name) {
+  EntityType::bind(new Api());
+  $ret = EntityType::getAccessSchema($type_name);
+  dsm($ret);
+  return $ret;
+}
+
+function janrain_views_page_settings_keys() {
+  $api = new Api();
+  $ret = $api('settings/keys');
+  dsm($ret);
+  return "<pre>" . var_export($ret, TRUE) . "</pre>";
+}

+ 136 - 0
lib/Drupal/janrain_views/Api.php

@@ -0,0 +1,136 @@
+<?php
+namespace Drupal\janrain_views;
+
+class Api {
+  public $ver;
+  public $clientId;
+  public $clientSecret;
+  public $captureAddress;
+  public $captureUrl;
+
+  /**
+   * Retrieves API access credentials from settings.
+   */
+  function __construct() {
+    $this->ver = variable_get('janrain_capture_ver', array());
+
+    if ($this->ver == '1.0') {
+      $main = variable_get('janrain_capture_main', array());
+    }
+    else {
+      $main = variable_get('janrain_capture_main2', array());
+    }
+dsm($main, __METHOD__);
+    $this->clientId = isset($main['capture_client_id'])
+        ? $main['capture_client_id']
+        : '';
+
+    $this->clientSecret = isset($main['capture_client_secret'])
+        ? $main['capture_client_secret']
+        : '';
+
+    $this->captureAddress = !empty($main['capture_address'])
+      ? str_replace('https://', '', $main['capture_address'])
+      : '';
+  }
+
+  public function defaultArgs() {
+    $ret = array(
+      'client_id' => $this->clientId,
+      'client_secret' => $this->clientSecret,
+    );
+
+    return $ret;
+  }
+
+  /**
+   * Performs the HTTP request.
+   *
+   * @param string $command
+   *   The Capture command to perform
+   * @param array $args
+   *   The data set to pass via POST
+   * @param string $access_token
+   *   The client access token to use when performing user-specific calls
+   *
+   * @return mixed
+   *   The HTTP request result data
+   */
+  public function __invoke($command, array $args = array(), $access_token = NULL) {
+    $url = "https://{$this->captureAddress}/$command";
+
+    $headers = array(
+      'Content-Type' => 'application/x-www-form-urlencoded',
+      'User-Agent' => 'Drupal',
+    );
+
+    if (isset($access_token)) {
+      $headers['Authorization'] = "OAuth $access_token";
+    }
+
+    $options = array('headers' => $headers);
+
+    $args = array_merge($args, $this->defaultArgs());
+    $options['method'] = 'POST';
+    $options['data'] = http_build_query($args, '', '&');
+
+    dsm($options, $url);
+    $result = drupal_http_request($url, $options);
+
+    if (!isset($result->data) || $result->code != '200') {
+      $this->reportError($result);
+      return FALSE;
+    }
+
+    $data = json_decode($result->data, TRUE);
+
+    // NULL decoded value indicates a parse error.
+    if (!isset($data)) {
+      $data['stat'] = 'error';
+      $data['code'] = '0';
+      $data['error'] = t('JSON parse error for: %data.', array('%data' => $result->data));
+    }
+    if ($data['stat'] == 'error') {
+      $error = (object) array(
+        'code' => $data['code'],
+        'error' => t('@error: @description', array(
+          '@error' => $data['error'],
+          '@description' => $data['error_description'],
+        )),
+      );
+      $this->reportError($error);
+      return FALSE;
+    }
+
+    return $data;
+  }
+
+  /**
+   * Helper function for the Engage web API wrappers.
+   *
+   * @param stdClass $result
+   *   Result containing error code and message
+   */
+  public function reportError($result) {
+    $args = array(
+      '%code' => $result->code,
+      '%error' => $result->error,
+    );
+    watchdog('janrain_capture', 'Capture web API seems to be inaccessible due to "%code - %error".', $args, WATCHDOG_WARNING);
+    drupal_set_message(t('Capture web API seems to be inaccessible because of error "%code - %error".', $args), 'error');
+  }
+}
+
+
+/*
+use Drupal\janrain_views\Api.php;
+
+$path = drupal_get_path('module', 'janrain_views');
+require_once("$path/lib/Drupal/janrain_views/Api.inc");
+
+$j = new Api();
+dsm($j);
+$s = $j('entityType', array('type_name' => 'user'));
+dsm($s);
+
+*/

+ 83 - 0
lib/Drupal/janrain_views/EntityType.php

@@ -0,0 +1,83 @@
+<?php
+namespace Drupal\janrain_views;
+
+class EntityType {
+
+  /**
+   *
+   * @var Api
+   */
+  public static $api;
+
+  public static function bind(Api $api) {
+    static::$api = $api;
+  }
+
+  /**
+   * Janrain entityType/list
+   *
+   * Cannot use the "list" name as it is a PHP keyword.
+   */
+  public static function getList()  {
+    $api = static::$api;
+    $ret = $api('entityType.list');
+    return $ret;
+  }
+
+  public static function getEntityType($type_name = 'user') {
+    $api = static::$api;
+    $result = $api('entityType', array('type_name' => $type_name));
+    if ($result['stat'] == 'ok') {
+      $name = $result['schema']['name'];
+      $rows = array();
+      foreach ($result['schema']['attr_defs'] as $typeInfo) {
+        $row = array();
+        $row['name'] = $typeInfo['name'];
+        $row['type'] = $typeInfo['type'];
+        $row['description'] = isset($typeInfo['description']) ? $typeInfo['description'] : NULL;
+        unset($typeInfo['name'], $typeInfo['type'], $typeInfo['description']);
+        $items = array();
+        foreach ($typeInfo as $k => $v) {
+          $items[] = t('@k: @v', array('@k' => $k, '@v' => print_r($v, TRUE)));
+        }
+        $row['other'] = theme('item_list', array('items' => $items));
+        $rows[] = $row;
+      }
+      $header = array(
+        t('Name'),
+        t('Type'),
+        t('Description'),
+        t('Other'),
+      );
+      $ret = array(
+        '#theme' => 'table',
+        '#header' => $header,
+        '#rows' => $rows,
+      );
+    }
+    else {
+      $ret = t('Operation did not succeed.');
+    }
+    return $ret;
+  }
+
+  public static function getAccessSchema($type_name = 'user', $client_id = NULL) {
+    $api = static::$api;
+    if (empty($client_id)) {
+      $client_id = $api->clientId;
+    }
+    $read = $api('entityType.getAccessSchema', array(
+      'type_name' => $type_name,
+      'for_client_id' => $client_id,
+      'access_type' => 'read',
+    ));
+    dsm($read);
+
+    $write = $api('entityType.getAccessSchema', array(
+      'type_name' => $type_name,
+      'for_client_id' => $client_id,
+      'access_type' => 'read',
+    ));
+    dsm($write);
+  }
+}