protected Map<String, Layout> getLayoutsMap() throws Exception {
    Map<String, Layout> layouts = new LinkedHashMap<>(4);

    User user = TestPropsValues.getUser();

    layouts.put(_PRIVATE_LAYOUT, LayoutTestUtil.addLayout(group, true));
    layouts.put(_PRIVATE_USER_LAYOUT, LayoutTestUtil.addLayout(user.getGroupId(), true));
    layouts.put(_PUBLIC_LAYOUT, LayoutTestUtil.addLayout(group, false));
    layouts.put(_PUBLIC_USER_LAYOUT, LayoutTestUtil.addLayout(user.getGroupId(), false));

    return layouts;
  }
  @Override
  public String getURLViewInContext(
      LiferayPortletRequest liferayPortletRequest,
      LiferayPortletResponse liferayPortletResponse,
      String noSuchEntryRedirect) {

    try {
      ThemeDisplay themeDisplay =
          (ThemeDisplay) liferayPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);

      User user = themeDisplay.getUser();

      long portletPlid =
          PortalUtil.getPlidFromPortletId(user.getGroupId(), true, PortletKeys.TASKS);

      PortletURL portletURL =
          PortletURLFactoryUtil.create(
              liferayPortletRequest, PortletKeys.TASKS, portletPlid, PortletRequest.RENDER_PHASE);

      portletURL.setParameter("mvcPath", "/tasks/view.jsp");

      return portletURL.toString();
    } catch (Exception e) {
    }

    return null;
  }
  @Test
  public void testUnassignUserFromRequiredGroups() throws Exception {
    long[] userIds = addUsers();
    long[] standardGroupIds = addStandardGroups();
    long[] requiredGroupIds = addRequiredGroups();

    User user = UserLocalServiceUtil.getUser(userIds[0]);

    List<Group> groups = user.getGroups();

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

    long[] userGroupIds =
        ArrayUtil.append(standardGroupIds, requiredGroupIds, new long[] {user.getGroupId()});

    MembershipPolicyTestUtil.updateUser(
        user, null, null, userGroupIds, null, Collections.<UserGroupRole>emptyList());

    groups = user.getGroups();

    Assert.assertEquals(userGroupIds.length, groups.size());

    MembershipPolicyTestUtil.updateUser(
        user, null, null, requiredGroupIds, null, Collections.<UserGroupRole>emptyList());

    groups = user.getGroups();

    Assert.assertEquals(requiredGroupIds.length, groups.size());
  }
  protected void importLayouts(
      long userId,
      Map<String, String[]> parameterMap,
      File privateLayoutsFile,
      File publicLayoutsFile)
      throws PortalException {

    User user = userPersistence.findByPrimaryKey(userId);

    long groupId = user.getGroupId();

    if (privateLayoutsFile != null) {
      Map<String, Serializable> importLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildImportLayoutSettingsMap(
              user, groupId, true, null, parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT,
              importLayoutSettingsMap);

      exportImportLocalService.importLayouts(exportImportConfiguration, privateLayoutsFile);
    }

    if (publicLayoutsFile != null) {
      Map<String, Serializable> importLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildImportLayoutSettingsMap(
              user, groupId, false, null, parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT,
              importLayoutSettingsMap);

      exportImportLocalService.importLayouts(exportImportConfiguration, publicLayoutsFile);
    }
  }
  @Override
  public String getURLViewInContext(
      LiferayPortletRequest liferayPortletRequest,
      LiferayPortletResponse liferayPortletResponse,
      String noSuchEntryRedirect) {

    try {
      ThemeDisplay themeDisplay =
          (ThemeDisplay) liferayPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);

      User user = themeDisplay.getUser();

      long portletPlid =
          PortalUtil.getPlidFromPortletId(user.getGroupId(), true, "1_WAR_microblogsportlet");

      PortletURL portletURL =
          PortletURLFactoryUtil.create(
              liferayPortletRequest,
              "1_WAR_microblogsportlet",
              portletPlid,
              PortletRequest.RENDER_PHASE);

      portletURL.setParameter("mvcPath", "/microblogs/view.jsp");

      long microblogsEntryId = _entry.getMicroblogsEntryId();

      if (_entry.getReceiverMicroblogsEntryId() > 0) {
        microblogsEntryId = _entry.getReceiverMicroblogsEntryId();
      }

      portletURL.setParameter("receiverMicroblogsEntryId", String.valueOf(microblogsEntryId));

      return portletURL.toString();
    } catch (Exception e) {
    }

    return null;
  }
  protected boolean isViewableGroup(
      PermissionChecker permissionChecker,
      Layout layout,
      String controlPanelCategory,
      boolean checkResourcePermission)
      throws PortalException, SystemException {

    Group group = GroupLocalServiceUtil.getGroup(layout.getGroupId());

    // Inactive sites are not viewable

    if (!group.isActive()) {
      return false;
    } else if (group.isStagingGroup()) {
      Group liveGroup = group.getLiveGroup();

      if (!liveGroup.isActive()) {
        return false;
      }
    }

    // User private layouts are only viewable by the user and anyone who can
    // update the user. The user must also be active.

    if (group.isUser()) {
      long groupUserId = group.getClassPK();

      if (groupUserId == permissionChecker.getUserId()) {
        return true;
      }

      User groupUser = UserLocalServiceUtil.getUserById(groupUserId);

      if (!groupUser.isActive()) {
        return false;
      }

      if (layout.isPrivateLayout()) {
        if (GroupPermissionUtil.contains(
                permissionChecker, groupUser.getGroupId(), ActionKeys.MANAGE_LAYOUTS)
            || UserPermissionUtil.contains(
                permissionChecker,
                groupUserId,
                groupUser.getOrganizationIds(),
                ActionKeys.UPDATE)) {

          return true;
        }

        return false;
      }
    }

    // If the current group is staging, only users with editorial rights
    // can access it

    if (group.isStagingGroup()) {
      if (GroupPermissionUtil.contains(
          permissionChecker, group.getGroupId(), ActionKeys.VIEW_STAGING)) {

        return true;
      }

      return false;
    }

    // Control panel layouts are only viewable by authenticated users

    if (group.isControlPanel()) {
      if (!permissionChecker.isSignedIn()) {
        return false;
      }

      if (PortalPermissionUtil.contains(permissionChecker, ActionKeys.VIEW_CONTROL_PANEL)) {

        return true;
      }

      if (Validator.isNotNull(controlPanelCategory)) {
        return true;
      }

      return false;
    }

    // Site layouts are only viewable by users who are members of the site
    // or by users who can update the site

    if (group.isSite()) {
      if (GroupPermissionUtil.contains(
              permissionChecker, group.getGroupId(), ActionKeys.MANAGE_LAYOUTS)
          || GroupPermissionUtil.contains(
              permissionChecker, group.getGroupId(), ActionKeys.UPDATE)) {

        return true;
      }

      if (layout.isPrivateLayout() && !permissionChecker.isGroupMember(group.getGroupId())) {

        return false;
      }
    }

    // Organization site layouts are also viewable by users who belong to
    // the organization or by users who can update organization

    if (group.isCompany()) {
      return false;
    } else if (group.isLayoutPrototype()) {
      if (LayoutPrototypePermissionUtil.contains(
          permissionChecker, group.getClassPK(), ActionKeys.VIEW)) {

        return true;
      }

      return false;
    } else if (group.isLayoutSetPrototype()) {
      if (LayoutSetPrototypePermissionUtil.contains(
          permissionChecker, group.getClassPK(), ActionKeys.VIEW)) {

        return true;
      }

      return false;
    } else if (group.isOrganization()) {
      long organizationId = group.getOrganizationId();

      if (OrganizationLocalServiceUtil.hasUserOrganization(
          permissionChecker.getUserId(), organizationId, false, false)) {

        return true;
      } else if (OrganizationPermissionUtil.contains(
          permissionChecker, organizationId, ActionKeys.UPDATE)) {

        return true;
      }

      if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
        List<Organization> userOrgs =
            OrganizationLocalServiceUtil.getUserOrganizations(permissionChecker.getUserId());

        for (Organization organization : userOrgs) {
          for (Organization ancestorOrganization : organization.getAncestors()) {

            if (organizationId == ancestorOrganization.getOrganizationId()) {

              return true;
            }
          }
        }
      }
    } else if (group.isUserGroup()) {
      if (UserGroupPermissionUtil.contains(
          permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {

        return true;
      }
    }

    // Only check the actual Layout if all of the above failed

    if (containsWithoutViewableGroup(
        permissionChecker, layout, controlPanelCategory, ActionKeys.VIEW)) {

      return true;
    }

    // As a last resort, check if any top level pages are viewable by the
    // user

    List<Layout> layouts =
        LayoutLocalServiceUtil.getLayouts(
            layout.getGroupId(),
            layout.isPrivateLayout(),
            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);

    for (Layout curLayout : layouts) {
      if (!curLayout.isHidden()
          && containsWithoutViewableGroup(
              permissionChecker, curLayout, controlPanelCategory, ActionKeys.VIEW)) {

        return true;
      }
    }

    return false;
  }