-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add direct rule link from public site - MEED-2849 - Meeds-io/MI…
…Ps#100 (#1300) This change will allow to access rules from links shared from aggregated site. This will allow to access actions from public site only when the public site is accessible and the USER registration is made as open. In fact, even if public site is published, when the User Registration is restricted, the Actions page will be hidden and thus not publically accessible.
- Loading branch information
Showing
9 changed files
with
290 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
portlets/src/main/webapp/WEB-INF/conf/gamification/filter-configuration.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
This file is part of the Meeds project (https://meeds.io/). | ||
Copyright (C) 2023 Meeds Association [email protected] | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 3 of the License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program; if not, write to the Free Software Foundation, | ||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
--> | ||
<configuration | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd http://www.exoplatform.org/xml/ns/kernel_1_2.xsd" | ||
xmlns="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd"> | ||
|
||
<external-component-plugins> | ||
<target-component>org.exoplatform.web.filter.ExtensibleFilter</target-component> | ||
<component-plugin> | ||
<name>AuthenticationHeaderFilter</name> | ||
<set-method>addFilterDefinitions</set-method> | ||
<type>org.exoplatform.web.filter.FilterDefinitionPlugin</type> | ||
<init-params> | ||
<object-param> | ||
<name>Oauth Filter Definition</name> | ||
<object type="org.exoplatform.web.filter.FilterDefinition"> | ||
<field name="filter"> | ||
<object type="io.meeds.gamification.web.filter.PublicActionAccessFilter" /> | ||
</field> | ||
<field name="patterns"> | ||
<collection type="java.util.ArrayList" item-type="java.lang.String"> | ||
<value> | ||
<string>.*/contributions/actions.*</string> | ||
</value> | ||
</collection> | ||
</field> | ||
</object> | ||
</object-param> | ||
</init-params> | ||
</component-plugin> | ||
</external-component-plugins> | ||
|
||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
services/src/main/java/io/meeds/gamification/web/filter/PublicActionAccessFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* This file is part of the Meeds project (https://meeds.io/). | ||
* | ||
* Copyright (C) 2020 - 2023 Meeds Association [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package io.meeds.gamification.web.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import org.exoplatform.container.ExoContainerContext; | ||
import org.exoplatform.portal.config.UserACL; | ||
import org.exoplatform.portal.config.model.PortalConfig; | ||
import org.exoplatform.portal.mop.SiteKey; | ||
import org.exoplatform.portal.mop.service.LayoutService; | ||
import org.exoplatform.web.filter.Filter; | ||
|
||
import io.meeds.portal.security.constant.UserRegistrationType; | ||
import io.meeds.portal.security.service.SecuritySettingService; | ||
|
||
public class PublicActionAccessFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, | ||
ServletResponse response, | ||
FilterChain filterChain) throws IOException, ServletException { | ||
HttpServletRequest req = (HttpServletRequest) request; | ||
HttpServletResponse res = (HttpServletResponse) response; | ||
String requestURI = req.getRequestURI(); | ||
|
||
SecuritySettingService securitySettingService = ExoContainerContext.getService(SecuritySettingService.class); | ||
if (req.getRemoteUser() == null | ||
&& securitySettingService.getRegistrationType() == UserRegistrationType.OPEN | ||
&& !StringUtils.contains(requestURI, "/portal/public")) { | ||
LayoutService layoutService = ExoContainerContext.getService(LayoutService.class); | ||
PortalConfig portalConfig = layoutService.getPortalConfig(SiteKey.portal("public")); | ||
if (portalConfig != null | ||
&& portalConfig.getAccessPermissions() != null | ||
&& StringUtils.equals(UserACL.EVERYONE, portalConfig.getAccessPermissions()[0])) { | ||
res.sendRedirect(requestURI.replaceFirst("/portal/(.*)/contributions/actions", | ||
"/portal/public/overview/actions")); | ||
return; | ||
} | ||
} | ||
filterChain.doFilter(req, res); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
services/src/test/java/io/meeds/gamification/web/filter/PublicActionAccessFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
|
||
/** | ||
* This file is part of the Meeds project (https://meeds.io/). | ||
* | ||
* Copyright (C) 2020 - 2023 Meeds Association [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package io.meeds.gamification.web.filter; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import org.exoplatform.container.ExoContainerContext; | ||
import org.exoplatform.portal.config.UserACL; | ||
import org.exoplatform.portal.config.model.PortalConfig; | ||
import org.exoplatform.portal.mop.SiteKey; | ||
import org.exoplatform.portal.mop.service.LayoutService; | ||
|
||
import io.meeds.portal.security.constant.UserRegistrationType; | ||
import io.meeds.portal.security.service.SecuritySettingService; | ||
|
||
@RunWith(MockitoJUnitRunner.Silent.class) | ||
public class PublicActionAccessFilterTest { | ||
|
||
private static MockedStatic<ExoContainerContext> containerContent; | ||
|
||
@BeforeClass | ||
public static void initClassContext() { | ||
containerContent = mockStatic(ExoContainerContext.class); | ||
} | ||
|
||
@AfterClass | ||
public static void endClassContext() { | ||
containerContent.close(); | ||
} | ||
|
||
@Mock | ||
LayoutService layoutService; | ||
|
||
@Mock | ||
SecuritySettingService securitySettingService; | ||
|
||
@Mock | ||
PortalConfig portalConfig; | ||
|
||
@Mock | ||
HttpServletRequest request; | ||
|
||
@Mock | ||
HttpServletResponse response; | ||
|
||
@Mock | ||
FilterChain filterChain; | ||
|
||
@Before | ||
public void setup() { | ||
containerContent.when(() -> ExoContainerContext.getService(LayoutService.class)).thenReturn(layoutService); | ||
containerContent.when(() -> ExoContainerContext.getService(SecuritySettingService.class)).thenReturn(securitySettingService); | ||
} | ||
|
||
@Test | ||
public void testDoFilterAnonymousUser() throws IOException, ServletException { | ||
when(request.getRequestURI()).thenReturn("/portal/meeds/overview/contributions/actions/1234"); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(1)).doFilter(request, response); | ||
|
||
when(securitySettingService.getRegistrationType()).thenReturn(UserRegistrationType.OPEN); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(layoutService, times(1)).getPortalConfig(any(SiteKey.class)); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(2)).doFilter(request, response); | ||
|
||
when(layoutService.getPortalConfig(SiteKey.portal("public"))).thenReturn(portalConfig); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(3)).doFilter(request, response); | ||
|
||
when(portalConfig.getAccessPermissions()).thenReturn(new String[] {"/platform/users"}); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(4)).doFilter(request, response); | ||
|
||
when(portalConfig.getAccessPermissions()).thenReturn(new String[] {UserACL.EVERYONE}); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(response, times(1)).sendRedirect("/portal/public/overview/actions/1234"); | ||
verify(filterChain, times(4)).doFilter(request, response); | ||
} | ||
|
||
@Test | ||
public void testDoFilterAuthenticatedUser() throws IOException, ServletException { | ||
when(request.getRemoteUser()).thenReturn("user"); | ||
when(request.getRequestURI()).thenReturn("/portal/meeds/overview/contributions/actions/1234"); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(1)).doFilter(request, response); | ||
|
||
when(securitySettingService.getRegistrationType()).thenReturn(UserRegistrationType.OPEN); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(layoutService, never()).getPortalConfig(any(SiteKey.class)); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(2)).doFilter(request, response); | ||
|
||
when(layoutService.getPortalConfig(SiteKey.portal("public"))).thenReturn(portalConfig); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(layoutService, never()).getPortalConfig(any(SiteKey.class)); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(3)).doFilter(request, response); | ||
|
||
when(portalConfig.getAccessPermissions()).thenReturn(new String[] {"/platform/users"}); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(layoutService, never()).getPortalConfig(any(SiteKey.class)); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(4)).doFilter(request, response); | ||
|
||
when(portalConfig.getAccessPermissions()).thenReturn(new String[] {UserACL.EVERYONE}); | ||
new PublicActionAccessFilter().doFilter(request, response, filterChain); | ||
verify(layoutService, never()).getPortalConfig(any(SiteKey.class)); | ||
verify(response, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(5)).doFilter(request, response); | ||
} | ||
|
||
} |