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