ProjectMatrixAuthorizationStrategy.java 4.5 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc., Seiji Sogabe, Tom Huybrechts
K
kohsuke 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 
 * 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.
 */
24 25
package hudson.security;

26
import hudson.model.AbstractItem;
27
import hudson.model.Descriptor;
28
import jenkins.model.Jenkins;
29 30
import hudson.model.Item;
import hudson.model.ItemGroup;
31
import hudson.model.Job;
32
import hudson.util.RobustReflectionConverter;
33
import hudson.Extension;
34 35 36 37 38
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.core.JVM;

39 40 41
import java.util.HashSet;
import java.util.Set;

42 43 44 45 46 47 48 49 50 51
/**
 * {@link GlobalMatrixAuthorizationStrategy} plus per-project ACL.
 *
 * <p>
 * Per-project ACL is stored in {@link AuthorizationMatrixProperty}.
 *
 * @author Kohsuke Kawaguchi
 */
public class ProjectMatrixAuthorizationStrategy extends GlobalMatrixAuthorizationStrategy {
    @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 {
K
Kohsuke Kawaguchi 已提交
57
            return getACL(project.getParent());
58 59 60
        }
    }

61 62 63 64 65 66 67 68 69 70 71 72 73
    public SidACL getACL(ItemGroup g) {
        if (g instanceof Item) {
            Item item = (Item) g;
            return (SidACL)item.getACL();
        }
        return getRootACL();
    }

    @Override
    public SidACL getACL(AbstractItem item) {
        return getACL(item.getParent());
    }

74 75 76 77
    @Override
    public Set<String> getGroups() {
        Set<String> r = new HashSet<String>();
        r.addAll(super.getGroups());
78
        for (Job<?,?> j : Jenkins.getInstance().getItems(Job.class)) {
79 80 81 82 83 84 85
            AuthorizationMatrixProperty amp = j.getProperty(AuthorizationMatrixProperty.class);
            if (amp != null)
                r.addAll(amp.getGroups());
        }
        return r;
    }

86
    @Extension
87
    public static final Descriptor<AuthorizationStrategy> DESCRIPTOR = new DescriptorImpl() {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        @Override
        protected GlobalMatrixAuthorizationStrategy create() {
            return new ProjectMatrixAuthorizationStrategy();
        }

        @Override
        public String getDisplayName() {
            return Messages.ProjectMatrixAuthorizationStrategy_DisplayName();
        }
    };

    public static class ConverterImpl extends GlobalMatrixAuthorizationStrategy.ConverterImpl {
        private RobustReflectionConverter ref;

        public ConverterImpl(Mapper m) {
            ref = new RobustReflectionConverter(m,new JVM().bestReflectionProvider());
        }

106
        @Override
107 108 109 110
        protected GlobalMatrixAuthorizationStrategy create() {
            return new ProjectMatrixAuthorizationStrategy();
        }

111
        @Override
112 113 114 115 116 117 118 119 120 121
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            String name = reader.peekNextChild();
            if(name!=null && (name.equals("permission") || name.equals("useProjectSecurity")))
                // the proper serialization form
                return super.unmarshal(reader, context);
            else
                // remain compatible with earlier problem where we used reflection converter
                return ref.unmarshal(reader,context);
        }

122
        @Override
123 124 125 126 127 128
        public boolean canConvert(Class type) {
            return type==ProjectMatrixAuthorizationStrategy.class;
        }
    }
}