-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.php
187 lines (164 loc) · 7.1 KB
/
index.php
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
<?php
require_once('../../config.php');
require_once("{$CFG->libdir}/formslib.php");
require_once($CFG->libdir . '/adminlib.php');
require_once('forms.php');
require_once('locallib.php');
require_login();
require_capability('local/oauth:manageclients', context_system::instance());
admin_externalpage_setup('local_oauth_settings');
$action = optional_param('action', '', PARAM_ALPHA);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'local_oauth'));
$view_table = false;
switch ($action) {
case 'edit':
$id = required_param('id', PARAM_TEXT);
if (!$client_edit = $DB->get_record('oauth_clients', array('id' => $id))) {
echo $OUTPUT->notification(get_string('client_not_exists', 'local_oauth'));
$view_table = true;
break;
}
case 'add':
$bform = new local_oauth_clients_form();
if ($bform->is_cancelled()) {
$view_table = true;
break;
}
if ($fromform = $bform->get_data() and confirm_sesskey()) {
// get values
$record = new stdClass();
$record->redirect_uri = $fromform->redirect_uri;
$record->grant_types = $fromform->grant_types;
$record->scope = $fromform->scope;
$record->user_id = $fromform->user_id ? $fromform->user_id : '';
// do save
if (!isset($client_edit)) {
$record->client_id = $fromform->client_id;
$record->client_secret = generate_secret();
if (!$DB->insert_record('oauth_clients', $record)) {
throw new \moodle_exception('insert_error', 'local_oauth');
}
} else {
$record->id = $client_edit->id;
if (!$DB->update_record('oauth_clients', $record)) {
throw new \moodle_exception('update_error', 'local_oauth');
}
}
echo $OUTPUT->notification(get_string('saveok', 'local_oauth'), 'notifysuccess');
$view_table = true;
break;
}
$form = new stdClass();
// set values
if (isset($client_edit)) {
$form->client_id = $client_edit->client_id;
$form->redirect_uri = $client_edit->redirect_uri;
$form->grant_types = $client_edit->grant_types;
$form->scope = $client_edit->scope;
$form->user_id = $client_edit->user_id;
$form->action = 'edit';
} else {
$form->client_id = '';
$form->redirect_uri = '';
$form->grant_types = 'authorization_code';
$form->scope = 'user_info';
$form->user_id = '0';
$form->action = 'add';
}
$bform->set_data($form);
$bform->display();
break;
case 'addwordpress':
$bform = new local_oauth_clients_wp_form();
if ($bform->is_cancelled()) {
$view_table = true;
break;
} else {
if ($fromform = $bform->get_data() and confirm_sesskey()) {
if (!oauth_add_wordpress_client($fromform->client_id, $fromform->url)) {
throw new \moodle_exception('insert_error', 'local_oauth');
}
echo $OUTPUT->notification(get_string('saveok', 'local_oauth'), 'notifysuccess');
$view_table = true;
break;
}
}
$bform->display();
break;
case 'addnodes':
if (!oauth_add_wordpress_client('nodes', get_service_url('nodes'))) {
throw new \moodle_exception('insert_error', 'local_oauth');
}
echo $OUTPUT->notification(get_string('saveok', 'local_oauth'), 'notifysuccess');
$view_table = true;
break;
case 'del':
// Get values
$confirm = optional_param('confirm', 0, PARAM_INT);
$id = required_param('id', PARAM_TEXT);
// Do delete
if (empty($confirm)) {
if (!$client_edit = $DB->get_record('oauth_clients', array('id' => $id))) {
echo $OUTPUT->notification(get_string('client_not_exists', 'local_oauth'));
$view_table = true;
break;
}
echo '<p>' . get_string('confirmdeletestr', 'local_oauth', $client_edit->client_id) . '</p>
<form action="index.php" method="GET">
<input type="hidden" name="action" value="del" />
<input type="hidden" name="confirm" value="1" />
<input type="hidden" name="id" value="' . $id . '" />
<input type="submit" value="' . get_string('confirm') . '" /> <input type="button" value="' . get_string('cancel') . '" onclick="javascript:history.back();" />
</form>';
} else {
if (!$DB->delete_records('oauth_clients', array('id' => $id))) {
throw new \moodle_exception('delete_error', 'local_oauth');
}
echo $OUTPUT->notification(get_string('delok', 'local_oauth'), 'notifysuccess');
$view_table = true;
break;
}
break;
default:
$view_table = true;
break;
}
if ($view_table) {
echo '<p>';
if (function_exists('is_agora') && is_agora()) {
if (is_service_enabled('nodes') && !$DB->record_exists('oauth_clients', array('client_id' => 'nodes'))) {
echo '<a href="index.php?action=addnodes" class="btn btn-primary" style="margin-right: 10px;">' . get_string('addnodesclient', 'local_oauth') . '</a>';
}
echo '<a href="index.php?action=addwordpress" class="btn btn-primary" style="margin-right: 10px;">' . get_string('addwordpressclient', 'local_oauth') . '</a>';
echo '<a href="index.php?action=add" class="btn">' . get_string('addotherclient', 'local_oauth') . '</a>';
} else {
echo '<a href="index.php?action=add" class="btn">' . get_string('addclient', 'local_oauth') . '</a>';
}
echo '</p>';
$clients = $DB->get_records('oauth_clients');
if (!empty($clients)) {
$table = new html_table();
$table->class = 'generaltable generalbox';
$table->head = array(
get_string('client_id', 'local_oauth'),
get_string('client_secret', 'local_oauth'),
get_string('grant_types', 'local_oauth'),
get_string('scope', 'local_oauth'),
get_string('user_id', 'local_oauth'),
get_string('actions'));
$table->align = array('left', 'left', 'center', 'center', 'center', 'center', 'center');
foreach ($clients as $client) {
$row = array();
$row[] = $client->client_id;
$row[] = $client->client_secret;
$row[] = $client->grant_types;
$row[] = $client->scope;
$row[] = $client->user_id;
$row[] = "<a href=\"index.php?action=edit&id=" . $client->id . "\">" . get_string('edit') . "</a> | <a href=\"index.php?action=del&id=" . $client->id . "\">" . get_string('delete') . "</a>";
$table->data[] = $row;
}
echo html_writer::table($table);
}
}
echo $OUTPUT->footer();