Proyectos de Subversion Moodle

Rev

| 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
 * This file contains the form add/update oauth2 issuer.
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
 
25
namespace tool_oauth2\form;
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use stdClass;
29
use core\form\persistent;
30
 
31
/**
32
 * Issuer form.
33
 *
34
 * @package   tool_oauth2
35
 * @copyright 2017 Damyon Wiese
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class issuer extends persistent {
39
 
40
    /** @var string $persistentclass */
41
    protected static $persistentclass = 'core\\oauth2\\issuer';
42
 
43
    /** @var array $fieldstoremove */
44
    protected static $fieldstoremove = array('type', 'submitbutton', 'action');
45
 
46
    /** @var string $type */
47
    protected $type;
48
 
49
    /**
50
     * Constructor.
51
     *
52
     * The 'persistent' has to be passed as custom data when 'editing'.
53
     * If a standard issuer is created the type can be passed as custom data, which alters the form according to the
54
     * type.
55
     *
56
     * Note that in order for your persistent to be reloaded after form submission you should
57
     * either override the URL to include the ID to your resource, or add the ID to the form
58
     * fields.
59
     *
60
     * @param mixed $action
61
     * @param mixed $customdata
62
     * @param string $method
63
     * @param string $target
64
     * @param mixed $attributes
65
     * @param bool $editable
66
     * @param array $ajaxformdata
67
     */
68
    public function __construct($action = null, $customdata = null, $method = 'post', $target = '', $attributes = null,
69
                                $editable = true, array $ajaxformdata = null) {
70
        // The type variable defines, if we are in the creation process of a standard issuer.
71
        if (array_key_exists('type', $customdata)) {
72
            $this->type = $customdata['type'];
73
        }
74
        parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
75
    }
76
 
77
    /**
78
     * Define the form - called by parent constructor
79
     */
