protected void convertResourcePermissions(String name, String tableName, String pkColumnName) throws Exception { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = DataAccess.getConnection(); ps = con.prepareStatement("select " + pkColumnName + ", companyId from " + tableName); rs = ps.executeQuery(); while (rs.next()) { long primKey = rs.getLong(pkColumnName); long companyId = rs.getLong("companyId"); ResourceBlock resourceBlock = convertResourcePermissions(companyId, name, primKey); if (_log.isInfoEnabled() && ((resourceBlock.getResourceBlockId() % 100) == 0)) { _log.info("Processed 100 resource blocks for " + name); } } } finally { DataAccess.cleanUp(con, ps, rs); } List<ResourcePermission> resourcePermissions = ResourcePermissionLocalServiceUtil.getScopeResourcePermissions(_SCOPES); for (ResourcePermission resourcePermission : resourcePermissions) { int scope = resourcePermission.getScope(); if (!name.equals(resourcePermission.getName())) { continue; } if ((scope == ResourceConstants.SCOPE_COMPANY) || (scope == ResourceConstants.SCOPE_GROUP_TEMPLATE)) { ResourceBlockLocalServiceUtil.setCompanyScopePermissions( resourcePermission.getCompanyId(), name, resourcePermission.getRoleId(), resourcePermission.getActionIds()); } else if (scope == ResourceConstants.SCOPE_GROUP) { ResourceBlockLocalServiceUtil.setGroupScopePermissions( resourcePermission.getCompanyId(), GetterUtil.getLong(resourcePermission.getPrimaryKey()), name, resourcePermission.getRoleId(), resourcePermission.getActionIds()); } } }
@Test public void testUpdateExisting() throws Exception { long pk = ServiceTestUtil.nextLong(); ResourcePermission newResourcePermission = _persistence.create(pk); newResourcePermission.setCompanyId(ServiceTestUtil.nextLong()); newResourcePermission.setName(ServiceTestUtil.randomString()); newResourcePermission.setScope(ServiceTestUtil.nextInt()); newResourcePermission.setPrimKey(ServiceTestUtil.randomString()); newResourcePermission.setRoleId(ServiceTestUtil.nextLong()); newResourcePermission.setOwnerId(ServiceTestUtil.nextLong()); newResourcePermission.setActionIds(ServiceTestUtil.nextLong()); _persistence.update(newResourcePermission, false); ResourcePermission existingResourcePermission = _persistence.findByPrimaryKey(newResourcePermission.getPrimaryKey()); Assert.assertEquals( existingResourcePermission.getResourcePermissionId(), newResourcePermission.getResourcePermissionId()); Assert.assertEquals( existingResourcePermission.getCompanyId(), newResourcePermission.getCompanyId()); Assert.assertEquals(existingResourcePermission.getName(), newResourcePermission.getName()); Assert.assertEquals(existingResourcePermission.getScope(), newResourcePermission.getScope()); Assert.assertEquals( existingResourcePermission.getPrimKey(), newResourcePermission.getPrimKey()); Assert.assertEquals(existingResourcePermission.getRoleId(), newResourcePermission.getRoleId()); Assert.assertEquals( existingResourcePermission.getOwnerId(), newResourcePermission.getOwnerId()); Assert.assertEquals( existingResourcePermission.getActionIds(), newResourcePermission.getActionIds()); }
protected ResourceBlockPermissionsContainer getResourceBlockPermissionsContainer( long companyId, long groupId, String name, long primKey) throws SystemException { ResourceBlockPermissionsContainer resourceBlockPermissionContainer = new ResourceBlockPermissionsContainer(); List<ResourcePermission> resourcePermissions = ResourcePermissionLocalServiceUtil.getResourceResourcePermissions( companyId, groupId, name, String.valueOf(primKey)); for (ResourcePermission resourcePermission : resourcePermissions) { resourceBlockPermissionContainer.addPermission( resourcePermission.getRoleId(), resourcePermission.getActionIds()); } return resourceBlockPermissionContainer; }
protected void importCalendarBookingResourcePermission( ResourcePermission resourcePermission, long calendarBookingId) throws PortalException, SystemException { CalendarBooking calendarBooking = calendarBookingPersistence.findByPrimaryKey(calendarBookingId); long actionIds = getActionIds(resourcePermission, CalEvent.class.getName(), CalendarBooking.class.getName()); resourceBlockLocalService.updateIndividualScopePermissions( calendarBooking.getCompanyId(), calendarBooking.getGroupId(), CalendarBooking.class.getName(), calendarBooking, resourcePermission.getRoleId(), actionIds, ResourceBlockConstants.OPERATOR_SET); }
protected void fixOrganizationRolePermissions() throws Exception { DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ResourcePermission.class); dynamicQuery.add(RestrictionsFactoryUtil.eq("name", Organization.class.getName())); List<ResourcePermission> resourcePermissions = ResourcePermissionLocalServiceUtil.dynamicQuery(dynamicQuery); for (ResourcePermission resourcePermission : resourcePermissions) { ResourcePermission groupResourcePermission = null; try { groupResourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission( resourcePermission.getCompanyId(), Group.class.getName(), resourcePermission.getScope(), resourcePermission.getPrimKey(), resourcePermission.getRoleId()); } catch (Exception e) { ResourcePermissionLocalServiceUtil.setResourcePermissions( resourcePermission.getCompanyId(), Group.class.getName(), resourcePermission.getScope(), resourcePermission.getPrimKey(), resourcePermission.getRoleId(), ResourcePermissionLocalServiceImpl.EMPTY_ACTION_IDS); groupResourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission( resourcePermission.getCompanyId(), Group.class.getName(), resourcePermission.getScope(), resourcePermission.getPrimKey(), resourcePermission.getRoleId()); } long organizationActions = resourcePermission.getActionIds(); long groupActions = groupResourcePermission.getActionIds(); for (Object[] actionIdToMask : _ORGANIZATION_ACTION_IDS_TO_MASKS) { long organizationActionMask = (Long) actionIdToMask[1]; long groupActionMask = (Long) actionIdToMask[2]; if ((organizationActions & organizationActionMask) == organizationActionMask) { organizationActions = organizationActions & (~organizationActionMask); groupActions = groupActions | groupActionMask; } } try { resourcePermission.resetOriginalValues(); resourcePermission.setActionIds(organizationActions); ResourcePermissionLocalServiceUtil.updateResourcePermission(resourcePermission); groupResourcePermission.resetOriginalValues(); groupResourcePermission.setActionIds(groupActions); ResourcePermissionLocalServiceUtil.updateResourcePermission(groupResourcePermission); } catch (Exception e) { _log.error(e, e); } } PermissionCacheUtil.clearCache(); }