See Release Notes
Long Term Support Release
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 namespace enrol_lti\local\ltiadvantage\service; 18 19 use enrol_lti\local\ltiadvantage\entity\application_registration; 20 use enrol_lti\local\ltiadvantage\repository\application_registration_repository; 21 use enrol_lti\local\ltiadvantage\repository\context_repository; 22 use enrol_lti\local\ltiadvantage\repository\deployment_repository; 23 use enrol_lti\local\ltiadvantage\repository\resource_link_repository; 24 use enrol_lti\local\ltiadvantage\repository\user_repository; 25 26 /** 27 * Class application_registration_service. 28 * 29 * @package enrol_lti 30 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class application_registration_service { 34 /** @var application_registration_repository repository to work with application_registration instances. */ 35 private $appregistrationrepo; 36 37 /** @var deployment_repository repository to work with deployment instances. */ 38 private $deploymentrepo; 39 40 /** @var resource_link_repository repository to work with resource link instances. */ 41 private $resourcelinkrepo; 42 43 /** @var context_repository repository to work with context instances. */ 44 private $contextrepo; 45 46 /** @var user_repository repository to work with user instances. */ 47 private $userrepo; 48 49 /** 50 * The application_registration_service constructor. 51 * 52 * @param application_registration_repository $appregistrationrepo an application registration repository instance. 53 * @param deployment_repository $deploymentrepo a deployment repository instance. 54 * @param resource_link_repository $resourcelinkrepo a resource_link_repository instance. 55 * @param context_repository $contextrepo a context_repository instance. 56 * @param user_repository $userrepo a user_repository instance. 57 */ 58 public function __construct(application_registration_repository $appregistrationrepo, 59 deployment_repository $deploymentrepo, resource_link_repository $resourcelinkrepo, 60 context_repository $contextrepo, user_repository $userrepo) { 61 62 $this->appregistrationrepo = $appregistrationrepo; 63 $this->deploymentrepo = $deploymentrepo; 64 $this->resourcelinkrepo = $resourcelinkrepo; 65 $this->contextrepo = $contextrepo; 66 $this->userrepo = $userrepo; 67 } 68 69 /** 70 * Convert a DTO into a new application_registration domain object. 71 * 72 * @param \stdClass $dto the object containing information needed to register an application. 73 * @return application_registration the application_registration object 74 */ 75 private function registration_from_dto(\stdClass $dto): application_registration { 76 $registration = $this->appregistrationrepo->find($dto->id); 77 $registration->set_name($dto->name); 78 $registration->set_platformid(new \moodle_url($dto->platformid)); 79 $registration->set_clientid($dto->clientid); 80 $registration->set_accesstokenurl(new \moodle_url($dto->accesstokenurl)); 81 $registration->set_jwksurl(new \moodle_url($dto->jwksurl)); 82 $registration->set_authenticationrequesturl(new \moodle_url($dto->authenticationrequesturl)); 83 $registration->complete_registration(); 84 return $registration; 85 } 86 87 /** 88 * Gets a unique id for the registration, with uniqueness guaranteed with a lookup. 89 * 90 * @return string the unique id. 91 */ 92 private function get_unique_id(): string { 93 global $DB; 94 do { 95 $bytes = random_bytes(30); 96 $uniqueid = bin2hex($bytes); 97 } while ($DB->record_exists('enrol_lti_app_registration', ['uniqueid' => $uniqueid])); 98 99 return $uniqueid; 100 } 101 102 /** 103 * Convert a DTO into a new DRAFT application_registration domain object. 104 * 105 * @param \stdClass $dto the object containing information needed to create the draft registration. 106 * @return application_registration the draft application_registration object 107 */ 108 private function draft_registration_from_dto(\stdClass $dto): application_registration { 109 return application_registration::create_draft( 110 $dto->name, 111 $this->get_unique_id() 112 ); 113 } 114 115 /** 116 * Application service handling the use case "As an admin I can create a draft platform registration". 117 * 118 * @param \stdClass $appregdto details of the draft application to create. 119 * @return application_registration the application_registration domain object. 120 * @throws \coding_exception if the DTO doesn't contain required fields. 121 */ 122 public function create_draft_application_registration(\stdClass $appregdto): application_registration { 123 if (empty($appregdto->name)) { 124 throw new \coding_exception('Cannot create draft registration. Name is missing.'); 125 } 126 $draftregistration = $this->draft_registration_from_dto($appregdto); 127 return $this->appregistrationrepo->save($draftregistration); 128 } 129 130 /** 131 * Application service handling the use case "As an admin I can update the registration of an LTI platform". 132 * 133 * @param \stdClass $appregdto details of the registration to update. 134 * @return application_registration the application_registration domain object. 135 */ 136 public function update_application_registration(\stdClass $appregdto): application_registration { 137 if (empty($appregdto->id)) { 138 throw new \coding_exception('Cannot update registration. Id is missing.'); 139 } 140 return $this->appregistrationrepo->save($this->registration_from_dto($appregdto)); 141 } 142 143 /** 144 * Application service handling the use case "As an admin I can delete a registration of an LTI platform". 145 * 146 * @param int $registrationid id of the registration to delete. 147 */ 148 public function delete_application_registration(int $registrationid): void { 149 150 $deployments = $this->deploymentrepo->find_all_by_registration($registrationid); 151 if ($deployments) { 152 $deploymentservice = new tool_deployment_service( 153 $this->appregistrationrepo, 154 $this->deploymentrepo, 155 $this->resourcelinkrepo, 156 $this->contextrepo, 157 $this->userrepo 158 ); 159 foreach ($deployments as $deployment) { 160 $deploymentservice->delete_tool_deployment($deployment->get_id()); 161 } 162 } 163 164 $this->appregistrationrepo->delete($registrationid); 165 } 166 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body