001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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
031            extends PluginSettingModelImpl implements PluginSetting {
032    
033            public PluginSettingImpl() {
034            }
035    
036            public PluginSettingImpl(PluginSetting pluginSetting) {
037                    setCompanyId(pluginSetting.getCompanyId());
038                    setPluginId(pluginSetting.getPluginId());
039                    setPluginType(pluginSetting.getPluginType());
040                    setRoles(pluginSetting.getRoles());
041                    setActive(pluginSetting.getActive());
042            }
043    
044            /**
045             * Adds a role to the list of roles.
046             */
047            public void addRole(String role) {
048                    setRolesArray(ArrayUtil.append(_rolesArray, role));
049            }
050    
051            /**
052             * Sets a string of ordered comma delimited plugin ids.
053             */
054            public void setRoles(String roles) {
055                    _rolesArray = StringUtil.split(roles);
056    
057                    super.setRoles(roles);
058            }
059    
060            /**
061             * Gets an array of required roles of the plugin.
062             *
063             * @return an array of required roles of the plugin
064             */
065            public String[] getRolesArray() {
066                    return _rolesArray;
067            }
068    
069            /**
070             * Sets an array of required roles of the plugin.
071             */
072            public void setRolesArray(String[] rolesArray) {
073                    _rolesArray = rolesArray;
074    
075                    super.setRoles(StringUtil.merge(rolesArray));
076            }
077    
078            /**
079             * Returns <code>true</code> if the plugin has a role with the specified
080             * name.
081             *
082             * @return <code>true</code> if the plugin has a role with the specified
083             *                 name
084             */
085            public boolean hasRoleWithName(String roleName) {
086                    for (int i = 0; i < _rolesArray.length; i++) {
087                            if (_rolesArray[i].equalsIgnoreCase(roleName)) {
088                                    return true;
089                            }
090                    }
091    
092                    return false;
093            }
094    
095            /**
096             * Returns <code>true</code> if the user has permission to use this plugin
097             *
098             * @return <code>true</code> if the user has permission to use this plugin
099             */
100            public boolean hasPermission(long userId) {
101                    try {
102                            if (_rolesArray.length == 0) {
103                                    return true;
104                            }
105                            else if (RoleLocalServiceUtil.hasUserRoles(
106                                                    userId, getCompanyId(), _rolesArray, true)) {
107    
108                                    return true;
109                            }
110                            else if (RoleLocalServiceUtil.hasUserRole(
111                                                    userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
112                                                    true)) {
113    
114                                    return true;
115                            }
116                            else {
117                                    User user = UserLocalServiceUtil.getUserById(userId);
118    
119                                    if (user.isDefaultUser() &&
120                                            hasRoleWithName(RoleConstants.GUEST)) {
121    
122                                            return true;
123                                    }
124                            }
125                    }
126                    catch (Exception e) {
127                            _log.error(e);
128                    }
129    
130                    return false;
131            }
132    
133            /**
134             * Log instance for this class.
135             */
136            private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
137    
138            /**
139             * An array of required roles of the plugin.
140             */
141            private String[] _rolesArray;
142    
143    }