| 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,
 | 
        
           | 1441 | ariadna | 69 |                                 $editable = true, ?array $ajaxformdata = null) {
 | 
        
           | 1 | efrain | 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'),
 | 
        
           | 1441 | ariadna | 132 |             \core\oauth2\issuer::SMTPWITHXOAUTH2 => get_string('issueruseinsmtpwithoauth', 'tool_oauth2'),
 | 
        
           | 1 | efrain | 133 |         ];
 | 
        
           |  |  | 134 |         $mform->addElement('select', 'showonloginpage', get_string('issuerusein', 'tool_oauth2'), $options);
 | 
        
           |  |  | 135 |         $mform->addHelpButton('showonloginpage', 'issuerusein', 'tool_oauth2');
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |         // Name on login page.
 | 
        
           |  |  | 138 |         $mform->addElement('text', 'loginpagename', get_string('issuerloginpagename', 'tool_oauth2'));
 | 
        
           |  |  | 139 |         $mform->addRule('loginpagename', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 | 
        
           |  |  | 140 |         $mform->addHelpButton('loginpagename', 'issuerloginpagename', 'tool_oauth2');
 | 
        
           |  |  | 141 |         $mform->hideIf('loginpagename', 'showonloginpage', 'eq', \core\oauth2\issuer::SERVICEONLY);
 | 
        
           | 1441 | ariadna | 142 |         $mform->hideIf('loginpagename', 'showonloginpage', 'eq', \core\oauth2\issuer::SMTPWITHXOAUTH2);
 | 
        
           | 1 | efrain | 143 |   | 
        
           | 1441 | ariadna | 144 |         // Connected email for XOAUTH2.
 | 
        
           |  |  | 145 |         $mform->addElement('text', 'systememail', get_string('issuersmtpsystememail', 'tool_oauth2'));
 | 
        
           |  |  | 146 |         $mform->setType('systememail', PARAM_EMAIL);
 | 
        
           |  |  | 147 |         $mform->addHelpButton('systememail', 'issuersmtpsystememail', 'tool_oauth2');
 | 
        
           |  |  | 148 |         $mform->hideIf('systememail', 'showonloginpage', 'ne', \core\oauth2\issuer::SMTPWITHXOAUTH2);
 | 
        
           |  |  | 149 |   | 
        
           | 1 | efrain | 150 |         // Login scopes.
 | 
        
           |  |  | 151 |         $mform->addElement('text', 'loginscopes', get_string('issuerloginscopes', 'tool_oauth2'));
 | 
        
           |  |  | 152 |         $mform->addRule('loginscopes', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 | 
        
           |  |  | 153 |         $mform->addHelpButton('loginscopes', 'issuerloginscopes', 'tool_oauth2');
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |         // Login scopes offline.
 | 
        
           |  |  | 156 |         $mform->addElement('text', 'loginscopesoffline', get_string('issuerloginscopesoffline', 'tool_oauth2'));
 | 
        
           |  |  | 157 |         $mform->addRule('loginscopesoffline', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 | 
        
           |  |  | 158 |         $mform->addHelpButton('loginscopesoffline', 'issuerloginscopesoffline', 'tool_oauth2');
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 |         // Login params.
 | 
        
           |  |  | 161 |         $mform->addElement('text', 'loginparams', get_string('issuerloginparams', 'tool_oauth2'));
 | 
        
           |  |  | 162 |         $mform->addRule('loginparams', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 | 
        
           |  |  | 163 |         $mform->addHelpButton('loginparams', 'issuerloginparams', 'tool_oauth2');
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |         // Login params offline.
 | 
        
           |  |  | 166 |         $mform->addElement('text', 'loginparamsoffline', get_string('issuerloginparamsoffline', 'tool_oauth2'));
 | 
        
           |  |  | 167 |         $mform->addRule('loginparamsoffline', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 | 
        
           |  |  | 168 |         $mform->addHelpButton('loginparamsoffline', 'issuerloginparamsoffline', 'tool_oauth2');
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 |         // Allowed Domains.
 | 
        
           |  |  | 171 |         $mform->addElement('text', 'alloweddomains', get_string('issueralloweddomains', 'tool_oauth2'));
 | 
        
           |  |  | 172 |         $mform->addRule('alloweddomains', get_string('maximumchars', '', 1024), 'maxlength', 1024, 'client');
 | 
        
           |  |  | 173 |         $mform->addHelpButton('alloweddomains', 'issueralloweddomains', 'tool_oauth2');
 | 
        
           |  |  | 174 |         $mform->hideIf('alloweddomains', 'showonloginpage', 'eq', \core\oauth2\issuer::SERVICEONLY);
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         // Require confirmation email for new accounts.
 | 
        
           |  |  | 177 |         $mform->addElement('advcheckbox', 'requireconfirmation',
 | 
        
           |  |  | 178 |                 get_string('issuerrequireconfirmation', 'tool_oauth2'));
 | 
        
           |  |  | 179 |         $mform->addHelpButton('requireconfirmation', 'issuerrequireconfirmation', 'tool_oauth2');
 | 
        
           |  |  | 180 |         $mform->hideIf('requireconfirmation', 'showonloginpage',
 | 
        
           |  |  | 181 |                 'eq', \core\oauth2\issuer::SERVICEONLY);
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |         $mform->addElement('checkbox', 'acceptrisk', get_string('acceptrisk', 'tool_oauth2'));
 | 
        
           |  |  | 184 |         $mform->addHelpButton('acceptrisk', 'acceptrisk', 'tool_oauth2');
 | 
        
           |  |  | 185 |         $mform->hideIf('acceptrisk', 'showonloginpage',
 | 
        
           |  |  | 186 |                 'eq', \core\oauth2\issuer::SERVICEONLY);
 | 
        
           |  |  | 187 |         $mform->hideIf('acceptrisk', 'requireconfirmation', 'checked');
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |         if ($this->type == 'imsobv2p1' || $issuer->get('servicetype') == 'imsobv2p1'
 | 
        
           |  |  | 191 |                 || $this->type == 'moodlenet' || $issuer->get('servicetype') == 'moodlenet') {
 | 
        
           |  |  | 192 |             $mform->addRule('baseurl', null, 'required', null, 'client');
 | 
        
           |  |  | 193 |         } else {
 | 
        
           |  |  | 194 |             $mform->addRule('clientid', null, 'required', null, 'client');
 | 
        
           |  |  | 195 |             $mform->addRule('clientsecret', null, 'required', null, 'client');
 | 
        
           |  |  | 196 |         }
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |         $mform->addElement('hidden', 'sortorder');
 | 
        
           |  |  | 199 |         $mform->setType('sortorder', PARAM_INT);
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |         $mform->addElement('hidden', 'servicetype');
 | 
        
           |  |  | 202 |         $mform->setType('servicetype', PARAM_ALPHANUM);
 | 
        
           |  |  | 203 |   | 
        
           |  |  | 204 |         if ($this->type) {
 | 
        
           |  |  | 205 |             $mform->addElement('hidden', 'action', 'savetemplate');
 | 
        
           |  |  | 206 |             $mform->setType('action', PARAM_ALPHA);
 | 
        
           |  |  | 207 |   | 
        
           |  |  | 208 |             $mform->addElement('hidden', 'type', $this->_customdata['type']);
 | 
        
           |  |  | 209 |             $mform->setType('type', PARAM_ALPHANUM);
 | 
        
           |  |  | 210 |         } else {
 | 
        
           |  |  | 211 |             $mform->addElement('hidden', 'action', 'edit');
 | 
        
           |  |  | 212 |             $mform->setType('action', PARAM_ALPHA);
 | 
        
           |  |  | 213 |         }
 | 
        
           |  |  | 214 |   | 
        
           |  |  | 215 |         $mform->addElement('hidden', 'enabled', $issuer->get('enabled'));
 | 
        
           |  |  | 216 |         $mform->setType('enabled', PARAM_BOOL);
 | 
        
           |  |  | 217 |   | 
        
           |  |  | 218 |         $mform->addElement('hidden', 'id', $issuer->get('id'));
 | 
        
           |  |  | 219 |         $mform->setType('id', PARAM_INT);
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 |         $this->add_action_buttons(true, get_string('savechanges', 'tool_oauth2'));
 | 
        
           |  |  | 222 |     }
 | 
        
           |  |  | 223 |   | 
        
           |  |  | 224 |     /**
 | 
        
           |  |  | 225 |      * This method implements changes to the form that need to be made once the form data is set.
 | 
        
           |  |  | 226 |      */
 | 
        
           |  |  | 227 |     public function definition_after_data() {
 | 
        
           |  |  | 228 |         $mform = $this->_form;
 | 
        
           |  |  | 229 |   | 
        
           |  |  | 230 |         if ($this->type) {
 | 
        
           |  |  | 231 |             // Set servicetype if it's defined.
 | 
        
           |  |  | 232 |             $mform->getElement('servicetype')->setValue($this->type);
 | 
        
           |  |  | 233 |         }
 | 
        
           |  |  | 234 |     }
 | 
        
           |  |  | 235 |   | 
        
           |  |  | 236 |     /**
 | 
        
           |  |  | 237 |      * Define extra validation mechanims.
 | 
        
           |  |  | 238 |      *
 | 
        
           |  |  | 239 |      * The data here:
 | 
        
           |  |  | 240 |      * - does not include {@see self::$fieldstoremove}.
 | 
        
           |  |  | 241 |      * - does include {@see self::$foreignfields}.
 | 
        
           |  |  | 242 |      * - was converted to map persistent-like data, e.g. array $description to string $description + int $descriptionformat.
 | 
        
           |  |  | 243 |      *
 | 
        
           |  |  | 244 |      * You can modify the $errors parameter in order to remove some validation errors should you
 | 
        
           |  |  | 245 |      * need to. However, the best practice is to return new or overriden errors. Only modify the
 | 
        
           |  |  | 246 |      * errors passed by reference when you have no other option.
 | 
        
           |  |  | 247 |      *
 | 
        
           |  |  | 248 |      * Do not add any logic here, it is only intended to be used by child classes.
 | 
        
           |  |  | 249 |      *
 | 
        
           |  |  | 250 |      * @param  stdClass $data Data to validate.
 | 
        
           |  |  | 251 |      * @param  array $files Array of files.
 | 
        
           |  |  | 252 |      * @param  array $errors Currently reported errors.
 | 
        
           |  |  | 253 |      * @return array of additional errors, or overridden errors.
 | 
        
           |  |  | 254 |      */
 | 
        
           |  |  | 255 |     protected function extra_validation($data, $files, array &$errors) {
 | 
        
           |  |  | 256 |         if ($data->showonloginpage != \core\oauth2\issuer::SERVICEONLY) {
 | 
        
           |  |  | 257 |             if (!strlen(trim($data->loginscopes))) {
 | 
        
           |  |  | 258 |                 $errors['loginscopes'] = get_string('required');
 | 
        
           |  |  | 259 |             }
 | 
        
           |  |  | 260 |             if (!strlen(trim($data->loginscopesoffline))) {
 | 
        
           |  |  | 261 |                 $errors['loginscopesoffline'] = get_string('required');
 | 
        
           |  |  | 262 |             }
 | 
        
           |  |  | 263 |             if (empty($data->requireconfirmation) && empty($data->acceptrisk)) {
 | 
        
           |  |  | 264 |                 $errors['acceptrisk'] = get_string('required');
 | 
        
           |  |  | 265 |             }
 | 
        
           |  |  | 266 |         }
 | 
        
           |  |  | 267 |         return $errors;
 | 
        
           |  |  | 268 |     }
 | 
        
           |  |  | 269 | }
 |