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.model;
016    
017    import com.liferay.portal.kernel.util.Digester;
018    import com.liferay.portal.kernel.util.DigesterUtil;
019    
020    import java.nio.ByteBuffer;
021    
022    import java.util.Map;
023    import java.util.SortedMap;
024    import java.util.TreeMap;
025    
026    /**
027     * Manages a list of the roles with permission to access a resource block and
028     * the actions they can perform.
029     *
030     * @author Connor McKay
031     */
032    public class ResourceBlockPermissionsContainer {
033    
034            public void addPermission(long roleId, long actionIdsLong) {
035                    actionIdsLong |= getActionIds(roleId);
036    
037                    setPermissions(roleId, actionIdsLong);
038            }
039    
040            public long getActionIds(long roleId) {
041                    Long actionIdsLong = _permissions.get(roleId);
042    
043                    if (actionIdsLong == null) {
044                            actionIdsLong = 0L;
045                    }
046    
047                    return actionIdsLong;
048            }
049    
050            public SortedMap<Long, Long> getPermissions() {
051                    return _permissions;
052            }
053    
054            /**
055             * Returns the permissions hash of the resource permissions. The permissions
056             * hash is a representation of all the roles with access to the resource
057             * along with the actions they can perform.
058             *
059             * @return the permissions hash of the resource permissions
060             */
061            public String getPermissionsHash() {
062    
063                    // long is 8 bytes, there are 2 longs per permission, so preallocate
064                    // byte buffer to 16 * the number of permissions.
065    
066                    ByteBuffer byteBuffer = ByteBuffer.allocate(_permissions.size() * 16);
067    
068                    for (Map.Entry<Long, Long> entry : _permissions.entrySet()) {
069                            byteBuffer.putLong(entry.getKey());
070                            byteBuffer.putLong(entry.getValue());
071                    }
072    
073                    byteBuffer.flip();
074    
075                    return DigesterUtil.digestHex(Digester.SHA_1, byteBuffer);
076            }
077    
078            public void removePermission(long roleId, long actionIdsLong) {
079                    actionIdsLong = getActionIds(roleId) & (~actionIdsLong);
080    
081                    setPermissions(roleId, actionIdsLong);
082            }
083    
084            public void setPermissions(long roleId, long actionIdsLong) {
085                    if (actionIdsLong == 0) {
086                            _permissions.remove(roleId);
087                    }
088                    else {
089                            _permissions.put(roleId, actionIdsLong);
090                    }
091            }
092    
093            private SortedMap<Long, Long> _permissions = new TreeMap<Long, Long>();
094    
095    }