Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
47.89% |
34 / 71 |
|
30.00% |
3 / 10 |
CRAP | |
0.00% |
0 / 1 |
LtiUser | |
47.89% |
34 / 71 |
|
30.00% |
3 / 10 |
148.02 | |
0.00% |
0 / 1 |
__construct | |
82.35% |
14 / 17 |
|
0.00% |
0 / 1 |
3.05 | |||
setRoles | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLaunchData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLanguage | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
getPropertyValues | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
110 | |||
refresh | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
determineTaoRoles | |
33.33% |
3 / 9 |
|
0.00% |
0 / 1 |
8.74 |
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) 2013-2022 (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 oat\generis\model\GenerisRdf; |
26 | use oat\generis\model\OntologyRdfs; |
27 | use oat\taoLti\models\classes\LtiInvalidVariableException; |
28 | use oat\taoLti\models\classes\LtiLaunchData; |
29 | use oat\taoLti\models\classes\LtiRoles; |
30 | use oat\taoLti\models\classes\LtiUtils; |
31 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
32 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
33 | use oat\oatbox\user\UserLanguageService; |
34 | |
35 | /** |
36 | * Authentication adapter interface to be implemented by authentication methodes |
37 | * |
38 | * @access public |
39 | * @author Joel Bout, <joel@taotesting.com> |
40 | * @package taoLti |
41 | */ |
42 | class LtiUser extends \common_user_User implements ServiceLocatorAwareInterface, \JsonSerializable, LtiUserInterface |
43 | { |
44 | use ServiceLocatorAwareTrait; |
45 | |
46 | public const ANONYMOUS_USER_URI = 'anonymous'; |
47 | |
48 | public const USER_IDENTIFIER = 'identifier'; |
49 | |
50 | /** |
51 | * Data with which this session was launched |
52 | * @var LtiLaunchData |
53 | */ |
54 | private $ltiLaunchData; |
55 | |
56 | /** |
57 | * Local representation of user |
58 | * @var \core_kernel_classes_Resource |
59 | */ |
60 | private $userUri; |
61 | |
62 | /** |
63 | * Cache of the current user's lti roles |
64 | * @var array |
65 | */ |
66 | protected $taoRoles; |
67 | |
68 | private $language; |
69 | |
70 | private $firstname; |
71 | |
72 | private $lastname; |
73 | |
74 | private $email; |
75 | |
76 | private $label; |
77 | |
78 | /** |
79 | * Currently used UI languages. |
80 | * |
81 | * @var array |
82 | */ |
83 | protected $uiLanguage; |
84 | |
85 | /** |
86 | * @param LtiLaunchData $launchData |
87 | * @param $userUri |
88 | * @throws \common_Exception |
89 | * @throws \common_exception_Error |
90 | * @throws \oat\taoLti\models\classes\LtiVariableMissingException |
91 | */ |
92 | public function __construct($launchData, $userUri) |
93 | { |
94 | $this->ltiLaunchData = $launchData; |
95 | $this->userUri = $userUri; |
96 | $taoRoles = $this->determineTaoRoles($launchData); |
97 | if (empty($taoRoles)) { |
98 | $message = "Invalid LTI role parameter value: " . $this->ltiLaunchData->getVariable(LtiLaunchData::ROLES); |
99 | throw new LtiInvalidVariableException($message); |
100 | } |
101 | |
102 | $this->setRoles($taoRoles); |
103 | |
104 | $email = ''; |
105 | $label = $launchData->getUserFullName(); |
106 | $firstname = $launchData->getUserGivenName(); |
107 | $lastname = $launchData->getUserFamilyName(); |
108 | |
109 | if ($launchData->hasVariable(LtiLaunchData::LIS_PERSON_CONTACT_EMAIL_PRIMARY)) { |
110 | $email = $launchData->getUserEmail(); |
111 | } |
112 | |
113 | $this->firstname = $firstname; |
114 | $this->lastname = $lastname; |
115 | $this->email = $email; |
116 | $this->label = $label; |
117 | } |
118 | |
119 | public function setRoles($roles) |
120 | { |
121 | $newRoles = array_map(function ($value) { |
122 | return ($value instanceof \core_kernel_classes_Resource) ? $value->getUri() : $value; |
123 | }, $roles); |
124 | |
125 | $this->taoRoles = $newRoles; |
126 | } |
127 | |
128 | /** |
129 | * (non-PHPdoc) |
130 | * @see \common_user_User::getIdentifier() |
131 | */ |
132 | public function getIdentifier() |
133 | { |
134 | return $this->userUri; |
135 | } |
136 | |
137 | public function setIdentifier($userId) |
138 | { |
139 | $this->userUri = $userId; |
140 | } |
141 | |
142 | /** |
143 | * @return LtiLaunchData |
144 | */ |
145 | public function getLaunchData() |
146 | { |
147 | return $this->ltiLaunchData; |
148 | } |
149 | |
150 | /** |
151 | * Get user langugae |
152 | * @return string |
153 | */ |
154 | public function getLanguage() |
155 | { |
156 | if (!isset($this->language)) { |
157 | if ($this->getLaunchData()->hasLaunchLanguage()) { |
158 | $launchLanguage = $this->getLaunchData()->getLaunchLanguage(); |
159 | $this->language = LtiUtils::mapCode2InterfaceLanguage($launchLanguage); |
160 | } else { |
161 | if ( |
162 | $this->userUri == self::ANONYMOUS_USER_URI |
163 | && defined('DEFAULT_ANONYMOUS_INTERFACE_LANG') |
164 | ) { |
165 | $this->language = DEFAULT_ANONYMOUS_INTERFACE_LANG; |
166 | } else { |
167 | $this->language = $this |
168 | ->getServiceLocator() |
169 | ->get(UserLanguageService::SERVICE_ID) |
170 | ->getDefaultLanguage(); |
171 | } |
172 | } |
173 | } |
174 | return $this->language; |
175 | } |
176 | |
177 | /** |
178 | * (non-PHPdoc) |
179 | * @see \common_user_User::getPropertyValues() |
180 | */ |
181 | public function getPropertyValues($property) |
182 | { |
183 | $returnValue = null; |
184 | switch ($property) { |
185 | case GenerisRdf::PROPERTY_USER_DEFLG: |
186 | $returnValue = [$this->getServiceLocator()->get(UserLanguageService::SERVICE_ID)->getDefaultLanguage()]; |
187 | break; |
188 | case GenerisRdf::PROPERTY_USER_UILG: |
189 | $returnValue = [$this->getLanguage()]; |
190 | break; |
191 | case GenerisRdf::PROPERTY_USER_ROLES: |
192 | $returnValue = $this->taoRoles; |
193 | break; |
194 | case GenerisRdf::PROPERTY_USER_FIRSTNAME: |
195 | $returnValue = $this->firstname !== null ? [$this->firstname] : ''; |
196 | break; |
197 | case GenerisRdf::PROPERTY_USER_LASTNAME: |
198 | $returnValue = $this->lastname !== null ? [$this->lastname] : ''; |
199 | break; |
200 | case OntologyRdfs::RDFS_LABEL: |
201 | $returnValue = [$this->label]; |
202 | break; |
203 | default: |
204 | \common_Logger::d('Unkown property ' . $property . ' requested from ' . __CLASS__); |
205 | $returnValue = []; |
206 | } |
207 | return $returnValue; |
208 | } |
209 | |
210 | /** |
211 | * (non-PHPdoc) |
212 | * @see \common_user_User::refresh() |
213 | */ |
214 | public function refresh() |
215 | { |
216 | // nothing to do |
217 | } |
218 | |
219 | public function jsonSerialize(): array |
220 | { |
221 | return [ |
222 | self::USER_IDENTIFIER => $this->userUri, |
223 | GenerisRdf::PROPERTY_USER_ROLES => $this->taoRoles, |
224 | GenerisRdf::PROPERTY_USER_UILG => $this->getLanguage(), |
225 | GenerisRdf::PROPERTY_USER_FIRSTNAME => $this->firstname, |
226 | GenerisRdf::PROPERTY_USER_LASTNAME => $this->lastname, |
227 | GenerisRdf::PROPERTY_USER_MAIL => $this->email, |
228 | OntologyRdfs::RDFS_LABEL => $this->label, |
229 | ]; |
230 | } |
231 | |
232 | /** |
233 | * Calculate your primary tao roles from the launchdata |
234 | * |
235 | * @param LtiLaunchData $ltiLaunchData |
236 | * @return array |
237 | * @throws \common_Exception |
238 | * @throws \common_exception_Error |
239 | */ |
240 | protected function determineTaoRoles(LtiLaunchData $ltiLaunchData) |
241 | { |
242 | $roles = []; |
243 | if ($ltiLaunchData->hasVariable(LtiLaunchData::ROLES)) { |
244 | foreach ($ltiLaunchData->getUserRoles() as $role) { |
245 | $taoRole = LtiUtils::mapLTIRole2TaoRole($role); |
246 | if (!is_null($taoRole)) { |
247 | $roles[] = $taoRole; |
248 | } |
249 | } |
250 | $roles = array_unique($roles); |
251 | } else { |
252 | return [LtiRoles::INSTANCE_LTI_BASE]; |
253 | } |
254 | return $roles; |
255 | } |
256 | } |