openidadminpanel.php 7.9 KB

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