1
1
<?php
2
- /* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
2
+ /* Icinga Web 2 | (c) 2022 Icinga Development Team | GPLv2+ */
3
3
4
4
namespace Icinga \Forms \Config \UserGroup ;
5
5
6
6
use Exception ;
7
- use Icinga \Data \Extensible ;
8
- use Icinga \Data \Filter \Filter ;
9
- use Icinga \Data \Selectable ;
10
7
use Icinga \Exception \NotFoundError ;
11
- use Icinga \Web \Form ;
12
8
use Icinga \Web \Notification ;
9
+ use ipl \Web \Control \SimpleSearchField ;
13
10
14
11
/**
15
12
* Form for adding one or more group members
16
13
*/
17
- class AddMemberForm extends Form
14
+ class AddMemberForm extends SimpleSearchField
18
15
{
19
- /**
20
- * The data source to fetch users from
21
- *
22
- * @var Selectable
23
- */
24
- protected $ ds ;
25
-
26
- /**
27
- * The user group backend to use
28
- *
29
- * @var Extensible
30
- */
31
16
protected $ backend ;
32
17
33
- /**
34
- * The group to add members for
35
- *
36
- * @var string
37
- */
38
18
protected $ groupName ;
39
19
40
20
/**
41
- * Set the data source to fetch users from
42
- *
43
- * @param Selectable $ds
44
- *
45
- * @return $this
46
- */
47
- public function setDataSource (Selectable $ ds )
48
- {
49
- $ this ->ds = $ ds ;
50
- return $ this ;
51
- }
52
-
53
- /**
54
- * Set the user group backend to use
55
- *
56
- * @param Extensible $backend
57
- *
58
- * @return $this
21
+ * @param mixed $backend
59
22
*/
60
- public function setBackend (Extensible $ backend )
23
+ public function setBackend ($ backend )
61
24
{
62
25
$ this ->backend = $ backend ;
26
+
63
27
return $ this ;
64
28
}
65
29
66
30
/**
67
- * Set the group to add members for
68
- *
69
- * @param string $groupName
70
- *
71
- * @return $this
31
+ * @param mixed $groupName
72
32
*/
73
33
public function setGroupName ($ groupName )
74
34
{
75
35
$ this ->groupName = $ groupName ;
76
- return $ this ;
77
- }
78
36
79
- /**
80
- * Create and add elements to this form
81
- *
82
- * @param array $formData The data sent by the user
83
- */
84
- public function createElements (array $ formData )
85
- {
86
- // TODO(jom): Fetching already existing members to prevent the user from mistakenly creating duplicate
87
- // memberships (no matter whether the data source permits it or not, a member does never need to be
88
- // added more than once) should be kept at backend level (GroupController::fetchUsers) but this does
89
- // not work currently as our ldap protocol stuff is unable to handle our filter implementation..
90
- $ members = $ this ->backend
91
- ->select ()
92
- ->from ('group_membership ' , array ('user_name ' ))
93
- ->where ('group_name ' , $ this ->groupName )
94
- ->fetchColumn ();
95
- $ filter = empty ($ members ) ? Filter::matchAll () : Filter::not (Filter::where ('user_name ' , $ members ));
96
-
97
- $ users = $ this ->ds ->select ()->from ('user ' , array ('user_name ' ))->applyFilter ($ filter )->fetchColumn ();
98
- if (! empty ($ users )) {
99
- $ this ->addElement (
100
- 'multiselect ' ,
101
- 'user_name ' ,
102
- array (
103
- 'multiOptions ' => array_combine ($ users , $ users ),
104
- 'label ' => $ this ->translate ('Backend Users ' ),
105
- 'description ' => $ this ->translate (
106
- 'Select one or more users (fetched from your user backends) to add as group member '
107
- ),
108
- 'class ' => 'grant-permissions '
109
- )
110
- );
111
- }
112
-
113
- $ this ->addElement (
114
- 'textarea ' ,
115
- 'users ' ,
116
- array (
117
- 'required ' => empty ($ users ),
118
- 'label ' => $ this ->translate ('Users ' ),
119
- 'description ' => $ this ->translate (
120
- 'Provide one or more usernames separated by comma to add as group member '
121
- )
122
- )
123
- );
124
-
125
- $ this ->setTitle (sprintf ($ this ->translate ('Add members for group %s ' ), $ this ->groupName ));
126
- $ this ->setSubmitLabel ($ this ->translate ('Add ' ));
37
+ return $ this ;
127
38
}
128
39
129
40
/**
@@ -133,48 +44,44 @@ public function createElements(array $formData)
133
44
*/
134
45
public function onSuccess ()
135
46
{
136
- $ userNames = $ this ->getValue ('user_name ' ) ?: array ();
137
- if (($ users = $ this ->getValue ('users ' ))) {
138
- $ userNames = array_merge ($ userNames , array_map ('trim ' , explode (', ' , $ users )));
139
- }
140
-
141
- if (empty ($ userNames )) {
142
- $ this ->info ($ this ->translate (
143
- 'Please provide at least one username, either by choosing one '
144
- . 'in the list or by manually typing one in the text box below '
145
- ));
47
+ $ q = $ this ->getValue ($ this ->getSearchParameter ());
48
+ if (empty ($ q )) {
49
+ Notification::error (t ('Please provide at least one username ' ));
146
50
return false ;
147
51
}
148
52
53
+ $ userNames = array_unique (explode (self ::TERM_SEPARATOR , urldecode ($ q )));
54
+ $ userNames = array_map ('trim ' , $ userNames );
55
+
149
56
$ single = null ;
150
57
foreach ($ userNames as $ userName ) {
151
58
try {
152
59
$ this ->backend ->insert (
153
60
'group_membership ' ,
154
- array (
61
+ [
155
62
'group_name ' => $ this ->groupName ,
156
63
'user_name ' => $ userName
157
- )
64
+ ]
158
65
);
159
66
} catch (NotFoundError $ e ) {
160
67
throw $ e ; // Trigger 404, the group name is initially accessed as GET parameter
161
68
} catch (Exception $ e ) {
162
69
Notification::error (sprintf (
163
- $ this -> translate ('Failed to add "%s" as group member for "%s" ' ),
70
+ t ('Failed to add "%s" as group member for "%s" ' ),
164
71
$ userName ,
165
72
$ this ->groupName
166
73
));
167
- $ this -> error ( $ e -> getMessage ());
74
+
168
75
return false ;
169
76
}
170
77
171
78
$ single = $ single === null ;
172
79
}
173
80
174
81
if ($ single ) {
175
- Notification::success (sprintf ($ this -> translate ('Group member "%s" added successfully ' ), $ userName ));
82
+ Notification::success (sprintf (t ('Group member "%s" added successfully ' ), $ userName ));
176
83
} else {
177
- Notification::success ($ this -> translate ('Group members added successfully ' ));
84
+ Notification::success (t ('Group members added successfully ' ));
178
85
}
179
86
180
87
return true ;
0 commit comments