protected void updateAction_1to5(
      Role role,
      long groupId,
      String selResource,
      String actionId,
      boolean selected,
      int scope,
      String[] groupIds)
      throws Exception {

    long roleId = role.getRoleId();

    if (selected) {
      if (scope == ResourceConstants.SCOPE_COMPANY) {
        PermissionServiceUtil.setRolePermission(
            roleId, groupId, selResource, scope, String.valueOf(role.getCompanyId()), actionId);
      } else if (scope == ResourceConstants.SCOPE_GROUP_TEMPLATE) {
        PermissionServiceUtil.setRolePermission(
            roleId,
            groupId,
            selResource,
            ResourceConstants.SCOPE_GROUP_TEMPLATE,
            String.valueOf(GroupConstants.DEFAULT_PARENT_GROUP_ID),
            actionId);
      } else {
        PermissionServiceUtil.unsetRolePermissions(
            roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP, actionId);

        for (String curGroupId : groupIds) {
          PermissionServiceUtil.setRolePermission(
              roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP, curGroupId, actionId);
        }
      }
    } else {

      // Remove company, group template, and group permissions

      PermissionServiceUtil.unsetRolePermissions(
          roleId, groupId, selResource, ResourceConstants.SCOPE_COMPANY, actionId);

      PermissionServiceUtil.unsetRolePermissions(
          roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP_TEMPLATE, actionId);

      PermissionServiceUtil.unsetRolePermissions(
          roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP, actionId);
    }
  }
  @Override
  public ActionForward render(
      ActionMapping actionMapping,
      ActionForm actionForm,
      PortletConfig portletConfig,
      RenderRequest renderRequest,
      RenderResponse renderResponse)
      throws Exception {

    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long groupId =
        ParamUtil.getLong(renderRequest, "resourceGroupId", themeDisplay.getScopeGroupId());

    String portletResource = ParamUtil.getString(renderRequest, "portletResource");
    String modelResource = ParamUtil.getString(renderRequest, "modelResource");
    String resourcePrimKey = ParamUtil.getString(renderRequest, "resourcePrimKey");

    String selResource = portletResource;

    if (Validator.isNotNull(modelResource)) {
      selResource = modelResource;
    }

    try {
      PermissionServiceUtil.checkPermission(groupId, selResource, resourcePrimKey);
    } catch (PrincipalException pe) {
      SessionErrors.add(renderRequest, PrincipalException.class.getName());

      setForward(renderRequest, "portlet.portlet_configuration.error");
    }

    Portlet portlet =
        PortletLocalServiceUtil.getPortletById(themeDisplay.getCompanyId(), portletResource);

    if (portlet != null) {
      renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
    }

    return actionMapping.findForward(
        getForward(renderRequest, "portlet.portlet_configuration.edit_permissions"));
  }
  protected void deletePermission(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long roleId = ParamUtil.getLong(actionRequest, "roleId");
    long permissionId = ParamUtil.getLong(actionRequest, "permissionId");
    String name = ParamUtil.getString(actionRequest, "name");
    int scope = ParamUtil.getInteger(actionRequest, "scope");
    String primKey = ParamUtil.getString(actionRequest, "primKey");
    String actionId = ParamUtil.getString(actionRequest, "actionId");

    Role role = RoleLocalServiceUtil.getRole(roleId);

    String roleName = role.getName();

    if (roleName.equals(RoleConstants.ADMINISTRATOR)
        || roleName.equals(RoleConstants.ORGANIZATION_ADMINISTRATOR)
        || roleName.equals(RoleConstants.ORGANIZATION_OWNER)
        || roleName.equals(RoleConstants.OWNER)
        || roleName.equals(RoleConstants.SITE_ADMINISTRATOR)
        || roleName.equals(RoleConstants.SITE_OWNER)) {

      throw new RolePermissionsException(roleName);
    }

    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
      if (ResourceBlockLocalServiceUtil.isSupported(name)) {
        if (scope == ResourceConstants.SCOPE_GROUP) {
          ResourceBlockServiceUtil.removeGroupScopePermission(
              themeDisplay.getScopeGroupId(),
              themeDisplay.getCompanyId(),
              GetterUtil.getLong(primKey),
              name,
              roleId,
              actionId);
        } else {
          ResourceBlockServiceUtil.removeCompanyScopePermission(
              themeDisplay.getScopeGroupId(), themeDisplay.getCompanyId(), name, roleId, actionId);
        }
      } else {
        ResourcePermissionServiceUtil.removeResourcePermission(
            themeDisplay.getScopeGroupId(),
            themeDisplay.getCompanyId(),
            name,
            scope,
            primKey,
            roleId,
            actionId);
      }
    } else {
      PermissionServiceUtil.unsetRolePermission(
          roleId, themeDisplay.getScopeGroupId(), permissionId);
    }

    // Send redirect

    SessionMessages.add(actionRequest, "permissionDeleted");

    String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

    if (Validator.isNotNull(redirect)) {
      actionResponse.sendRedirect(redirect);
    }
  }