Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RdsUserImportService | |
0.00% |
0 / 34 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| persist | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| getUserClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| triggerUserUpdated | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| mergeUserProperties | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\user\import; |
| 22 | |
| 23 | use oat\generis\Helper\UserHashForEncryption; |
| 24 | use oat\generis\model\OntologyAwareTrait; |
| 25 | use oat\generis\model\user\UserRdf; |
| 26 | use oat\oatbox\event\EventManager; |
| 27 | use oat\tao\model\event\UserUpdatedEvent; |
| 28 | use oat\tao\model\import\service\AbstractImportService; |
| 29 | use oat\tao\model\import\service\ImportMapperInterface; |
| 30 | use oat\tao\model\TaoOntology; |
| 31 | |
| 32 | class RdsUserImportService extends AbstractImportService |
| 33 | { |
| 34 | use OntologyAwareTrait; |
| 35 | |
| 36 | /** |
| 37 | * Persist a user, create or update |
| 38 | * |
| 39 | * @param ImportMapperInterface $userMapper |
| 40 | * @return \core_kernel_classes_Resource |
| 41 | * @throws \Exception |
| 42 | */ |
| 43 | protected function persist(ImportMapperInterface $userMapper) |
| 44 | { |
| 45 | if (!$userMapper instanceof UserMapperInterface) { |
| 46 | throw new \Exception('Mapper should be a UserMapper'); |
| 47 | } |
| 48 | |
| 49 | $plainPassword = $userMapper->getPlainPassword(); |
| 50 | $properties = $userMapper->getProperties(); |
| 51 | |
| 52 | $class = $this->getUserClass($properties); |
| 53 | |
| 54 | $results = $class->searchInstances( |
| 55 | [ |
| 56 | UserRdf::PROPERTY_LOGIN => $properties[UserRdf::PROPERTY_LOGIN] |
| 57 | ], |
| 58 | [ |
| 59 | 'like' => false, |
| 60 | 'recursive' => true |
| 61 | ] |
| 62 | ); |
| 63 | |
| 64 | if (count($results) > 0) { |
| 65 | $resource = $this->mergeUserProperties(current($results), $properties); |
| 66 | } else { |
| 67 | $resource = $class->createInstanceWithProperties($properties); |
| 68 | } |
| 69 | |
| 70 | $this->triggerUserUpdated($resource, $properties, $plainPassword); |
| 71 | |
| 72 | return $resource; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the user class |
| 77 | * |
| 78 | * @param array $properties |
| 79 | * @return \core_kernel_classes_Class |
| 80 | */ |
| 81 | protected function getUserClass(array $properties) |
| 82 | { |
| 83 | return $this->getClass(TaoOntology::CLASS_URI_TAO_USER); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Trigger UserEvent at user update |
| 88 | * |
| 89 | * @param \core_kernel_classes_Resource $resource |
| 90 | * @param array $properties |
| 91 | * @param string $plainPassword |
| 92 | */ |
| 93 | protected function triggerUserUpdated(\core_kernel_classes_Resource $resource, array $properties, $plainPassword) |
| 94 | { |
| 95 | /** @var EventManager $eventManager */ |
| 96 | $eventManager = $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 97 | $eventManager->trigger(new UserUpdatedEvent( |
| 98 | $resource, |
| 99 | array_merge( |
| 100 | $properties, |
| 101 | [ |
| 102 | 'hashForKey' => UserHashForEncryption::hash($plainPassword) |
| 103 | ] |
| 104 | ) |
| 105 | )); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Flush rdf properties to a resource |
| 110 | * - delete old |
| 111 | * - insert new |
| 112 | * |
| 113 | * @param \core_kernel_classes_Resource $user |
| 114 | * @param $properties |
| 115 | * @return \core_kernel_classes_Resource |
| 116 | */ |
| 117 | protected function mergeUserProperties(\core_kernel_classes_Resource $user, $properties) |
| 118 | { |
| 119 | foreach ($properties as $property => $value) { |
| 120 | $user->removePropertyValues($this->getProperty($property)); |
| 121 | $user->editPropertyValues($this->getProperty($property), $value); |
| 122 | } |
| 123 | |
| 124 | return $user; |
| 125 | } |
| 126 | } |