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
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
57 public void addRole(String role) {
58 setRolesArray(ArrayUtil.append(_rolesArray, role));
59 }
60
61
64 public void setRoles(String roles) {
65 _rolesArray = StringUtil.split(roles);
66
67 super.setRoles(roles);
68 }
69
70
75 public String[] getRolesArray() {
76 return _rolesArray;
77 }
78
79
82 public void setRolesArray(String[] rolesArray) {
83 _rolesArray = rolesArray;
84
85 super.setRoles(StringUtil.merge(rolesArray));
86 }
87
88
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
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
144 private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
145
146
149 private String[] _rolesArray;
150
151 }