ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilUserCertificateRepository.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
8 {
12  private $database;
13 
17  private $logger;
18 
22  private $defaultTitle;
23 
29  public function __construct(
30  \ilDBInterface $database = null,
31  ilLogger $logger = null,
32  string $defaultTitle = null
33  ) {
34  if (null === $database) {
35  global $DIC;
36  $database = $DIC->database();
37  }
38  $this->database = $database;
39 
40  if (null === $logger) {
41  global $DIC;
42  $logger = $DIC->logger()->cert();
43  }
44  $this->logger = $logger;
45 
46  if (null === $defaultTitle) {
47  global $DIC;
48  $defaultTitle = $DIC->language()->txt('certificate_no_object_title');
49  }
50  $this->defaultTitle = $defaultTitle;
51  }
52 
58  public function save(ilUserCertificate $userCertificate) : ilUserCertificate
59  {
60  $this->logger->info('START - saving of user certificate');
61 
62  $version = (int) $this->fetchLatestVersion($userCertificate->getObjId(), $userCertificate->getUserId());
63  $version += 1;
64 
65  $id = (int) $this->database->nextId('il_cert_user_cert');
66 
67  $objId = $userCertificate->getObjId();
68  $userId = $userCertificate->getUserId();
69 
70  $this->deactivatePreviousCertificates($objId, $userId);
71 
72  $columns = array(
73  'id' => array('integer', $id),
74  'pattern_certificate_id' => array('integer', $userCertificate->getPatternCertificateId()),
75  'obj_id' => array('integer', $objId),
76  'obj_type' => array('text', $userCertificate->getObjType()),
77  'user_id' => array('integer', $userId),
78  'user_name' => array('text', $userCertificate->getUserName()),
79  'acquired_timestamp' => array('integer', $userCertificate->getAcquiredTimestamp()),
80  'certificate_content' => array('clob', $userCertificate->getCertificateContent()),
81  'template_values' => array('clob', $userCertificate->getTemplateValues()),
82  'valid_until' => array('integer', $userCertificate->getValidUntil()),
83  'version' => array('integer', $version),
84  'ilias_version' => array('text', $userCertificate->getIliasVersion()),
85  'currently_active' => array('integer', (integer) $userCertificate->isCurrentlyActive()),
86  'background_image_path' => array('text', $userCertificate->getBackgroundImagePath()),
87  'thumbnail_image_path' => array('text', $userCertificate->getThumbnailImagePath())
88  );
89 
90  $this->logger->debug(sprintf('END - Save certificate with following values: %s', json_encode($columns, JSON_PRETTY_PRINT)));
91 
92  $this->database->insert('il_cert_user_cert', $columns);
93 
94  return $userCertificate->withId($id)->withVersion($version);
95  }
96 
102  public function fetchActiveCertificates(int $userId) : array
103  {
104  $this->logger->info(sprintf('START - Fetching all active certificates for user: "%s"', $userId));
105 
106  $sql = '
107 SELECT
108  il_cert_user_cert.pattern_certificate_id,
109  il_cert_user_cert.obj_id,
110  il_cert_user_cert.obj_type,
111  il_cert_user_cert.user_id,
112  il_cert_user_cert.user_name,
113  il_cert_user_cert.acquired_timestamp,
114  il_cert_user_cert.certificate_content,
115  il_cert_user_cert.template_values,
116  il_cert_user_cert.valid_until,
117  il_cert_user_cert.version,
118  il_cert_user_cert.ilias_version,
119  il_cert_user_cert.currently_active,
120  il_cert_user_cert.background_image_path,
121  il_cert_user_cert.id,
122  il_cert_user_cert.thumbnail_image_path,
123  (CASE WHEN (object_data.title IS NULL)
124  THEN
125  CASE WHEN (object_data_del.title IS NULL)
126  THEN ' . $this->database->quote($this->defaultTitle, 'text') . '
127  ELSE object_data_del.title
128  END
129  ELSE object_data.title
130  END
131  ) as title
132 FROM il_cert_user_cert
133 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
134 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
135 WHERE user_id = ' . $this->database->quote($userId, 'integer') . '
136 AND currently_active = 1';
137 
138  $query = $this->database->query($sql);
139 
140  $result = array();
141  while ($row = $this->database->fetchAssoc($query)) {
142  $userCertificate = $this->createUserCertificate($row);
143 
144  $presentation = new ilUserCertificatePresentation($userCertificate, $row['title'], '');
145  $result[] = $presentation;
146  }
147 
148  $this->logger->debug(sprintf('Actual results:', json_encode($result)));
149  $this->logger->info(sprintf('END - All active certificates for user: "%s" total: "%s"', $userId, count($result)));
150 
151  return $result;
152  }
153 
159  public function fetchActiveCertificatesInIntervalForPresentation(int $userId, int $startTimestamp, int $endTimeStamp) : array
160  {
161  $this->logger->info(sprintf('START - Fetching all active certificates for user: "%s"', $userId));
162 
163  $sql = '
164 SELECT
165  il_cert_user_cert.pattern_certificate_id,
166  il_cert_user_cert.obj_id,
167  il_cert_user_cert.obj_type,
168  il_cert_user_cert.user_id,
169  il_cert_user_cert.user_name,
170  il_cert_user_cert.acquired_timestamp,
171  il_cert_user_cert.certificate_content,
172  il_cert_user_cert.template_values,
173  il_cert_user_cert.valid_until,
174  il_cert_user_cert.version,
175  il_cert_user_cert.ilias_version,
176  il_cert_user_cert.currently_active,
177  il_cert_user_cert.background_image_path,
178  il_cert_user_cert.id,
179  il_cert_user_cert.thumbnail_image_path,
180  (CASE WHEN (object_data.title IS NULL)
181  THEN
182  CASE WHEN (object_data_del.title IS NULL)
183  THEN ' . $this->database->quote($this->defaultTitle, 'text') . '
184  ELSE object_data_del.title
185  END
186  ELSE object_data.title
187  END
188  ) as title
189 FROM il_cert_user_cert
190 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
191 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
192 WHERE user_id = ' . $this->database->quote($userId, 'integer') . '
193 AND currently_active = 1
194 AND acquired_timestamp >= ' . $this->database->quote($startTimestamp, 'integer') . '
195 AND acquired_timestamp <= ' . $this->database->quote($endTimeStamp, 'integer');
196 
197  $query = $this->database->query($sql);
198 
199  $result = array();
200  while ($row = $this->database->fetchAssoc($query)) {
201  $userCertificate = $this->createUserCertificate($row);
202 
203  $presentation = new ilUserCertificatePresentation($userCertificate, $row['title'], '');
204  $result[] = $presentation;
205  }
206 
207  $this->logger->debug(sprintf('Actual results:', json_encode($result)));
208  $this->logger->info(sprintf('END - All active certificates for user: "%s" total: "%s"', $userId, count($result)));
209 
210  return $result;
211  }
212 
213  public function fetchActiveCertificate(int $userId, int $objectId) : ilUserCertificate
214  {
215  $this->logger->info(sprintf('START - Fetching all active certificates for user: "%s" and object: "%s"', $userId, $objectId));
216 
217  $sql = 'SELECT *
218 FROM il_cert_user_cert
219 WHERE user_id = ' . $this->database->quote($userId, 'integer') . '
220 AND obj_id = ' . $this->database->quote($objectId, 'integer') . '
221 AND currently_active = 1';
222 
223  $query = $this->database->query($sql);
224 
225  while ($row = $this->database->fetchAssoc($query)) {
226  $this->logger->debug(sprintf('Active certificate values: %s', json_encode($row)));
227 
228  $this->logger->info(sprintf('END -Found active user certificate for user: "%s" and object: "%s"', $userId, $objectId));
229 
230  return $this->createUserCertificate($row);
231  }
232 
233  throw new ilException(sprintf('There is no active entry for user id: "%s" and object id: "%s"', $userId, $objectId));
234  }
235 
242  public function fetchActiveCertificateForPresentation(int $userId, int $objectId) : ilUserCertificatePresentation
243  {
244  $this->logger->info(sprintf('START - Fetching all active certificates for user: "%s" and object: "%s"', $userId, $objectId));
245 
246  $sql = 'SELECT
247  il_cert_user_cert.pattern_certificate_id,
248  il_cert_user_cert.obj_id,
249  il_cert_user_cert.obj_type,
250  il_cert_user_cert.user_id,
251  il_cert_user_cert.user_name,
252  il_cert_user_cert.acquired_timestamp,
253  il_cert_user_cert.certificate_content,
254  il_cert_user_cert.template_values,
255  il_cert_user_cert.valid_until,
256  il_cert_user_cert.version,
257  il_cert_user_cert.ilias_version,
258  il_cert_user_cert.currently_active,
259  il_cert_user_cert.background_image_path,
260  il_cert_user_cert.id,
261  il_cert_user_cert.thumbnail_image_path,
262  usr_data.lastname,
263  (CASE WHEN (object_data.title IS NULL)
264  THEN
265  CASE WHEN (object_data_del.title IS NULL)
266  THEN ' . $this->database->quote($this->defaultTitle, 'text') . '
267  ELSE object_data_del.title
268  END
269  ELSE object_data.title
270  END
271  ) as title
272 FROM il_cert_user_cert
273 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
274 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
275 LEFT JOIN usr_data ON usr_data.usr_id = il_cert_user_cert.user_id
276 WHERE il_cert_user_cert.user_id = ' . $this->database->quote($userId, 'integer') . '
277 AND il_cert_user_cert.obj_id = ' . $this->database->quote($objectId, 'integer') . '
278 AND il_cert_user_cert.currently_active = 1';
279 
280  $query = $this->database->query($sql);
281 
282  while ($row = $this->database->fetchAssoc($query)) {
283  $this->logger->debug(sprintf('Active certificate values: %s', json_encode($row)));
284 
285  $this->logger->info(sprintf('END -Found active user certificate for user: "%s" and object: "%s"', $userId, $objectId));
286 
287  $userCertificate = $this->createUserCertificate($row);
288  return new ilUserCertificatePresentation($userCertificate, $row['title'], '', $row['lastname']);
289  }
290 
291  throw new ilException(sprintf('There is no active entry for user id: "%s" and object id: "%s"', $userId, $objectId));
292  }
293 
299  public function fetchActiveCertificatesByTypeForPresentation(int $userId, string $type) : array
300  {
301  $this->logger->info(sprintf('START - Fetching all active certificates for user: "%s" and type: "%s"', $userId, $type));
302 
303  $sql = 'SELECT
304  il_cert_user_cert.pattern_certificate_id,
305  il_cert_user_cert.obj_id,
306  il_cert_user_cert.obj_type,
307  il_cert_user_cert.user_id,
308  il_cert_user_cert.user_name,
309  il_cert_user_cert.acquired_timestamp,
310  il_cert_user_cert.certificate_content,
311  il_cert_user_cert.template_values,
312  il_cert_user_cert.valid_until,
313  il_cert_user_cert.version,
314  il_cert_user_cert.ilias_version,
315  il_cert_user_cert.currently_active,
316  il_cert_user_cert.background_image_path,
317  il_cert_user_cert.id,
318  il_cert_user_cert.thumbnail_image_path,
319  (CASE WHEN (object_data.title IS NULL)
320  THEN
321  CASE WHEN (object_data_del.title IS NULL)
322  THEN ' . $this->database->quote($this->defaultTitle, 'text') . '
323  ELSE object_data_del.title
324  END
325  ELSE object_data.title
326  END
327  ) as title
328 FROM il_cert_user_cert
329 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
330 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
331 WHERE user_id = ' . $this->database->quote($userId, 'integer') . '
332  AND obj_type = ' . $this->database->quote($type, 'text') . '
333  AND currently_active = 1';
334 
335  $query = $this->database->query($sql);
336 
337  $result = array();
338  while ($row = $this->database->fetchAssoc($query)) {
339  $userCertificate = $this->createUserCertificate($row);
340 
341  $presentation = new ilUserCertificatePresentation($userCertificate, $row['title'], '');
342  $result[] = $presentation;
343  }
344 
345  $this->logger->info(sprintf('END - Fetching all active certificates for user: "%s" and type: "%s"', $userId, $type));
346 
347  return $result;
348  }
349 
355  public function fetchCertificate(int $id) : ilUserCertificate
356  {
357  $this->logger->info(sprintf('START - Fetch certificate by id: "%s"', $id));
358 
359  $sql = 'SELECT * FROM il_cert_user_cert WHERE id = ' . $this->database->quote($id, 'integer');
360 
361  $query = $this->database->query($sql);
362 
363  while ($row = $this->database->fetchAssoc($query)) {
364  $this->logger->debug(sprintf('Fetched certificate: "%s"', json_encode($row)));
365 
366  $this->logger->info(sprintf('END - Fetch certificate by id: "%s"', $id));
367 
368  return $this->createUserCertificate($row);
369  }
370 
371  throw new ilException('No certificate found for user certificate id: ' . $id);
372  }
373 
374  public function fetchObjectIdsWithCertificateForUser(int $userId, array $objectIds)
375  {
376  $this->logger->info(sprintf('START - Fetch certificate for user("%s") and ids: "%s"', $userId, json_encode($objectIds)));
377 
378  if (0 === count($objectIds)) {
379  return [];
380  }
381 
382  $inStatementObjectIds = $this->database->in(
383  'obj_id',
384  $objectIds,
385  false,
386  'integer'
387  );
388 
389  $sql = 'SELECT obj_id FROM il_cert_user_cert
390  WHERE user_id = ' . $this->database->quote($userId, 'integer') .
391 ' AND ' . $inStatementObjectIds .
392 ' AND currently_active = ' . $this->database->quote(1, 'integer');
393 
394  $query = $this->database->query($sql);
395 
396  $result = array();
397 
398  while ($row = $this->database->fetchAssoc($query)) {
399  $this->logger->debug(sprintf('Fetched certificate: "%s"', json_encode($row)));
400  $result[] = $row['obj_id'];
401  }
402 
403  return $result;
404  }
405 
406  public function fetchUserIdsWithCertificateForObject(int $objectId)
407  {
408  $this->logger->info(sprintf('START - Fetch certificate for object("%s")"', $objectId));
409 
410  $sql = 'SELECT user_id FROM il_cert_user_cert
411 WHERE obj_id = ' . $this->database->quote($objectId, 'integer') . '
412  AND currently_active = ' . $this->database->quote(1, 'integer');
413 
414  $query = $this->database->query($sql);
415 
416  $result = array();
417 
418  while ($row = $this->database->fetchAssoc($query)) {
419  $this->logger->debug(sprintf('Fetched certificate: "%s"', json_encode($row)));
420  $result[] = $row['user_id'];
421  }
422 
423  return $result;
424  }
425 
426  public function deleteUserCertificates(int $userId)
427  {
428  $this->logger->info(sprintf('START - Delete certificate for user("%s")"', $userId));
429 
430  $sql = 'DELETE FROM il_cert_user_cert WHERE user_id = ' . $this->database->quote($userId, 'integer');
431 
432  $this->database->manipulate($sql);
433 
434  $this->logger->info(sprintf('END - Successfully deleted certificate for user("%s")"', $userId));
435  }
436 
442  private function fetchCertificatesOfObject(int $objId, int $userId) : array
443  {
444  $this->logger->info(sprintf(
445  'START - fetching all certificates of object(user id: "%s", object id: "%s")',
446  $userId,
447  $objId
448  ));
449 
450  $sql = 'SELECT * FROM il_cert_user_cert
451 WHERE user_id = ' . $this->database->quote($userId, 'integer') . '
452 AND obj_id = ' . $this->database->quote($objId, 'integer');
453 
454  $query = $this->database->query($sql);
455 
456  $result = array();
457  while ($row = $this->database->fetchAssoc($query)) {
458  $this->logger->debug(sprintf(
459  'Certificate found: "%s")',
460  json_encode($row, JSON_PRETTY_PRINT)
461  ));
462 
463  $this->logger->info(sprintf('Certificate: ', json_encode($row)));
464 
465  $result[] = $this->createUserCertificate($row);
466  }
467 
468  $this->logger->info(sprintf(
469  'END - fetching all certificates of object(user id: "%s", object id: "%s")',
470  $userId,
471  $objId
472  ));
473 
474  return $result;
475  }
476 
482  private function fetchLatestVersion(int $objId, int $userId) : string
483  {
484  $this->logger->info(sprintf(
485  'START - fetching of latest certificates of object(user id: "%s", object id: "%s")',
486  $userId,
487  $objId
488  ));
489 
490  $templates = $this->fetchCertificatesOfObject($objId, $userId);
491 
492  $version = 0;
493  foreach ($templates as $template) {
494  if ($template->getVersion() > $version) {
495  $version = $template->getVersion();
496  }
497  }
498 
499  $this->logger->info(sprintf(
500  'END - fetching of latest certificates of object(user id: "%s", object id: "%s") with verision',
501  $userId,
502  $objId,
503  $version
504  ));
505 
506  return $version;
507  }
508 
514  private function deactivatePreviousCertificates(int $objId, int $userId)
515  {
516  $this->logger->info(sprintf('START - deactivating previous certificates for user id: "%s" and object id: "%s"', $userId, $objId));
517 
518  $sql = '
519 UPDATE il_cert_user_cert
520 SET currently_active = 0
521 WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
522 AND user_id = ' . $this->database->quote($userId, 'integer');
523 
524  $this->database->manipulate($sql);
525 
526  $this->logger->info(sprintf('END - deactivating previous certificates for user id: "%s" and object id: "%s"', $userId, $objId));
527  }
528 
534  {
535  return new ilUserCertificate(
536  $row['pattern_certificate_id'],
537  $row['obj_id'],
538  $row['obj_type'],
539  $row['user_id'],
540  $row['user_name'],
541  (int) $row['acquired_timestamp'],
542  $row['certificate_content'],
543  $row['template_values'],
544  $row['valid_until'],
545  $row['version'],
546  $row['ilias_version'],
547  $row['currently_active'],
548  $row['background_image_path'],
549  $row['thumbnail_image_path'],
550  $row['id']
551  );
552  }
553 }
$result
fetchActiveCertificatesByTypeForPresentation(int $userId, string $type)
$template
$type
global $DIC
Definition: saml.php:7
fetchActiveCertificatesInIntervalForPresentation(int $userId, int $startTimestamp, int $endTimeStamp)
if(!array_key_exists('StateId', $_REQUEST)) $id
save(ilUserCertificate $userCertificate)
__construct(\ilDBInterface $database=null, ilLogger $logger=null, string $defaultTitle=null)
fetchActiveCertificate(int $userId, int $objectId)
$version
Definition: build.php:27
deactivatePreviousCertificates(int $objId, int $userId)
$query
$row
Component logger with individual log levels by component id.
fetchObjectIdsWithCertificateForUser(int $userId, array $objectIds)
if(! $in) $columns
Definition: Utf8Test.php:45
fetchActiveCertificateForPresentation(int $userId, int $objectId)