Ported user banner FTL lib

Jens Pelzetter 2019-12-03 20:35:16 +01:00
parent aa585fb7c1
commit 3165ff4442
2 changed files with 177 additions and 1 deletions

View File

@ -18,14 +18,24 @@
*/ */
package org.libreccm.pagemodel; package org.libreccm.pagemodel;
import com.arsdigita.ui.UI;
import com.arsdigita.ui.login.LoginConstants;
import com.arsdigita.ui.login.LoginServlet;
import com.arsdigita.web.URL;
import org.apache.shiro.subject.Subject;
import org.libreccm.security.Shiro;
import org.libreccm.security.User;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.Optional; import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
/** /**
* An abstract base class for implementations of the {@link PageRenderer} * An abstract base class for implementations of the {@link PageRenderer}
* interface providing some functionality needed by all implementations of the * interface providing some functionality needed by all implementations of the
@ -39,6 +49,12 @@ public abstract class AbstractPageRenderer implements PageRenderer {
@Inject @Inject
private ComponentRendererManager componentRendererManager; private ComponentRendererManager componentRendererManager;
@Inject
private HttpServletRequest request;
@Inject
private Shiro shiro;
/** /**
* Renders a {@code Page} based on a {@link PageModel}. This implementation * Renders a {@code Page} based on a {@link PageModel}. This implementation
* first calls {@link #renderPage()} to create the page object. After that * first calls {@link #renderPage()} to create the page object. After that
@ -60,6 +76,46 @@ public abstract class AbstractPageRenderer implements PageRenderer {
final Map<String, Object> page = renderPage(parameters); final Map<String, Object> page = renderPage(parameters);
final Map<String, Object> currentUserData = new HashMap<>();
if (shiro.getUser().isPresent()) {
final User currentUser = shiro.getUser().get();
final Subject currentSubject = shiro.getSubject();
currentUserData.put(
"authenticated", currentSubject.isAuthenticated()
);
currentUserData.put("username", currentUser.getName());
currentUserData.put(
"email", currentUser.getPrimaryEmailAddress().toString()
);
currentUserData.put("familyName", currentUser.getFamilyName());
currentUserData.put("givenName", currentUser.getGivenName());
currentUserData.put(
"workspaceUrl",
URL.there(request, UI.getWorkspaceURL()).toString()
);
currentUserData.put(
"loginUrl",
URL.there(request, LoginConstants.LOGIN_PAGE_URL).toString()
);
currentUserData.put(
"logoutUrl",
URL.there(
request,
LoginServlet.getLogoutPageURL()).toString()
);
currentUserData.put(
"changePasswordUrl",
URL.there(
request,
LoginServlet.getLogoutPageURL()).toString()
);
} else {
currentUserData.put("authenticated", false);
}
page.put("currentUser", currentUserData);
for (final ContainerModel containerModel : pageModel.getContainers()) { for (final ContainerModel containerModel : pageModel.getContainers()) {
final Map<String, Object> container = renderContainer( final Map<String, Object> container = renderContainer(

View File

@ -0,0 +1,120 @@
<#--filedoc
The user banner component provides several informations about the current
user.
-->
<#--doc
Retreives the value the `greeting` property from the user banner.
Not supported.
@depcreated Without replacement.
@return Empty string.
-->
<#function getGreeting>
<#return "">
</#function>
<#--doc
Determines if the current user is logged in.
@depcrecated Use `isAuthenticated`
@return `true` if the current user is logged in, `false` if not.
-->
<#function isLoggedIn>
<#return isAuthenticated()>
</#function>
<#--doc
Determines if the current user is *not* logged in.
@depcrecated Use `isNotAuthenticated`
@return `true` if the current user is *not* logged in, `false` if the user
is logged in.
-->
<#function isNotLoggedIn>
<#return isNotAuthenticated()>
</#function>
<#--doc
Determines if the current user is authenticated.
@return `true` if the current user is authenticated, `false` if not.
-->
<#function isAuthenticated>
<#return currentUser.authenticated>
</#function>
<#--doc
Determines if the current user is *not* authenticated.
@return `true` if the current user is *not* authenticated, `false` if the user
is authenticated.
-->
<#function isNotAuthenticated>
<#return !currentUser.authenticated>
</#function>
<#--doc
Retrieves the URL for changing the password. Only available if the current
user is logged in.
@return The URL for changing the password.
-->
<#function getChangePasswordUrl>
<#return currentUser.changePasswordUrl>
</#function>
<#--doc
Retrieves the link to the login form. Only available if the current user
is not logged in.
@return The link to the login form.
-->
<#function getLoginLink>
<#return currentUser.loginLink>
</#function>
<#--doc
Retrieves the link for logging out. Only available if the current user
is logged in.
@return The link for logging out.
-->
<#function getLogoutLink>
<#return currentUser.logoutUrl>
</#function>
<#--doc
Retrieves the screen name (user name) of the current user. Only available
if the current user is logged in.
@return The screen name of the current user.
-->
<#function getScreenName>
<#return currentUser.username>
</#function>
<#--doc
The given name of the current user. Only available if the current user is
logged in.
@return The given name of the current user.
-->
<#function getUserGivenName>
<#return currentUser.givenName>
</#function>
<#--doc
The family name of the current user. Only available if the current user is
logged in.
@return The family name of the current user.
-->
<#function getUserFamilyName>
<#return currentUser.familyName>
</#function>