File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\GlobalSecurityConfiguration.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.security;
25
26 import com.google.common.base.Predicate;
27 import hudson.BulkChange;
28 import hudson.Extension;
29 import hudson.Functions;
30 import hudson.markup.MarkupFormatter;
31 import hudson.model.Descriptor;
32 import hudson.model.Descriptor.FormException;
33 import hudson.model.ManagementLink;
34 import hudson.util.FormApply;
35
36 import java.io.IOException;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 import javax.servlet.ServletException;
41
42 import jenkins.model.GlobalConfigurationCategory;
43 import jenkins.model.Jenkins;
44 import jenkins.util.ServerTcpPort;
45 import net.sf.json.JSONObject;
46
47 import org.kohsuke.stapler.StaplerRequest;
48 import org.kohsuke.stapler.StaplerResponse;
49
50 /**
51 * Security configuration.
52 *
53 * @author Kohsuke Kawaguchi
54 */
55 @Extension(ordinal = Integer.MAX_VALUE - 210)
56 public class GlobalSecurityConfiguration extends ManagementLink {
57
58 private static final Logger LOGGER = Logger.getLogger(GlobalSecurityConfiguration.class.getName());
59
60 public MarkupFormatter getMarkupFormatter() {
61 return Jenkins.getInstance().getMarkupFormatter();
62 }
63
64 public int getSlaveAgentPort() {
65 return Jenkins.getInstance().getSlaveAgentPort();
66 }
67
68 public synchronized void doConfigure(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, FormException {
69 // for compatibility reasons, the actual value is stored in Jenkins
70 BulkChange bc = new BulkChange(Jenkins.getInstance());
71 try{
72 boolean result = configure(req, req.getSubmittedForm());
73 LOGGER.log(Level.FINE, "security saved: "+result);
74 Jenkins.getInstance().save();
75 FormApply.success(req.getContextPath()+"/manage").generateResponse(req, rsp, null);
76 } finally {
77 bc.commit();
78 }
79 }
80
81 public boolean configure(StaplerRequest req, JSONObject json) throws hudson.model.Descriptor.FormException {
82 // for compatibility reasons, the actual value is stored in Jenkins
83 Jenkins j = Jenkins.getInstance();
84 j.checkPermission(Jenkins.ADMINISTER);
85 if (json.has("useSecurity")) {
86 JSONObject security = json.getJSONObject("useSecurity");
87 j.setSecurityRealm(SecurityRealm.all().newInstanceFromRadioList(security, "realm"));
88 j.setAuthorizationStrategy(AuthorizationStrategy.all().newInstanceFromRadioList(security, "authorization"));
89 try {
90 j.setSlaveAgentPort(new ServerTcpPort(security.getJSONObject("slaveAgentPort")).getPort());
91 } catch (IOException e) {
92 throw new hudson.model.Descriptor.FormException(e, "slaveAgentPortType");
93 }
94 if (security.has("markupFormatter")) {
95 j.setMarkupFormatter(req.bindJSON(MarkupFormatter.class, security.getJSONObject("markupFormatter")));
96 } else {
97 j.setMarkupFormatter(null);
98 }
99 } else {
100 j.disableSecurity();
101 }
102
103 // persist all the additional security configs
104 boolean result = true;
105 for(Descriptor<?> d : Functions.getSortedDescriptorsForGlobalConfig(FILTER)){
106 result &= configureDescriptor(req,json,d);
107 }
108
109 return result;
110 }
111
112 private boolean configureDescriptor(StaplerRequest req, JSONObject json, Descriptor<?> d) throws FormException {
113 // collapse the structure to remain backward compatible with the JSON structure before 1.
114 String name = d.getJsonSafeClassName();
115 JSONObject js = json.has(name) ? json.getJSONObject(name) : new JSONObject(); // if it doesn't have the property, the method returns invalid null object.
116 json.putAll(js);
117 return d.configure(req, js);
118 }
119
120 @Override
121 public String getDisplayName() {
122 return Messages.GlobalSecurityConfiguration_DisplayName();
123 }
124
125 @Override
126 public String getDescription() {
127 return Messages.GlobalSecurityConfiguration_Description();
128 }
129
130 @Override
131 public String getIconFileName() {
132 return "secure.png";
133 }
134
135 @Override
136 public String getUrlName() {
137 return "configureSecurity";
138 }
139
140 @Override
141 public Permission getRequiredPermission() {
142 return Jenkins.ADMINISTER;
143 }
144
145 public static Predicate<GlobalConfigurationCategory> FILTER = new Predicate<GlobalConfigurationCategory>() {
146 public boolean apply(GlobalConfigurationCategory input) {
147 return input instanceof GlobalConfigurationCategory.Security;
148 }
149 };
150 }
151