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/connectsettingsaction.php';
35 require_once INSTALLDIR . '/lib/applicationlist.php';
36 require_once INSTALLDIR . '/lib/apioauthstore.php';
39 * Show connected OAuth applications
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
50 class OauthconnectionssettingsAction extends ConnectSettingsAction
54 var $oauth_token = null;
56 function prepare($args)
58 parent::prepare($args);
59 $this->oauth_token = $this->arg('oauth_token');
60 $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
67 * @return string Title of the page
72 return _('Connected applications');
76 * Instructions for use
78 * @return instructions for use
81 function getInstructions()
83 return _('The following connections exist for your account.');
87 * Content area of the page
92 function showContent()
94 $user = common_current_user();
95 $profile = $user->getProfile();
97 $offset = ($this->page - 1) * APPS_PER_PAGE;
98 $limit = APPS_PER_PAGE + 1;
100 $connection = $profile->getConnectedApps($offset, $limit);
104 if (!empty($connection)) {
105 $cal = new ConnectedAppsList($connection, $user, $this);
110 $this->showEmptyListMessage();
115 $cnt > APPS_PER_PAGE,
117 'connectionssettings',
118 array('nickname' => $user->nickname)
123 * Handle posts to this form
125 * Based on the button that was pressed, muxes out to other functions
126 * to do the actual task requested.
128 * All sub-functions reload the form with a message -- success or failure.
133 function handlePost()
137 $token = $this->trimmed('token');
138 if (!$token || $token != common_session_token()) {
139 $this->showForm(_('There was a problem with your session token. '.
140 'Try again, please.'));
144 if ($this->arg('revoke')) {
145 $this->revokeAccess($this->oauth_token);
147 $this->clientError(_('Unexpected form submission.'), 401);
153 * Revoke an access token
155 * XXX: Confirm revoke before doing it
157 * @param int $appId the ID of the application
161 function revokeAccess($token)
163 $cur = common_current_user();
165 $appUser = Oauth_application_user::getByUserAndToken($cur, $token);
167 if (empty($appUser)) {
168 $this->clientError(_('You are not a user of that application.'), 401);
172 $app = Oauth_application::staticGet('id', $appUser->application_id);
174 $datastore = new ApiStatusNetOAuthDataStore();
175 $datastore->revoke_token($appUser->token, 1);
177 $result = $appUser->delete();
180 common_log_db_error($orig, 'DELETE', __FILE__);
181 $this->clientError(sprintf(_('Unable to revoke access for app: %s.'), $app->id));
185 $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
193 $appUser->application_id
198 _('You have successfully revoked access for %s and the access token starting with %s'),
200 substr($appUser->token, 0, 7)
203 $this->showForm($msg, true);
206 function showEmptyListMessage()
208 $message = _('You have not authorized any applications to use your account.');
210 $this->elementStart('div', 'guide');
211 $this->raw(common_markup_to_html($message));
212 $this->elementEnd('div');
215 function showSections()
217 $cur = common_current_user();
219 $this->elementStart('div', array('id' => 'developer-help', 'class' => 'section'));
221 $this->element('h2', null, 'Developers');
222 $this->elementStart('p');
225 _('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'),
226 common_local_url('oauthappssettings')
229 $output = common_markup_to_html($devMsg);
232 $this->elementEnd('p');
234 $this->elementEnd('section');