@Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    ResourcePermission newResourcePermission = addResourcePermission();

    ResourcePermission existingResourcePermission =
        _persistence.fetchByPrimaryKey(newResourcePermission.getPrimaryKey());

    Assert.assertEquals(existingResourcePermission, newResourcePermission);
  }
示例#2
0
  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 testCreate() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    ResourcePermission resourcePermission = _persistence.create(pk);

    Assert.assertNotNull(resourcePermission);

    Assert.assertEquals(resourcePermission.getPrimaryKey(), pk);
  }
  @Test
  public void testRemove() throws Exception {
    ResourcePermission newResourcePermission = addResourcePermission();

    _persistence.remove(newResourcePermission);

    ResourcePermission existingResourcePermission =
        _persistence.fetchByPrimaryKey(newResourcePermission.getPrimaryKey());

    Assert.assertNull(existingResourcePermission);
  }
  protected void updateResourcePermissions(
      long companyId, String name, int scope, String primKey, String newPrimKey)
      throws SystemException {

    List<ResourcePermission> resourcePermissions =
        resourcePermissionLocalService.getResourcePermissions(companyId, name, scope, primKey);

    for (ResourcePermission resourcePermission : resourcePermissions) {
      resourcePermission.setPrimKey(newPrimKey);

      resourcePermissionPersistence.update(resourcePermission);
    }
  }
  /**
   * Adds the resource permission to the database. Also notifies the appropriate model listeners.
   *
   * @param resourcePermission the resource permission
   * @return the resource permission that was added
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public ResourcePermission addResourcePermission(ResourcePermission resourcePermission) {
    resourcePermission.setNew(true);

    return resourcePermissionPersistence.update(resourcePermission);
  }
示例#7
0
  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;
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static ResourcePermission toModel(ResourcePermissionSoap soapModel) {
    ResourcePermission model = new ResourcePermissionImpl();

    model.setResourcePermissionId(soapModel.getResourcePermissionId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setName(soapModel.getName());
    model.setScope(soapModel.getScope());
    model.setPrimKey(soapModel.getPrimKey());
    model.setRoleId(soapModel.getRoleId());
    model.setOwnerId(soapModel.getOwnerId());
    model.setActionIds(soapModel.getActionIds());

    return model;
  }
  protected void deletePermissions_6(long companyId) throws Exception {
    Group group = GroupLocalServiceUtil.getGroup(companyId, GroupConstants.CONTROL_PANEL);

    long plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId(), true);

    Role role = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);

    List<ResourcePermission> resourcePermissions =
        ResourcePermissionLocalServiceUtil.getRoleResourcePermissions(role.getRoleId());

    for (ResourcePermission resourcePermission : resourcePermissions) {
      if (isControlPanelLayout(plid, resourcePermission.getPrimKey())
          || isPrivateLayout(resourcePermission.getName(), resourcePermission.getPrimKey())) {

        ResourcePermissionLocalServiceUtil.deleteResourcePermission(
            resourcePermission.getResourcePermissionId());
      }
    }
  }
  public int compareTo(ResourcePermission resourcePermission) {
    long primaryKey = resourcePermission.getPrimaryKey();

    if (getPrimaryKey() < primaryKey) {
      return -1;
    } else if (getPrimaryKey() > primaryKey) {
      return 1;
    } else {
      return 0;
    }
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }

    if (!(obj instanceof ResourcePermission)) {
      return false;
    }

    ResourcePermission resourcePermission = (ResourcePermission) obj;

    long primaryKey = resourcePermission.getPrimaryKey();

    if (getPrimaryKey() == primaryKey) {
      return true;
    } else {
      return false;
    }
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    ResourcePermission newResourcePermission = addResourcePermission();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            ResourcePermission.class, ResourcePermission.class.getClassLoader());

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq(
            "resourcePermissionId", newResourcePermission.getResourcePermissionId()));

    List<ResourcePermission> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    ResourcePermission existingResourcePermission = result.get(0);

    Assert.assertEquals(existingResourcePermission, newResourcePermission);
  }
  protected void deleteRolePermissions(PortletDataContext portletDataContext, Role importedRole)
      throws SystemException {

    List<ResourcePermission> resourcePermissions =
        ResourcePermissionLocalServiceUtil.getRoleResourcePermissions(
            importedRole.getRoleId(),
            new int[] {ResourceConstants.SCOPE_COMPANY, ResourceConstants.SCOPE_GROUP_TEMPLATE},
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS);

    for (ResourcePermission resourcePermission : resourcePermissions) {
      ResourcePermissionLocalServiceUtil.deleteResourcePermission(resourcePermission);
    }

    List<ResourcePermission> groupResourcePermissions =
        ResourcePermissionLocalServiceUtil.getRoleResourcePermissions(
            importedRole.getRoleId(),
            new int[] {ResourceConstants.SCOPE_GROUP},
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS);

    for (ResourcePermission groupResourcePermission : groupResourcePermissions) {

      long groupId = GetterUtil.getLong(groupResourcePermission.getPrimKey());

      if ((groupId == portletDataContext.getCompanyGroupId())
          || (groupId == portletDataContext.getUserPersonalSiteGroupId())) {

        ResourcePermissionLocalServiceUtil.deleteResourcePermission(groupResourcePermission);
      }
    }

    List<ResourceTypePermission> resourceTypePermissions =
        getResourceTypePermissions(portletDataContext, importedRole);

    for (ResourceTypePermission resourceTypePermission : resourceTypePermissions) {

      ResourceTypePermissionLocalServiceUtil.deleteResourceTypePermission(resourceTypePermission);
    }
  }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }

    ResourcePermission resourcePermission = null;

    try {
      resourcePermission = (ResourcePermission) obj;
    } catch (ClassCastException cce) {
      return false;
    }

    long primaryKey = resourcePermission.getPrimaryKey();

    if (getPrimaryKey() == primaryKey) {
      return true;
    } else {
      return false;
    }
  }
  @Test
  public void testResetOriginalValues() throws Exception {
    if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
      return;
    }

    ResourcePermission newResourcePermission = addResourcePermission();

    _persistence.clearCache();

    ResourcePermissionModelImpl existingResourcePermissionModelImpl =
        (ResourcePermissionModelImpl)
            _persistence.findByPrimaryKey(newResourcePermission.getPrimaryKey());

    Assert.assertEquals(
        existingResourcePermissionModelImpl.getCompanyId(),
        existingResourcePermissionModelImpl.getOriginalCompanyId());
    Assert.assertTrue(
        Validator.equals(
            existingResourcePermissionModelImpl.getName(),
            existingResourcePermissionModelImpl.getOriginalName()));
    Assert.assertEquals(
        existingResourcePermissionModelImpl.getScope(),
        existingResourcePermissionModelImpl.getOriginalScope());
    Assert.assertTrue(
        Validator.equals(
            existingResourcePermissionModelImpl.getPrimKey(),
            existingResourcePermissionModelImpl.getOriginalPrimKey()));
    Assert.assertEquals(
        existingResourcePermissionModelImpl.getRoleId(),
        existingResourcePermissionModelImpl.getOriginalRoleId());
    Assert.assertEquals(
        existingResourcePermissionModelImpl.getOwnerId(),
        existingResourcePermissionModelImpl.getOriginalOwnerId());
    Assert.assertEquals(
        existingResourcePermissionModelImpl.getActionIds(),
        existingResourcePermissionModelImpl.getOriginalActionIds());
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    ResourcePermission newResourcePermission = addResourcePermission();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            ResourcePermission.class, ResourcePermission.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("resourcePermissionId"));

    Object newResourcePermissionId = newResourcePermission.getResourcePermissionId();

    dynamicQuery.add(
        RestrictionsFactoryUtil.in("resourcePermissionId", new Object[] {newResourcePermissionId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingResourcePermissionId = result.get(0);

    Assert.assertEquals(existingResourcePermissionId, newResourcePermissionId);
  }
  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);
  }
  public static ResourcePermission addResourcePermission(
      long actionIds, String name, String primKey, long roleId, int scope) throws Exception {

    long resourcePermissionId =
        CounterLocalServiceUtil.increment(ResourcePermission.class.getName());

    ResourcePermission resourcePermission =
        ResourcePermissionLocalServiceUtil.createResourcePermission(resourcePermissionId);

    resourcePermission.setCompanyId(TestPropsValues.getCompanyId());
    resourcePermission.setName(name);
    resourcePermission.setScope(scope);
    resourcePermission.setPrimKey(primKey);
    resourcePermission.setRoleId(roleId);
    resourcePermission.setActionIds(actionIds);

    return ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
  }
  protected ResourcePermission addResourcePermission() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    ResourcePermission resourcePermission = _persistence.create(pk);

    resourcePermission.setCompanyId(ServiceTestUtil.nextLong());

    resourcePermission.setName(ServiceTestUtil.randomString());

    resourcePermission.setScope(ServiceTestUtil.nextInt());

    resourcePermission.setPrimKey(ServiceTestUtil.randomString());

    resourcePermission.setRoleId(ServiceTestUtil.nextLong());

    resourcePermission.setOwnerId(ServiceTestUtil.nextLong());

    resourcePermission.setActionIds(ServiceTestUtil.nextLong());

    _persistence.update(resourcePermission, false);

    return resourcePermission;
  }
  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();
  }
  @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());
  }
  public boolean containsWithoutViewableGroup(
      PermissionChecker permissionChecker,
      Layout layout,
      String controlPanelCategory,
      boolean checkLayoutUpdateable,
      String actionId)
      throws PortalException, SystemException {

    if (checkLayoutUpdateable
        && !actionId.equals(ActionKeys.CUSTOMIZE)
        && !actionId.equals(ActionKeys.VIEW)
        && (layout instanceof VirtualLayout)) {

      return false;
    }

    if (actionId.equals(ActionKeys.CUSTOMIZE) && (layout instanceof VirtualLayout)) {

      VirtualLayout virtualLayout = (VirtualLayout) layout;

      layout = virtualLayout.getWrappedModel();
    }

    if (actionId.equals(ActionKeys.DELETE) && !SitesUtil.isLayoutDeleteable(layout)) {

      return false;
    }

    Group group = layout.getGroup();

    if (checkLayoutUpdateable
        && !group.isLayoutSetPrototype()
        && isAttemptToModifyLockedLayout(layout, actionId)) {

      return false;
    }

    User user = UserLocalServiceUtil.getUserById(permissionChecker.getUserId());

    if ((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6)
        && !user.isDefaultUser()
        && !group.isUser()) {

      // This is new way of doing an ownership check without having to
      // have a userId field on the model. When the instance model was
      // first created, we set the user's userId as the ownerId of the
      // individual scope ResourcePermission of the Owner Role.
      // Therefore, ownership can be determined by obtaining the Owner
      // role ResourcePermission for the current instance model and
      // testing it with the hasOwnerPermission call.

      ResourcePermission resourcePermission =
          ResourcePermissionLocalServiceUtil.getResourcePermission(
              layout.getCompanyId(),
              Layout.class.getName(),
              ResourceConstants.SCOPE_INDIVIDUAL,
              String.valueOf(layout.getPlid()),
              permissionChecker.getOwnerRoleId());

      if (permissionChecker.hasOwnerPermission(
          layout.getCompanyId(),
          Layout.class.getName(),
          String.valueOf(layout.getPlid()),
          resourcePermission.getOwnerId(),
          actionId)) {

        return true;
      }
    }

    if (GroupPermissionUtil.contains(
        permissionChecker, layout.getGroupId(), ActionKeys.MANAGE_LAYOUTS)) {

      return true;
    } else if (actionId.equals(ActionKeys.ADD_LAYOUT)
        && GroupPermissionUtil.contains(
            permissionChecker, layout.getGroupId(), ActionKeys.ADD_LAYOUT)) {

      return true;
    }

    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE && !actionId.equals(ActionKeys.VIEW)) {

      // Check upward recursively to see if any pages above grant the
      // action

      long parentLayoutId = layout.getParentLayoutId();

      while (parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
        Layout parentLayout =
            LayoutLocalServiceUtil.getLayout(
                layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);

        if (contains(permissionChecker, parentLayout, controlPanelCategory, actionId)) {

          return true;
        }

        parentLayoutId = parentLayout.getParentLayoutId();
      }
    }

    try {
      if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
        if (ResourcePermissionLocalServiceUtil.getResourcePermissionsCount(
                layout.getCompanyId(),
                Layout.class.getName(),
                ResourceConstants.SCOPE_INDIVIDUAL,
                String.valueOf(layout.getPlid()))
            == 0) {

          throw new NoSuchResourceException();
        }
      } else {
        ResourceLocalServiceUtil.getResource(
            layout.getCompanyId(),
            Layout.class.getName(),
            ResourceConstants.SCOPE_INDIVIDUAL,
            String.valueOf(layout.getPlid()));
      }
    } catch (NoSuchResourceException nsre) {
      boolean addGroupPermission = true;
      boolean addGuestPermission = true;

      if (layout.isPrivateLayout()) {
        addGuestPermission = false;
      }

      ResourceLocalServiceUtil.addResources(
          layout.getCompanyId(),
          layout.getGroupId(),
          0,
          Layout.class.getName(),
          layout.getPlid(),
          false,
          addGroupPermission,
          addGuestPermission);
    }

    return permissionChecker.hasPermission(
        layout.getGroupId(), Layout.class.getName(), layout.getPlid(), actionId);
  }