diff --git a/src/main/java/fr/cgi/magneto/controller/SectionController.java b/src/main/java/fr/cgi/magneto/controller/SectionController.java index f5dd1113..4ef2c71c 100644 --- a/src/main/java/fr/cgi/magneto/controller/SectionController.java +++ b/src/main/java/fr/cgi/magneto/controller/SectionController.java @@ -8,6 +8,7 @@ import fr.cgi.magneto.model.boards.BoardPayload; import fr.cgi.magneto.model.cards.Card; import fr.cgi.magneto.security.DuplicateCardRight; +import fr.cgi.magneto.security.ManageBoardRight; import fr.cgi.magneto.security.ViewRight; import fr.cgi.magneto.security.WriteBoardRight; import fr.cgi.magneto.service.BoardService; @@ -17,9 +18,11 @@ import fr.wseduc.rs.*; import fr.wseduc.security.ActionType; import fr.wseduc.security.SecuredAction; +import fr.wseduc.webutils.http.Binding; import fr.wseduc.webutils.request.RequestUtils; import io.vertx.core.CompositeFuture; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.http.HttpServerRequest; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; @@ -27,6 +30,7 @@ import org.entcore.common.events.EventStore; import org.entcore.common.events.EventStoreFactory; import org.entcore.common.http.filter.ResourceFilter; +import org.entcore.common.user.UserInfos; import org.entcore.common.user.UserUtils; import java.util.Collections; @@ -50,20 +54,22 @@ public SectionController(ServiceFactory serviceFactory) { this.eventStore = EventStoreFactory.getFactory().getEventStore(Magneto.class.getSimpleName()); } - @Get("/sections/:boardId") + @Get("/sections/:id") @ApiDoc("Get sections by board id") @ResourceFilter(ViewRight.class) @SecuredAction(value = "", type = ActionType.RESOURCE) public void getSectionsByBoardId(HttpServerRequest request) { - String boardId = request.getParam(Field.BOARDID); - boardService.getBoards(Collections.singletonList(boardId)) + String boardId = request.getParam(Field.ID); + UserUtils.getUserInfos(eb, request, user -> new ManageBoardRight().authorize(request, null, user, readOnly -> + boardService.getBoards(Collections.singletonList(boardId)) .compose(boards -> { if (boards.isEmpty()) { String message = String.format("[Magneto@%s::getSectionsByBoardId] Failed to get boards with board id : %s", this.getClass().getSimpleName(), boardId); return Future.failedFuture(message); } else { - return sectionService.getSectionsByBoard(boards.get(0)); + + return sectionService.getSectionsByBoard(boards.get(0), !readOnly); } }) .onFailure(err -> { @@ -79,7 +85,7 @@ public void getSectionsByBoardId(HttpServerRequest request) { .collect(Collectors.toList())); renderJson(request, new JsonObject() .put(Field.ALL, sectionsResult)); - }); + }))); } @Post("/section") diff --git a/src/main/java/fr/cgi/magneto/model/Section.java b/src/main/java/fr/cgi/magneto/model/Section.java index f39c2ace..48af01f6 100644 --- a/src/main/java/fr/cgi/magneto/model/Section.java +++ b/src/main/java/fr/cgi/magneto/model/Section.java @@ -12,7 +12,7 @@ public class Section implements Model { private String title; private List cardIds; private String boardId; - private boolean displayed; + private Boolean displayed; @SuppressWarnings("unchecked") public Section(JsonObject section) { @@ -20,8 +20,10 @@ public Section(JsonObject section) { this.title = section.getString(Field.TITLE); this.cardIds = section.getJsonArray(Field.CARDIDS, new JsonArray()).getList(); this.boardId = section.getString(Field.BOARDID); - this.displayed = section.getBoolean(Field.DISPLAYED); + if (section.containsKey(Field.DISPLAYED)) + this.displayed = section.getBoolean(Field.DISPLAYED); } + public Section() { } @@ -30,14 +32,29 @@ public String getId() { return _id; } + public Section setId(String id) { + this._id = id; + return this; + } + public String getTitle() { return title; } + public Section setTitle(String title) { + this.title = title; + return this; + } + public List getCardIds() { return cardIds; } + public Section setCardIds(List cardIds) { + this.cardIds = cardIds; + return this; + } + public Section removeCardIds(List cardIds) { this.cardIds = this.cardIds.stream() .filter(s -> !cardIds.contains(s)) @@ -45,37 +62,25 @@ public Section removeCardIds(List cardIds) { return this; } - public String getBoardId() { return boardId; } - public Section addCardIds(List cardIds) { - this.cardIds.addAll(0, cardIds); - return this; - } - - public Section setId(String id) { - this._id = id; - return this; - } - - public Section setTitle(String title) { - this.title = title; - return this; - } - public Section setBoardId(String boardId) { this.boardId = boardId; return this; } - public Section setCardIds(List cardIds) { - this.cardIds = cardIds; + public Section addCardIds(List cardIds) { + this.cardIds.addAll(0, cardIds); return this; } + public boolean getDisplayed() { - return displayed; + if (this.displayed != null) + return displayed; + else + return true; } public void setDisplayed(boolean displayed) { @@ -88,8 +93,9 @@ public JsonObject toJson() { .put(Field._ID, this.getId()) .put(Field.TITLE, this.getTitle()) .put(Field.CARDIDS, this.getCardIds()) - .put(Field.BOARDID, this.getBoardId()) - .put(Field.DISPLAYED, this.getDisplayed()); + .put(Field.BOARDID, this.getBoardId()); + if (this.displayed != null) + json.put(Field.DISPLAYED, this.getDisplayed()); return json; } @@ -99,5 +105,4 @@ public Section model(JsonObject section) { } - } diff --git a/src/main/java/fr/cgi/magneto/model/SectionPayload.java b/src/main/java/fr/cgi/magneto/model/SectionPayload.java index 7d85ba94..5df2aa3a 100644 --- a/src/main/java/fr/cgi/magneto/model/SectionPayload.java +++ b/src/main/java/fr/cgi/magneto/model/SectionPayload.java @@ -13,7 +13,7 @@ public class SectionPayload implements Model { private String title; private List cardIds; private String boardId; - private boolean displayed; + private Boolean displayed; @SuppressWarnings("unchecked") public SectionPayload(JsonObject section) { @@ -115,7 +115,7 @@ public JsonObject toJson() { json.put(Field.CARDIDS, new JsonArray()); } if (this.displayed() != null ) { - json.put(Field.DISPLAYED, new JsonArray()); + json.put(Field.DISPLAYED, this.displayed()); } return json; diff --git a/src/main/java/fr/cgi/magneto/service/SectionService.java b/src/main/java/fr/cgi/magneto/service/SectionService.java index 0affc5c5..4a9b036a 100644 --- a/src/main/java/fr/cgi/magneto/service/SectionService.java +++ b/src/main/java/fr/cgi/magneto/service/SectionService.java @@ -28,7 +28,13 @@ public interface SectionService { */ Future> getSectionsByBoardId(String boardId); - Future> getSectionsByBoard(Board board); + /** + * + * @param board board of the sections + * @param isReadOnly + * @return + */ + Future> getSectionsByBoard(Board board, boolean isReadOnly); /** * Create a section diff --git a/src/main/java/fr/cgi/magneto/service/impl/DefaultSectionService.java b/src/main/java/fr/cgi/magneto/service/impl/DefaultSectionService.java index c8ab7742..8c1d9e95 100644 --- a/src/main/java/fr/cgi/magneto/service/impl/DefaultSectionService.java +++ b/src/main/java/fr/cgi/magneto/service/impl/DefaultSectionService.java @@ -12,8 +12,6 @@ import fr.cgi.magneto.model.boards.Board; import fr.cgi.magneto.model.boards.BoardPayload; import fr.cgi.magneto.model.cards.Card; -import fr.cgi.magneto.service.BoardService; -import fr.cgi.magneto.service.CardService; import fr.cgi.magneto.service.SectionService; import fr.cgi.magneto.service.ServiceFactory; import fr.wseduc.mongodb.MongoDb; @@ -80,7 +78,7 @@ public Future> getSectionsByBoardId(String boardId) { } @Override - public Future> getSectionsByBoard(Board board) { + public Future> getSectionsByBoard(Board board, boolean isReadOnly) { Promise> promise = Promise.promise(); JsonObject query = this.getAllSectionsByBoardQuery(board); mongoDb.command(query.toString(), MongoDbResult.validResultHandler(either -> { @@ -92,7 +90,12 @@ public Future> getSectionsByBoard(Board board) { JsonArray result = either.right().getValue() .getJsonObject(Field.CURSOR, new JsonObject()) .getJsonArray(Field.FIRSTBATCH, new JsonArray()); - promise.complete(ModelHelper.toList(result, Section.class)); + if (isReadOnly) { + promise.complete(((List
)ModelHelper.toList(result, Section.class)).stream() + .filter(Section::getDisplayed).collect(Collectors.toList())); + } + else + promise.complete(ModelHelper.toList(result, Section.class)); } })); @@ -115,7 +118,9 @@ private JsonObject getAllSectionsByBoardQuery(Board board) { .put(Field._ID, 1) .put(Field.TITLE, 1) .put(Field.BOARDID, 1) - .put(Field.CARDIDS, 1)); + .put(Field.CARDIDS, 1) + .put(Field.DISPLAYED, 1)) + ; return query.getAggregate(); } @@ -140,7 +145,6 @@ public Future update(SectionPayload section) { Promise promise = Promise.promise(); JsonObject sectionUpdate = new JsonObject() .put(Field._ID, section.getId()); - log.info(section.toJson()); JsonObject update = new JsonObject().put(Mongo.SET, section.toJson()); mongoDb.update(this.collection, sectionUpdate, update, MongoDbResult.validResultHandler(results -> { if (results.isLeft()) { diff --git a/src/main/resources/public/ts/directives/section-list/section-list-item.html b/src/main/resources/public/ts/directives/section-list/section-list-item.html index 843e24d4..5e2a5759 100644 --- a/src/main/resources/public/ts/directives/section-list/section-list-item.html +++ b/src/main/resources/public/ts/directives/section-list/section-list-item.html @@ -1,4 +1,4 @@ -
+