1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ArrayUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.PluginSetting;
30  import com.liferay.portal.model.RoleConstants;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.service.RoleLocalServiceUtil;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  
35  /**
36   * <a href="PluginSettingImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PluginSettingImpl
42      extends PluginSettingModelImpl implements PluginSetting {
43  
44      public PluginSettingImpl() {
45      }
46  
47      public PluginSettingImpl(PluginSetting pluginSetting) {
48          setCompanyId(pluginSetting.getCompanyId());
49          setPluginId(pluginSetting.getPluginId());
50          setPluginType(pluginSetting.getPluginType());
51          setRoles(pluginSetting.getRoles());
52          setActive(pluginSetting.getActive());
53      }
54  
55      /**
56       * Adds a role to the list of roles.
57       *
58       * @param       role a role name
59       */
60      public void addRole(String role) {
61          setRolesArray(ArrayUtil.append(_rolesArray, role));
62      }
63  
64      /**
65       * Sets a string of ordered comma delimited plugin ids.
66       *
67       * @param       roles a string of ordered comma delimited plugin ids
68       */
69      public void setRoles(String roles) {
70          _rolesArray = StringUtil.split(roles);
71  
72          super.setRoles(roles);
73      }
74  
75      /**
76       * Gets an array of required roles of the plugin.
77       *
78       * @return      an array of required roles of the plugin
79       */
80      public String[] getRolesArray() {
81          return _rolesArray;
82      }
83  
84      /**
85       * Sets an array of required roles of the plugin.
86       *
87       * @param       rolesArray an array of required roles of the plugin
88       */
89      public void setRolesArray(String[] rolesArray) {
90          _rolesArray = rolesArray;
91  
92          super.setRoles(StringUtil.merge(rolesArray));
93      }
94  
95      /**
96       * Returns true if the plugin has a role with the specified name.
97       *
98       * @return      true if the plugin has a role with the specified name
99       */
100     public boolean hasRoleWithName(String roleName) {
101         for (int i = 0; i < _rolesArray.length; i++) {
102             if (_rolesArray[i].equalsIgnoreCase(roleName)) {
103                 return true;
104             }
105         }
106 
107         return false;
108     }
109 
110     /**
111      * Returns true if the user has permission to use this plugin
112      *
113      * @param       userId the id of the user
114      * @return      true if the user has permission to use this plugin
115      */
116     public boolean hasPermission(long userId) {
117         try {
118             if (_rolesArray.length == 0) {
119                 return true;
120             }
121             else if (RoleLocalServiceUtil.hasUserRoles(
122                         userId, getCompanyId(), _rolesArray, true)) {
123 
124                 return true;
125             }
126             else if (RoleLocalServiceUtil.hasUserRole(
127                         userId, getCompanyId(), RoleConstants.ADMINISTRATOR,
128                         true)) {
129 
130                 return true;
131             }
132             else {
133                 User user = UserLocalServiceUtil.getUserById(userId);
134 
135                 if (user.isDefaultUser() &&
136                     hasRoleWithName(RoleConstants.GUEST)) {
137 
138                     return true;
139                 }
140             }
141         }
142         catch (Exception e) {
143             _log.error(e);
144         }
145 
146         return false;
147     }
148 
149     /**
150      * Log instance for this class.
151      */
152     private static Log _log = LogFactoryUtil.getLog(PluginSettingImpl.class);
153 
154     /**
155      * An array of required roles of the plugin.
156      */
157     private String[] _rolesArray;
158 
159 }