1
14
15 package com.liferay.portal.model.impl;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.util.ArrayUtil;
20 import com.liferay.portal.kernel.util.StringUtil;
21 import com.liferay.portal.model.PluginSetting;
22 import com.liferay.portal.model.RoleConstants;
23 import com.liferay.portal.model.User;
24 import com.liferay.portal.service.RoleLocalServiceUtil;
25 import com.liferay.portal.service.UserLocalServiceUtil;
26
27
32 public class PluginSettingImpl
33 extends PluginSettingModelImpl implements PluginSetting {
34
35 public PluginSettingImpl() {
36 }
37
38 public PluginSettingImpl(PluginSetting pluginSetting) {
39 setCompanyId(pluginSetting.getCompanyId());
40 setPluginId(pluginSetting.getPluginId());
41 setPluginType(pluginSetting.getPluginType());
42 setRoles(pluginSetting.getRoles());
43 setActive(pluginSetting.getActive());
44 }
45
46
49 public void addRole(String role) {
50 setRolesArray(ArrayUtil.append(_rolesArray, role));
51 }
52
53
56 public void setRoles(String roles) {
57 _rolesArray = StringUtil.split(roles);
58
59 super.setRoles(roles);
60 }
61
62
67 public String[] getRolesArray() {
68 return _rolesArray;
69 }
70
71
74 public void setRolesArray(String[] rolesArray) {
75 _rolesArray = rolesArray;
76
77 super.setRoles(StringUtil.merge(rolesArray));
78 }
79
80
85 public boolean hasRoleWithName(String roleName) {
86 for (int i = 0; i < _rolesArray.length; i++) {
87 if (_rolesArray[i].equalsIgnoreCase(roleName)) {
88 return true;
89 }
90 }
91
92 return false;
93 }
94
95
100 public boolean hasPermission(long userId) {
101 try {
102 if (_rolesArray.length == 0) {
103 return true;
104 }
105 else if (RoleLocalServiceUtil.hasUserRoles(
106 userId, getCompanyId(), _rolesArray, true)) {
107
108 return true;
109 }
110 else if (RoleLocalServiceUtil.hasUserRole(
111 userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
112 true)) {
113
114 return true;
115 }
116 else {
117 User user = UserLocalServiceUtil.getUserById(userId);
118
119 if (user.isDefaultUser() &&
120 hasRoleWithName(RoleConstants.GUEST)) {
121
122 return true;
123 }
124 }
125 }
126 catch (Exception e) {
127 _log.error(e);
128 }
129
130 return false;
131 }
132
133
136 private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
137
138
141 private String[] _rolesArray;
142
143 }