001
014
015 package com.liferay.portal.model.impl;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.model.PluginSetting;
022 import com.liferay.portal.model.RoleConstants;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.RoleLocalServiceUtil;
025 import com.liferay.portal.service.UserLocalServiceUtil;
026
027
030 public class PluginSettingImpl
031 extends PluginSettingModelImpl implements PluginSetting {
032
033 public PluginSettingImpl() {
034 }
035
036 public PluginSettingImpl(PluginSetting pluginSetting) {
037 setCompanyId(pluginSetting.getCompanyId());
038 setPluginId(pluginSetting.getPluginId());
039 setPluginType(pluginSetting.getPluginType());
040 setRoles(pluginSetting.getRoles());
041 setActive(pluginSetting.getActive());
042 }
043
044
047 public void addRole(String role) {
048 setRolesArray(ArrayUtil.append(_rolesArray, role));
049 }
050
051
054 public void setRoles(String roles) {
055 _rolesArray = StringUtil.split(roles);
056
057 super.setRoles(roles);
058 }
059
060
065 public String[] getRolesArray() {
066 return _rolesArray;
067 }
068
069
072 public void setRolesArray(String[] rolesArray) {
073 _rolesArray = rolesArray;
074
075 super.setRoles(StringUtil.merge(rolesArray));
076 }
077
078
085 public boolean hasRoleWithName(String roleName) {
086 for (int i = 0; i < _rolesArray.length; i++) {
087 if (_rolesArray[i].equalsIgnoreCase(roleName)) {
088 return true;
089 }
090 }
091
092 return false;
093 }
094
095
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 }