| PluginSettingImpl.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 *
5 *
6 *
7 * The contents of this file are subject to the terms of the Liferay Enterprise
8 * Subscription License ("License"). You may not use this file except in
9 * compliance with the License. You can obtain a copy of the License by
10 * contacting Liferay, Inc. See the License for the specific language governing
11 * permissions and limitations under the License, including but not limited to
12 * distribution rights of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.model.impl;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.ArrayUtil;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.model.PluginSetting;
30 import com.liferay.portal.model.RoleConstants;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.RoleLocalServiceUtil;
33 import com.liferay.portal.service.UserLocalServiceUtil;
34
35 /**
36 * <a href="PluginSettingImpl.java.html"><b><i>View Source</i></b></a>
37 *
38 * @author Brian Wing Shun Chan
39 */
40 public class PluginSettingImpl
41 extends PluginSettingModelImpl implements PluginSetting {
42
43 public PluginSettingImpl() {
44 }
45
46 public PluginSettingImpl(PluginSetting pluginSetting) {
47 setCompanyId(pluginSetting.getCompanyId());
48 setPluginId(pluginSetting.getPluginId());
49 setPluginType(pluginSetting.getPluginType());
50 setRoles(pluginSetting.getRoles());
51 setActive(pluginSetting.getActive());
52 }
53
54 /**
55 * Adds a role to the list of roles.
56 */
57 public void addRole(String role) {
58 setRolesArray(ArrayUtil.append(_rolesArray, role));
59 }
60
61 /**
62 * Sets a string of ordered comma delimited plugin ids.
63 */
64 public void setRoles(String roles) {
65 _rolesArray = StringUtil.split(roles);
66
67 super.setRoles(roles);
68 }
69
70 /**
71 * Gets an array of required roles of the plugin.
72 *
73 * @return an array of required roles of the plugin
74 */
75 public String[] getRolesArray() {
76 return _rolesArray;
77 }
78
79 /**
80 * Sets an array of required roles of the plugin.
81 */
82 public void setRolesArray(String[] rolesArray) {
83 _rolesArray = rolesArray;
84
85 super.setRoles(StringUtil.merge(rolesArray));
86 }
87
88 /**
89 * Returns true if the plugin has a role with the specified name.
90 *
91 * @return true if the plugin has a role with the specified name
92 */
93 public boolean hasRoleWithName(String roleName) {
94 for (int i = 0; i < _rolesArray.length; i++) {
95 if (_rolesArray[i].equalsIgnoreCase(roleName)) {
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 /**
104 * Returns true if the user has permission to use this plugin
105 *
106 * @return true if the user has permission to use this plugin
107 */
108 public boolean hasPermission(long userId) {
109 try {
110 if (_rolesArray.length == 0) {
111 return true;
112 }
113 else if (RoleLocalServiceUtil.hasUserRoles(
114 userId, getCompanyId(), _rolesArray, true)) {
115
116 return true;
117 }
118 else if (RoleLocalServiceUtil.hasUserRole(
119 userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
120 true)) {
121
122 return true;
123 }
124 else {
125 User user = UserLocalServiceUtil.getUserById(userId);
126
127 if (user.isDefaultUser() &&
128 hasRoleWithName(RoleConstants.GUEST)) {
129
130 return true;
131 }
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 = LogFactoryUtil.getLog(PluginSettingImpl.class);
145
146 /**
147 * An array of required roles of the plugin.
148 */
149 private String[] _rolesArray;
150
151 }