Skip to content

Commit e6b7f01

Browse files
committed
Include files from glpi-project#20247
1 parent cd57823 commit e6b7f01

File tree

2 files changed

+157
-71
lines changed

2 files changed

+157
-71
lines changed

ajax/visibility.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
if (
4747
isset($_POST['type']) && !empty($_POST['type'])
48-
&& isset($_POST['right'])
4948
) {
5049
$display = false;
5150
$rand = mt_rand();
@@ -62,9 +61,16 @@
6261
switch ($_POST['type']) {
6362
case 'User':
6463
$params = [
65-
'right' => isset($_POST['allusers']) ? 'all' : $_POST['right'],
64+
'right' => 'all',
6665
'name' => $prefix . 'users_id' . $suffix,
6766
];
67+
if (isset($_POST['right']) && !isset($_POST['allusers'])) {
68+
$params['right'] = $_POST['right'];
69+
}
70+
if (isset($_POST['entity']) && $_POST['entity'] >= 0) {
71+
$params['entity'] = $_POST['entity'];
72+
$params['entity_sons'] = $_POST['is_recursive'] ?? false;
73+
}
6874
User::dropdown($params);
6975
$display = true;
7076
break;
@@ -82,7 +88,6 @@
8288
'prefix' => $_POST['prefix'],
8389
],
8490
];
85-
8691
Group::dropdown($params);
8792
echo "<span id='subvisibility$rand'></span>";
8893
$display = true;
@@ -101,27 +106,26 @@
101106
break;
102107

