Skip to content

Commit

Permalink
MGR-113
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Jun 24, 2021
1 parent d97b4c3 commit 80f84b8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
5 changes: 4 additions & 1 deletion application-home/dictionary/manager-messages.properties
Expand Up @@ -135,7 +135,7 @@ lastLogin=Last login
lastUpdated=Last updated
latestRelease=Latest release
latestSnapshot=Latest snapshot
ldap.disabled=LDAP Authentication is disabled. Set site property ''{0}'' to ''true'' to enable.
ldap.disabled=LDAP Authentication is disabled. Set site property ''{0}'' to ''false'' to enable.
ldap.users=LDAP Users
ldap.settings=LDAP Settings
ldap.not.working=Unable to connect to LDAP server at {0}, please check configuration!
Expand Down Expand Up @@ -320,6 +320,9 @@ session.expireAll=Expire all
sessions=Sessions
sessions.expired.global=Expired {0} sessions
sessions.expired.site=Expired {0} sessions for site {1}
sessions.showing.max=Showing only first {0} of {1} sessions.
sessions.read.error=Error reading sessions. See log for details.
sessions.too.many=To many sessions to show: {0}, can only show {1}.
site.alias.deleted=Alias "{0}" has been deleted.
site.create.error=Failed to create the site.
site.create=Create site
Expand Down
Expand Up @@ -62,9 +62,9 @@ public DataContainer getData(Site site, Application application, Environment env
Boolean ldapDisabled = site.getProperties().getBoolean(LdapService.LDAP_DISABLED);
if ("settings".equals(mode)) {
List<Property> ldapProps = new ArrayList<>();
ldapProps.add(getProperty(siteProps, LdapService.LDAP_DISABLED));
SimpleProperty ldapHost = getProperty(siteProps, LdapService.LDAP_HOST);
ldapProps.add(ldapHost);
ldapProps.add(getProperty(siteProps, LdapService.LDAP_DISABLED));
ldapProps.add(getProperty(siteProps, LdapService.LDAP_DOMAIN));
ldapProps.add(getProperty(siteProps, LdapService.LDAP_GROUP_BASE_DN));
SimpleProperty ldapUser = getProperty(siteProps, LdapService.LDAP_USER);
Expand Down
45 changes: 35 additions & 10 deletions src/main/java/org/appng/application/manager/business/Sessions.java
Expand Up @@ -28,6 +28,7 @@
import javax.servlet.ServletContext;

import org.apache.catalina.Manager;
import org.apache.catalina.StoreManager;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -57,6 +58,7 @@
import org.appng.xml.platform.SelectionGroup;
import org.appng.xml.platform.SelectionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.Data;
Expand All @@ -76,6 +78,8 @@ public class Sessions extends ServiceAware implements ActionProvider<Void>, Data
private static final String F_LGN = "fLgn";
private static final String MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
private final FastDateFormat hourMinutes = FastDateFormat.getInstance(MM_DD_HH_MM);
@Value("${maxSessions:250}")
private int maxSessions;

@Autowired
private SelectionFactory selectionFactory;
Expand All @@ -87,7 +91,7 @@ public void perform(Site site, Application application, Environment environment,
Integer siteId = options.getInteger("site", "id");
String siteName = null == siteId ? null : getService().getNameForSite(siteId);
if (null == sessionId) {
List<Session> sessions = getSessionsFromManager(environment);
List<Session> sessions = getSessionsFromManager(request, fp);
int cnt = 0;
for (Session session : sessions) {
if (expire(currentSession, session.getId(), siteName, getManager(environment))) {
Expand All @@ -112,9 +116,9 @@ public void perform(Site site, Application application, Environment environment,
}

public DataContainer getData(Site site, Application application, Environment environment, Options options,
Request request, FieldProcessor fieldProcessor) {
DataContainer dataContainer = new DataContainer(fieldProcessor);
List<Session> immutableSessions = getSessionsFromManager(environment);
Request request, FieldProcessor fp) {
DataContainer dataContainer = new DataContainer(fp);
List<Session> immutableSessions = getSessionsFromManager(request, fp);

String fDmn = request.getParameter(F_DMN);
String fSess = request.getParameter(F_SESS);
Expand All @@ -129,17 +133,38 @@ public DataContainer getData(Site site, Application application, Environment env
Boolean currentSiteOnly = options.getBoolean("site", "currentSiteOnly");
List<Session> sessions = getSessions(options, request, currentSiteOnly, immutableSessions, userAgents, fDmn,
fSess, fAgnt, fUsr, getDate(fCrBf), getDate(fCrAf), fLgn);
if (getManager(environment).getActiveSessions() <= maxSessions) {
addFilters(request, currentSiteOnly, dataContainer, userAgents, fAgnt, fDmn, fUsr);
}

addFilters(request, currentSiteOnly, dataContainer, userAgents, fAgnt, fDmn, fUsr);

dataContainer.setPage(sessions, fieldProcessor.getPageable());
dataContainer.setPage(sessions, fp.getPageable());
return dataContainer;
}

protected List<Session> getSessionsFromManager(Environment env) {
protected List<Session> getSessionsFromManager(Request request, FieldProcessor fp) {
List<Session> immutableSessions = new ArrayList<>();
for (org.apache.catalina.Session session : getManager(env).findSessions()) {
immutableSessions.add(getSessionMetaData(session));
Manager manager = getManager(request.getEnvironment());
int activeSessions = manager.getActiveSessions();
if (activeSessions > maxSessions) {
if (manager instanceof StoreManager) {
try {
String[] sessionsKeys = StoreManager.class.cast(manager).getStore().keys();
for (int i = 0; i < maxSessions; i++) {
immutableSessions.add(getSessionMetaData(manager.findSession(sessionsKeys[i])));
}
fp.addNoticeMessage(request.getMessage(MessageConstants.SESSIONS_SHOWING_MAX, maxSessions,
sessionsKeys.length));
} catch (IOException e) {
log.error("error while retrieving sessions keys from store", e);
fp.addErrorMessage(request.getMessage(MessageConstants.SESSIONS_READ_ERROR));
}
} else {
fp.addErrorMessage(request.getMessage(MessageConstants.SESSIONS_TOO_MANY, activeSessions, maxSessions));
}
} else {
for (org.apache.catalina.Session session : manager.findSessions()) {
immutableSessions.add(getSessionMetaData(session));
}
}
return immutableSessions;
}
Expand Down
Expand Up @@ -19,11 +19,16 @@
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.catalina.session.PersistentManagerBase;
import org.apache.commons.lang3.time.DateUtils;
import org.appng.api.Scope;
import org.appng.api.support.CallableAction;
import org.appng.api.support.CallableDataSource;
import org.appng.api.support.environment.DefaultEnvironment;
import org.appng.core.controller.Session;
import org.appng.core.controller.SessionListener;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
Expand All @@ -39,6 +44,10 @@ public class SessionsTest extends AbstractTest {
public void testShowSessions() throws Exception {
setSessions();
environment.setAttribute(Scope.SESSION, org.appng.api.Session.Environment.SID, "47124712");
ServletContext servletCtx = ((DefaultEnvironment) environment).getServletContext();
PersistentManagerBase manager = Mockito.mock(PersistentManagerBase.class);
Mockito.when(manager.getActiveSessions()).thenReturn(250);
servletCtx.setAttribute(SessionListener.SESSION_MANAGER, manager);
CallableDataSource siteDatasource = getDataSource("sessions")
.withParam("deleteLink", "/system?action=expire&sessid=").getCallableDataSource();
siteDatasource.perform("test");
Expand Down
Expand Up @@ -19,7 +19,8 @@
import java.util.List;

import org.apache.catalina.Manager;
import org.appng.api.Environment;
import org.appng.api.FieldProcessor;
import org.appng.api.Request;
import org.appng.core.controller.Session;
import org.mockito.Mockito;

Expand All @@ -28,7 +29,7 @@ public class SessionsTestBean extends Sessions {
public static List<Session> SESSIONS;

@Override
protected List<Session> getSessionsFromManager(Environment env) {
protected List<Session> getSessionsFromManager(Request request, FieldProcessor fp) {
return SESSIONS;
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/xml/LdapUsersTest-testSettings.xml
Expand Up @@ -15,18 +15,18 @@
<resultset chunk="0" chunkname="ldapSettings" chunksize="8" nextchunk="0" previouschunk="0" firstchunk="0" lastchunk="0" hits="8">
<result>
<field name="name" type="text">
<value>ldapHost</value>
<value>ldapDisabled</value>
</field>
<field name="value" type="text">
<value>ldap://localhost:389</value>
<value>true</value>
</field>
</result>
<result>
<field name="name" type="text">
<value>ldapDisabled</value>
<value>ldapHost</value>
</field>
<field name="value" type="text">
<value>true</value>
<value>ldap://localhost:389</value>
</field>
</result>
<result>
Expand Down

0 comments on commit 80f84b8

Please sign in to comment.