1
1
<?php
2
- /* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
2
+ /* Icinga Web 2 | (c) 2015 Icinga GmbH | GPLv2+ */
3
3
4
4
namespace Icinga \Forms \Config \UserGroup ;
5
5
6
6
use Exception ;
7
7
use Icinga \Data \Extensible ;
8
- use Icinga \Data \Filter \Filter ;
9
- use Icinga \Data \Selectable ;
10
- 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
16
/**
27
17
* The user group backend to use
28
18
*
@@ -37,19 +27,6 @@ class AddMemberForm extends Form
37
27
*/
38
28
protected $ groupName ;
39
29
40
- /**
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
30
/**
54
31
* Set the user group backend to use
55
32
*
@@ -76,107 +53,50 @@ public function setGroupName($groupName)
76
53
return $ this ;
77
54
}
78
55
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 )
56
+ public function onSuccess ()
85
57
{
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 ));
58
+ $ q = $ this ->getValue ($ this ->getSearchParameter ());
59
+ if (empty ($ q )) {
60
+ Notification::info (t ('Please provide at least one username ' ));
96
61
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
- );
62
+ return ;
111
63
}
112
64
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
- )
65
+ $ userNames = array_unique (
66
+ explode (self ::TERM_SEPARATOR , urldecode ($ q ))
123
67
);
124
68
125
- $ this ->setTitle (sprintf ($ this ->translate ('Add members for group %s ' ), $ this ->groupName ));
126
- $ this ->setSubmitLabel ($ this ->translate ('Add ' ));
127
- }
128
-
129
- /**
130
- * Insert the members for the group
131
- *
132
- * @return bool
133
- */
134
- public function onSuccess ()
135
- {
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
- ));
146
- return false ;
147
- }
69
+ $ userNames = array_filter (
70
+ array_map ('trim ' , $ userNames )
71
+ );
148
72
149
73
$ single = null ;
150
74
foreach ($ userNames as $ userName ) {
151
75
try {
152
76
$ this ->backend ->insert (
153
77
'group_membership ' ,
154
- array (
78
+ [
155
79
'group_name ' => $ this ->groupName ,
156
80
'user_name ' => $ userName
157
- )
81
+ ]
158
82
);
159
- } catch (NotFoundError $ e ) {
160
- throw $ e ; // Trigger 404, the group name is initially accessed as GET parameter
161
83
} catch (Exception $ e ) {
162
84
Notification::error (sprintf (
163
- $ this -> translate ('Failed to add "%s" as group member for "%s" ' ),
85
+ t ('Failed to add "%s" as group member for "%s" ' ),
164
86
$ userName ,
165
87
$ this ->groupName
166
88
));
167
- $ this -> error ( $ e -> getMessage ());
168
- return false ;
89
+
90
+ return ;
169
91
}
170
92
171
93
$ single = $ single === null ;
172
94
}
173
95
174
96
if ($ single ) {
175
- Notification::success (sprintf ($ this -> translate ('Group member "%s" added successfully ' ), $ userName ));
97
+ Notification::success (sprintf (t ('Group member "%s" added successfully ' ), $ userName ));
176
98
} else {
177
- Notification::success ($ this -> translate ('Group members added successfully ' ));
99
+ Notification::success (t ('Group members added successfully ' ));
178
100
}
179
-
180
- return true ;
181
101
}
182
102
}
0 commit comments