oauth_common.authorizations.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @file
  4. * Functions related to a user's authorization section
  5. */
  6. function oauth_common_page_user_authorizations($account) {
  7. $header = array(
  8. array('data' => t('Application'), 'class' => array("oauth-common-authorization-application")),
  9. array('data' => t('Key'), 'class' => array("oauth-common-authorization-key")),
  10. array('data' => t('Created'), 'class' => array("oauth-common-authorization-created")),
  11. array('data' => t('Expires'), 'class' => array("oauth-common-authorization-expires")),
  12. array('data' => t('Operations'), 'class' => array("oauth-common-authorization-operations")),
  13. );
  14. $access_tokens = oauth_common_get_user_provider_tokens($account->uid);
  15. $rows = array();
  16. foreach ($access_tokens as $token) {
  17. $consumer = $token->consumer;
  18. $data = array(
  19. 'application' => array(
  20. 'data' => check_plain($consumer->name),
  21. 'class' => array("oauth-common-authorization-application"),
  22. ),
  23. 'key' => array(
  24. 'data' => substr($token->key, 0, 6) . '...',
  25. 'class' => array("oauth-common-authorization-key"),
  26. ),
  27. 'created' => array(
  28. 'data' => format_date($token->created),
  29. 'class' => array("oauth-common-authorization-created"),
  30. ),
  31. );
  32. $operations = array();
  33. $operations[] = array(
  34. 'title' => t('Edit'),
  35. 'href' => sprintf('user/%d/oauth/authorizations/%s', $account->uid, $token->tid),
  36. 'query' => array('destination' => $_GET['q']),
  37. );
  38. $operations[] = array(
  39. 'title' => t('Delete'),
  40. 'href' => sprintf('user/%d/oauth/authorizations/%s', $account->uid, $token->tid) . '/delete',
  41. 'query' => array('destination' => $_GET['q']),
  42. );
  43. $data['expires'] = array(
  44. 'data' => $token->expires ? format_date($token->expires) : t('Never'),
  45. 'class' => array("oauth-common-authorization-expires"),
  46. );
  47. $rows[] = array(
  48. 'data' => $data + array(
  49. 'operations' => array(
  50. 'data' => theme('links', array('links' => $operations)),
  51. 'class' => array("oauth-common-authorization-operations"),
  52. ),
  53. ),
  54. );
  55. }
  56. $table = theme('table', array(
  57. 'header' => $header,
  58. 'rows' => $rows,
  59. 'attributes' => array('id' => 'oauth-common-list-authorization')
  60. ));
  61. return $table;
  62. }
  63. function oauth_common_authorization_add($consumer) {
  64. $token = new DrupalOAuthToken(user_password(32), user_password(32), $consumer, array(
  65. 'uid' => $account->uid,
  66. ));
  67. return drupal_get_form('oauth_common_form_authorization', $token);
  68. }
  69. /**
  70. * Provide a form to edit and add authorizations.
  71. *
  72. * Despite what appears above, this function is actually invoked by
  73. * `drupal_retrieve_form` (by way of `drupal_build_form`, by way of
  74. * `drupal_get_form`), so the second argument isn't the token, but a reference
  75. * to the form state. Luckily, PHP made that incredibly non-obvious by
  76. * neglecting to notify me that this function was being called with one too few
  77. * arguments. Go team.
  78. */
  79. function oauth_common_form_authorization($form_id, &$form_state, $token) {
  80. $form = array();
  81. $consumer = $token->consumer;
  82. $context = oauth_common_context_load($consumer->context);
  83. drupal_set_title(t('Authorization for @app', array('@app' => $consumer->name)), PASS_THROUGH);
  84. $form['token_object'] = array(
  85. '#type' => 'value',
  86. '#value' => $token,
  87. );
  88. $form['authorized'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Authorized'),
  91. '#default_value' => $token->authorized,
  92. );
  93. $form['created'] = array(
  94. '#type' => 'item',
  95. '#title' => t('Created'),
  96. '#markup' => format_date($token->created),
  97. );
  98. $form['changed'] = array(
  99. '#type' => 'item',
  100. '#title' => t('Changed'),
  101. '#markup' => format_date($token->changed),
  102. );
  103. $form['key'] = array(
  104. '#type' => 'item',
  105. '#title' => t('Key'),
  106. '#markup' => $token->key,
  107. );
  108. if ($token->in_database) {
  109. $form['secret'] = array(
  110. '#type' => 'item',
  111. '#prefix' => '<div id="token-secret-wrapper">',
  112. '#title' => t('Secret'),
  113. '#markup' => substr($token->secret, 0, 6) . '...',
  114. );
  115. $form['show_secret'] = array(
  116. '#type' => 'button',
  117. '#value' => t('Show secret'),
  118. '#ajax' => array(
  119. 'callback' => 'oauth_common_form_authorization_secret_ajax_callback',
  120. 'wrapper' => 'token-secret-wrapper',
  121. ),
  122. '#suffix' => '</div>',
  123. );
  124. }
  125. else {
  126. $form['secret'] = array(
  127. '#type' => 'item',
  128. '#title' => t('Secret'),
  129. '#markup' => $token->secret
  130. );
  131. }
  132. $form['allowed'] = array(
  133. '#type' => 'fieldset',
  134. '#title' => t('Permissions'),
  135. );
  136. global $user;
  137. oauth_common_permissions_form($user, $form['allowed'], $consumer, $context, $token->services);
  138. $form['actions'] = array('#type' => 'actions');
  139. $form['actions']['submit'] = array(
  140. '#type' => 'submit',
  141. '#value' => t('Save'),
  142. );
  143. return $form;
  144. }
  145. /**
  146. * AJAX callback for showing secrets
  147. *
  148. * @return void
  149. */
  150. function oauth_common_form_authorization_secret_ajax_callback($form, $form_state) {
  151. $form['secret']['#markup'] = $form_state['values']['token_object']->secret;
  152. $form['secret']['#prefix'] = '';
  153. return $form['secret'];
  154. }
  155. function oauth_common_permissions_form($account, &$form, $consumer, $context, $default_services = array('*')) {
  156. $tvars = array(
  157. '@appname' => $consumer->name,
  158. '@user' => $account->name,
  159. '@sitename' => variable_get('site_name', ''),
  160. );
  161. if ($context) {
  162. foreach ($context->authorization_levels as $name => $level) {
  163. $auth_opt = array(
  164. '#type' => 'checkbox',
  165. '#title' => t($level['title'], $tvars),
  166. '#description' => t($level['description'], $tvars),
  167. '#default_value' => in_array($name, $default_services),
  168. );
  169. $form['authorization']['levels'][$name] = $auth_opt;
  170. }
  171. }
  172. }
  173. function oauth_common_form_authorization_submit($form, &$form_state) {
  174. $values = $form_state['values'];
  175. $token = $values['token_object'];
  176. $consumer = $token->consumer;
  177. // Collect the authorization levels
  178. if (isset($values['levels'])) {
  179. $token->services = array_keys(array_filter($values['levels']));
  180. }
  181. $token->authorized = $values['authorized'];
  182. $token->write(TRUE);
  183. drupal_set_message(t('The @consumer token @token was updated.', array(
  184. '@consumer' => $consumer->name,
  185. '@token' => $token->key)));
  186. drupal_goto(sprintf('user/%d/applications', $token->uid));
  187. }
  188. /**
  189. * Provide a form for deleting an authorization.
  190. *
  191. * We've got the same symptom here that we do with
  192. * `oauth_common_form_authorization`, i.e., this is actually called by
  193. * `drupal_retrieve_form`.
  194. */
  195. function oauth_common_form_authorization_delete($form_id, &$form_state, $user, $token) {
  196. $consumer = $token->consumer;
  197. $cancel_url = 'user/%d/oauth/authorizations';
  198. if (!empty($_GET['destination'])) {
  199. $cancel_url = $_GET['destination'];
  200. }
  201. drupal_set_title(t('Deleting authorization for "@consumer"', array(
  202. '@consumer' => $consumer->name,
  203. )), PASS_THROUGH);
  204. $form = array(
  205. 'token_object' => array(
  206. '#type' => 'value',
  207. '#value' => $token,
  208. ),
  209. );
  210. $form['user'] = array(
  211. '#type' => 'value',
  212. '#value' => $user->uid,
  213. );
  214. $form['key'] = array(
  215. '#type' => 'value',
  216. '#value' => $token->key,
  217. );
  218. $form['description'] = array(
  219. '#type' => 'item',
  220. '#markup' => t('Are you sure that you want to delete the authorization for "@consumer"?', array(
  221. '@consumer' => $consumer->name,
  222. )),
  223. );
  224. $form['actions'] = array('#type' => 'actions');
  225. $form['actions']['cancel'] = array(
  226. '#markup' => l(t('Cancel'), sprintf($cancel_url, $user->uid, $token->key)),
  227. );
  228. $form['actions']['submit'] = array(
  229. '#type' => 'submit',
  230. '#value' => t('Delete'),
  231. );
  232. return $form;
  233. }
  234. function oauth_common_form_authorization_delete_submit($form, &$form_state) {
  235. $values = $form_state['values'];
  236. $token = $values['token_object'];
  237. $consumer = $token->consumer;
  238. $token->delete();
  239. drupal_set_message(t('The @consumer token @token was deleted.', array(
  240. '@consumer' => $consumer->name,
  241. '@token' => $token->key)));
  242. }