1   /**
2    * Copyright (c) 2000-2007 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.service.impl;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.DuplicateRoleException;
27  import com.liferay.portal.NoSuchRoleException;
28  import com.liferay.portal.PortalException;
29  import com.liferay.portal.RequiredRoleException;
30  import com.liferay.portal.RoleNameException;
31  import com.liferay.portal.SystemException;
32  import com.liferay.portal.kernel.security.permission.ActionKeys;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Group;
36  import com.liferay.portal.model.Role;
37  import com.liferay.portal.model.impl.GroupImpl;
38  import com.liferay.portal.model.impl.ResourceImpl;
39  import com.liferay.portal.model.impl.RoleImpl;
40  import com.liferay.portal.security.permission.PermissionCacheUtil;
41  import com.liferay.portal.security.permission.ResourceActionsUtil;
42  import com.liferay.portal.service.PermissionLocalServiceUtil;
43  import com.liferay.portal.service.ResourceLocalServiceUtil;
44  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
45  import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
46  import com.liferay.portal.service.persistence.GroupUtil;
47  import com.liferay.portal.service.persistence.RoleFinder;
48  import com.liferay.portal.service.persistence.RoleUtil;
49  import com.liferay.portal.service.persistence.UserUtil;
50  import com.liferay.portal.util.PortalUtil;
51  
52  import java.util.List;
53  import java.util.Map;
54  
55  /**
56   * <a href="RoleLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
62  
63      public Role addRole(long userId, long companyId, String name, int type)
64          throws PortalException, SystemException {
65  
66          return addRole(userId, companyId, name, type, null, 0);
67      }
68  
69      public Role addRole(
70              long userId, long companyId, String name, int type,
71              String className, long classPK)
72          throws PortalException, SystemException {
73  
74          // Role
75  
76          long classNameId = PortalUtil.getClassNameId(className);
77  
78          validate(0, companyId, name);
79  
80          long roleId = CounterLocalServiceUtil.increment();
81  
82          Role role = RoleUtil.create(roleId);
83  
84          role.setCompanyId(companyId);
85          role.setClassNameId(classNameId);
86          role.setClassPK(classPK);
87          role.setName(name);
88          role.setType(type);
89  
90          RoleUtil.update(role);
91  
92          // Resources
93  
94          if (userId > 0) {
95              ResourceLocalServiceUtil.addResources(
96                  companyId, 0, userId, Role.class.getName(), role.getRoleId(),
97                  false, false, false);
98          }
99  
100         return role;
101     }
102 
103     public void checkSystemRoles(long companyId)
104         throws PortalException, SystemException {
105 
106         // Regular roles
107 
108         String[] systemRoles = PortalUtil.getSystemRoles();
109 
110         for (int i = 0; i < systemRoles.length; i++) {
111             try {
112                 RoleFinder.findByC_N(companyId, systemRoles[i]);
113             }
114             catch (NoSuchRoleException nsre) {
115                 addRole(0, companyId, systemRoles[i], RoleImpl.TYPE_REGULAR);
116             }
117         }
118 
119         // Community roles
120 
121         String[] systemCommunityRoles = PortalUtil.getSystemCommunityRoles();
122 
123         for (int i = 0; i < systemCommunityRoles.length; i++) {
124             try {
125                 RoleFinder.findByC_N(companyId, systemCommunityRoles[i]);
126             }
127             catch (NoSuchRoleException nsre) {
128                 Role role = addRole(
129                     0, companyId, systemCommunityRoles[i],
130                     RoleImpl.TYPE_COMMUNITY);
131 
132                 if (systemCommunityRoles[i].equals(RoleImpl.COMMUNITY_OWNER)) {
133                     List actions = ResourceActionsUtil.getModelResourceActions(
134                         Group.class.getName());
135 
136                     PermissionLocalServiceUtil.setRolePermissions(
137                         role.getRoleId(), role.getCompanyId(),
138                         Group.class.getName(),
139                         ResourceImpl.SCOPE_GROUP_TEMPLATE,
140                         String.valueOf(GroupImpl.DEFAULT_PARENT_GROUP_ID),
141                         (String[])actions.toArray(new String[0]));
142                 }
143                 else if (systemCommunityRoles[i].equals(
144                             RoleImpl.COMMUNITY_ADMINISTRATOR)) {
145 
146                     String[] actionIds = new String[] {
147                         ActionKeys.ASSIGN_USERS, ActionKeys.MANAGE_LAYOUTS,
148                         ActionKeys.UPDATE
149                     };
150 
151                     PermissionLocalServiceUtil.setRolePermissions(
152                         role.getRoleId(), role.getCompanyId(),
153                         Group.class.getName(),
154                         ResourceImpl.SCOPE_GROUP_TEMPLATE,
155                         String.valueOf(GroupImpl.DEFAULT_PARENT_GROUP_ID),
156                         actionIds);
157                 }
158             }
159         }
160     }
161 
162     public void deleteRole(long roleId)
163         throws PortalException, SystemException {
164 
165         Role role = RoleUtil.findByPrimaryKey(roleId);
166 
167         if (PortalUtil.isSystemRole(role.getName())) {
168             throw new RequiredRoleException();
169         }
170 
171         // Resources
172 
173         if ((role.getClassNameId() <= 0) && (role.getClassPK() <= 0)) {
174             ResourceLocalServiceUtil.deleteResource(
175                 role.getCompanyId(), Role.class.getName(),
176                 ResourceImpl.SCOPE_INDIVIDUAL, role.getRoleId());
177         }
178 
179         if (role.getType() == RoleImpl.TYPE_COMMUNITY) {
180             UserGroupRoleLocalServiceUtil.deleteUserGroupRolesByRoleId(
181                 role.getRoleId());
182         }
183 
184         // Role
185 
186         RoleUtil.remove(roleId);
187 
188         // Permission cache
189 
190         PermissionCacheUtil.clearCache();
191     }
192 
193     public Role getGroupRole(long companyId, long groupId)
194         throws PortalException, SystemException {
195 
196         long classNameId = PortalUtil.getClassNameId(Group.class);
197 
198         return RoleUtil.findByC_C_C(companyId, classNameId, groupId);
199     }
200 
201     public List getGroupRoles(long groupId)
202         throws PortalException, SystemException {
203 
204         return GroupUtil.getRoles(groupId);
205     }
206 
207     public Map getResourceRoles(
208             long companyId, String name, int scope, String primKey)
209         throws SystemException {
210 
211         return RoleFinder.findByC_N_S_P(companyId, name, scope, primKey);
212     }
213 
214     public Role getRole(long roleId) throws PortalException, SystemException {
215         return RoleUtil.findByPrimaryKey(roleId);
216     }
217 
218     public Role getRole(long companyId, String name)
219         throws PortalException, SystemException {
220 
221         return RoleFinder.findByC_N(companyId, name);
222     }
223 
224     public List getUserGroupRoles(long userId, long groupId)
225         throws SystemException {
226 
227         return RoleFinder.findByUserGroupRole(userId, groupId);
228     }
229 
230     public List getUserRelatedRoles(long userId, long groupId)
231         throws SystemException {
232 
233         return RoleFinder.findByU_G(userId, groupId);
234     }
235 
236     public List getUserRelatedRoles(long userId, long[] groupIds)
237         throws SystemException {
238 
239         return RoleFinder.findByU_G(userId, groupIds);
240     }
241 
242     public List getUserRelatedRoles(long userId, List groups)
243         throws SystemException {
244 
245         return RoleFinder.findByU_G(userId, groups);
246     }
247 
248     public List getUserRoles(long userId)
249         throws PortalException, SystemException {
250 
251         return UserUtil.getRoles(userId);
252     }
253 
254     /**
255      * Returns true if the user has the role.
256      *
257      * @param       userId the user id of the user
258      * @param       companyId the company id of the company
259      * @param       name the name of the role
260      * @param       inherited boolean value for whether to check roles inherited
261      *              from the community, organization, location, or user group
262      * @return      true if the user has the role
263      */
264     public boolean hasUserRole(
265             long userId, long companyId, String name, boolean inherited)
266         throws PortalException, SystemException {
267 
268         Role role = RoleFinder.findByC_N(companyId, name);
269 
270         if (inherited) {
271             if (RoleFinder.countByR_U(role.getRoleId(), userId) > 0) {
272                 return true;
273             }
274             else {
275                 return false;
276             }
277         }
278         else {
279             return UserUtil.containsRole(userId, role.getRoleId());
280         }
281     }
282 
283     /**
284      * Returns true if the user has any one of the specified roles.
285      *
286      * @param       userId the user id of the user
287      * @param       companyId the company id of the company
288      * @param       names an array of role names
289      * @param       inherited boolean value for whether to check roles inherited
290      *              from the community, organization, location, or user group
291      * @return      true if the user has the role
292      */
293     public boolean hasUserRoles(
294             long userId, long companyId, String[] names, boolean inherited)
295         throws PortalException, SystemException {
296 
297         for (int i = 0; i < names.length; i++) {
298             if (hasUserRole(userId, companyId, names[i], inherited)) {
299                 return true;
300             }
301         }
302 
303         return false;
304     }
305 
306     public List search(
307             long companyId, String name, String description, Integer type,
308             int begin, int end)
309         throws SystemException {
310 
311         return RoleFinder.findByC_N_D_T(
312             companyId, name, description, type, begin, end);
313     }
314 
315     public int searchCount(
316             long companyId, String name, String description, Integer type)
317         throws SystemException {
318 
319         return RoleFinder.countByC_N_D_T(companyId, name, description, type);
320     }
321 
322     public void setUserRoles(long userId, long[] roleIds)
323         throws PortalException, SystemException {
324 
325         UserUtil.setRoles(userId, roleIds);
326 
327         PermissionCacheUtil.clearCache();
328     }
329 
330     public Role updateRole(long roleId, String name)
331         throws PortalException, SystemException {
332 
333         Role role = RoleUtil.findByPrimaryKey(roleId);
334 
335         validate(roleId, role.getCompanyId(), name);
336 
337         if (PortalUtil.isSystemRole(role.getName())) {
338             throw new RequiredRoleException();
339         }
340 
341         role.setName(name);
342 
343         RoleUtil.update(role);
344 
345         return role;
346     }
347 
348     protected void validate(long roleId, long companyId, String name)
349         throws PortalException, SystemException {
350 
351         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
352             (name.indexOf(StringPool.COMMA) != -1) ||
353             (name.indexOf(StringPool.STAR) != -1)) {
354 
355             throw new RoleNameException();
356         }
357 
358         try {
359             Role role = RoleFinder.findByC_N(companyId, name);
360 
361             if (role.getRoleId() != roleId) {
362                 throw new DuplicateRoleException();
363             }
364         }
365         catch (NoSuchRoleException nsge) {
366         }
367     }
368 
369 }