80
    public function definition() {
81
        global $PAGE, $OUTPUT;
82
 
83
        $mform = $this->_form;
84
        $issuer = $this->get_persistent();
85
 
86
        $docslink = optional_param('docslink', '', PARAM_ALPHAEXT);
87
        if ($docslink) {
88
            $name = s($issuer->get('name'));
89
            $mform->addElement('html', $OUTPUT->doc_link($docslink, get_string('issuersetuptype', 'tool_oauth2', $name)));
90
        } else {
91
            $mform->addElement('html', $OUTPUT->page_doc_link(get_string('issuersetup', 'tool_oauth2')));
92
        }
93
 
94
        // Name.
95
        $mform->addElement('text', 'name', get_string('issuername', 'tool_oauth2'));
96
        $mform->addRule('name', null, 'required', null, 'client');
97
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
98
        $mform->addHelpButton('name', 'issuername', 'tool_oauth2');
99
 
100
        // Client ID.
101
        $mform->addElement('text', 'clientid', get_string('issuerclientid', 'tool_oauth2'));
102
        $mform->addRule('clientid', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
103
        $mform->addHelpButton('clientid', 'issuerclientid', 'tool_oauth2');
104
 
105
        // Client Secret.
106
        $mform->addElement('text', 'clientsecret', get_string('issuerclientsecret', 'tool_oauth2'));
107
        $mform->addRule('clientsecret', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
108
        $mform->addHelpButton('clientsecret', 'issuerclientsecret', 'tool_oauth2');
109
 
110
        // Use basic authentication.
111
        $mform->addElement('advcheckbox', 'basicauth', get_string('usebasicauth', 'tool_oauth2'));
112
        $mform->addHelpButton('basicauth', 'usebasicauth', 'tool_oauth2');
113
 
114
        // Base Url.
115
        $mform->addElement('text', 'baseurl', get_string('issuerbaseurl', 'tool_oauth2'));
116
        $mform->addRule('baseurl', get_string('maximumchars', '', 1024), 'maxlength', 1024, 'client');
117
        $mform->addHelpButton('baseurl', 'issuerbaseurl', 'tool_oauth2');
118
        if ($this->type && $this->type == 'nextcloud') {
119
            $mform->addRule('baseurl', null, 'required', null, 'client');
120
        }
121
 
122
        // Image.
123
        $mform->addElement('text', 'image', get_string('issuerimage', 'tool_oauth2'), 'maxlength="1024"');
124
        $mform->addRule('image', get_string('maximumchars', '', 1024), 'maxlength', 1024, 'client');
125
        $mform->addHelpButton('image', 'issuername', 'tool_oauth2');
126
 
127
        // Show on login page.
128
        $options = [
129
            \core\oauth2\issuer::EVERYWHERE => get_string('issueruseineverywhere', 'tool_oauth2'),
130
            \core\oauth2\issuer::LOGINONLY => get_string('issueruseinloginonly', 'tool_oauth2'),
131
            \core\oauth2\issuer::SERVICEONLY => get_string('issueruseininternalonly', 'tool_oauth2'),
132
        ];
133
        $mform->addElement('select', 'showonloginpage', get_string('issuerusein', 'tool_oauth2'), $options);
134
        $mform->addHelpButton('showonloginpage', 'issuerusein', 'tool_oauth2');
135
 
136
        // Name on login page.
137
        $mform->addElement('text', 'loginpagename', get_string('issuerloginpagename', 'tool_oauth2'));
138
        $mform->addRule('loginpagename', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
139
        $mform->addHelpButton('loginpagename', 'issuerloginpagename', 'tool_oauth2');
140
        $mform->hideIf('loginpagename', 'showonloginpage', 'eq', \core\oauth2\issuer::SERVICEONLY);
141
 
142
        // Login scopes.
143
        $mform->addElement('text', 'loginscopes', get_string('issuerloginscopes', 'tool_oauth2'));
144
        $mform->addRule('loginscopes', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
145
        $mform->addHelpButton('loginscopes', 'issuerloginscopes', 'tool_oauth2');
146
 
147
        // Login scopes offline.
148
        $mform->addElement('text', 'loginscopesoffline', get_string('issuerloginscopesoffline', 'tool_oauth2'));
149
        $mform->addRule('loginscopesoffline', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
150
        $mform->addHelpButton('loginscopesoffline', 'issuerloginscopesoffline', 'tool_oauth2');
151
 
152
        // Login params.
153
        $mform->addElement('text', 'loginparams', get_string('issuerloginparams', 'tool_oauth2'));
154
        $mform->addRule('loginparams', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
155
        $mform->addHelpButton('loginparams', 'issuerloginparams', 'tool_oauth2');
156
 
157
        // Login params offline.
158
        $mform->addElement('text', 'loginparamsoffline', get_string('issuerloginparamsoffline', 'tool_oauth2'));
159
        $mform->addRule('loginparamsoffline', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
160
        $mform->addHelpButton('loginparamsoffline', 'issuerloginparamsoffline', 'tool_oauth2');
161
 
162
        // Allowed Domains.
163
        $mform->addElement('text', 'alloweddomains', get_string('issueralloweddomains', 'tool_oauth2'));
164
        $mform->addRule('alloweddomains', get_string('maximumchars', '', 1024), 'maxlength', 1024, 'client');
165
        $mform->addHelpButton('alloweddomains', 'issueralloweddomains', 'tool_oauth2');
166
        $mform->hideIf('alloweddomains', 'showonloginpage', 'eq', \core\oauth2\issuer::SERVICEONLY);
167
 
168
        // Require confirmation email for new accounts.
169
        $mform->addElement('advcheckbox', 'requireconfirmation',
170
                get_string('issuerrequireconfirmation', 'tool_oauth2'));
171
        $mform->addHelpButton('requireconfirmation', 'issuerrequireconfirmation', 'tool_oauth2');
172
        $mform->hideIf('requireconfirmation', 'showonloginpage',
173
                'eq', \core\oauth2\issuer::SERVICEONLY);
174
 
175
        $mform->addElement('checkbox', 'acceptrisk', get_string('acceptrisk', 'tool_oauth2'));
176
        $mform->addHelpButton('acceptrisk', 'acceptrisk', 'tool_oauth2');
177
        $mform->hideIf('acceptrisk', 'showonloginpage',
178
                'eq', \core\oauth2\issuer::SERVICEONLY);
179
        $mform->hideIf('acceptrisk', 'requireconfirmation', 'checked');
180
 
181
 
182
        if ($this->type == 'imsobv2p1' || $issuer->get('servicetype') == 'imsobv2p1'
183
                || $this->type == 'moodlenet' || $issuer->get('servicetype') == 'moodlenet') {
184
            $mform->addRule('baseurl', null, 'required', null, 'client');
185
        } else {
186
            $mform->addRule('clientid', null, 'required', null, 'client');
187
            $mform->addRule('clientsecret', null, 'required', null, 'client');
188
        }
189
 
190
        $mform->addElement('hidden', 'sortorder');
191
        $mform->setType('sortorder', PARAM_INT);
192
 
193
        $mform->addElement('hidden', 'servicetype');
194
        $mform->setType('servicetype', PARAM_ALPHANUM);
195
 
196
        if ($this->type) {
197
            $mform->addElement('hidden', 'action', 'savetemplate');
198
            $mform->setType('action', PARAM_ALPHA);
199
 
200
            $mform->addElement('hidden', 'type', $this->_customdata['type']);
201
            $mform->setType('type', PARAM_ALPHANUM);
202
        } else {
203
            $mform->addElement('hidden', 'action', 'edit');
204
            $mform->setType('action', PARAM_ALPHA);
205
        }
206
 
207
        $mform->addElement('hidden', 'enabled', $issuer->get('enabled'));
208
        $mform->setType('enabled', PARAM_BOOL);
209
 
210
        $mform->addElement('hidden', 'id', $issuer->get('id'));
211
        $mform->setType('id', PARAM_INT);
212
 
213
        $this->add_action_buttons(true, get_string('savechanges', 'tool_oauth2'));
214
    }
215
 
216
    /**
217
     * This method implements changes to the form that need to be made once the form data is set.
218
     */
219
    public function definition_after_data() {
220
        $mform = $this->_form;
221
 
222
        if ($this->type) {
223
            // Set servicetype if it's defined.
224
            $mform->getElement('servicetype')->setValue($this->type);
225
        }
226
    }
227
 
228
    /**
229
     * Define extra validation mechanims.
230
     *
231
     * The data here:
232
     * - does not include {@see self::$fieldstoremove}.
233
     * - does include {@see self::$foreignfields}.
234
     * - was converted to map persistent-like data, e.g. array $description to string $description + int $descriptionformat.
235
     *
236
     * You can modify the $errors parameter in order to remove some validation errors should you
237
     * need to. However, the best practice is to return new or overriden errors. Only modify the
238
     * errors passed by reference when you have no other option.
239
     *
240
     * Do not add any logic here, it is only intended to be used by child classes.
241
     *
242
     * @param  stdClass $data Data to validate.
243
     * @param  array $files Array of files.
244
     * @param  array $errors Currently reported errors.
245
     * @return array of additional errors, or overridden errors.
246
     */
247
    protected function extra_validation($data, $files, array &$errors) {
248
        if ($data->showonloginpage != \core\oauth2\issuer::SERVICEONLY) {
249
            if (!strlen(trim($data->loginscopes))) {
250
                $errors['loginscopes'] = get_string('required');
251
            }
252
            if (!strlen(trim($data->loginscopesoffline))) {
253
                $errors['loginscopesoffline'] = get_string('required');
254
            }
255
            if (empty($data->requireconfirmation) && empty($data->acceptrisk)) {
256
                $errors['acceptrisk'] = get_string('required');
257
            }
258
        }
259
        return $errors;
260
    }
261
}