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