Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Output rendering for the plugin.
19
 *
20
 * @package     tool_oauth2
21
 * @copyright   2017 Damyon Wiese
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace tool_oauth2\output;
25
 
26
use plugin_renderer_base;
27
use html_table;
28
use html_table_cell;
29
use html_table_row;
30
use html_writer;
31
use core\oauth2\issuer;
32
use core\oauth2\api;
33
use moodle_url;
34
 
35
defined('MOODLE_INTERNAL') || die();
36
 
37
/**
38
 * Implements the plugin renderer
39
 *
40
 * @copyright 2017 Damyon Wiese
41
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class renderer extends plugin_renderer_base {
44
    /**
45
     * This function will render one beautiful table with all the issuers.
46
     *
47
     * @param \core\oauth2\issuer[] $issuers - list of all issuers.
48
     * @return string HTML to output.
49
     */
50
    public function issuers_table($issuers) {
51
        global $CFG;
52
 
53
        $table = new html_table();
54
        $table->head  = [
55
            get_string('name'),
56
            get_string('issuerusedforlogin', 'tool_oauth2'),
57
            get_string('logindisplay', 'tool_oauth2'),
58
            get_string('issuerusedforinternal', 'tool_oauth2'),
59
            get_string('discoverystatus', 'tool_oauth2') . ' ' . $this->help_icon('discovered', 'tool_oauth2'),
60
            get_string('systemauthstatus', 'tool_oauth2') . ' ' . $this->help_icon('systemaccountconnected', 'tool_oauth2'),
61
            get_string('edit'),
62
        ];
63
        $table->attributes['class'] = 'admintable generaltable';
64
        $data = [];
65
 
66
        $index = 0;
67
 
68
        foreach ($issuers as $issuer) {
69
            // We need to handle the first and last ones specially.
70
            $first = false;
71
            if ($index == 0) {
72
                $first = true;
73
            }
74
            $last = false;
75
            if ($index == count($issuers) - 1) {
76
                $last = true;
77
            }
78
 
79
            // Name.
80
            $name = $issuer->get('name');
81
            $image = $issuer->get('image');
82
            if ($image) {
83
                $name = '<img width="24" height="24" alt="" src="' . s($image) . '"> ' . s($name);
84
            }
85
            $namecell = new html_table_cell($name);
86
            $namecell->header = true;
87
 
88
            // Login issuer.
1441 ariadna 89
            if ((int)$issuer->get('showonloginpage') == issuer::SERVICEONLY ||
90
                    (int)$issuer->get('showonloginpage') == issuer::SMTPWITHXOAUTH2) {
1 efrain 91
                $loginissuer = $this->pix_icon('no', get_string('notloginissuer', 'tool_oauth2'), 'tool_oauth2');
92
                $logindisplayas = '';
93
            } else {
94
                $logindisplayas = s($issuer->get_display_name());
95
                if ($issuer->get('id') && $issuer->is_configured() && !empty($issuer->get_endpoint_url('userinfo'))) {
96
                    $loginissuer = $this->pix_icon('yes', get_string('loginissuer', 'tool_oauth2'), 'tool_oauth2');
97
                } else {
98
                    $loginissuer = $this->pix_icon('notconfigured', get_string('notconfigured', 'tool_oauth2'), 'tool_oauth2');
99
                }
100
            }
101
            $loginissuerstatuscell = new html_table_cell($loginissuer);
102
 
103
            // Internal services issuer.
104
            if ((int)$issuer->get('showonloginpage') == issuer::LOGINONLY) {
105
                $serviceissuer = $this->pix_icon('no', get_string('issuersservicesnotallow', 'tool_oauth2'), 'tool_oauth2');
106
            } else if ($issuer->get('id') && $issuer->is_configured()) {
107
                $serviceissuer = $this->pix_icon('yes', get_string('issuersservicesallow', 'tool_oauth2'), 'tool_oauth2');
108
            } else {
109
                $serviceissuer = $this->pix_icon('notconfigured', get_string('notconfigured', 'tool_oauth2'), 'tool_oauth2');
110
            }
111
            $internalissuerstatuscell = new html_table_cell($serviceissuer);
112
 
113
            // Discovered.
114
            if (!empty($issuer->get('scopessupported'))) {
115
                $discovered = $this->pix_icon('yes', get_string('discovered', 'tool_oauth2'), 'tool_oauth2');
116
            } else {
117
                if (!empty($issuer->get_endpoint_url('discovery'))) {
118
                    $discovered = $this->pix_icon('no', get_string('notdiscovered', 'tool_oauth2'), 'tool_oauth2');
119
                } else {
120
                    $discovered = '-';
121
                }
122
            }
123
 
124
            $discoverystatuscell = new html_table_cell($discovered);
125
 
126
            // Connected.
127
            if ($issuer->is_system_account_connected()) {
128
                $systemaccount = \core\oauth2\api::get_system_account($issuer);
129
                $systemauth = s($systemaccount->get('email')) . ' (' . s($systemaccount->get('username')). ') ';
130
                $systemauth .= $this->pix_icon('yes', get_string('systemaccountconnected', 'tool_oauth2'), 'tool_oauth2');
131
            } else {
132
                $systemauth = $this->pix_icon('no', get_string('systemaccountnotconnected', 'tool_oauth2'), 'tool_oauth2');
133
            }
134
 
135
            $params = ['id' => $issuer->get('id'), 'action' => 'auth'];
136
            $authurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
137
            $icon = $this->pix_icon('auth', get_string('connectsystemaccount', 'tool_oauth2'), 'tool_oauth2');
138
            $authlink = html_writer::link($authurl, $icon);
139
            $systemauth .= ' ' . $authlink;
140
 
141
            $systemauthstatuscell = new html_table_cell($systemauth);
142
 
143
            $links = '';
144
            // Action links.
145
            $editurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'edit']);
