oauth_common.consumers.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Menu system callback for listing a users consumers.
  4. *
  5. * @param object $account
  6. */
  7. function oauth_common_page_user_consumers($account) {
  8. module_load_include('inc', 'oauth_common');
  9. $ci = oauth_common_user_consumers($account->uid);
  10. $header = array(
  11. array('data' => t('Name'), 'class' => array('oauth-common-consumer-name')),
  12. array('data' => t('Key'), 'class' => array('oauth-common-consumer-key')),
  13. array('data' => t('Created'), 'class' => array('oauth-common-consumer-created')),
  14. array('data' => t('Operations'), 'class' => array('oauth-common-consumer-operations')),
  15. );
  16. $rows = array();
  17. foreach ($ci as $consumer) {
  18. $data = array(
  19. 'name' => array(
  20. 'data' => $consumer->name,
  21. 'class' => array('oauth-common-consumer-name'),
  22. ),
  23. 'key' => array(
  24. 'data' => substr($consumer->key, 0, 6) . '...',
  25. 'class' => array('oauth-common-consumer-key'),
  26. ),
  27. 'created' => array(
  28. 'data' => format_date($consumer->created),
  29. 'class' => array('oauth-common-consumer-created'),
  30. ),
  31. );
  32. $operations = array();
  33. if (oauth_common_can_edit_consumer($consumer)) {
  34. $operations[] = array(
  35. 'title' => t('Edit'),
  36. 'href' => sprintf('user/%d/oauth/consumer/%s', $account->uid, $consumer->csid),
  37. );
  38. $operations[] = array(
  39. 'title' => t('Delete'),
  40. 'href' => sprintf('user/%d/oauth/consumer/%s', $account->uid, $consumer->csid) . '/delete',
  41. );
  42. }
  43. $rows[] = array(
  44. 'data' => $data + array(
  45. 'operations' => array(
  46. 'data' => theme('links', array('links' => $operations)),
  47. 'class' => array('oauth-common-consumer-operations'),
  48. ),
  49. ),
  50. 'class' => array('oauth-common-consumer'),
  51. );
  52. }
  53. $table = theme('table', array(
  54. 'header' => $header,
  55. 'rows' => $rows,
  56. 'attributes' => array('id' => 'oauth-common-list-consumers')
  57. ));
  58. return $table;
  59. }
  60. /**
  61. * Menu system callback for the add consumer page.
  62. */
  63. function oauth_common_add_consumer($account) {
  64. $consumer = new DrupalOAuthConsumer(user_password(32), user_password(32), array(
  65. 'callback_url' => '',
  66. 'uid' => $account->uid,
  67. 'provider_consumer' => TRUE,
  68. ));
  69. return drupal_get_form('oauth_common_form_consumer', $consumer);
  70. }
  71. /**
  72. * Menu system callback for the edit consumer page.
  73. */
  74. function oauth_common_edit_consumer($consumer) {
  75. return drupal_get_form('oauth_common_form_consumer', $consumer);
  76. }
  77. /**
  78. * The consumer form that is shared by the add and edit page.
  79. */
  80. function oauth_common_form_consumer($form, &$form_state, $consumer) {
  81. $form = array();
  82. $form['consumer_object'] = array(
  83. '#type' => 'value',
  84. '#value' => $consumer,
  85. );
  86. $form['name'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Consumer name'),
  89. '#required' => TRUE,
  90. '#default_value' => $consumer->name,
  91. );
  92. $form['callback_url'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('Callback url'),
  95. '#required' => FALSE,
  96. '#default_value' => $consumer->callback_url,
  97. );
  98. if ($consumer->in_database) {
  99. $contexts = oauth_common_context_list();
  100. $form['context'] = array(
  101. '#type' => 'item',
  102. '#title' => t('Application context'),
  103. '#markup' => isset($contexts[$consumer->context]) ? $contexts[$consumer->context] : $consumer->context,
  104. );
  105. }
  106. else {
  107. $allowed_contexts = array();
  108. foreach (oauth_common_context_list() as $context => $title) {
  109. if (user_access(sprintf('oauth register consumers in %s', $context))) {
  110. $allowed_contexts[$context] = $title;
  111. }
  112. }
  113. $form['context'] = array(
  114. '#type' => 'select',
  115. '#title' => t('Application context'),
  116. '#options' => $allowed_contexts,
  117. '#default_value' => $consumer->context,
  118. );
  119. }
  120. if ($consumer->in_database) {
  121. $form['key'] = array(
  122. '#type' => 'item',
  123. '#title' => t('Key'),
  124. '#markup' => $consumer->key,
  125. );
  126. $form['secret'] = array(
  127. '#type' => 'item',
  128. '#prefix' => '<div id="consumer-secret-wrapper">',
  129. '#title' => t('Secret'),
  130. '#markup' => substr($consumer->secret, 0, 6) . '...',
  131. );
  132. $form['show_secret'] = array(
  133. '#type' => 'button',
  134. '#value' => t('Show secret'),
  135. '#ajax' => array(
  136. 'callback' => 'oauth_common_form_consumer_secret_ajax_callback',
  137. 'wrapper' => 'consumer-secret-wrapper',
  138. ),
  139. '#suffix' => '</div>',
  140. );
  141. }
  142. $form['actions'] = array('#type' => 'actions');
  143. $form['actions']['submit'] = array(
  144. '#type' => 'submit',
  145. '#value' => t('Save'),
  146. );
  147. return $form;
  148. }
  149. /**
  150. * AJAX callback for showing secrets
  151. *
  152. * @return void
  153. */
  154. function oauth_common_form_consumer_secret_ajax_callback($form, $form_state) {
  155. $form['secret']['#markup'] = $form_state['values']['consumer_object']->secret;
  156. $form['secret']['#prefix'] = '';
  157. return $form['secret'];
  158. }
  159. /**
  160. * Submit handler for oauth_common_form_consumer.
  161. */
  162. function oauth_common_form_consumer_submit($form, &$form_state) {
  163. $values = $form_state['values'];
  164. $consumer = $values['consumer_object'];
  165. // Translate empty callback url to oob (out of band).
  166. if (empty($values['callback_url'])) {
  167. $values['callback_url'] = 'oob';
  168. }
  169. // Add scheme if missing, and if the callback_url isn't out of band.
  170. else if ($values['callback_url'] != 'oob' && preg_match('/^http:\/\/|https:\/\//', $values['callback_url']) === 0) {
  171. //TODO: What about custom callback url:s used by eg iphone-apps? We should allow them - right?
  172. $values['callback_url'] = 'http://' . $values['callback_url'];
  173. }
  174. // Remove trailing slash
  175. $values['callback_url'] = rtrim($values['callback_url'], '/');
  176. // Transfer editable attributes to the consumer.
  177. $names = array('name', 'callback_url', 'context');
  178. foreach ($names as $name) {
  179. if (isset($values[$name])) {
  180. $consumer->$name = $values[$name];
  181. }
  182. }
  183. // Update or create the consumer.
  184. $update = $consumer->in_database;
  185. $consumer->write();
  186. if ($update) {
  187. drupal_set_message(t('Updated the consumer @name', array('@name' => $values['name'])));
  188. }
  189. else {
  190. drupal_set_message(t('Added the consumer @name', array('@name' => $values['name'])));
  191. }
  192. drupal_goto(sprintf('user/%d/oauth/consumers', $consumer->uid));
  193. }
  194. /**
  195. * Consumer deletion form.
  196. */
  197. function oauth_common_form_consumer_delete($form_state, $consumer) {
  198. $form = array(
  199. 'consumer_object' => array(
  200. '#type' => 'value',
  201. '#value' => $consumer,
  202. ),
  203. 'confirm' => array(
  204. '#type' => 'item',
  205. '#markup' => t('Are you sure you want to delete application <strong>@a</strong>?', array('@a' => $consumer->name)),
  206. ),
  207. 'actions' => array(
  208. '#type' => 'actions',
  209. 'delete' => array(
  210. '#type' => 'submit',
  211. '#title' => t('Delete'),
  212. '#default_value' => t('Delete'),
  213. )
  214. ),
  215. );
  216. return $form;
  217. }
  218. /**
  219. * Submit handler for oauth_common_form_consumer_delete.
  220. */
  221. function oauth_common_form_consumer_delete_submit($form, &$form_state) {
  222. $consumer = $form_state['values']['consumer_object'];
  223. $consumer->delete();
  224. drupal_set_message(t('Deleted the consumer @name', array('@name' => $consumer->name)));
  225. drupal_goto(sprintf('user/%d/oauth/consumers', $consumer->uid));
  226. }