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 extends PluginSettingBaseImpl {
031
032 public PluginSettingImpl() {
033 }
034
035 public PluginSettingImpl(PluginSetting pluginSetting) {
036 setCompanyId(pluginSetting.getCompanyId());
037 setPluginId(pluginSetting.getPluginId());
038 setPluginType(pluginSetting.getPluginType());
039 setRoles(pluginSetting.getRoles());
040 setActive(pluginSetting.getActive());
041 }
042
043
046 public void addRole(String role) {
047 setRolesArray(ArrayUtil.append(_rolesArray, role));
048 }
049
050
055 public String[] getRolesArray() {
056 return _rolesArray;
057 }
058
059
064 public boolean hasPermission(long userId) {
065 try {
066 if (_rolesArray.length == 0) {
067 return true;
068 }
069 else if (RoleLocalServiceUtil.hasUserRoles(
070 userId, getCompanyId(), _rolesArray, true)) {
071
072 return true;
073 }
074 else if (RoleLocalServiceUtil.hasUserRole(
075 userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
076 true)) {
077
078 return true;
079 }
080 else {
081 User user = UserLocalServiceUtil.getUserById(userId);
082
083 if (user.isDefaultUser() &&
084 hasRoleWithName(RoleConstants.GUEST)) {
085
086 return true;
087 }
088 }
089 }
090 catch (Exception e) {
091 _log.error(e);
092 }
093
094 return false;
095 }
096
097
104 public boolean hasRoleWithName(String roleName) {
105 for (int i = 0; i < _rolesArray.length; i++) {
106 if (_rolesArray[i].equalsIgnoreCase(roleName)) {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114
117 @Override
118 public void setRoles(String roles) {
119 _rolesArray = StringUtil.split(roles);
120
121 super.setRoles(roles);
122 }
123
124
127 public void setRolesArray(String[] rolesArray) {
128 _rolesArray = rolesArray;
129
130 super.setRoles(StringUtil.merge(rolesArray));
131 }
132
133
136 private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
137
138
141 private String[] _rolesArray;
142
143 }