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.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.PluginSetting;
022    import com.liferay.portal.model.RoleConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.RoleLocalServiceUtil;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PluginSettingImpl extends PluginSettingBaseImpl {
031    
032            public PluginSettingImpl() {
033            }
034    
035            public PluginSettingImpl(PluginSetting pluginSetting) {
036                    setCompanyId(pluginSetting.getCompanyId());
037                    setPluginId(pluginSetting.getPluginId());
038                    setPluginType(pluginSetting.getPluginType());
039                    setRoles(pluginSetting.getRoles());
040                    setActive(pluginSetting.getActive());
041            }
042    
043            /**
044             * Adds a role to the list of roles.
045             */
046            @Override
047            public void addRole(String role) {
048                    setRolesArray(ArrayUtil.append(_rolesArray, role));
049            }
050    
051            /**
052             * Returns an array of required roles of the plugin.
053             *
054             * @return an array of required roles of the plugin
055             */
056            @Override
057            public String[] getRolesArray() {
058                    return _rolesArray;
059            }
060    
061            /**
062             * Returns <code>true</code> if the user has permission to use this plugin.
063             *
064             * @param  userId the primary key of the user
065             * @return <code>true</code> if the user has permission to use this plugin
066             */
067            @Override
068            public boolean hasPermission(long userId) {
069                    try {
070                            if (_rolesArray.length == 0) {
071                                    return true;
072                            }
073                            else if (RoleLocalServiceUtil.hasUserRoles(
074                                                    userId, getCompanyId(), _rolesArray, true)) {
075    
076                                    return true;
077                            }
078                            else if (RoleLocalServiceUtil.hasUserRole(
079                                                    userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
080                                                    true)) {
081    
082                                    return true;
083                            }
084                            else {
085                                    User user = UserLocalServiceUtil.getUserById(userId);
086    
087                                    if (user.isDefaultUser() &&
088                                            hasRoleWithName(RoleConstants.GUEST)) {
089    
090                                            return true;
091                                    }
092                            }
093                    }
094                    catch (Exception e) {
095                            _log.error(e);
096                    }
097    
098                    return false;
099            }
100    
101            /**
102             * Returns <code>true</code> if the plugin has a role with the specified
103             * name.
104             *
105             * @param  roleName the role name
106             * @return <code>true</code> if the plugin has a role with the specified
107             *         name
108             */
109            @Override
110            public boolean hasRoleWithName(String roleName) {
111                    for (int i = 0; i < _rolesArray.length; i++) {
112                            if (_rolesArray[i].equalsIgnoreCase(roleName)) {
113                                    return true;
114                            }
115                    }
116    
117                    return false;
118            }
119    
120            /**
121             * Sets a string of ordered comma delimited plugin IDs.
122             */
123            @Override
124            public void setRoles(String roles) {
125                    _rolesArray = StringUtil.split(roles);
126    
127                    super.setRoles(roles);
128            }
129    
130            /**
131             * Sets an array of required roles of the plugin.
132             */
133            @Override
134            public void setRolesArray(String[] rolesArray) {
135                    _rolesArray = rolesArray;
136    
137                    super.setRoles(StringUtil.merge(rolesArray));
138            }
139    
140            /**
141             * Log instance for this class.
142             */
143            private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
144    
145            /**
146             * An array of required roles of the plugin.
147             */
148            private String[] _rolesArray;
149    
150    }