Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OntologyLtiUserService
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 updateUser
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
30
 getUserIdentifier
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 getUserDataFromId
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
56
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
23namespace oat\taoLti\models\classes\user;
24
25use oat\generis\model\GenerisRdf;
26use oat\generis\model\OntologyRdfs;
27use oat\taoLti\models\classes\LtiException;
28use oat\taoLti\models\classes\LtiLaunchData;
29use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage;
30use oat\generis\model\data\ModelManager;
31use oat\taoLti\models\classes\LtiVariableMissingException;
32
33/**
34 * Ontology implementation of the lti user service
35 *
36 * @access public
37 * @author Antoine Robin, <antoine@taotesting.com>
38 * @package taoLti
39 */
40class OntologyLtiUserService extends LtiUserService
41{
42    public const CLASS_LTI_USER = 'http://www.tao.lu/Ontologies/TAOLTI.rdf#LTIUser';
43
44    /**
45     * @deprecated no longer serves any purpose
46     */
47    public const OPTION_TRANSACTION_SAFE = 'transaction-safe';
48
49    /**
50     * @deprecated no longer serves any purpose
51     */
52    public const OPTION_TRANSACTION_SAFE_RETRY = 'transaction-safe-retry';
53
54    /**
55     * @TODO TT-273 split method in separate action (create and update)
56     * @param LtiUser $user
57     * @param LtiLaunchData $ltiContext
58     * @return mixed|void
59     * @throws \common_exception_Error
60     * @throws \common_exception_InvalidArgumentType
61     * @throws LtiVariableMissingException
62     */
63    protected function updateUser(LtiUserInterface $user, LtiLaunchData $ltiContext)
64    {
65        $userResource = new \core_kernel_classes_Resource($user->getIdentifier());
66
67        if ($userResource->exists()) {
68            $properties = $userResource->getPropertiesValues([
69                GenerisRdf::PROPERTY_USER_UILG,
70                GenerisRdf::PROPERTY_USER_FIRSTNAME,
71                GenerisRdf::PROPERTY_USER_LASTNAME,
72                GenerisRdf::PROPERTY_USER_MAIL,
73                GenerisRdf::PROPERTY_USER_ROLES,
74            ]);
75
76            $hasUpdates = false;
77            foreach ($properties as $key => $values) {
78                if ($values != $user->getPropertyValues($key)) {
79                    $userResource->editPropertyValues(
80                        new \core_kernel_classes_Property($key),
81                        $user->getPropertyValues($key)
82                    );
83                    $hasUpdates = true;
84                }
85            }
86
87            if ($hasUpdates) {
88                $this->userUpdatedEvent($userResource->getUri());
89            }
90        } else {
91            $class = new \core_kernel_classes_Class(self::CLASS_LTI_USER);
92
93            $props = [
94                self::PROPERTY_USER_LTIKEY => $ltiContext->getUserID(),
95                self::PROPERTY_USER_LTICONSUMER => $ltiContext->getLtiConsumer(),
96                GenerisRdf::PROPERTY_USER_UILG => $user->getPropertyValues(GenerisRdf::PROPERTY_USER_UILG),
97                OntologyRdfs::RDFS_LABEL => $user->getPropertyValues(OntologyRdfs::RDFS_LABEL),
98                GenerisRdf::PROPERTY_USER_FIRSTNAME => $user->getPropertyValues(GenerisRdf::PROPERTY_USER_FIRSTNAME),
99                GenerisRdf::PROPERTY_USER_LASTNAME => $user->getPropertyValues(GenerisRdf::PROPERTY_USER_LASTNAME),
100                GenerisRdf::PROPERTY_USER_MAIL => $user->getPropertyValues(GenerisRdf::PROPERTY_USER_MAIL),
101                GenerisRdf::PROPERTY_USER_ROLES => $user->getPropertyValues(GenerisRdf::PROPERTY_USER_ROLES),
102            ];
103
104            $userResource = $class->createInstanceWithProperties($props);
105            $this->logInfo(
106                sprintf(
107                    'added User %s, LTI user Id: %s, LTI consumer %s',
108                    $userResource->getLabel(),
109                    $props[self::PROPERTY_USER_LTIKEY],
110                    $props[self::PROPERTY_USER_LTICONSUMER]
111                )
112            );
113            $this->userCreatedEvent($userResource->getUri());
114        }
115        $user->setIdentifier($userResource->getUri());
116    }
117
118
119    /**
120     * @inheritdoc
121     */
122    public function getUserIdentifier($ltiUserId, $ltiConsumer)
123    {
124        $class = new \core_kernel_classes_Class(self::CLASS_LTI_USER);
125        $instances = $class->searchInstances([
126            self::PROPERTY_USER_LTIKEY => $ltiUserId,
127            self::PROPERTY_USER_LTICONSUMER => $ltiConsumer
128        ], [
129            'like' => false
130        ]);
131        if (count($instances) > 1) {
132            throw new LtiException(
133                'Multiple user accounts found for user key \'' . $ltiUserId . '\'',
134                LtiErrorMessage::ERROR_SYSTEM_ERROR
135            );
136        }
137        /** @var \core_kernel_classes_Resource $instance */
138        if (count($instances) == 1) {
139            $instance = current($instances);
140            return $instance->getUri();
141        } else {
142            return null;
143        }
144    }
145
146    /**
147     * @inheritdoc
148     */
149    public function getUserDataFromId($taoUserId)
150    {
151        $user = new \core_kernel_classes_Resource($taoUserId);
152        if ($user->exists()) {
153            $properties = $user->getPropertiesValues([
154                GenerisRdf::PROPERTY_USER_UILG,
155                OntologyRdfs::RDFS_LABEL,
156                GenerisRdf::PROPERTY_USER_FIRSTNAME,
157                GenerisRdf::PROPERTY_USER_LASTNAME,
158                GenerisRdf::PROPERTY_USER_MAIL,
159                GenerisRdf::PROPERTY_USER_ROLES
160            ]);
161
162            $userData = [];
163            foreach ($properties as $key => $values) {
164                if (count($values) > 1) {
165                    foreach ($values as $value) {
166                        $userData[$key][] = ($value instanceof \core_kernel_classes_Resource)
167                            ? $value->getUri()
168                            : (string) $value;
169                    }
170                } else {
171                    $value = current($values);
172                    $userData[$key] = ($value instanceof \core_kernel_classes_Resource)
173                        ? $value->getUri()
174                        : (string) $value;
175                }
176            }
177
178            return $userData;
179        }
180
181        return null;
182    }
183}