1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model.impl;
24  
25  import com.liferay.portal.kernel.util.ArrayUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.model.PluginSetting;
28  import com.liferay.portal.model.RoleConstants;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.RoleLocalServiceUtil;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * <a href="PluginSettingImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PluginSettingImpl
43      extends PluginSettingModelImpl implements PluginSetting {
44  
45      public PluginSettingImpl() {
46      }
47  
48      public PluginSettingImpl(PluginSetting pluginSetting) {
49          setCompanyId(pluginSetting.getCompanyId());
50          setPluginId(pluginSetting.getPluginId());
51          setPluginType(pluginSetting.getPluginType());
52          setRoles(pluginSetting.getRoles());
53          setActive(pluginSetting.getActive());
54      }
55  
56      /**
57       * Adds a role to the list of roles.
58       *
59       * @param       role a role name
60       */
61      public void addRole(String role) {
62          setRolesArray(ArrayUtil.append(_rolesArray, role));
63      }
64  
65      /**
66       * Sets a string of ordered comma delimited plugin ids.
67       *
68       * @param       roles a string of ordered comma delimited plugin ids
69       */
70      public void setRoles(String roles) {
71          _rolesArray = StringUtil.split(roles);
72  
73          super.setRoles(roles);
74      }
75  
76      /**
77       * Gets an array of required roles of the plugin.
78       *
79       * @return      an array of required roles of the plugin
80       */
81      public String[] getRolesArray() {
82          return _rolesArray;
83      }
84  
85      /**
86       * Sets an array of required roles of the plugin.
87       *
88       * @param       rolesArray an array of required roles of the plugin
89       */
90      public void setRolesArray(String[] rolesArray) {
91          _rolesArray = rolesArray;
92  
93          super.setRoles(StringUtil.merge(rolesArray));
94      }
95  
96      /**
97       * Returns true if the plugin has a role with the specified name.
98       *
99       * @return      true if the plugin has a role with the specified name
100      */
101     public boolean hasRoleWithName(String roleName) {
102         for (int i = 0; i < _rolesArray.length; i++) {
103             if (_rolesArray[i].equalsIgnoreCase(roleName)) {
104                 return true;
105             }
106         }
107 
108         return false;
109     }
110 
111     /**
112      * Returns true if the user has permission to use this plugin
113      *
114      * @param       userId the id of the user
115      * @return      true if the user has permission to use this plugin
116      */
117     public boolean hasPermission(long userId) {
118         try {
119             if (_rolesArray.length == 0) {
120                 return true;
121             }
122             else if (RoleLocalServiceUtil.hasUserRoles(
123                         userId, getCompanyId(), _rolesArray, true)) {
124 
125                 return true;
126             }
127             else if (RoleLocalServiceUtil.hasUserRole(
128                         userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
129                         true)) {
130 
131                 return true;
132             }
133             else {
134                 User user = UserLocalServiceUtil.getUserById(userId);
135 
136                 if (user.isDefaultUser() &&
137                     hasRoleWithName(RoleConstants.GUEST)) {
138 
139                     return true;
140                 }
141             }
142         }
143         catch (Exception e) {
144             _log.error(e);
145         }
146 
147         return false;
148     }
149 
150     /**
151      * Log instance for this class.
152      */
153     private static Log _log = LogFactory.getLog(PluginSettingImpl.class);
154 
155     /**
156      * An array of required roles of the plugin.
157      */
158     private String[] _rolesArray;
159 
160 }