34
34
*/
35
35
36
36
use Glpi \Application \View \TemplateRenderer ;
37
+ use Glpi \Event ;
37
38
38
39
/**
39
40
* Common DataBase visibility for items
40
41
*/
41
- abstract class CommonDBVisible extends CommonDBTM
42
+ trait CommonDBVisible
42
43
{
44
+ /**
45
+ * Types of target available for the itemtype
46
+ * @var string[]
47
+ */
48
+ public static $ types = ['Entity ' , 'Group ' , 'Profile ' , 'User ' ];
49
+
43
50
/**
44
51
* Entities on which item is visible.
45
52
* Keys are ID, values are DB fields values.
@@ -68,46 +75,56 @@ abstract class CommonDBVisible extends CommonDBTM
68
75
*/
69
76
protected $ users = [];
70
77
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 ;
92
89
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 ()
94
109
{
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 ();
110
119
}
120
+ if (!$ this ->profileClass ) {
121
+ $ this ->profileClass = 'Profile_ ' . $ this ->getType ();
122
+ }
123
+ if (!$ this ->service ) {
124
+ $ this ->service = 'tools ' ;
125
+ }
126
+
127
+ parent ::__construct ();
111
128
}
112
129
113
130
/**
@@ -204,6 +221,16 @@ public function countVisibilities()
204
221
+ count ($ this ->profiles ));
205
222
}
206
223
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
+
207
234
/**
208
235
* Show visibility configuration
209
236
*
@@ -220,9 +247,10 @@ public function showVisibility()
220
247
221
248
if ($ canedit ) {
222
249
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 ,
226
254
'add_target_msg ' => __ ('Add a target ' ),
227
255
'visiblity_dropdown_params ' => $ this ->getShowVisibilityDropdownParams (),
228
256
]);
@@ -233,7 +261,7 @@ public function showVisibility()
233
261
foreach ($ this ->users as $ val ) {
234
262
foreach ($ val as $ data ) {
235
263
$ entries [] = [
236
- 'itemtype ' => static ::class . '_User ' ,
264
+ 'itemtype ' => $ this instanceof SavedSearch ? SavedSearch_UserTarget::class : static ::class . '_User ' ,
237
265
'id ' => $ data ['id ' ],
238
266
'type ' => User::getTypeName (1 ),
239
267
'recipient ' => htmlescape (getUserName ($ data ['users_id ' ])),
@@ -270,7 +298,7 @@ public function showVisibility()
270
298
}
271
299
}
272
300
$ entries [] = [
273
- 'itemtype ' => static ::class . ' _Group ' ,
301
+ 'itemtype ' => ' Group_ ' . static ::class,
274
302
'id ' => $ data ['id ' ],
275
303
'type ' => Group::getTypeName (1 ),
276
304
'recipient ' => $ recipient ,
@@ -347,7 +375,8 @@ public function showVisibility()
347
375
'container ' => 'mass ' . static ::class . $ rand ,
348
376
'specific_actions ' => ['delete ' => _x ('button ' , 'Delete permanently ' )],
349
377
];
350
- if ($ this ->fields ['users_id ' ] !== Session::getLoginUserID ()) {
378
+ if (isset ($ this ->fields ['users_id ' ])
379
+ && $ this ->fields ['users_id ' ] !== Session::getLoginUserID ()) {
351
380
$ massiveactionparams ['confirm ' ] = __ ('Caution! You are not the author of this item. Deleting targets can result in loss of access. ' );
352
381
}
353
382
@@ -379,10 +408,10 @@ public function showVisibility()
379
408
*/
380
409
protected function getShowVisibilityDropdownParams ()
381
410
{
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
+ }
386
415
if (isset ($ this ->fields ['entities_id ' ])) {
387
416
$ params ['entity ' ] = $ this ->fields ['entities_id ' ];
388
417
}
@@ -391,4 +420,57 @@ protected function getShowVisibilityDropdownParams()
391
420
}
392
421
return $ params ;
393
422
}
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
+ }
394
476
}
0 commit comments