|
20 | 20 | package org.entcore.common.user;
|
21 | 21 |
|
22 | 22 | import com.fasterxml.jackson.databind.ObjectMapper;
|
23 |
| - |
24 | 23 | import fr.wseduc.mongodb.MongoDb;
|
25 | 24 | import fr.wseduc.webutils.I18n;
|
26 | 25 | import fr.wseduc.webutils.Utils;
|
27 |
| -import static fr.wseduc.webutils.Utils.getOrElse; |
28 |
| -import static fr.wseduc.webutils.Utils.handlerToAsyncHandler; |
29 |
| -import static fr.wseduc.webutils.Utils.isEmpty; |
30 |
| -import static fr.wseduc.webutils.Utils.isNotEmpty; |
31 | 26 | import fr.wseduc.webutils.http.Renders;
|
32 |
| -import static fr.wseduc.webutils.http.Renders.unauthorized; |
33 | 27 | import fr.wseduc.webutils.request.CookieHelper;
|
34 | 28 | import fr.wseduc.webutils.security.JWT;
|
35 | 29 | import fr.wseduc.webutils.security.SecureHttpServerRequest;
|
|
49 | 43 | import io.vertx.core.json.JsonObject;
|
50 | 44 | import io.vertx.core.logging.Logger;
|
51 | 45 | import io.vertx.core.logging.LoggerFactory;
|
52 |
| -import static org.entcore.common.http.filter.AppOAuthResourceProvider.getTokenId; |
53 | 46 |
|
54 | 47 | import io.vertx.core.shareddata.LocalMap;
|
55 | 48 | import org.entcore.common.neo4j.Neo4j;
|
|
58 | 51 | import org.entcore.common.utils.StringUtils;
|
59 | 52 |
|
60 | 53 | import java.io.IOException;
|
| 54 | +import java.util.Arrays; |
| 55 | +import java.util.Collections; |
| 56 | +import java.util.Comparator; |
61 | 57 | import java.util.HashMap;
|
62 | 58 | import java.util.HashSet;
|
63 | 59 | import java.util.List;
|
|
66 | 62 | import java.util.Set;
|
67 | 63 | import java.util.stream.Collectors;
|
68 | 64 |
|
| 65 | +import static fr.wseduc.webutils.Utils.getOrElse; |
| 66 | +import static fr.wseduc.webutils.Utils.handlerToAsyncHandler; |
| 67 | +import static fr.wseduc.webutils.Utils.isEmpty; |
| 68 | +import static fr.wseduc.webutils.Utils.isNotEmpty; |
| 69 | +import static fr.wseduc.webutils.http.Renders.unauthorized; |
| 70 | +import static org.entcore.common.http.filter.AppOAuthResourceProvider.getTokenId; |
| 71 | + |
69 | 72 | public class UserUtils {
|
70 | 73 |
|
71 | 74 | private static final Vertx vertx = Vertx.currentContext().owner();
|
@@ -235,6 +238,7 @@ public static void findVisibles(EventBus eb, String userId, String customReturn,
|
235 | 238 | public void handle(AsyncResult<Message<JsonArray>> res) {
|
236 | 239 | if (res.succeeded()) {
|
237 | 240 | JsonArray r = res.result().body();
|
| 241 | + log.info("UserUtils.findVisibles - r.size = " + r.size()); // TODO JBER : exposer métrique |
238 | 242 | if (acceptLanguage != null) {
|
239 | 243 | translateGroupsNames(r, acceptLanguage);
|
240 | 244 | }
|
@@ -348,6 +352,106 @@ private static void formatPositions(JsonObject dbResult) {
|
348 | 352 | dbResult.put("positions", positions);
|
349 | 353 | }
|
350 | 354 |
|
| 355 | + public static JsonArray mapObjectToContact(final String profile, final JsonArray shareBookmarks, final JsonArray visible, final String acceptLanguage) { |
| 356 | + final List<String> usedInAll = Arrays.asList("TO", "CC", "CCI"); |
| 357 | + final List<String> usedInCCI = Collections.singletonList("CCI"); |
| 358 | + |
| 359 | + /* |
| 360 | + final JsonArray sb = new JsonArray(); |
| 361 | + if (shareBookmarks != null) { |
| 362 | + for (String id: shareBookmarks.fieldNames()) { |
| 363 | + final JsonArray value = shareBookmarks.getJsonArray(id); |
| 364 | + if (value == null || value.size() < 2) { |
| 365 | + continue; |
| 366 | + } |
| 367 | + final JsonObject r = new fr.wseduc.webutils.collections.JsonObject(); |
| 368 | + r.put("id", id); |
| 369 | + r.put("displayName", value.remove(0)); |
| 370 | + r.put("type", "ShareBookmark"); |
| 371 | + sb.add(r); |
| 372 | + } |
| 373 | + } |
| 374 | +
|
| 375 | + final JsonArray res = !sb.isEmpty() ? sortShareBookmarksByName(sb) : new JsonArray(); |
| 376 | + */ |
| 377 | + |
| 378 | + final JsonArray res = new JsonArray(); |
| 379 | + for (Object o: shareBookmarks) { |
| 380 | + if (!(o instanceof JsonObject)) continue; |
| 381 | + JsonObject j = (JsonObject) o; |
| 382 | + j.put("type", "ShareBookmark"); |
| 383 | + j.put("usedIn", usedInAll); |
| 384 | + res.add(j); |
| 385 | + } |
| 386 | + |
| 387 | + for (Object o: visible) { |
| 388 | + if (!(o instanceof JsonObject)) continue; |
| 389 | + JsonObject j = (JsonObject) o; |
| 390 | + if (j.getString("name") != null) { |
| 391 | + j.remove("profile"); |
| 392 | + j.remove("children"); |
| 393 | + j.remove("classrooms"); |
| 394 | + j.remove("disciplines"); |
| 395 | + j.remove("functions"); |
| 396 | + j.remove("relatives"); |
| 397 | + |
| 398 | + Object gt = j.remove("groupType"); |
| 399 | + Object gp = j.remove("groupProfile"); |
| 400 | + if (gt instanceof Iterable) { |
| 401 | + for (Object gti: (Iterable) gt) { |
| 402 | + if (gti != null && !"Group".equals(gti) && gti.toString().endsWith("Group")) { |
| 403 | + j.put("groupType", gti); |
| 404 | + if ("ProfileGroup".equals(gti)) { |
| 405 | + j.put("profile", gp); |
| 406 | + } |
| 407 | + break; |
| 408 | + } |
| 409 | + } |
| 410 | + } |
| 411 | + |
| 412 | + UserUtils.groupDisplayName(j, acceptLanguage); |
| 413 | + j.put("displayName", j.getString("name")); |
| 414 | + |
| 415 | + if ("ManualGroup".equals(j.getString("groupType")) && "BroadcastGroup".equals(j.getString("subType"))) { |
| 416 | + j.put("type", "BroadcastGroup"); |
| 417 | + j.put("usedIn", usedInCCI); |
| 418 | + } else { |
| 419 | + j.put("type", "Group"); |
| 420 | + j.put("usedIn", usedInAll); |
| 421 | + } |
| 422 | + } else { |
| 423 | + j.put("type", "User"); |
| 424 | + j.put("usedIn", usedInAll); |
| 425 | + j.remove("groupProfile"); |
| 426 | + j.remove("groupType"); |
| 427 | + j.remove("nbUsers"); |
| 428 | + if (profile.equals("Student")) { |
| 429 | + j.remove("relatives"); |
| 430 | + } |
| 431 | + } |
| 432 | + |
| 433 | + j.remove("name"); |
| 434 | + j.remove("groupDisplayName"); |
| 435 | + j.remove("sortDisplayName"); |
| 436 | + j.remove("sortWeight"); |
| 437 | + j.remove("subjects"); |
| 438 | + j.remove("subType"); |
| 439 | + j.remove("sorted_children_names"); |
| 440 | + j.remove("sorted_functions"); |
| 441 | + j.remove("sorted_disciplines"); |
| 442 | + |
| 443 | + res.add(j); |
| 444 | + } |
| 445 | + return res; |
| 446 | + } |
| 447 | + |
| 448 | + private static JsonArray sortShareBookmarksByName(JsonArray sb) { |
| 449 | + List<JsonObject> list = sb.getList(); |
| 450 | + list.sort(Comparator.comparing(o -> o.getString("displayName"))); |
| 451 | + |
| 452 | + return new JsonArray(list); |
| 453 | + } |
| 454 | + |
351 | 455 | public static void findUsersCanSeeMe(final EventBus eb, HttpServerRequest request,
|
352 | 456 | final Handler<JsonArray> handler) {
|
353 | 457 | JsonObject m = new JsonObject()
|
@@ -667,6 +771,13 @@ public void handle(JsonObject session) {
|
667 | 771 | });
|
668 | 772 | }
|
669 | 773 |
|
| 774 | + /** |
| 775 | + * Fetch the user's session information and return an unauthorized response if the user has no session. Therefore, |
| 776 | + * <b>there is no need to handle a failure of the returned Future</b>. |
| 777 | + * @param eb Event bus to be used to fetch the user's session |
| 778 | + * @param request Caller's request |
| 779 | + * @return The user's session information |
| 780 | + */ |
670 | 781 | public static Future<UserInfos> getAuthenticatedUserInfos(EventBus eb, HttpServerRequest request) {
|
671 | 782 | final Promise<UserInfos> promise = Promise.promise();
|
672 | 783 | getSession(eb, request, session -> {
|
|
0 commit comments