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.user2;
20  
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.mycore.common.MCRException;
28  import org.mycore.common.MCRSessionMgr;
29  import org.mycore.common.MCRUserInformation;
30  
31  /**
32   * @author Thomas Scheffler (yagee)
33   *
34   */
35  public class MCRTransientUser extends MCRUser {
36  
37      private static final long serialVersionUID = 1L;
38  
39      private static final Logger LOGGER = LogManager.getLogger(MCRTransientUser.class);
40  
41      private MCRUserInformation userInfo;
42  
43      public MCRTransientUser(MCRUserInformation userInfo) {
44          super();
45          this.userInfo = userInfo;
46          setLocked(true);
47  
48          String userName = userInfo.getUserID();
49          if (userName.contains("@")) {
50              userName = userName.substring(0, userName.indexOf("@"));
51          }
52          String realmId = getUserAttribute(MCRRealm.USER_INFORMATION_ATTR);
53  
54          super.setUserName(userName);
55          super.setRealmID(realmId);
56          super.setLastLogin(new Date(MCRSessionMgr.getCurrentSession().getLoginTime()));
57  
58          if (realmId != null && !MCRRealmFactory.getLocalRealm().equals(MCRRealmFactory.getRealm(realmId))) {
59              MCRUserAttributeMapper attributeMapper = MCRRealmFactory.getAttributeMapper(realmId);
60              if (attributeMapper != null) {
61                  Map<String, Object> attributes = new HashMap<>();
62                  for (String key : attributeMapper.getAttributeNames()) {
63                      attributes.put(key, userInfo.getUserAttribute(key));
64                  }
65  
66                  try {
67                      attributeMapper.mapAttributes(this, attributes);
68                  } catch (Exception e) {
69                      throw new MCRException(e.getMessage(), e);
70                  }
71              }
72          } else {
73              super.setRealName(getUserAttribute(MCRUserInformation.ATT_REAL_NAME));
74              for (MCRRole role : MCRRoleManager.listSystemRoles()) {
75                  LOGGER.debug("Test is in role: {}", role.getName());
76                  if (userInfo.isUserInRole(role.getName())) {
77                      assignRole(role.getName());
78                  }
79              }
80  
81          }
82      }
83  
84      @Override
85      public String getUserAttribute(String attribute) {
86          return this.userInfo.getUserAttribute(attribute);
87      }
88  
89  }