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