103108
case 'Profile':
104-
$checkright = (READ | CREATE | UPDATE | PURGE);
105-
$righttocheck = $_POST['right'];
106-
if ($_POST['right'] == 'faq') {
107-
$righttocheck = 'knowbase';
108-
$checkright = KnowbaseItem::READFAQ;
109+
$righttocheck = $_POST['right'] ?? null;
110+
if ($righttocheck) {
111+
$checkright = (READ | CREATE | UPDATE | PURGE);
112+
if ($_POST['right'] == 'faq') {
113+
$righttocheck = 'knowbase';
114+
$checkright = KnowbaseItem::READFAQ;
115+
}
116+
$params['condition'] = [
117+
'glpi_profilerights.name' => $righttocheck,
118+
'glpi_profilerights.rights' => ['&', $checkright],
119+
];
109120
}
110-
$params = [
111-
'rand' => $rand,
112-
'name' => $prefix . 'profiles_id' . $suffix,
113-
'condition' => [
114-
'glpi_profilerights.name' => $righttocheck,
115-
'glpi_profilerights.rights' => ['&', $checkright],
116-
],
117-
];
118-
$params['toupdate'] = ['value_fieldname'
119-
=> 'value',
120-
'to_update' => "subvisibility$rand",
121-
'url' => $CFG_GLPI["root_doc"] . "/ajax/subvisibility.php",
122-
'moreparams' => ['items_id' => '__VALUE__',
123-
'type' => $_POST['type'],
124-
'prefix' => $_POST['prefix'],
121+
$params['toupdate'] = [
122+
'value_fieldname' => 'value',
123+
'to_update' => "subvisibility$rand",
124+
'url' => $CFG_GLPI["root_doc"] . "/ajax/subvisibility.php",
125+
'moreparams' => [
126+
'items_id' => '__VALUE__',
127+
'type' => $_POST['type'],
128+
'prefix' => $_POST['prefix'],
125129
],
126130
];
127131

src/CommonDBVisible.php

Lines changed: 130 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@
3434
*/
3535

3636
use Glpi\Application\View\TemplateRenderer;
37+
use Glpi\Event;
3738

3839
/**
3940
* Common DataBase visibility for items
4041
*/
41-
abstract class CommonDBVisible extends CommonDBTM
42+
trait CommonDBVisible
4243
{
44+
/**
45+
* Types of target available for the itemtype
46+
* @var string[]
47+
*/
48+
public static $types = ['Entity', 'Group', 'Profile', 'User'];
49+
4350
/**
4451
* Entities on which item is visible.
4552
* Keys are ID, values are DB fields values.
@@ -68,46 +75,56 @@ abstract class CommonDBVisible extends CommonDBTM
6875
*/
6976
protected $users = [];
7077

71-
public function __get(string $property)
72-
{
73-
// TODO Deprecate access to variables in GLPI 11.0.
74-
$value = null;
75-
switch ($property) {
76-
case 'entities':
77-
case 'groups':
78-
case 'profiles':
79-
case 'users':
80-
$value = $this->$property;
81-
break;
82-
default:
83-
$trace = debug_backtrace();
84-
trigger_error(
85-
sprintf('Undefined property: %s::%s in %s on line %d', __CLASS__, $property, $trace[0]['file'], $trace[0]['line']),
86-
E_USER_WARNING
87-
);
88-
break;
89-
}
90-
return $value;
91-
}
78+
/**
79+
* Class defining relation to $users
80+
* @var string
81+
*/
82+
protected $userClass;
83+
84+
/**
85+
* Class defining relation to $profiles
86+
* @var string
87+
*/
88+
protected $profileClass;
9289

93-
public function __set(string $property, $value)
90+
/**
91+
* Class defining relation to $groups
92+
* @var string
93+
*/
94+
protected $groupClass;
95+
96+
/**
97+
* Class defining relation to entities
98+
* @var string
99+
*/
100+
protected $entityClass;
101+
102+
/**
103+
* Service for visibility target log
104+
* @var string
105+
*/
106+
protected $service;
107+
108+
public function __construct()
94109
{
95-
// TODO Deprecate access to variables in GLPI 11.0.
96-
switch ($property) {
97-
case 'entities':
98-
case 'groups':
99-
case 'profiles':
100-
case 'users':
101-
$this->$property = $value;
102-
break;
103-
default:
104-
$trace = debug_backtrace();
105-
trigger_error(
106-
sprintf('Undefined property: %s::%s in %s on line %d', __CLASS__, $property, $trace[0]['file'], $trace[0]['line']),
107-
E_USER_WARNING
108-
);
109-
break;
110+
// define default values
111+
if (!$this->userClass) {
112+
$this->userClass = $this->getType() . '_UserTarget';
113+
}
114+
if (!$this->groupClass) {
115+
$this->groupClass = 'Group_' . $this->getType();
116+
}
117+
if (!$this->entityClass) {
118+
$this->entityClass = 'Entity_' . $this->getType();
110119
}
120+
if (!$this->profileClass) {
121+
$this->profileClass = 'Profile_' . $this->getType();
122+
}
123+
if (!$this->service) {
124+
$this->service = 'tools';
125+
}
126+
127+
parent::__construct();
111128
}
112129

113130
/**
@@ -204,6 +221,16 @@ public function countVisibilities()
204221
+ count($this->profiles));
205222
}
206223

224+
225+
/**
226+
* Get right which will be used to determine which users can be targeted
227+
* @return string
228+
*/
229+
public function getVisibilityRight()
230+
{
231+
return strtolower($this::getType()) . '_public';
232+
}
233+
207234
/**
208235
* Show visibility configuration
209236
*
@@ -220,9 +247,10 @@ public function showVisibility()
220247

221248
if ($canedit) {
222249
TemplateRenderer::getInstance()->display('components/add_visibility_target.html.twig', [
223-
'type' => static::class,
224-
'rand' => $rand,
225-
'id' => $ID,
250+
'type' => static::class,
251+
'types' => static::$types,
252+
'rand' => $rand,
253+
'id' => $ID,
226254
'add_target_msg' => __('Add a target'),
227255
'visiblity_dropdown_params' => $this->getShowVisibilityDropdownParams(),
228256
]);
@@ -233,7 +261,7 @@ public function showVisibility()
233261
foreach ($this->users as $val) {
234262
foreach ($val as $data) {
235263
$entries[] = [
236-
'itemtype' => static::class . '_User',
264+
'itemtype' => $this instanceof SavedSearch ? SavedSearch_UserTarget::class : static::class . '_User',
237265
'id' => $data['id'],
238266
'type' => User::getTypeName(1),
239267
'recipient' => htmlescape(getUserName($data['users_id'])),
@@ -270,7 +298,7 @@ public function showVisibility()
270298
}
271299
}
272300
$entries[] = [
273-
'itemtype' => static::class . '_Group',
301+
'itemtype' => 'Group_' . static::class,
274302
'id' => $data['id'],
275303
'type' => Group::getTypeName(1),
276304
'recipient' => $recipient,
@@ -347,7 +375,8 @@ public function showVisibility()
347375
'container' => 'mass' . static::class . $rand,
348376
'specific_actions' => ['delete' => _x('button', 'Delete permanently')],
349377
];
350-
if ($this->fields['users_id'] !== Session::getLoginUserID()) {
378+
if (isset($this->fields['users_id'])
379+
&& $this->fields['users_id'] !== Session::getLoginUserID()) {
351380
$massiveactionparams['confirm'] = __('Caution! You are not the author of this item. Deleting targets can result in loss of access.');
352381
}
353382

@@ -379,10 +408,10 @@ public function showVisibility()
379408
*/
380409
protected function getShowVisibilityDropdownParams()
381410
{
382-
$params = [
383-
'type' => '__VALUE__',
384-
'right' => strtolower($this::getType()) . '_public',
385-
];
411+
$params = ['type' => '__VALUE__'];
412+
if ($right = $this->getVisibilityRight()) {
413+
$params['right'] = $right;
414+
}
386415
if (isset($this->fields['entities_id'])) {
387416
$params['entity'] = $this->fields['entities_id'];
388417
}
@@ -391,4 +420,57 @@ protected function getShowVisibilityDropdownParams()
391420
}
392421
return $params;
393422
}
423+
424+
/**
425+
* Add a visibility target to the item
426+
* @param array $inputs key '_type' determine the type of target
427+
* @return void
428+
*/
429+
public function addVisibility(array $inputs)
430+
{
431+
$fkField = getForeignKeyFieldForItemType($this->getType());
432+
$item = null;
433+
switch ($inputs['_type']) {
434+
case User::class:
435+
$class = $this->getType() . '_UserTarget';
436+
if (is_a($class, CommonDBRelation::class, true)) {
437+
$item = new $class();
438+
}
439+
break;
440+
case Group::class:
441+
$class = 'Group_' . $this->getType();
442+
if (is_a($class, CommonDBRelation::class, true)) {
443+
$item = new $class();
444+
}
445+
break;
446+
case Entity::class:
447+
$class = 'Entity_' . $this->getType();
448+
if (is_a($class, CommonDBRelation::class, true)) {
449+
$item = new $class();
450+
}
451+
break;
452+
case Profile::class:
453+
$class = 'Profile_' . $this->getType();
454+
if (is_a($class, CommonDBRelation::class, true)) {
455+
$item = new $class();
456+
}
457+
break;
458+
}
459+
if (array_key_exists('entities_id', $inputs) && $inputs['entities_id'] == -1) {
460+
// "No restriction" value selected
461+
$inputs['entities_id'] = 'NULL';
462+
$inputs['no_entity_restriction'] = 1;
463+
}
464+
if (!is_null($item)) {
465+
$item->add($inputs);
466+
Event::log(
467+
$inputs[$fkField],
468+
$this->getType(),
469+
4,
470+
$this->service,
471+
//TRANS: %s is the user login
472+
sprintf(__('%s adds a target'), $_SESSION["glpiname"])
473+
);
474+
}
475+
}
394476
}

0 commit comments

Comments
 (0)