forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyViewsProperty.java
More file actions
349 lines (296 loc) · 10.6 KB
/
MyViewsProperty.java
File metadata and controls
349 lines (296 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Tom Huybrechts
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor.FormException;
import hudson.model.userproperty.UserPropertyCategory;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.views.MyViewsTabBar;
import hudson.views.ViewsTabBar;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerFallback;
import org.kohsuke.stapler.StaplerProxy;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.verb.POST;
/**
* A UserProperty that remembers user-private views.
*
* @author Tom Huybrechts
*/
public class MyViewsProperty extends UserProperty implements ModifiableViewGroup, Action, StaplerFallback, StaplerProxy {
/**
* Escape hatch for StaplerProxy-based access control
*/
@Restricted(NoExternalUse.class)
@SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "for script console")
public static /* non-final */ boolean SKIP_PERMISSION_CHECK = SystemProperties.getBoolean(MyViewsProperty.class.getName() + ".skipPermissionCheck");
/**
* Name of the primary view defined by the user.
* {@code null} means that the View is not defined.
*/
@CheckForNull
private String primaryViewName;
/**
* Always hold at least one view.
*/
private CopyOnWriteArrayList<View> views = new CopyOnWriteArrayList<>();
private transient ViewGroupMixIn viewGroupMixIn;
@DataBoundConstructor
public MyViewsProperty(@CheckForNull String primaryViewName) {
this.primaryViewName = primaryViewName;
readResolve(); // initialize fields
}
private MyViewsProperty() {
this(null);
}
@Restricted(NoExternalUse.class)
public Object readResolve() {
if (views == null)
// this shouldn't happen, but an error in 1.319 meant the last view could be deleted
views = new CopyOnWriteArrayList<>();
if (views.isEmpty()) {
// preserve the non-empty invariant
views.add(new AllView(AllView.DEFAULT_VIEW_NAME, this));
}
if (primaryViewName != null) {
// It may happen when the default constructor is invoked
primaryViewName = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryViewName);
}
viewGroupMixIn = new ViewGroupMixIn(this) {
@Override
protected List<View> views() { return views; }
@Override
protected String primaryView() { return primaryViewName; }
@Override
protected void primaryView(String name) { primaryViewName = name; }
};
return this;
}
@CheckForNull
public String getPrimaryViewName() {
return primaryViewName;
}
/**
* Sets the primary view.
* @param primaryViewName Name of the primary view to be set.
* {@code null} to make the primary view undefined.
*/
public void setPrimaryViewName(@CheckForNull String primaryViewName) {
this.primaryViewName = primaryViewName;
}
public User getUser() {
return user;
}
///// ViewGroup methods /////
@Override
public String getUrl() {
return user.getUrl() + "/my-views/";
}
@Override
public void save() throws IOException {
if (user != null) {
user.save();
}
}
@Override
public Collection<View> getViews() {
return viewGroupMixIn.getViews();
}
@Override
public View getView(String name) {
return viewGroupMixIn.getView(name);
}
@Override
public boolean canDelete(View view) {
return viewGroupMixIn.canDelete(view);
}
@Override
public void deleteView(View view) throws IOException {
viewGroupMixIn.deleteView(view);
}
@Override
public void onViewRenamed(View view, String oldName, String newName) {
viewGroupMixIn.onViewRenamed(view, oldName, newName);
}
@Override
public void addView(View view) throws IOException {
viewGroupMixIn.addView(view);
}
@Override
public View getPrimaryView() {
return viewGroupMixIn.getPrimaryView();
}
public HttpResponse doIndex() {
return new HttpRedirect("view/" + Util.rawEncode(getPrimaryView().getViewName()) + "/");
}
@POST
public synchronized void doCreateView(StaplerRequest2 req, StaplerResponse2 rsp)
throws IOException, ServletException, ParseException, FormException {
checkPermission(View.CREATE);
addView(View.create(req, rsp, this));
}
/**
* Checks if a private view with the given name exists.
* An error is returned if exists==true but the view does not exist.
* An error is also returned if exists==false but the view does exist.
**/
public FormValidation doViewExistsCheck(@QueryParameter String value, @QueryParameter boolean exists) {
checkPermission(View.CREATE);
String view = Util.fixEmpty(value);
if (view == null) return FormValidation.ok();
if (exists) {
return getView(view) != null ?
FormValidation.ok() :
FormValidation.error(Messages.MyViewsProperty_ViewExistsCheck_NotExist(view));
} else {
return getView(view) == null ?
FormValidation.ok() :
FormValidation.error(Messages.MyViewsProperty_ViewExistsCheck_AlreadyExists(view));
}
}
@Override
public ACL getACL() {
return user.getACL();
}
///// Action methods /////
@Override
public String getDisplayName() {
return Messages.MyViewsProperty_DisplayName();
}
@Override
public String getIconFileName() {
if (SKIP_PERMISSION_CHECK || getACL().hasPermission(Jenkins.ADMINISTER))
return "symbol-browsers";
else
return null;
}
@Override
public String getUrlName() {
return "my-views";
}
@Override
public Object getTarget() {
if (!SKIP_PERMISSION_CHECK) {
checkPermission(Jenkins.ADMINISTER);
}
return this;
}
@Extension @Symbol("myView")
public static class DescriptorImpl extends UserPropertyDescriptor {
@NonNull
@Override
public String getDisplayName() {
return Messages.MyViewsProperty_DisplayName();
}
@Override
public UserProperty newInstance(User user) {
return new MyViewsProperty();
}
@Override
public @NonNull UserPropertyCategory getUserPropertyCategory() {
return UserPropertyCategory.get(UserPropertyCategory.Preferences.class);
}
@POST
public ListBoxModel doFillPrimaryViewNameItems(@AncestorInPath User user) throws IOException {
ListBoxModel items = new ListBoxModel();
user = user == null ? User.current() : user;
if (user != null) {
MyViewsProperty property = user.getProperty(MyViewsProperty.class);
if (property == null) {
property = new MyViewsProperty();
user.addProperty(property);
}
for (View view : property.views) {
items.add(new ListBoxModel.Option(view.getDisplayName(), view.getViewName(),
view == property.getPrimaryView()));
}
}
return items;
}
}
@Override
public UserProperty reconfigure(StaplerRequest2 req, JSONObject form) throws FormException {
req.bindJSON(this, form);
return this;
}
@Override
public ViewsTabBar getViewsTabBar() {
return Jenkins.get().getViewsTabBar();
}
@Override
public List<Action> getViewActions() {
// Jenkins.get().getViewActions() are tempting but they are in a wrong scope
return Collections.emptyList();
}
@Override
public Object getStaplerFallback() {
return getPrimaryView();
}
public MyViewsTabBar getMyViewsTabBar() {
return Jenkins.get().getMyViewsTabBar();
}
@Symbol("myView")
public static class GlobalAction implements RootAction {
@Override
public String getDisplayName() {
return Messages.MyViewsProperty_GlobalAction_DisplayName();
}
@Override
public String getIconFileName() {
// do not show when not logged in
if (User.current() == null) {
return null;
}
return "symbol-browsers";
}
@Override
public String getUrlName() {
return "/me/my-views";
}
}
}