ILIAS  trunk Revision v5.2.0beta1-34115-g3a2438be29
ResourceLinkShareKey.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\LTI\ToolProvider;
20 
22 
31 {
35  public const MAX_SHARE_KEY_LIFE = 168; // in hours (1 week)
36 
40  public const DEFAULT_SHARE_KEY_LIFE = 24; // in hours
41 
45  public const MIN_SHARE_KEY_LENGTH = 5;
46 
50  public const MAX_SHARE_KEY_LENGTH = 32;
51 
57  public ?int $resourceLinkId = null;
58 
64  public ?int $length = null;
65 
71  public ?int $life = null; // in hours
72 
78  public bool $autoApprove = false;
79 
85  public ?int $expires = null;
86 
92  private ?string $id = null;
93 
99  private ?DataConnector $dataConnector = null;
100 
106  public function __construct(ResourceLink $resourceLink, string $id = null)
107  {
108  $this->initialize();
109  $this->dataConnector = $resourceLink->getDataConnector();
110  $this->id = $id;
111  if (!empty($id)) {
112  $this->load();
113  } else {
114  $this->resourceLinkId = $resourceLink->getRecordId();
115  }
116  }
117 
121  public function initialize()
122  {
123  $this->length = null;
124  $this->life = null;
125  $this->autoApprove = false;
126  $this->expires = null;
127  }
128 
134  public function initialise()
135  {
136  $this->initialize();
137  }
138 
144  public function save(): bool
145  {
146  if (empty($this->life)) {
147  $this->life = self::DEFAULT_SHARE_KEY_LIFE;
148  } else {
149  $this->life = max(min($this->life, self::MAX_SHARE_KEY_LIFE), 0);
150  }
151  $this->expires = time() + ($this->life * 60 * 60);
152  if (empty($this->id)) {
153  if (empty($this->length) || !is_numeric($this->length)) {
154  $this->length = self::MAX_SHARE_KEY_LENGTH;
155  } else {
156  $this->length = max(min($this->length, self::MAX_SHARE_KEY_LENGTH), self::MIN_SHARE_KEY_LENGTH);
157  }
158  $this->id = Util::getRandomString($this->length);
159  }
160 
161  return $this->dataConnector->saveResourceLinkShareKey($this);
162  }
163 
169  public function delete(): bool
170  {
171  return $this->dataConnector->deleteResourceLinkShareKey($this);
172  }
173 
179  public function getId(): ?string
180  {
181  return $this->id;
182  }
183 
184  ###
185  ### PRIVATE METHOD
186  ###
187 
191  private function load()
192  {
193  $this->initialize();
194  $this->dataConnector->loadResourceLinkShareKey($this);
195  if (!is_null($this->id)) {
196  $this->length = strlen(strval($this->id));
197  }
198  if (!is_null($this->expires)) {
199  $this->life = ($this->expires - time()) / 60 / 60;
200  }
201  }
202 }
initialize()
Initialise the resource link share key.
const DEFAULT_SHARE_KEY_LIFE
Default life for a share key value.
const MIN_SHARE_KEY_LENGTH
Minimum length for a share key value.
__construct(ResourceLink $resourceLink, string $id=null)
Class constructor.
int $expires
Timestamp for when the share key expires.
static getRandomString(int $length=8)
Generate a random string.
Definition: Util.php:558
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AccessToken.php:19
load()
Load the resource link share key from the database.
bool $autoApprove
Whether the sharing arrangement should be automatically approved when first used. ...
Class to represent a platform resource link share key.
const MAX_SHARE_KEY_LIFE
Maximum permitted life for a share key value.
initialise()
Initialise the resource link share key.
Class to provide a connection to a persistent store for LTI objects.
save()
Save the resource link share key to the database.
int $resourceLinkId
ID for resource link being shared.
const MAX_SHARE_KEY_LENGTH
Maximum length for a share key value.