Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| KvLtiUserService | |
0.00% |
0 / 44 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| getPersistence | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| updateUser | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
12 | |||
| getUserIdentifier | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getUserDataFromId | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program 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 this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoLti\models\classes\user; |
| 24 | |
| 25 | use common_Logger; |
| 26 | use oat\generis\model\OntologyRdfs; |
| 27 | use oat\taoLti\models\classes\LtiLaunchData; |
| 28 | |
| 29 | /** |
| 30 | * Key value implementation of the lti user service |
| 31 | * |
| 32 | * @access public |
| 33 | * @author Antoine Robin, <antoine@taotesting.com> |
| 34 | * @package taoLti |
| 35 | */ |
| 36 | class KvLtiUserService extends LtiUserService |
| 37 | { |
| 38 | public const OPTION_PERSISTENCE = 'persistence'; |
| 39 | |
| 40 | public const LTI_USER = 'lti_ku_'; |
| 41 | |
| 42 | public const LTI_USER_LOOKUP = 'lti_ku_lkp_'; |
| 43 | |
| 44 | /** |
| 45 | * @var \common_persistence_KeyValuePersistence |
| 46 | */ |
| 47 | private $persistence; |
| 48 | |
| 49 | /** |
| 50 | * @return \common_persistence_KeyValuePersistence|\common_persistence_Persistence |
| 51 | */ |
| 52 | protected function getPersistence() |
| 53 | { |
| 54 | if (is_null($this->persistence)) { |
| 55 | $persistenceOption = $this->getOption(self::OPTION_PERSISTENCE); |
| 56 | $this->persistence = (is_object($persistenceOption)) |
| 57 | ? $persistenceOption |
| 58 | : \common_persistence_KeyValuePersistence::getPersistence($persistenceOption); |
| 59 | } |
| 60 | return $this->persistence; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param LtiUser $user |
| 65 | * @param LtiLaunchData $ltiContext |
| 66 | * @return mixed|void |
| 67 | * @throws \common_Exception |
| 68 | * @throws \oat\taoLti\models\classes\LtiVariableMissingException |
| 69 | */ |
| 70 | protected function updateUser(LtiUserInterface $user, LtiLaunchData $ltiContext) |
| 71 | { |
| 72 | $technicalId = self::LTI_USER . $ltiContext->getUserID() . $ltiContext->getLtiConsumer()->getUri(); |
| 73 | $isUpdate = $this->getPersistence()->exists($technicalId); |
| 74 | |
| 75 | if (empty($user->getIdentifier())) { |
| 76 | $user->setIdentifier($technicalId); |
| 77 | } |
| 78 | |
| 79 | $taoUserId = $user->getIdentifier(); |
| 80 | |
| 81 | $data = $user->jsonSerialize(); |
| 82 | $data[self::PROPERTY_USER_LTIKEY] = $ltiContext->getUserID(); |
| 83 | $data[self::PROPERTY_USER_LTICONSUMER] = $ltiContext->getLtiConsumer()->getOnePropertyValue( |
| 84 | new \core_kernel_classes_Property(OntologyRdfs::RDFS_LABEL) |
| 85 | )->literal; |
| 86 | |
| 87 | $this->getPersistence()->set($technicalId, json_encode($data)); |
| 88 | $this->getPersistence()->set(self::LTI_USER_LOOKUP . $taoUserId, $technicalId); |
| 89 | |
| 90 | if ($isUpdate) { |
| 91 | $this->userUpdatedEvent($technicalId); |
| 92 | } else { |
| 93 | $this->userCreatedEvent($technicalId); |
| 94 | $this->logInfo( |
| 95 | sprintf( |
| 96 | 'added User %s, LTI user Id: %s, LTI consumer %s', |
| 97 | $data[OntologyRdfs::RDFS_LABEL], |
| 98 | $data[self::PROPERTY_USER_LTIKEY], |
| 99 | $data[self::PROPERTY_USER_LTICONSUMER] |
| 100 | ) |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @inheritdoc |
| 107 | */ |
| 108 | public function getUserIdentifier($ltiUserId, $consumer) |
| 109 | { |
| 110 | $data = $this->getPersistence()->get(self::LTI_USER . $ltiUserId . $consumer); |
| 111 | if ($data === false) { |
| 112 | return null; |
| 113 | } |
| 114 | $decodedData = json_decode($data, true); |
| 115 | if (isset($decodedData[LtiUser::USER_IDENTIFIER])) { |
| 116 | return $decodedData[LtiUser::USER_IDENTIFIER]; |
| 117 | } |
| 118 | |
| 119 | return self::LTI_USER . $ltiUserId . $consumer; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * @inheritdoc |
| 125 | */ |
| 126 | public function getUserDataFromId($taoUserId) |
| 127 | { |
| 128 | $id = $this->getPersistence()->get(self::LTI_USER_LOOKUP . $taoUserId); |
| 129 | if ($id !== false) { |
| 130 | $data = $this->getPersistence()->get($id); |
| 131 | } else { |
| 132 | $data = $this->getPersistence()->get($taoUserId); |
| 133 | if ($data === false) { |
| 134 | return null; |
| 135 | } |
| 136 | $this->getPersistence()->set(self::LTI_USER_LOOKUP . $taoUserId, $taoUserId); |
| 137 | } |
| 138 | |
| 139 | return json_decode($data, true); |
| 140 | } |
| 141 | } |