001    /**
002     * Copyright (c) 2000-2012 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            public void addRole(String role) {
047                    setRolesArray(ArrayUtil.append(_rolesArray, role));
048            }
049    
050            /**
051             * Returns an array of required roles of the plugin.
052             *
053             * @return an array of required roles of the plugin
054             */
055            public String[] getRolesArray() {
056                    return _rolesArray;
057            }
058    
059            /**
060             * Returns <code>true</code> if the user has permission to use this plugin
061             *
062             * @param  userId the primary key of the user
063             * @return <code>true</code> if the user has permission to use this plugin
064             */
065            public boolean hasPermission(long userId) {
066                    try {
067                            if (_rolesArray.length == 0) {
068                                    return true;
069                            }
070                            else if (RoleLocalServiceUtil.hasUserRoles(
071                                                    userId, getCompanyId(), _rolesArray, true)) {
072    
073                                    return true;
074                            }
075                            else if (RoleLocalServiceUtil.hasUserRole(
076                                                    userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
077                                                    true)) {
078    
079                                    return true;
080                            }
081                            else {
082                                    User user = UserLocalServiceUtil.getUserById(userId);
083    
084                                    if (user.isDefaultUser() &&
085                                            hasRoleWithName(RoleConstants.GUEST)) {
086    
087                                            return true;
088                                    }
089                            }
090                    }
091                    catch (Exception e) {
092                            _log.error(e);
093                    }
094    
095                    return false;
096            }
097    
098            /**
099             * Returns <code>true</code> if the plugin has a role with the specified
100             * name.
101             *
102             * @param  roleName the role name
103             * @return <code>true</code> if the plugin has a role with the specified
104             *         name
105             */
106            public boolean hasRoleWithName(String roleName) {
107                    for (int i = 0; i < _rolesArray.length; i++) {
108                            if (_rolesArray[i].equalsIgnoreCase(roleName)) {
109                                    return true;
110                            }
111                    }
112    
113                    return false;
114            }
115    
116            /**
117             * Sets a string of ordered comma delimited plugin IDs.
118             */
119            @Override
120            public void setRoles(String roles) {
121                    _rolesArray = StringUtil.split(roles);
122    
123                    super.setRoles(roles);
124            }
125    
126            /**
127             * Sets an array of required roles of the plugin.
128             */
129            public void setRolesArray(String[] rolesArray) {
130                    _rolesArray = rolesArray;
131    
132                    super.setRoles(StringUtil.merge(rolesArray));
133            }
134    
135            /**
136             * Log instance for this class.
137             */
138            private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
139    
140            /**
141             * An array of required roles of the plugin.
142             */
143            private String[] _rolesArray;
144    
145    }