public void initPermissions(long companyId, List<Portlet> portlets) {
    Role powerUserRole = getPowerUserRole(companyId);

    if (powerUserRole == null) {
      return;
    }

    Group userPersonalSiteGroup = getUserPersonalSiteGroup(companyId);

    if (userPersonalSiteGroup == null) {
      return;
    }

    for (Portlet portlet : portlets) {
      try {
        initPermissions(
            companyId,
            powerUserRole.getRoleId(),
            portlet.getRootPortletId(),
            userPersonalSiteGroup.getGroupId());
      } catch (PortalException pe) {
        _log.error(
            "Unable to initialize user personal site permissions "
                + "for portlet "
                + portlet.getPortletId()
                + " in company "
                + companyId,
            pe);
      }
    }
  }
  @Override
  public String getURL(PortletRequest portletRequest, PortletResponse portletResponse) {

    String url = StringPool.BLANK;

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

    try {
      long roleId = _getRoleId(portletRequest);

      Role role = _roleService.fetchRole(roleId);

      int[] roleTypes = {role.getType()};

      if (role.getType() != RoleConstants.TYPE_REGULAR) {
        roleTypes = new int[] {RoleConstants.TYPE_REGULAR, role.getType()};
      }

      url =
          PermissionsURLTag.doTag(
              StringPool.BLANK,
              Role.class.getName(),
              themeDisplay.getScopeGroupName(),
              null,
              String.valueOf(_getRoleId(portletRequest)),
              LiferayWindowState.POP_UP.toString(),
              roleTypes,
              themeDisplay.getRequest());
    } catch (Exception e) {
    }

    return url;
  }
  public void initPermissions(List<Company> companies, Portlet portlet) {
    String rootPortletId = portlet.getRootPortletId();

    for (Company company : companies) {
      long companyId = company.getCompanyId();

      Role powerUserRole = getPowerUserRole(companyId);

      if (powerUserRole == null) {
        continue;
      }

      Group userPersonalSiteGroup = getUserPersonalSiteGroup(companyId);

      if (userPersonalSiteGroup == null) {
        continue;
      }

      try {
        initPermissions(
            companyId,
            powerUserRole.getRoleId(),
            rootPortletId,
            userPersonalSiteGroup.getGroupId());
      } catch (PortalException pe) {
        _log.error(
            "Unable to initialize user personal site permissions "
                + "for portlet "
                + portlet.getPortletId()
                + " in company "
                + companyId,
            pe);
      }
    }
  }
 @Override
 public void onBeforeRemove(Role role) throws ModelListenerException {
   try {
     AnnouncementsEntryLocalServiceUtil.deleteEntries(role.getClassNameId(), role.getRoleId());
   } catch (Exception e) {
     throw new ModelListenerException(e);
   }
 }
  @Override
  public void onAfterAddAssociation(
      Object classPK, String associationClassName, Object associationClassPK)
      throws ModelListenerException {

    try {
      User user = userLocalService.getUser((Long) classPK);

      if (associationClassName.equals(Group.class.getName())
          || associationClassName.equals(Organization.class.getName())
          || associationClassName.equals(UserGroup.class.getName())) {

        Role role =
            roleLocalService.fetchRole(user.getCompanyId(), RoleConstants.SOCIAL_OFFICE_USER);

        if (role == null) {
          return;
        }

        Group group = null;

        if (associationClassName.equals(Group.class.getName())) {
          group = groupLocalService.getGroup((Long) associationClassPK);
        } else if (associationClassName.equals(Organization.class.getName())) {

          group =
              groupLocalService.getOrganizationGroup(
                  user.getCompanyId(), (Long) associationClassPK);
        } else if (associationClassName.equals(UserGroup.class.getName())) {

          group =
              groupLocalService.getUserGroupGroup(user.getCompanyId(), (Long) associationClassPK);
        }

        if (groupLocalService.hasRoleGroup(role.getRoleId(), group.getGroupId())) {

          enableSocialOffice(user.getGroup());
        }
      } else if (associationClassName.equals(Role.class.getName())) {
        Role role = roleLocalService.getRole((Long) associationClassPK);

        String name = role.getName();

        if (name.equals(RoleConstants.SOCIAL_OFFICE_USER)) {
          enableSocialOffice(user.getGroup());
        }
      }
    } catch (NoSuchGroupException nsge) {

      // LPS-52675

      if (_log.isDebugEnabled()) {
        _log.debug(nsge, nsge);
      }
    } catch (Exception e) {
      throw new ModelListenerException(e);
    }
  }
  protected boolean isValidAssignment(KaleoTaskAssignment kaleoTaskAssignment, Group group)
      throws PortalException {

    long roleId = kaleoTaskAssignment.getAssigneeClassPK();

    Role role = _roleLocalService.getRole(roleId);

    if (role.getType() == RoleConstants.TYPE_REGULAR) {
      return true;
    } else if ((group != null)
        && group.isOrganization()
        && (role.getType() == RoleConstants.TYPE_ORGANIZATION)) {

      return true;
    } else if ((group != null) && group.isSite() && (role.getType() == RoleConstants.TYPE_SITE)) {

      return true;
    }

    return false;
  }
  @Override
  public boolean isShow(PortletRequest portletRequest) {
    try {
      ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

      long roleId = _getRoleId(portletRequest);

      Role role = _roleService.fetchRole(roleId);

      String roleName = role.getName();

      if (!roleName.equals(RoleConstants.OWNER)
          && RolePermissionUtil.contains(
              themeDisplay.getPermissionChecker(), roleId, ActionKeys.PERMISSIONS)) {

        return true;
      }

      return false;
    } catch (Exception e) {
    }

    return false;
  }
  @Override
  public void onAfterRemoveAssociation(
      Object classPK, String associationClassName, Object associationClassPK)
      throws ModelListenerException {

    try {
      User user = userLocalService.getUser((Long) classPK);

      FinderCacheUtil.clearCache(_MAPPING_TABLE_USERS_ROLES_NAME_LEFT_TO_RIGHT);
      FinderCacheUtil.clearCache(_MAPPING_TABLE_USERS_ROLES_NAME_RIGHT_TO_LEFT);

      ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);

      if (userLocalService.hasRoleUser(
          user.getCompanyId(), RoleConstants.SOCIAL_OFFICE_USER, user.getUserId(), true)) {

        return;
      }

      if (associationClassName.equals(Group.class.getName())
          || associationClassName.equals(Organization.class.getName())
          || associationClassName.equals(UserGroup.class.getName())) {

        Role role = roleLocalService.getRole(user.getCompanyId(), RoleConstants.SOCIAL_OFFICE_USER);

        Group group = null;

        if (associationClassName.equals(Group.class.getName())) {
          group = groupLocalService.getGroup((Long) associationClassPK);
        } else if (associationClassName.equals(Organization.class.getName())) {

          group =
              groupLocalService.getOrganizationGroup(
                  user.getCompanyId(), (Long) associationClassPK);
        } else if (associationClassName.equals(UserGroup.class.getName())) {

          group =
              groupLocalService.getUserGroupGroup(user.getCompanyId(), (Long) associationClassPK);
        }

        if (groupLocalService.hasRoleGroup(role.getRoleId(), group.getGroupId())) {

          disableSocialOffice(user.getGroup());
        }
      } else if (associationClassName.equals(Role.class.getName())) {
        Role role = roleLocalService.getRole((Long) associationClassPK);

        String name = role.getName();

        if (name.equals(RoleConstants.SOCIAL_OFFICE_USER)) {
          disableSocialOffice(user.getGroup());
        }
      }
    } catch (NoSuchGroupException nsge) {

      // LPS-52675

      if (_log.isDebugEnabled()) {
        _log.debug(nsge, nsge);
      }
    } catch (Exception e) {
      throw new ModelListenerException(e);
    }
  }