openidadminpanel.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * OpenID bridge administration panel
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Administer global OpenID settings
  28. *
  29. * @category Admin
  30. * @package GNUsocial
  31. * @author Zach Copley <zach@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class OpenidadminpanelAction extends AdminPanelAction
  35. {
  36. /**
  37. * Returns the page title
  38. *
  39. * @return string page title
  40. */
  41. public function title()
  42. {
  43. // TRANS: Title for OpenID bridge administration page.
  44. return _m('TITLE', 'OpenID Settings');
  45. }
  46. /**
  47. * Instructions for using this form.
  48. *
  49. * @return string instructions
  50. */
  51. public function getInstructions()
  52. {
  53. // TRANS: Page instructions.
  54. return _m('OpenID settings');
  55. }
  56. /**
  57. * Show the OpenID admin panel form
  58. *
  59. * @return void
  60. */
  61. public function showForm()
  62. {
  63. $form = new OpenIDAdminPanelForm($this);
  64. $form->show();
  65. return;
  66. }
  67. /**
  68. * Save settings from the form
  69. *
  70. * @return void
  71. */
  72. public function saveSettings()
  73. {
  74. static $settings = array(
  75. 'openid' => array('trusted_provider', 'required_team')
  76. );
  77. static $booleans = array(
  78. 'openid' => array('append_username'),
  79. 'site' => array('openidonly')
  80. );
  81. $values = array();
  82. foreach ($settings as $section => $parts) {
  83. foreach ($parts as $setting) {
  84. $values[$section][$setting]
  85. = $this->trimmed($setting);
  86. }
  87. }
  88. foreach ($booleans as $section => $parts) {
  89. foreach ($parts as $setting) {
  90. $values[$section][$setting]
  91. = ($this->boolean($setting)) ? 1 : 0;
  92. }
  93. }
  94. // This throws an exception on validation errors
  95. $this->validate($values);
  96. // assert(all values are valid);
  97. $config = new Config();
  98. $config->query('START TRANSACTION');
  99. foreach ($settings as $section => $parts) {
  100. foreach ($parts as $setting) {
  101. Config::save($section, $setting, $values[$section][$setting]);
  102. }
  103. }
  104. foreach ($booleans as $section => $parts) {
  105. foreach ($parts as $setting) {
  106. Config::save($section, $setting, $values[$section][$setting]);
  107. }
  108. }
  109. $config->query('COMMIT');
  110. return;
  111. }
  112. public function validate(&$values)
  113. {
  114. // Validate consumer key and secret (can't be too long)
  115. if (mb_strlen($values['openid']['trusted_provider']) > 255) {
  116. $this->clientError(
  117. // TRANS: Client error displayed when OpenID provider URL is too long.
  118. _m('Invalid provider URL. Maximum length is 255 characters.')
  119. );
  120. }
  121. if (mb_strlen($values['openid']['required_team']) > 255) {
  122. $this->clientError(
  123. // TRANS: Client error displayed when Launchpad team name is too long.
  124. _m('Invalid team name. Maximum length is 255 characters.')
  125. );
  126. }
  127. }
  128. }
  129. class OpenIDAdminPanelForm extends AdminForm
  130. {
  131. /**
  132. * ID of the form
  133. *
  134. * @return int ID of the form
  135. */
  136. public function id()
  137. {
  138. return 'openidadminpanel';
  139. }
  140. /**
  141. * class of the form
  142. *
  143. * @return string class of the form
  144. */
  145. public function formClass()
  146. {
  147. return 'form_settings';
  148. }
  149. /**
  150. * Action of the form
  151. *
  152. * @return string URL of the action
  153. */
  154. public function action()
  155. {
  156. return common_local_url('openidadminpanel');
  157. }
  158. /**
  159. * Data elements of the form
  160. *
  161. * @return void
  162. *
  163. * @todo Some of the options could prevent users from logging in again.
  164. * Make sure that the acting administrator has a valid OpenID matching,
  165. * or more carefully warn folks.
  166. */
  167. public function formData()
  168. {
  169. $this->out->elementStart(
  170. 'fieldset',
  171. array('id' => 'settings_openid')
  172. );
  173. // TRANS: Fieldset legend.
  174. $this->out->element('legend', null, _m('LEGEND', 'Trusted provider'));
  175. $this->out->element(
  176. 'p',
  177. 'form_guide',
  178. // TRANS: Form guide.
  179. _m('By default, users are allowed to authenticate with any OpenID provider. ' .
  180. 'If you are using your own OpenID service for shared sign-in, ' .
  181. 'you can restrict access to only your own users here.')
  182. );
  183. $this->out->elementStart('ul', 'form_data');
  184. $this->li();
  185. $this->input(
  186. 'trusted_provider',
  187. // TRANS: Field label.
  188. _m('Provider URL'),
  189. // TRANS: Field title.
  190. _m('All OpenID logins will be sent to this URL; other providers may not be used.'),
  191. 'openid'
  192. );
  193. $this->unli();
  194. $this->li();
  195. $this->out->checkbox(
  196. // TRANS: Checkbox label.
  197. 'append_username',
  198. _m('Append a username to base URL'),
  199. (bool) $this->value('append_username', 'openid'),
  200. // TRANS: Checkbox title.
  201. _m('Login form will show the base URL and prompt for a username to add at the end. Use when OpenID provider URL should be the profile page for individual users.'),
  202. 'true'
  203. );
  204. $this->unli();
  205. $this->li();
  206. $this->input(
  207. 'required_team',
  208. // TRANS: Field label.
  209. _m('Required team'),
  210. // TRANS: Field title.
  211. _m('Only allow logins from users in the given team (Launchpad extension).'),
  212. 'openid'
  213. );
  214. $this->unli();
  215. $this->out->elementEnd('ul');
  216. $this->out->elementEnd('fieldset');
  217. $this->out->elementStart(
  218. 'fieldset',
  219. array('id' => 'settings_openid-options')
  220. );
  221. // TRANS: Fieldset legend.
  222. $this->out->element('legend', null, _m('LEGEND', 'Options'));
  223. $this->out->elementStart('ul', 'form_data');
  224. $this->li();
  225. $this->out->checkbox(
  226. // TRANS: Checkbox label.
  227. 'openidonly',
  228. _m('Enable OpenID-only mode'),
  229. (bool) $this->value('openidonly', 'site'),
  230. // TRANS: Checkbox title.
  231. _m('Require all users to login via OpenID. Warning: disables password authentication for all users!'),
  232. 'true'
  233. );
  234. $this->unli();
  235. $this->out->elementEnd('ul');
  236. $this->out->elementEnd('fieldset');
  237. }
  238. /**
  239. * Action elements
  240. *
  241. * @return void
  242. */
  243. public function formActions()
  244. {
  245. // TRANS: Button text to save OpenID settings.
  246. $this->out->submit(
  247. 'submit',
  248. _m('BUTTON', 'Save'),
  249. 'submit',
  250. null,
  251. // TRANS: Button title to save OpenID settings.
  252. _m('Save OpenID settings.')
  253. );
  254. }
  255. }