001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.model.impl;
022:
023: import com.liferay.portal.kernel.util.ArrayUtil;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.model.PluginSetting;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.service.RoleLocalServiceUtil;
028: import com.liferay.portal.service.UserLocalServiceUtil;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * <a href="PluginSettingImpl.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class PluginSettingImpl extends PluginSettingModelImpl implements
040: PluginSetting {
041:
042: public PluginSettingImpl() {
043: }
044:
045: public PluginSettingImpl(PluginSetting pluginSetting) {
046: setCompanyId(pluginSetting.getCompanyId());
047: setPluginId(pluginSetting.getPluginId());
048: setPluginType(pluginSetting.getPluginType());
049: setRoles(pluginSetting.getRoles());
050: setActive(pluginSetting.getActive());
051: }
052:
053: /**
054: * Adds a role to the list of roles.
055: *
056: * @param role a role name
057: */
058: public void addRole(String role) {
059: setRolesArray(ArrayUtil.append(_rolesArray, role));
060: }
061:
062: /**
063: * Sets a string of ordered comma delimited plugin ids.
064: *
065: * @param roles a string of ordered comma delimited plugin ids
066: */
067: public void setRoles(String roles) {
068: _rolesArray = StringUtil.split(roles);
069:
070: super .setRoles(roles);
071: }
072:
073: /**
074: * Gets an array of required roles of the plugin.
075: *
076: * @return an array of required roles of the plugin
077: */
078: public String[] getRolesArray() {
079: return _rolesArray;
080: }
081:
082: /**
083: * Sets an array of required roles of the plugin.
084: *
085: * @param rolesArray an array of required roles of the plugin
086: */
087: public void setRolesArray(String[] rolesArray) {
088: _rolesArray = rolesArray;
089:
090: super .setRoles(StringUtil.merge(rolesArray));
091: }
092:
093: /**
094: * Returns true if the plugin has a role with the specified name.
095: *
096: * @return true if the plugin has a role with the specified name
097: */
098: public boolean hasRoleWithName(String roleName) {
099: for (int i = 0; i < _rolesArray.length; i++) {
100: if (_rolesArray[i].equalsIgnoreCase(roleName)) {
101: return true;
102: }
103: }
104:
105: return false;
106: }
107:
108: /**
109: * Returns true if the user has permission to use this plugin
110: *
111: * @param userId the id of the user
112: * @return true if the user has permission to use this plugin
113: */
114: public boolean hasPermission(long userId) {
115: try {
116: if (_rolesArray.length == 0) {
117: return true;
118: } else if (RoleLocalServiceUtil.hasUserRoles(userId,
119: getCompanyId(), _rolesArray, true)) {
120:
121: return true;
122: } else if (RoleLocalServiceUtil.hasUserRole(userId,
123: getCompanyId(), RoleImpl.ADMINISTRATOR, true)) {
124:
125: return true;
126: } else {
127: User user = UserLocalServiceUtil.getUserById(userId);
128:
129: if (user.isDefaultUser()
130: && hasRoleWithName(RoleImpl.GUEST)) {
131: return true;
132: }
133: }
134: } catch (Exception e) {
135: _log.error(e);
136: }
137:
138: return false;
139: }
140:
141: /**
142: * Log instance for this class.
143: */
144: private static Log _log = LogFactory
145: .getLog(PluginSettingImpl.class);
146:
147: /**
148: * An array of required roles of the plugin.
149: */
150: private String[] _rolesArray;
151:
152: }
|