001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.permission;
016    
017    import java.io.Serializable;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    /**
025     * Manages a list resource block IDs and the actions that can be performed on
026     * the resources in each.
027     *
028     * @author Connor McKay
029     */
030    public class ResourceBlockIdsBag implements Serializable {
031    
032            public void addResourceBlockId(long resourceBlockId, long actionIdsLong) {
033                    actionIdsLong |= getActionIds(resourceBlockId);
034    
035                    _permissions.put(resourceBlockId, actionIdsLong);
036            }
037    
038            public long getActionIds(long resourceBlockId) {
039                    Long oldActionIdsLong = _permissions.get(resourceBlockId);
040    
041                    if (oldActionIdsLong == null) {
042                            oldActionIdsLong = 0L;
043                    }
044    
045                    return oldActionIdsLong;
046            }
047    
048            public List<Long> getResourceBlockIds(long actionIdsLong) {
049                    List<Long> resourceBlockIds = new ArrayList<Long>();
050    
051                    for (Map.Entry<Long, Long> permission : _permissions.entrySet()) {
052                            if ((permission.getValue() & actionIdsLong) == actionIdsLong) {
053                                    resourceBlockIds.add(permission.getKey());
054                            }
055                    }
056    
057                    return resourceBlockIds;
058            }
059    
060            public boolean hasResourceBlockId(
061                    long resourceBlockId, long actionIdsLong) {
062    
063                    if ((getActionIds(resourceBlockId) & actionIdsLong) == actionIdsLong) {
064                            return true;
065                    }
066                    else {
067                            return false;
068                    }
069            }
070    
071            private Map<Long, Long> _permissions = new HashMap<Long, Long>();
072    
073    }