3 * StatusNet, the distributed open-source microblogging tool
5 * List a user's OAuth connected applications
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.
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.
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/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2008-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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR . '/lib/apioauthstore.php';
37 * Show connected OAuth applications
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
47 class OauthconnectionssettingsAction extends SettingsAction
50 var $oauth_token = null;
52 function prepare($args)
54 parent::prepare($args);
55 $this->oauth_token = $this->arg('oauth_token');
56 $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
63 * @return string Title of the page
67 // TRANS: Title for OAuth connection settings.
68 return _('Connected applications');
72 * Instructions for use
74 * @return instructions for use
76 function getInstructions()
78 // TRANS: Instructions for OAuth connection settings.
79 return _('The following connections exist for your account.');
83 * Content area of the page
88 function showContent()
90 $user = common_current_user();
91 $profile = $user->getProfile();
93 $offset = ($this->page - 1) * APPS_PER_PAGE;
94 $limit = APPS_PER_PAGE + 1;
96 $connection = $user->getConnectedApps($offset, $limit);
100 if (!empty($connection)) {
101 $cal = new ConnectedAppsList($connection, $user, $this);
106 $this->showEmptyListMessage();
111 $cnt > APPS_PER_PAGE,
113 'connectionssettings',
114 array('nickname' => $user->nickname)
119 * Handle posts to this form
121 * Based on the button that was pressed, muxes out to other functions
122 * to do the actual task requested.
124 * All sub-functions reload the form with a message -- success or failure.
128 function handlePost()
132 $token = $this->trimmed('token');
133 if (!$token || $token != common_session_token()) {
134 $this->showForm(_('There was a problem with your session token. '.
135 'Try again, please.'));
139 if ($this->arg('revoke')) {
140 $this->revokeAccess($this->oauth_token);
142 // TRANS: Client error when submitting a form with unexpected information.
143 $this->clientError(_('Unexpected form submission.'), 401);
149 * Revoke an access token
151 * XXX: Confirm revoke before doing it
153 * @param int $appId the ID of the application
156 function revokeAccess($token)
158 $cur = common_current_user();
160 $appUser = Oauth_application_user::getByUserAndToken($cur, $token);
162 if (empty($appUser)) {
163 // TRANS: Client error when trying to revoke access for an application while not being a user of it.
164 $this->clientError(_('You are not a user of that application.'), 401);
168 $app = Oauth_application::staticGet('id', $appUser->application_id);
170 $datastore = new ApiStatusNetOAuthDataStore();
171 $datastore->revoke_token($appUser->token, 1);
173 $result = $appUser->delete();
176 common_log_db_error($orig, 'DELETE', __FILE__);
177 // TRANS: Client error when revoking access has failed for some reason.
178 // TRANS: %s is the application ID revoking access failed for.
179 $this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id));
183 $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
191 $appUser->application_id
196 // TRANS: Success message after revoking access for an application.
197 // TRANS: %1$s is the application name, %2$s is the first part of the user token.
198 _('You have successfully revoked access for %1$s and the access token starting with %2$s.'),
200 substr($appUser->token, 0, 7)
203 $this->showForm($msg, true);
206 function showEmptyListMessage()
208 // TRANS: Empty list message when no applications have been authorised yet.
209 $message = _('You have not authorized any applications to use your account.');
211 $this->elementStart('div', 'guide');
212 $this->raw(common_markup_to_html($message));
213 $this->elementEnd('div');
216 function showSections()
218 $cur = common_current_user();
220 $this->elementStart('div', array('id' => 'developer-help', 'class' => 'section'));
222 $this->element('h2', null, 'Developers');
223 $this->elementStart('p');
226 // TRANS: Note for developers in the OAuth connection settings form.
227 // TRANS: This message contains a Markdown link. Do not separate "](".
228 // TRANS: %s is the URL to the OAuth settings.
229 _('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'),
230 common_local_url('oauthappssettings')
233 $output = common_markup_to_html($devMsg);
236 $this->elementEnd('p');
238 $this->elementEnd('section');