ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilOrgUnitPermissionQueries.php
Go to the documentation of this file.
1 <?php
2 
9 {
10 
21  public static function getTemplateSetForContextName($context_name, $position_id, $editable = false)
22  {
23  // TODO write performant query
25  if (!$context) {
26  throw new ilException('No context found');
27  }
28  if (!$position_id) {
29  throw new ilException('$position_id cannot be null');
30  }
31 
32  $template_set = ilOrgUnitPermission::where([
34  'context_id' => $context->getId(),
35  'position_id' => $position_id,
36  ])->first();
37 
38  if (!$template_set) {
39  $template_set = new ilOrgUnitPermission();
40  $template_set->setParentId(ilOrgUnitPermission::PARENT_TEMPLATE);
41  $template_set->setContextId($context->getId());
42  $template_set->setPositionId($position_id);
43  $template_set->setNewlyCreated(true);
44  $template_set->create();
45  $template_set->afterObjectLoad();
46  }
47 
48  $template_set->setProtected(!$editable);
49 
50  return $template_set;
51  }
52 
53 
60  public static function hasLocalSet($ref_id, $position_id)
61  {
63  'parent_id' => $ref_id,
64  'position_id' => $position_id,
65  ])->hasSets());
66  }
67 
68 
78  public static function getSetForRefId($ref_id, $position_id)
79  {
80  // TODO write performant query
81  self::checkRefIdAndPositionId($ref_id, $position_id);
82 
83  $context = self::getContextByRefId($ref_id);
84 
85  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
86  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context->getContext());
87 
88  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
89  throw new ilPositionPermissionsNotActive("Postion-related permissions not active in {$context->getContext()}", $context->getContext());
90  }
91 
95  $dedicated_set = ilOrgUnitPermission::where([
96  'parent_id' => $ref_id,
97  'context_id' => $context->getId(),
98  'position_id' => $position_id,
99  ])->first();
100  if ($dedicated_set) {
101  return $dedicated_set;
102  }
103 
104  return ilOrgUnitPermissionQueries::getTemplateSetForContextName($context->getContext(), $position_id);
105  }
106 
107 
115  public static function findOrCreateSetForRefId($ref_id, $position_id)
116  {
120  self::checkRefIdAndPositionId($ref_id, $position_id);
121 
122  $context = self::getContextByRefId($ref_id);
123 
124  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
125  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context->getContext());
126 
127  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
128  throw new ilPositionPermissionsNotActive("Position-related permissions not active in {$context->getContext()}", $context->getContext());
129  }
130 
131  $dedicated_set = ilOrgUnitPermission::where([
132  'parent_id' => $ref_id,
133  'context_id' => $context->getId(),
134  'position_id' => $position_id,
135  ])->first();
136  if ($dedicated_set) {
137  return $dedicated_set;
138  }
139 
140  $template = self::getTemplateSetForContextName($context->getContext(), $position_id);
141 
142  $set = new ilOrgUnitPermission();
143  $set->setProtected(false);
144  $set->setParentId($ref_id);
145  $set->setPositionId($position_id);
146  $set->setContextId($context->getId());
147  $set->setOperations($template->getOperations());
148  $set->setNewlyCreated(true);
149  $set->create();
150 
151  return $set;
152  }
153 
154 
162  public static function removeLocalSetForRefId($ref_id, $position_id)
163  {
167  self::checkRefIdAndPositionId($ref_id, $position_id);
168 
169  $context = self::getContextByRefId($ref_id);
170 
171  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
172  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context->getContext());
173 
174  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
175  throw new ilPositionPermissionsNotActive("Position-related permissions not active in {$context->getContext()}", $context->getContext());
176  }
177 
178  $dedicated_set = ilOrgUnitPermission::where([
179  'parent_id' => $ref_id,
180  'context_id' => $context->getId(),
181  'position_id' => $position_id,
182  'protected' => false,
183  ])->first();
184  if ($dedicated_set) {
185  $dedicated_set->delete();
186 
187  return true;
188  }
189 
190  return false;
191  }
192 
193 
201  public static function getAllTemplateSetsForAllActivedContexts($position_id, $editable = false)
202  {
203  $activated_components = [];
204  foreach (ilOrgUnitGlobalSettings::getInstance()->getPositionSettings() as $ilOrgUnitObjectPositionSetting) {
205  if ($ilOrgUnitObjectPositionSetting->isActive()) {
206  $activated_components[] = $ilOrgUnitObjectPositionSetting->getType();
207  }
208  }
209  $sets = [];
210  foreach ($activated_components as $context) {
211  $sets[] = ilOrgUnitPermissionQueries::getTemplateSetForContextName($context, $position_id, $editable);
212  }
213 
214  return $sets;
215  }
216 
217 
223  public static function getRelevantPermissionSetsForUserIdAndRefIdAndOperation($user_id, $ref_id, $operation_string)
224  {
225  $q = 'SELECT @OP_ID:= CONCAT("%\"",operation_id, "\"%") FROM il_orgu_op_contexts
226 JOIN il_orgu_operations ON il_orgu_operations.context_id = il_orgu_op_contexts.id
227 WHERE il_orgu_op_contexts.context IN(\'crs\', \'object\') AND operation_string = \'viewmembers\';';
228  }
229 
230 
231  private static function getAllowedOperationsOnRefIdAndPosition($ref_id, $position_id)
232  {
233  global $DIC;
234  $db = $DIC->database();
235 
236  $q = 'SELECT @CONTEXT_TYPE:= object_data.type
237  FROM object_reference
238  JOIN object_data ON object_data.obj_id = object_reference.obj_id
239  WHERE object_reference.ref_id = %s;';
240  $db->queryF($q, [ 'integer' ], [ $ref_id ]);
241 
242  $q = 'SELECT @OP_ID:= CONCAT("%\"", il_orgu_operations.operation_id, "%\"")
243  FROM il_orgu_operations
244  JOIN il_orgu_op_contexts ON il_orgu_op_contexts.context = @CONTEXT_TYPE -- AND il_orgu_op_contexts.id = il_orgu_operations.context_id
245  WHERE il_orgu_operations.operation_string = %s';
246  $db->queryF($q, [ 'text' ], [ $pos_perm ]);
247  $q = 'SELECT * FROM il_orgu_permissions WHERE operations LIKE @OP_ID AND position_id = %s;';
248  $r = $db->queryF($q, [ 'integer' ], [ $position_id ]);
249 
250  ($r->numRows() > 0);
251  }
252 
253 
260  protected static function getContextByRefId($ref_id)
261  {
263  if (!$context) {
264  throw new ilException('Context not found');
265  }
266 
267  return $context;
268  }
269 
270 
277  protected static function checkRefIdAndPositionId($ref_id, $position_id)
278  {
279  if (!$ref_id) {
280  throw new ilException('$ref_id cannot be null');
281  }
282  if (!$position_id) {
283  throw new ilException('$position_id cannot be null');
284  }
285  }
286 }
static checkRefIdAndPositionId($ref_id, $position_id)
$context
Definition: webdav.php:25
$template
global $DIC
Definition: saml.php:7
static hasLocalSet($ref_id, $position_id)
static where($where, $operator=null)
Class ilOrgUnitPermissionGUI.
$r
Definition: example_031.php:79
Class ilOrgUnitPermissionQueries.
Class ilPositionPermissionsNotActive.
$sets
Definition: fetch.php:11
static getTemplateSetForContextName($context_name, $position_id, $editable=false)
static getAllTemplateSetsForAllActivedContexts($position_id, $editable=false)
static getAllowedOperationsOnRefIdAndPosition($ref_id, $position_id)
static getRelevantPermissionSetsForUserIdAndRefIdAndOperation($user_id, $ref_id, $operation_string)