[FEATURE] Wizard to add OpenID to backend user
[Packages/TYPO3.CMS.git] / typo3 / sysext / openid / Classes / Wizard.php
1 <?php
2 namespace TYPO3\CMS\Openid;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Weiske <cweiske@cweiske.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
29
30 /**
31 * OpenID selection wizard for the backend
32 *
33 * @author Christian Weiske <cweiske@cweiske.de>
34 */
35 class Wizard extends OpenidService {
36 /**
37 * OpenID of the user after authentication
38 *
39 * @var string
40 */
41 protected $claimedId;
42
43 /**
44 * Name of the form element this wizard should write the OpenID into
45 *
46 * @var string
47 */
48 protected $parentFormItemName;
49
50 /**
51 * Name of the function that needs to be called after setting the value
52 *
53 * @var string
54 */
55 protected $parentFormFieldChangeFunc;
56
57 /**
58 * Run the wizard and output HTML.
59 *
60 * @return void
61 */
62 public function main() {
63 $p = GeneralUtility::_GP('P');
64 if (isset($p['itemName'])) {
65 $this->parentFormItemName = $p['itemName'];
66 }
67 if (isset($p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
68 $this->parentFormFieldChangeFunc = $p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
69 }
70
71 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_openid_mode') === 'finish'
72 && $this->openIDResponse === NULL
73 ) {
74 $this->includePHPOpenIDLibrary();
75 $openIdConsumer = $this->getOpenIDConsumer();
76 $this->openIDResponse = $openIdConsumer->complete($this->getReturnUrl());
77 $this->handleResponse();
78 $this->renderHtml();
79 return;
80 } elseif (GeneralUtility::_POST('openid_url') != '') {
81 $this->openIDIdentifier = GeneralUtility::_POST('openid_url');
82 $this->sendOpenIDRequest();
83
84 // When sendOpenIDRequest() returns, there was an error
85 $flashMessageService = GeneralUtility::makeInstance(
86 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
87 );
88 $flashMessage = GeneralUtility::makeInstance(
89 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
90 sprintf(
91 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.setup'),
92 htmlspecialchars($this->openIDIdentifier)
93 ),
94 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
95 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
96 );
97 $flashMessageService->getMessageQueueByIdentifier()->enqueue($flashMessage);
98 }
99 $this->renderHtml();
100 }
101
102 /**
103 * Return URL to this wizard
104 *
105 * @return string Full URL with protocol and hostname
106 */
107 protected function getSelfUrl() {
108 return GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir .
109 $GLOBALS['MCONF']['_'];
110 }
111
112 /**
113 * Return URL that shall be called by the OpenID server
114 *
115 * @return string Full URL with protocol and hostname
116 */
117 protected function getReturnUrl() {
118 return $this->getSelfURL() .
119 '&tx_openid_mode=finish' .
120 '&P[itemName]=' . urlencode($this->parentFormItemName) .
121 '&P[fieldChangeFunc][TBE_EDITOR_fieldChanged]]=' . urlencode($this->parentFormFieldChangeFunc);
122 }
123
124 /**
125 * Check OpenID response and set flash messages depending on its state
126 *
127 * @return void
128 *
129 * @uses $openIDResponse
130 */
131 protected function handleResponse() {
132 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
133 $flashMessageService = GeneralUtility::makeInstance(
134 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
135 );
136 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
137
138 if (!$this->openIDResponse instanceof \Auth_OpenID_ConsumerResponse) {
139 $flashMessage = GeneralUtility::makeInstance(
140 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
141 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.no-response'),
142 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
143 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
144 );
145 } elseif ($this->openIDResponse->status == Auth_OpenID_SUCCESS) {
146 // all fine
147 $this->claimedId = $this->getSignedParameter('openid_claimed_id');
148 $flashMessage = GeneralUtility::makeInstance(
149 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
150 sprintf(
151 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:youropenid'),
152 htmlspecialchars($this->claimedId)
153 ),
154 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.success'),
155 \TYPO3\CMS\Core\Messaging\FlashMessage::OK
156 );
157 } elseif ($this->openIDResponse->status == Auth_OpenID_CANCEL) {
158 $flashMessage = GeneralUtility::makeInstance(
159 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
160 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.cancelled'),
161 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
162 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
163 );
164 } else {
165 // another failure. show error message and form again
166 $flashMessage = GeneralUtility::makeInstance(
167 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
168 sprintf(
169 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.general'),
170 htmlspecialchars($this->openIDResponse->status),
171 htmlspecialchars($this->openIDResponse->message)
172 ),
173 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
174 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
175 );
176 }
177
178 $defaultFlashMessageQueue->enqueue($flashMessage);
179 }
180
181 /**
182 * Render HTML with messagse and OpenID form and output it
183 *
184 * @return void
185 */
186 protected function renderHtml() {
187 // use FLUID standalone view for wizard content
188 $view = GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
189 $view->setTemplatePathAndFilename(
190 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('openid') .
191 'Resources/Private/Templates/Wizard/Content.html'
192 );
193
194 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
195 $flashMessageService = GeneralUtility::makeInstance(
196 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
197 );
198 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
199 $messages = array();
200 foreach ($defaultFlashMessageQueue->getAllMessagesAndFlush() as $message) {
201 $messages[] = $message->render();
202 }
203 $view->assign('messages', $messages);
204 $view->assign('formAction', htmlspecialchars($this->getSelfURL()));
205 $view->assign('claimedId', $this->claimedId);
206 $view->assign('parentFormItemName', $this->parentFormItemName);
207 $view->assign('parentFormItemNameNoHr', strtr($this->parentFormItemName, array('_hr' => '')));
208 $view->assign('parentFormFieldChangeFunc', $this->parentFormFieldChangeFunc);
209 $view->assign('showForm', TRUE);
210 if (isset($_REQUEST['openid_url'])) {
211 $view->assign('openid_url', $_REQUEST['openid_url']);
212 }
213
214 header('HTTP/1.0 200 OK');
215 echo $view->render();
216 }
217 }
218 ?>