001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.web.shared;
017
018import java.io.Serializable;
019import java.util.ArrayList;
020import java.util.Collection;
021
022/**
023 * @author kulikov
024 */
025public class JcrPolicy implements Serializable {
026    private static final long serialVersionUID = 1L;
027    private String principal;
028    private ArrayList<JcrPermission> permissions = new ArrayList<JcrPermission>();
029
030    public static JcrPolicy everyone() {
031        return new JcrPolicy("EVERYONE");
032    }
033
034    public JcrPolicy() {
035    }
036
037    /**
038     * Creates ACL entry for the given principal and granting all permissions.
039     * 
040     * @param principal the name of the principal.
041     */
042    public JcrPolicy( String principal ) {
043        this.principal = principal;
044        this.permissions.add(JcrPermission.ALL);
045    }
046
047    public String getPrincipal() {
048        return principal;
049    }
050
051    public void setPrincipal( String principal ) {
052        this.principal = principal;
053    }
054
055    public void add( JcrPermission permission ) {
056        permissions.add(permission);
057    }
058
059    public void remove( JcrPermission permission ) {
060        permissions.remove(permission);
061    }
062
063    public Collection<JcrPermission> getPermissions() {
064        return permissions;
065    }
066
067    public void update( String action,
068                        String value ) {
069        Boolean enable = value.equals("Allow");
070        if (enable) {
071            permissions.add(JcrPermission.fromDisplayName(action));
072        } else {
073            JcrPermission p = find(action);
074            permissions.remove(p);
075        }
076    }
077
078    private JcrPermission find( String name ) {
079        JcrPermission permission = JcrPermission.fromDisplayName(name);
080        for (JcrPermission p : permissions) {
081            if (permission.getName().equals(p.getName())) {
082                return p;
083            }
084        }
085        return null;
086    }
087
088}