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 @Override
047 public void addRole(String role) {
048 setRolesArray(ArrayUtil.append(_rolesArray, role));
049 }
050
051
056 @Override
057 public String[] getRolesArray() {
058 return _rolesArray;
059 }
060
061
067 @Override
068 public boolean hasPermission(long userId) {
069 try {
070 if (_rolesArray.length == 0) {
071 return true;
072 }
073
074 if (RoleLocalServiceUtil.hasUserRoles(
075 userId, getCompanyId(), _rolesArray, true)) {
076
077 return true;
078 }
079
080 if (RoleLocalServiceUtil.hasUserRole(
081 userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
082 true)) {
083
084 return true;
085 }
086
087 User user = UserLocalServiceUtil.getUserById(userId);
088
089 if (user.isDefaultUser() && hasRoleWithName(RoleConstants.GUEST)) {
090 return true;
091 }
092 }
093 catch (Exception e) {
094 _log.error(e);
095 }
096
097 return false;
098 }
099
100
108 @Override
109 public boolean hasRoleWithName(String roleName) {
110 for (int i = 0; i < _rolesArray.length; i++) {
111 if (StringUtil.equalsIgnoreCase(_rolesArray[i], roleName)) {
112 return true;
113 }
114 }
115
116 return false;
117 }
118
119
122 @Override
123 public void setRoles(String roles) {
124 _rolesArray = StringUtil.split(roles);
125
126 super.setRoles(roles);
127 }
128
129
132 @Override
133 public void setRolesArray(String[] rolesArray) {
134 _rolesArray = rolesArray;
135
136 super.setRoles(StringUtil.merge(rolesArray));
137 }
138
139
142 private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
143
144
147 private String[] _rolesArray;
148
149 }