1
19
20 package com.liferay.portal.model.impl;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.ArrayUtil;
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.model.PluginSetting;
27 import com.liferay.portal.model.RoleConstants;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.RoleLocalServiceUtil;
30 import com.liferay.portal.service.UserLocalServiceUtil;
31
32
38 public class PluginSettingImpl
39 extends PluginSettingModelImpl implements PluginSetting {
40
41 public PluginSettingImpl() {
42 }
43
44 public PluginSettingImpl(PluginSetting pluginSetting) {
45 setCompanyId(pluginSetting.getCompanyId());
46 setPluginId(pluginSetting.getPluginId());
47 setPluginType(pluginSetting.getPluginType());
48 setRoles(pluginSetting.getRoles());
49 setActive(pluginSetting.getActive());
50 }
51
52
57 public void addRole(String role) {
58 setRolesArray(ArrayUtil.append(_rolesArray, role));
59 }
60
61
66 public void setRoles(String roles) {
67 _rolesArray = StringUtil.split(roles);
68
69 super.setRoles(roles);
70 }
71
72
77 public String[] getRolesArray() {
78 return _rolesArray;
79 }
80
81
86 public void setRolesArray(String[] rolesArray) {
87 _rolesArray = rolesArray;
88
89 super.setRoles(StringUtil.merge(rolesArray));
90 }
91
92
97 public boolean hasRoleWithName(String roleName) {
98 for (int i = 0; i < _rolesArray.length; i++) {
99 if (_rolesArray[i].equalsIgnoreCase(roleName)) {
100 return true;
101 }
102 }
103
104 return false;
105 }
106
107
113 public boolean hasPermission(long userId) {
114 try {
115 if (_rolesArray.length == 0) {
116 return true;
117 }
118 else if (RoleLocalServiceUtil.hasUserRoles(
119 userId, getCompanyId(), _rolesArray, true)) {
120
121 return true;
122 }
123 else if (RoleLocalServiceUtil.hasUserRole(
124 userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
125 true)) {
126
127 return true;
128 }
129 else {
130 User user = UserLocalServiceUtil.getUserById(userId);
131
132 if (user.isDefaultUser() &&
133 hasRoleWithName(RoleConstants.GUEST)) {
134
135 return true;
136 }
137 }
138 }
139 catch (Exception e) {
140 _log.error(e);
141 }
142
143 return false;
144 }
145
146
149 private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
150
151
154 private String[] _rolesArray;
155
156 }