146
            $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
147
            $links .= ' ' . $editlink;
148
 
149
            // Endpoints.
150
            $editendpointsurl = new moodle_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => $issuer->get('id')]);
151
            $str = get_string('editendpoints', 'tool_oauth2');
152
            $editendpointlink = html_writer::link($editendpointsurl, $this->pix_icon('t/viewdetails', $str));
153
            $links .= ' ' . $editendpointlink;
154
 
155
            // User field mapping.
156
            $params = ['issuerid' => $issuer->get('id')];
157
            $edituserfieldmappingsurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $params);
158
            $str = get_string('edituserfieldmappings', 'tool_oauth2');
159
            $edituserfieldmappinglink = html_writer::link($edituserfieldmappingsurl, $this->pix_icon('t/user', $str));
160
            $links .= ' ' . $edituserfieldmappinglink;
161
 
162
            // Delete.
163
            $deleteurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'delete']);
164
            $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
165
            $links .= ' ' . $deletelink;
166
            // Enable / Disable.
167
            if ($issuer->get('enabled')) {
168
                // Disable.
169
                $disableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'disable'];
170
                $disableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $disableparams);
171
                $disablelink = html_writer::link($disableurl, $this->pix_icon('t/hide', get_string('disable')));
172
                $links .= ' ' . $disablelink;
173
            } else {
174
                // Enable.
175
                $enableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'enable'];
176
                $enableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $enableparams);
177
                $enablelink = html_writer::link($enableurl, $this->pix_icon('t/show', get_string('enable')));
178
                $links .= ' ' . $enablelink;
179
            }
180
            if (!$last) {
181
                // Move down.
182
                $params = ['id' => $issuer->get('id'), 'action' => 'movedown', 'sesskey' => sesskey()];
183
                $movedownurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
184
                $movedownlink = html_writer::link($movedownurl, $this->pix_icon('t/down', get_string('movedown')));
185
                $links .= ' ' . $movedownlink;
186
            }
187
            if (!$first) {
188
                // Move up.
189
                $params = ['id' => $issuer->get('id'), 'action' => 'moveup', 'sesskey' => sesskey()];
190
                $moveupurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
191
                $moveuplink = html_writer::link($moveupurl, $this->pix_icon('t/up', get_string('moveup')));
192
                $links .= ' ' . $moveuplink;
193
            }
194
 
195
            $editcell = new html_table_cell($links);
196
 
