View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.servlets;
20  
21  import java.security.Principal;
22  import java.util.Optional;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.mycore.common.MCRSession;
27  import org.mycore.common.MCRSessionMgr;
28  import org.mycore.common.MCRSystemUserInformation;
29  import org.mycore.common.MCRUserInformation;
30  import org.mycore.frontend.MCRFrontendUtil;
31  
32  import jakarta.servlet.http.HttpServletRequest;
33  
34  /**
35   * @author Thomas Scheffler (yagee)
36   *
37   */
38  public class MCRContainerLoginServlet extends MCRServlet {
39  
40      private static final long serialVersionUID = 1L;
41  
42      private static final Logger LOGGER = LogManager.getLogger(MCRContainerLoginServlet.class);
43  
44      /* (non-Javadoc)
45       * @see org.mycore.frontend.servlets.MCRServlet#think(org.mycore.frontend.servlets.MCRServletJob)
46       */
47      @Override
48      protected void think(MCRServletJob job) throws Exception {
49          MCRSession session = MCRSessionMgr.getCurrentSession();
50          session.setUserInformation(new ContainerUserInformation(session));
51          LOGGER.info("Logged in: {}", session.getUserInformation().getUserID());
52      }
53  
54      /* (non-Javadoc)
55       * @see org.mycore.frontend.servlets.MCRServlet#render(org.mycore.frontend.servlets.MCRServletJob, java.lang.Exception)
56       */
57      @Override
58      protected void render(MCRServletJob job, Exception ex) throws Exception {
59          String backToUrl = getProperty(job.getRequest(), "url");
60  
61          if (backToUrl == null) {
62              String referer = job.getRequest().getHeader("Referer");
63              backToUrl = (referer != null) ? referer : MCRFrontendUtil.getBaseURL();
64          }
65          job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(backToUrl));
66      }
67  
68      protected static class ContainerUserInformation implements MCRUserInformation {
69          protected MCRSession session;
70  
71          String lastUser;
72  
73          public ContainerUserInformation(MCRSession session) {
74              this.session = session;
75          }
76  
77          @Override
78          public String getUserID() {
79              lastUser = getCurrentRequest()
80                  .flatMap(r -> Optional.ofNullable(r.getUserPrincipal()))
81                  .map(Principal::getName)
82                  .orElseGet(() -> Optional.ofNullable(lastUser)
83                      .orElseGet(MCRSystemUserInformation.getGuestInstance()::getUserID));
84              return lastUser;
85          }
86  
87          @Override
88          public boolean isUserInRole(String role) {
89              return getCurrentRequest().map(r -> r.isUserInRole(role)).orElse(Boolean.FALSE);
90          }
91  
92          @Override
93          public String getUserAttribute(String attribute) {
94              return null;
95          }
96  
97          protected Optional<HttpServletRequest> getCurrentRequest() {
98              LogManager.getLogger(getClass()).debug("Getting request from session: {}", session.getID());
99              return MCRFrontendUtil.getCurrentServletJob()
100                 .map(MCRServletJob::getRequest);
101         }
102     }
103 
104 }