Browse code

BM-12820 Fix: restore user pwd

David Phan authored on 13/04/2018 07:04:44
Showing 5 changed files
... ...
@@ -36,6 +36,7 @@ Require-Bundle: org.eclipse.core.runtime,
36 36
  net.bluemind.dataprotect.addressbook;bundle-version="3.1.0",
37 37
  net.bluemind.dataprotect.todolist;bundle-version="3.1.0",
38 38
  net.bluemind.dataprotect.mailbox;bundle-version="3.1.0",
39
- net.bluemind.backend.cyrus;bundle-version="3.1.0"
39
+ net.bluemind.backend.cyrus;bundle-version="3.1.0",
40
+ net.bluemind.user.service
40 41
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
41 42
 Bundle-ActivationPolicy: lazy
... ...
@@ -66,6 +66,7 @@ import net.bluemind.tests.defaultdata.PopulateHelper;
66 66
 import net.bluemind.user.api.IUser;
67 67
 import net.bluemind.user.api.IUserSettings;
68 68
 import net.bluemind.user.api.User;
69
+import net.bluemind.user.service.IUserServerApi;
69 70
 
70 71
 public class RestoreUserTests {
71 72
 
... ...
@@ -175,6 +176,10 @@ public class RestoreUserTests {
175 175
 		assertNotNull(restoredUser);
176 176
 
177 177
 		assertTrue("restore failed", monitor.success);
178
+
179
+		// testUser password = testUser login
180
+		IUserServerApi userServerService = testContext.provider().instance(IUserServerApi.class, domain);
181
+		assertTrue(userServerService.checkPassword(restoredUser.value.login, restoredUser.value.login));
178 182
 	}
179 183
 
180 184
 	@Test
... ...
@@ -23,7 +23,9 @@ Require-Bundle: org.eclipse.core.runtime,
23 23
  net.bluemind.dataprotect.addressbook,
24 24
  net.bluemind.user.hook,
25 25
  net.bluemind.group.api,
26
- net.bluemind.core.caches.registry
26
+ net.bluemind.core.caches.registry;bundle-version="3.1.0",
27
+ net.bluemind.user.persistance,
28
+ net.bluemind.core.container.persistance
27 29
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
28 30
 Bundle-ActivationPolicy: lazy
29 31
 Export-Package: net.bluemind.dataprotect.user
... ...
@@ -18,18 +18,26 @@
18 18
 */
19 19
 package net.bluemind.dataprotect.user;
20 20
 
21
+import java.sql.SQLException;
21 22
 import java.util.ArrayList;
22 23
 import java.util.Arrays;
23 24
 import java.util.Collections;
24 25
 import java.util.List;
25 26
 import java.util.UUID;
26 27
 
28
+import javax.sql.DataSource;
29
+
27 30
 import org.slf4j.Logger;
28 31
 import org.slf4j.LoggerFactory;
29 32
 
30 33
 import net.bluemind.core.api.fault.ServerFault;
34
+import net.bluemind.core.container.model.Container;
35
+import net.bluemind.core.container.model.Item;
31 36
 import net.bluemind.core.container.model.ItemValue;
37
+import net.bluemind.core.container.persistance.ContainerStore;
32 38
 import net.bluemind.core.context.SecurityContext;
39
+import net.bluemind.core.jdbc.JdbcActivator;
40
+import net.bluemind.core.rest.BmContext;
33 41
 import net.bluemind.core.rest.IServiceProvider;
34 42
 import net.bluemind.core.rest.ServerSideServiceProvider;
35 43
 import net.bluemind.core.task.api.ITask;
... ...
@@ -54,6 +62,7 @@ import net.bluemind.tag.api.TagsContainerType;
54 54
 import net.bluemind.user.api.IUser;
55 55
 import net.bluemind.user.api.IUserSettings;
56 56
 import net.bluemind.user.api.User;
57
+import net.bluemind.user.persistance.UserStore;
57 58
 
58 59
 public class RestoreUserTask implements IServerTask {
59 60
 
... ...
@@ -74,23 +83,28 @@ public class RestoreUserTask implements IServerTask {
74 74
 		IServiceProvider live = ServerSideServiceProvider.getProvider(SecurityContext.SYSTEM);
75 75
 		IDataProtect dp = live.instance(IDataProtect.class);
76 76
 		try (BackupDataProvider bdp = new BackupDataProvider(null, SecurityContext.SYSTEM, monitor)) {
77
-			IServiceProvider back = bdp.create(backup).provider();
77
+			BmContext backupContext = bdp.create(backup);
78
+			IServiceProvider back = backupContext.provider();
78 79
 
79 80
 			IUser userService = back.instance(IUser.class, item.domainUid);
80 81
 			ItemValue<User> user = userService.getComplete(item.entryUid);
81 82
 			List<ItemValue<Group>> memberOf = userService.memberOf(item.entryUid);
82
-			// FIXME restore pw
83 83
 			user.value.password = UUID.randomUUID().toString();
84 84
 
85 85
 			IUser userServiceLive = live.instance(IUser.class, item.domainUid);
86 86
 			IGroup groupService = live.instance(IGroup.class, item.domainUid);
87 87
 
88
-			if (userServiceLive.getComplete(item.entryUid) != null) {
88
+			ItemValue<User> liveUser = userServiceLive.getComplete(item.entryUid);
89
+			if (liveUser != null) {
89 90
 				userServiceLive.update(item.entryUid, user.value);
90 91
 			} else {
91 92
 				userServiceLive.create(item.entryUid, user.value);
93
+				liveUser = userServiceLive.getComplete(item.entryUid);
92 94
 			}
93 95
 
96
+			// restore user pwd
97
+			restoreUserPassword(backupContext, user.internalId, liveUser.internalId);
98
+
94 99
 			restoreUserSettings(item, live, back);
95 100
 			restoreUserTags(item, live, back);
96 101
 			restoreUserFilters(item, live, back);
... ...
@@ -137,6 +151,24 @@ public class RestoreUserTask implements IServerTask {
137 137
 
138 138
 	}
139 139
 
140
+	private void restoreUserPassword(BmContext backupContext, long oldUserId, long newUserId) throws SQLException {
141
+		DataSource ds = backupContext.getDataSource();
142
+		ContainerStore cs = new ContainerStore(backupContext, ds, backupContext.getSecurityContext());
143
+		Container domain = cs.get(item.domainUid);
144
+		UserStore userStore = new UserStore(ds, domain);
145
+		String pwd = userStore.getPassword(oldUserId);
146
+
147
+		ds = JdbcActivator.getInstance().getDataSource();
148
+		cs = new ContainerStore(backupContext, ds, backupContext.getSecurityContext());
149
+		domain = cs.get(item.domainUid);
150
+		userStore = new UserStore(ds, domain);
151
+
152
+		userStore = new UserStore(JdbcActivator.getInstance().getDataSource(), domain);
153
+		Item i = Item.create(item.entryUid, null);
154
+		i.id = newUserId;
155
+		userStore.setPassword(i, pwd);
156
+	}
157
+
140 158
 	private void restoreUserFilters(Restorable item, IServiceProvider live, IServiceProvider back) {
141 159
 		IMailboxes mboxesBackup = back.instance(IMailboxes.class, item.domainUid);
142 160
 		IMailboxes mboxesLive = live.instance(IMailboxes.class, item.domainUid);
... ...
@@ -149,4 +149,11 @@ public class UserStore extends AbstractItemValueStore<User> {
149 149
 				new Object[] { container.id });
150 150
 
151 151
 	}
152
+
153
+	public String getPassword(long itemId) throws SQLException {
154
+		return unique(
155
+				"select password from t_domain_user innser join t_container_item item on item_id = item.id where item.container_id = ? and item.id = ? ",
156
+				STRING_CREATOR, Arrays.asList(), new Object[] { container.id, itemId });
157
+
158
+	}
152 159
 }