197
            $row = new html_table_row([
198
                $namecell,
199
                $loginissuerstatuscell,
200
                $logindisplayas,
201
                $internalissuerstatuscell,
202
                $discoverystatuscell,
203
                $systemauthstatuscell,
204
                $editcell,
205
            ]);
206
 
207
            if (!$issuer->get('enabled')) {
208
                $row->attributes['class'] = 'dimmed_text';
209
            }
210
 
211
            $data[] = $row;
212
            $index++;
213
        }
214
        $table->data = $data;
215
        return html_writer::table($table);
216
    }
217
 
218
    /**
219
     * This function will render one beautiful table with all the endpoints.
220
     *
221
     * @param \core\oauth2\endpoint[] $endpoints - list of all endpoints.
222
     * @param int $issuerid
223
     * @return string HTML to output.
224
     */
225
    public function endpoints_table($endpoints, $issuerid) {
226
        global $CFG;
227
 
228
        $table = new html_table();
229
        $table->head  = [
230
            get_string('name'),
231
            get_string('url'),
232
            get_string('edit'),
233
        ];
234
        $table->attributes['class'] = 'admintable generaltable';
235
        $data = [];
236
 
237
        $index = 0;
238
 
239
        foreach ($endpoints as $endpoint) {
240
            // Name.
241
            $name = $endpoint->get('name');
242
            $namecell = new html_table_cell(s($name));
243
            $namecell->header = true;
244
 
245
            // Url.
246
            $url = $endpoint->get('url');
247
            $urlcell = new html_table_cell(s($url));
248
 
249
            $links = '';
250
            // Action links.
251
            $editparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'edit'];
252
            $editurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $editparams);
253
            $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
254
            $links .= ' ' . $editlink;
255
 
256
            // Delete.
257
            $deleteparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'delete'];
258
            $deleteurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $deleteparams);
259
            $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
260
            $links .= ' ' . $deletelink;
261
 
262
            $editcell = new html_table_cell($links);
263
 
264
            $row = new html_table_row([
265
                $namecell,
266
                $urlcell,
267
                $editcell,
268
            ]);
269
 
270
            $data[] = $row;
271
            $index++;
272
        }
273
        $table->data = $data;
274
        return html_writer::table($table);
275
    }
276
 
277
    /**
278
     * This function will render one beautiful table with all the user_field_mappings.
279
     *
280
     * @param \core\oauth2\user_field_mapping[] $userfieldmappings - list of all user_field_mappings.
281
     * @param int $issuerid
282
     * @return string HTML to output.
283
     */
284
    public function user_field_mappings_table($userfieldmappings, $issuerid) {
285
        global $CFG;
286
 
287
        $table = new html_table();
288
        $table->head  = [
289
            get_string('userfieldexternalfield', 'tool_oauth2'),
290
            get_string('userfieldinternalfield', 'tool_oauth2'),
291
            get_string('edit'),
292
        ];
293
        $table->attributes['class'] = 'admintable generaltable';
294
        $data = [];
295
 
296
        $index = 0;
297
 
298
        foreach ($userfieldmappings as $userfieldmapping) {
299
            // External field.
300
            $externalfield = $userfieldmapping->get('externalfield');
301
            $externalfieldcell = new html_table_cell(s($externalfield));
302
 
303
            // Internal field.
304
            $internalfield = $userfieldmapping->get('internalfield');
305
            $internalfieldcell = new html_table_cell(s($internalfield));
306
 
307
            $links = '';
308
            // Action links.
309
            $editparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'edit'];
310
            $editurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $editparams);
311
            $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
312
            $links .= ' ' . $editlink;
313
 
314
            // Delete.
315
            $deleteparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'delete'];
316
            $deleteurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $deleteparams);
317
            $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
318
            $links .= ' ' . $deletelink;
319
 
320
            $editcell = new html_table_cell($links);
321
 
322
            $row = new html_table_row([
323
                $externalfieldcell,
324
                $internalfieldcell,
325
                $editcell,
326
            ]);
327
 
328
            $data[] = $row;
329
            $index++;
330
        }
331
        $table->data = $data;
332
        return html_writer::table($table);
333
    }
334
}