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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc., Seiji Sogabe, Tom Huybrechts
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 hudson.model.AbstractItem;
27 import hudson.model.Descriptor;
28 import jenkins.model.Jenkins;
29 import hudson.model.Item;
30 import hudson.model.ItemGroup;
31 import hudson.model.Job;
32 import hudson.util.RobustReflectionConverter;
33 import hudson.Extension;
34 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
35 import com.thoughtworks.xstream.converters.UnmarshallingContext;
36 import com.thoughtworks.xstream.mapper.Mapper;
37 import com.thoughtworks.xstream.core.JVM;
38
39 import java.util.HashSet;
40 import java.util.Set;
41
42 /**
43 * {@link GlobalMatrixAuthorizationStrategy} plus per-project ACL.
44 *
45 * <p>
46 * Per-project ACL is stored in {@link AuthorizationMatrixProperty}.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50 public class ProjectMatrixAuthorizationStrategy extends GlobalMatrixAuthorizationStrategy {
51 @Override
52 public ACL getACL(Job<?,?> project) {
53 AuthorizationMatrixProperty amp = project.getProperty(AuthorizationMatrixProperty.class);
54 if (amp != null) {
55 return amp.getACL().newInheritingACL(getACL(project.getParent()));
56 } else {
57 return getACL(project.getParent());
58 }
59 }
60
61 public SidACL getACL(ItemGroup g) {
62 if (g instanceof Item) {
63 Item item = (Item) g;
64 return (SidACL)item.getACL();
65 }
66 return getRootACL();
67 }
68
69 @Override
70 public SidACL getACL(AbstractItem item) {
71 return getACL(item.getParent());
72 }
73
74 @Override
75 public Set<String> getGroups() {
76 Set<String> r = new HashSet<String>();
77 r.addAll(super.getGroups());
78 for (Job<?,?> j : Jenkins.getInstance().getItems(Job.class)) {
79 AuthorizationMatrixProperty amp = j.getProperty(AuthorizationMatrixProperty.class);
80 if (amp != null)
81 r.addAll(amp.getGroups());
82 }
83 return r;
84 }
85
86 @Extension
87 public static final Descriptor<AuthorizationStrategy> DESCRIPTOR = new DescriptorImpl() {
88 @Override
89 protected GlobalMatrixAuthorizationStrategy create() {
90 return new ProjectMatrixAuthorizationStrategy();
91 }
92
93 @Override
94 public String getDisplayName() {
95 return Messages.ProjectMatrixAuthorizationStrategy_DisplayName();
96 }
97 };
98
99 public static class ConverterImpl extends GlobalMatrixAuthorizationStrategy.ConverterImpl {
100 private RobustReflectionConverter ref;
101
102 public ConverterImpl(Mapper m) {
103 ref = new RobustReflectionConverter(m,new JVM().bestReflectionProvider());
104 }
105
106 @Override
107 protected GlobalMatrixAuthorizationStrategy create() {
108 return new ProjectMatrixAuthorizationStrategy();
109 }
110
111 @Override
112 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
113 String name = reader.peekNextChild();
114 if(name!=null && (name.equals("permission") || name.equals("useProjectSecurity")))
115 // the proper serialization form
116 return super.unmarshal(reader, context);
117 else
118 // remain compatible with earlier problem where we used reflection converter
119 return ref.unmarshal(reader,context);
120 }
121
122 @Override
123 public boolean canConvert(Class type) {
124 return type==ProjectMatrixAuthorizationStrategy.class;
125 }
126 }
127 }
128
129