Reputation: 1658
My webserver prevents access to the system if the user is not logged in, but some users may have more than one profile and the system only allows users to log in choosing one of them, i.e., a user may be a regular user with some permissions and this same user may logout and login again with admin permissions and have access to everything.
When this situation occurs I redirect the user to an action that forces the user to choose his/her profile, but the server no longer knows the action that the user tried to access. I'm able to retrieve this information through ActionInvocation like below:
public String initFeature() {
final ActionInvocation invocation = ServletActionContext.getActionContext(BaseAction.getRequest())
.getActionInvocation();
final Object action = invocation.getAction();
if (!this.isUserFullyLogged() && !(action instanceof ProfileSelectionAction)) {
final Boolean isPendingProfileSelection = (Boolean) this
.retrieveSessionAttribute(Constants.PENDING_PROFILE_SELECTION);
if (isPendingProfileSelection != null && isPendingProfileSelection.booleanValue()) {
this.saveSessionAttribute("action", action);
this.saveSessionAttribute("namespace", invocation.getProxy().getNamespace());
return Constants.PROFILE_SELECTION;
}
return Action.LOGIN;
}
return Action.SUCCESS;
}
When the system reaches to the profile selection action I have the action and namespace that the user have tried to access and was forbid but I don't know what I have to do to redirect the system to this namespace and action because I only have done it using action result of the struts.xml file so far.
Upvotes: 1
Views: 1473
Reputation: 24396
You can use parameters in result
configuration in struts.xml
file. Create two properties inside your action class that will hold the name of the action and namespace and use them in struts.xml
like that:
<action name="..." class="...">
<result type="redirectAction">
<param name="actionName">${action}</param>
<param name="namespace">${namespace}</param>
</result>
</action>
Additionally if don't want to create them and you already store your variables in session, you can retrieve them from session as well using #session
expression.
<action name="..." class="...">
<result type="redirectAction">
<param name="actionName">${#session.action}</param>
<param name="namespace">${#session.namespace}</param>
</result>
</action>
Upvotes: 1