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