Browse code

BM-13081 Imp: get ldap/AD authentication user from authcontext

Anthony Prades authored on 13/04/2018 09:57:37
Showing 1 changed files
... ...
@@ -32,14 +32,12 @@ import net.bluemind.core.api.fault.ErrorCode;
32 32
 import net.bluemind.core.api.fault.ServerFault;
33 33
 import net.bluemind.core.container.model.ItemValue;
34 34
 import net.bluemind.core.context.SecurityContext;
35
-import net.bluemind.core.rest.IServiceProvider;
36 35
 import net.bluemind.core.rest.ServerSideServiceProvider;
37 36
 import net.bluemind.domain.api.Domain;
38 37
 import net.bluemind.domain.api.IDomainSettings;
39 38
 import net.bluemind.system.ldap.importation.internal.tools.ImportLdapParameters;
40 39
 import net.bluemind.system.ldap.importation.internal.tools.LdapHelper;
41 40
 import net.bluemind.system.ldap.importation.internal.tools.UuidMapper;
42
-import net.bluemind.user.api.IUser;
43 41
 import net.bluemind.user.api.User;
44 42
 
45 43
 public class ImportLdapAuthenticationService implements IAuthProvider {
... ...
@@ -53,10 +51,6 @@ public class ImportLdapAuthenticationService implements IAuthProvider {
53 53
 	public ImportLdapAuthenticationService() {
54 54
 	}
55 55
 
56
-	private IServiceProvider core() {
57
-		return ServerSideServiceProvider.getProvider(SecurityContext.SYSTEM);
58
-	}
59
-
60 56
 	@Override
61 57
 	public int priority() {
62 58
 		return 100;
... ...
@@ -64,7 +58,6 @@ public class ImportLdapAuthenticationService implements IAuthProvider {
64 64
 
65 65
 	@Override
66 66
 	public AuthResult check(IAuthContext authContext) {
67
-		ImportLdapParameters ldapParameters;
68 67
 		ItemValue<Domain> domain = authContext.getDomain();
69 68
 		Map<String, String> domainSettings = null;
70 69
 		try {
... ...
@@ -75,12 +68,12 @@ public class ImportLdapAuthenticationService implements IAuthProvider {
75 75
 			return AuthResult.UNKNOWN;
76 76
 		}
77 77
 
78
-		ldapParameters = ImportLdapParameters.build(domain.value, domainSettings);
78
+		ImportLdapParameters ldapParameters = ImportLdapParameters.build(domain.value, domainSettings);
79 79
 		if (!ldapParameters.isEnabled()) {
80 80
 			return AuthResult.UNKNOWN;
81 81
 		}
82 82
 
83
-		String userDn = getLdapUserDn(ldapParameters, domain, authContext.getRealUserLogin());
83
+		String userDn = getLdapUserDn(ldapParameters, domain, authContext.getUser());
84 84
 		if (userDn == null) {
85 85
 			if (logger.isDebugEnabled()) {
86 86
 				logger.debug("Can't find user: {}@{} in LDAP server: {}, return: {}", authContext.getRealUserLogin(),
... ...
@@ -109,17 +102,13 @@ public class ImportLdapAuthenticationService implements IAuthProvider {
109 109
 		}
110 110
 	}
111 111
 
112
-	private String getLdapUserDn(ImportLdapParameters ldapParameters, ItemValue<Domain> domain, String userLogin) {
112
+	private String getLdapUserDn(ImportLdapParameters ldapParameters, ItemValue<Domain> domain, ItemValue<User> user) {
113 113
 		try {
114 114
 			long bmSearchTime = System.currentTimeMillis();
115 115
 
116
-			// Try to search user in BM database.
117
-			IUser userApi = core().instance(IUser.class, domain.value.name);
118
-			ItemValue<User> found = userApi.byLogin(userLogin);
119
-
120 116
 			UuidMapper bmUserUid = null;
121
-			if (found != null) {
122
-				bmUserUid = UuidMapper.fromExtId(found.externalId);
117
+			if (user != null) {
118
+				bmUserUid = UuidMapper.fromExtId(user.externalId);
123 119
 			}
124 120
 
125 121
 			bmSearchTime = System.currentTimeMillis() - bmSearchTime;
... ...
@@ -138,17 +127,17 @@ public class ImportLdapAuthenticationService implements IAuthProvider {
138 138
 					}
139 139
 					ldSearchTime = System.currentTimeMillis() - ldSearchTime;
140 140
 				}
141
-				logger.info("Found: " + udn + ", searched for extId '" + bmUserUid.getExtId() + "', u: " + userLogin
142
-						+ "@" + domain.value.name + ". bmSearch: " + bmSearchTime + "ms, ldapSearch: " + ldSearchTime
143
-						+ "ms.");
141
+
142
+				logger.info("Found: {}, searched for extId '{}', u: {}@{}. bmSearch: {}ms, ldapSearch: {}ms.", udn,
143
+						bmUserUid.getExtId(), user.value.login, domain.value.name, bmSearchTime, ldSearchTime);
144 144
 				return udn;
145 145
 			}
146 146
 		} catch (ServerFault sf) {
147 147
 			if (sf.getCode() == ErrorCode.NOT_FOUND) {
148
-				logger.info("User: " + userLogin + " not found in database");
148
+				logger.info("User: {}@{} not found in database", user.value.login, domain.value.name);
149 149
 			} else {
150
-				logger.error("Fail to check user: " + userLogin + "@" + domain.value.name + " in database: "
151
-						+ sf.getCode() + " - " + sf.getMessage());
150
+				logger.error("Fail to check user: {}@{} in database: {} - {}", user.value.login, domain.value.name,
151
+						sf.getCode(), sf.getMessage());
152 152
 				return null;
153 153
 			}
154 154
 		} catch (Exception e) {
... ...
@@ -157,6 +146,6 @@ public class ImportLdapAuthenticationService implements IAuthProvider {
157 157
 		}
158 158
 
159 159
 		// Try to search user in LDAP using his login
160
-		return LdapHelper.getLdapUserDnByUserLogin(ldapParameters, domain.value.name, userLogin);
160
+		return LdapHelper.getLdapUserDnByUserLogin(ldapParameters, domain.value.name, user.value.login);
161 161
 	}
162 162
 }