-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgauth.admin.inc
More file actions
284 lines (270 loc) · 8.75 KB
/
gauth.admin.inc
File metadata and controls
284 lines (270 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* @file
* Administration pages for Google OAuth settings.
*/
/**
* Menu callback; Listing of all current API accounts.
*/
function gauth_account_list() {
$query = db_select('gauth_accounts', 'ga')->extend('PagerDefault');
$query->fields('ga');
$accounts = $query
->limit(25)
->execute();
return theme('gauth_account_list', array('accounts' => $accounts));
}
/**
* Returns HTML for the page containing the list of accounts.
*
* @param array $variables
* An associative array containing:
* - accounts: An array of all the accounts returned by
* gauth_account_load().
*
* @see gauth_account_load()
* @ingroup themeable
*/
function theme_gauth_account_list($variables) {
$accounts = $variables['accounts'];
$header = array(
t('Name'),
t('API key'),
t('Client Id'),
t('Client Secret'),
t('User'),
t('Services'),
t('Access Type'),
t('Redirect Url'),
array(
'data' => t('Operations'),
'colspan' => 3,
),
);
$rows = array();
foreach ($accounts as $account) {
$account_user = user_load($account->uid);
$row = array();
$row[] = $account->name;
$row[] = $account->developer_key;
$row[] = $account->client_id;
$row[] = $account->client_secret;
$row[] = l($account_user->name, "user/$account->uid");
$row[] = implode(", ", gauth_google_services_names($account->services));
$row[] = $account->access_type;
$row[] = '<pre>' . gauth_callback_url() . '</pre>';
$row[] = l(t('edit'), 'admin/config/services/gauth_account/edit/' . $account->id);
$row[] = l(t('delete'), 'admin/config/services/gauth_account/delete/' . $account->id);
$row[] = $account->is_authenticated ? l(t('revoke'), 'gauth/revoke_token/' . $account->id) : l(t('Authenticate'), 'gauth/response_handler/' . $account->id);
$rows[] = $row;
}
if (empty($rows)) {
$rows[] = array(
array(
'colspan' => 5,
'data' => t('There are currently no API Accounts. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/services/gauth_account/add'))),
),
);
}
$build['pager_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$build['pager_pager'] = array('#theme' => 'pager');
return $build;
}
/**
* Form builder; Edit an account.
*
* @param array $form_state
* An associative array containing the current state of the form.
* @param int $id
* An id of the account.
*
* @ingroup forms
* @see gauth_account_edit_form_submit()
* @see gauth_account_edit_form_validate()
*/
function gauth_account_edit_form($form, &$form_state, $id = NULL) {
if ($id) {
backdrop_set_title(t('Edit Google Auth account'));
}
else {
backdrop_set_title(t('Add Google Auth account'));
}
$form['id'] = array(
'#type' => 'value',
'#value' => $id,
);
$account = array();
if ($id) {
$account = gauth_account_load($id, FALSE);
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Account Name'),
'#description' => t('The unique name for this account.'),
'#default_value' => isset($account['name']) ? $account['name'] : '',
'#required' => TRUE,
);
$form['developer_key'] = array(
'#type' => 'textfield',
'#title' => t('API Key'),
'#description' => t('The server API key of web application.'),
'#default_value' => isset($account['developer_key']) ? $account['developer_key'] : '',
'#required' => TRUE,
);
$form['client_id'] = array(
'#type' => 'textfield',
'#title' => t('Client Id'),
'#description' => t('The Client ID of the web application.'),
'#default_value' => isset($account['client_id']) ? $account['client_id'] : '',
'#required' => TRUE,
);
$form['client_secret'] = array(
'#type' => 'textfield',
'#title' => t('Client Secret Key'),
'#description' => t('The client secret key of the web application.'),
'#default_value' => isset($account['client_secret']) ? $account['client_secret'] : '',
'#required' => TRUE,
);
$options = gauth_google_services_names();
$form['services'] = array(
'#type' => 'select',
'#title' => t('Services'),
'#description' => t('Services that will be enabled to be used by this account.'),
'#options' => $options,
'#multiple' => TRUE,
'#size' => 15,
'#default_value' => isset($account['services']) ? explode(",", $account['services']) : array(),
'#required' => TRUE,
);
$form['access_type'] = array(
'#type' => 'radios',
'#title' => t('Access Type'),
'#description' => t('The Access Type of the account. Select offline if the site can perform actions even when the user is not online.'),
'#options' => array(
'offline' => t('Offline'),
'online' => t('Online'),
),
'#default_value' => isset($account['access_type']) ? $account['access_type'] : 'offline',
);
$form['callback_url'] = array(
'#type' => 'textfield',
'#title' => t('Redirect Url'),
'#description' => t('Copy this url and paste it in google project as a authorized redirect url.'),
'#default_value' => gauth_callback_url(),
'#disabled' => TRUE,
);
$form['uid'] = array(
'#type' => 'value',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#suffix' => l(t('Cancel'), 'admin/config/services/gauth_account'),
);
return $form;
}
/**
* Validate handler for adding a new account to google auth accounts.
*/
function gauth_account_edit_form_validate($form, &$form_state) {
$accounts = gauth_account_load();
$accounts = array_keys($accounts);
if (in_array($form_state['values']['name'], $accounts)) {
if (!isset($form_state['values']['id'])) {
form_set_error('name', t('Name already in use. Please choose a unique name for the account'));
}
}
}
/**
* Submit handler for adding a new account to google auth accounts.
*/
function gauth_account_edit_form_submit($form, &$form_state) {
$id = gauth_account_save($form_state['values']);
if (!$id['is_authenticated']) {
gauth_account_authenticate($id['id'], FALSE);
}
backdrop_set_message(t('API Account saved'));
$form_state['redirect'] = 'admin/config/services/gauth_account';
}
/**
* Form builder; Form for deleting an google auth account.
*
* @param int $id
* Id of the account to be deleted.
*
* @ingroup forms
* @see gauth_account_delete_form_submit()
*/
function gauth_account_delete_form($form, &$form_state, $id) {
$form['id'] = array(
'#type' => 'value',
'#value' => $id,
);
$question = check_plain(t('Are you sure you want to delete this account'));
$path = 'admin/config/services/gauth_account';
$description = check_plain(t("This account will be deleted from the system and won't be available"));
$yes = check_plain(t('Delete'));
$no = check_plain(t('Cancel'));
return confirm_form($form, $question, $path, $description, $yes, $no);
}
/**
* Submit handler to delete an google auth account.
*/
function gauth_account_delete_form_submit($form, &$form_state) {
if ($form_state['values']['id'] != NULL) {
$rows = gauth_account_delete($form_state['values']['id'], FALSE);
if ($rows == 1) {
backdrop_set_message(t("The account is deleted successfully"));
}
else {
backdrop_set_message(t("Error occured while deleting the account"), "error");
}
}
else {
backdrop_set_message(t("Error occured: Can't find account to be deleted"), "error");
}
$form_state['redirect'] = 'admin/config/services/gauth_account';
}
/**
* Form builder; Form for revoking access of an google auth account.
*
* @param int $id
* Id of the account to be deleted.
*
* @ingroup forms
* @see gauth_account_token_revoke_form_submit()
*/
function gauth_account_token_revoke_form($form, &$form_state, $id) {
$form['id'] = array(
'#type' => 'value',
'#value' => $id,
);
$question = check_plain(t('Are you sure you want to revoke access token of this account'));
$path = 'admin/config/services/gauth_account';
$description = check_plain(t("This account can't be used for api call until authenticated again"));
$yes = check_plain(t('Revoke'));
$no = check_plain(t('Cancel'));
return confirm_form($form, $question, $path, $description, $yes, $no);
}
/**
* Submit handler to revoke access of an google auth account.
*/
function gauth_account_token_revoke_form_submit($form, &$form_state) {
if ($form_state['values']['id'] != NULL) {
$rows = gauth_account_revoke_token($form_state['values']['id'], FALSE);
if ($rows == TRUE) {
backdrop_set_message(t("Acess token revoked successfully"));
}
else {
backdrop_set_message(t("Error occured while revoking token of this account"), "error");
}
}
else {
backdrop_set_message(t("Error occured: Can't find account to be revoked"), "error");
}
$form_state['redirect'] = 'admin/config/services/gauth_account';
}