1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }