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.kernel.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.model.Role;
021    import com.liferay.portal.kernel.model.RoleConstants;
022    import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
023    import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
024    
025    import java.util.Map;
026    import java.util.Map.Entry;
027    
028    import javax.portlet.PortletRequest;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Jorge Ferrer
034     */
035    public class ModelPermissionsFactory {
036    
037            public static final String MODEL_PERMISSIONS_PREFIX = "modelPermissions";
038    
039            public static ModelPermissions create(HttpServletRequest request) {
040                    Map<String, String[]> parameterMap = request.getParameterMap();
041    
042                    return create(parameterMap);
043            }
044    
045            public static ModelPermissions create(Map<String, String[]> parameterMap) {
046                    ModelPermissions modelPermissions = new ModelPermissions();
047    
048                    for (Entry<String, String[]> entry : parameterMap.entrySet()) {
049                            String parameterName = entry.getKey();
050    
051                            if (!parameterName.startsWith(MODEL_PERMISSIONS_PREFIX)) {
052                                    continue;
053                            }
054    
055                            String roleName = parameterName.substring(
056                                    MODEL_PERMISSIONS_PREFIX.length());
057    
058                            Role role = null;
059    
060                            try {
061                                    role = RoleLocalServiceUtil.getRole(
062                                            CompanyThreadLocal.getCompanyId(), roleName);
063                            }
064                            catch (PortalException pe) {
065                                    if (_log.isInfoEnabled()) {
066                                            _log.info("Unable to get role " + roleName);
067                                    }
068    
069                                    continue;
070                            }
071    
072                            modelPermissions.addRolePermissions(
073                                    role.getName(), entry.getValue());
074                    }
075    
076                    return modelPermissions;
077            }
078    
079            public static ModelPermissions create(PortletRequest portletRequest) {
080                    Map<String, String[]> parameterMap = portletRequest.getParameterMap();
081    
082                    return create(parameterMap);
083            }
084    
085            public static ModelPermissions create(
086                    String[] groupPermissions, String[] guestPermissions) {
087    
088                    ModelPermissions modelPermissions = new ModelPermissions();
089    
090                    if ((groupPermissions != null) && (groupPermissions.length > 0)) {
091                            modelPermissions.addRolePermissions(
092                                    RoleConstants.PLACEHOLDER_DEFAULT_GROUP_ROLE, groupPermissions);
093                    }
094    
095                    if ((guestPermissions != null) && (guestPermissions.length > 0)) {
096                            modelPermissions.addRolePermissions(
097                                    RoleConstants.GUEST, guestPermissions);
098                    }
099    
100                    return modelPermissions;
101            }
102    
103            private static final Log _log = LogFactoryUtil.getLog(
104                    ModelPermissionsFactory.class);
105    
106    }