File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\PermissionScope.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.collect.ImmutableSet;
27 import hudson.model.Build;
28 import hudson.model.Computer;
29 import hudson.model.Item;
30 import hudson.model.ItemGroup;
31 import hudson.model.Job;
32 import hudson.model.Run;
33 import jenkins.model.Jenkins;
34
35 import java.util.Set;
36
37 /**
38 * Represents the model class {@link Permission} acts on and scoped to.
39 *
40 * <p>
41 * This abstraction primarily controls what permissions are configurable at what level.
42 *
43 * <p>
44 * For example, "create an item" is scoped to {@link ItemGroup} in the sense that
45 * it "acts" on the item group object (and therefore it won't make sense to control
46 * this permission at more finer scope, such as {@link Run}.)
47 *
48 * <p>
49 * Every {@link Permission} belongs to a set of scopes. Each scope is to be represented
50 * as a constant. This class defines a few constants, but plugins can define their own.
51 *
52 * @author Kohsuke Kawaguchi
53 * @since 1.421
54 */
55 public final class PermissionScope {
56 /**
57 * Domain model type that approximates this scope.
58 */
59 public final Class modelClass;
60
61 /**
62 * Other bigger scopes that this scope divides. For example, permissions scoped to {@link ItemGroup}
63 * should be automatically configurable at {@link Jenkins} level, and in situations like this,
64 * we say {@link ItemGroup} permission scope is contained in the {@link Jenkins} permission scope.
65 */
66 private final Set<PermissionScope> containers;
67
68 public PermissionScope(Class modelClass, PermissionScope... containers) {
69 this.modelClass = modelClass;
70 this.containers = ImmutableSet.copyOf(containers);
71 }
72
73 /**
74 * Returns true if this scope is directly or indirectly contained by the given scope.
75 *
76 * <p>
77 * A scope always contains itself.
78 */
79 public boolean isContainedBy(PermissionScope s) {
80 if (this==s) return true;
81 for (PermissionScope c : containers) {
82 if (c.isContainedBy(s))
83 return true;
84 }
85 return false;
86 }
87
88 //
89 // A few built-in permission scopes
90 //
91
92 /**
93 * Permissions scoped to the entire Jenkins instance.
94 */
95 public static final PermissionScope JENKINS = new PermissionScope(Jenkins.class);
96
97 /**
98 * Permissions scoped to containers of {@link Item}s.
99 */
100 public static final PermissionScope ITEM_GROUP = new PermissionScope(ItemGroup.class,JENKINS);
101
102 /**
103 * Permissions scoped to {@link Item}s (including {@link Job}s and other subtypes)
104 */
105 public static final PermissionScope ITEM = new PermissionScope(Item.class,ITEM_GROUP);
106
107 /**
108 * Permissions scoped to {@link Run}s (including {@link Build}s and other subtypes)
109 */
110 public static final PermissionScope RUN = new PermissionScope(Run.class,ITEM);
111
112 /**
113 * Permissions scoped to current instances of {@link Computer}s.
114 */
115 public static final PermissionScope COMPUTER = new PermissionScope(Computer.class,JENKINS);
116 }
117