1
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
41 public class PluginSettingImpl
42 extends PluginSettingModelImpl implements PluginSetting {
43
44 public PluginSettingImpl() {
45 }
46
47 public PluginSettingImpl(PluginSetting pluginSetting) {
48 setCompanyId(pluginSetting.getCompanyId());
49 setPluginId(pluginSetting.getPluginId());
50 setPluginType(pluginSetting.getPluginType());
51 setRoles(pluginSetting.getRoles());
52 setActive(pluginSetting.getActive());
53 }
54
55
60 public void addRole(String role) {
61 setRolesArray(ArrayUtil.append(_rolesArray, role));
62 }
63
64
69 public void setRoles(String roles) {
70 _rolesArray = StringUtil.split(roles);
71
72 super.setRoles(roles);
73 }
74
75
80 public String[] getRolesArray() {
81 return _rolesArray;
82 }
83
84
89 public void setRolesArray(String[] rolesArray) {
90 _rolesArray = rolesArray;
91
92 super.setRoles(StringUtil.merge(rolesArray));
93 }
94
95
100 public boolean hasRoleWithName(String roleName) {
101 for (int i = 0; i < _rolesArray.length; i++) {
102 if (_rolesArray[i].equalsIgnoreCase(roleName)) {
103 return true;
104 }
105 }
106
107 return false;
108 }
109
110
116 public boolean hasPermission(long userId) {
117 try {
118 if (_rolesArray.length == 0) {
119 return true;
120 }
121 else if (RoleLocalServiceUtil.hasUserRoles(
122 userId, getCompanyId(), _rolesArray, true)) {
123
124 return true;
125 }
126 else if (RoleLocalServiceUtil.hasUserRole(
127 userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
128 true)) {
129
130 return true;
131 }
132 else {
133 User user = UserLocalServiceUtil.getUserById(userId);
134
135 if (user.isDefaultUser() &&
136 hasRoleWithName(RoleConstants.GUEST)) {
137
138 return true;
139 }
140 }
141 }
142 catch (Exception e) {
143 _log.error(e);
144 }
145
146 return false;
147 }
148
149
152 private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
153
154
157 private String[] _rolesArray;
158
159 }