Skip to content

Commit

Permalink
feat: Allow space member to schedule post Article - EXO-75098 - Meeds…
Browse files Browse the repository at this point in the history
…-io/MIPs#161 (#294)

Allow space member to schedule post Article
  • Loading branch information
hakermi committed Nov 28, 2024
1 parent de1c900 commit ecfb638
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 21 deletions.
2 changes: 2 additions & 0 deletions content-service/src/main/java/io/meeds/news/model/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public class News {

private boolean canPublish;

private boolean canSchedule;

private List<String> sharedInSpacesList;

private String url;
Expand Down
10 changes: 8 additions & 2 deletions content-service/src/main/java/io/meeds/news/rest/NewsRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,9 @@ public ResponseEntity<List<NewsSearchResultEntity>> search(@Parameter(descriptio
@ApiResponse(responseCode = "401", description = "User not authorized to schedule a news"),
@ApiResponse(responseCode = "404", description = "Space not found"),
@ApiResponse(responseCode = "500", description = "Internal server error") })
public ResponseEntity<Boolean> canScheduleNews(@PathVariable("spaceId") String spaceId) {
public ResponseEntity<Boolean> canScheduleNews(@PathVariable("spaceId") String spaceId,
@Parameter(description = "target article id")
@RequestParam("articleId") String articleId) {
org.exoplatform.services.security.Identity currentIdentity = ConversationState.getCurrent().getIdentity();
try {
if (StringUtils.isBlank(spaceId)) {
Expand All @@ -722,8 +724,12 @@ public ResponseEntity<Boolean> canScheduleNews(@PathVariable("spaceId") String s
if (space == null) {
return ResponseEntity.notFound().build();
}
News news = newsService.getNewsArticleById(articleId);
if (news == null) {
return ResponseEntity.notFound().build();
}

return ResponseEntity.ok(newsService.canScheduleNews(space, currentIdentity));
return ResponseEntity.ok(newsService.canScheduleNews(space, currentIdentity, news));
} catch (Exception e) {
LOG.error("Error when checking if the authenticated user can schedule a news", e);
return ResponseEntity.internalServerError().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,12 @@ News scheduleNews(News news,
/**
* Checks if the user can schedule publishinga News
*
* @param space
* @param currentIdentity
* @param space target space
* @param currentIdentity current user identity
* @param article target article
* @return boolean : true if the user can schedule publishing a News
*/
boolean canScheduleNews(Space space, org.exoplatform.services.security.Identity currentIdentity);
boolean canScheduleNews(Space space, org.exoplatform.services.security.Identity currentIdentity, News article);

/**
* Checks if the user can view the News
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ public News getNewsByIdAndLang(String newsId,
news.setCanEdit(canEditNews(news, currentIdentity.getUserId()));
news.setCanDelete(canDeleteNews(currentIdentity, news));
news.setCanPublish(NewsUtils.canPublishNews(news.getSpaceId(), currentIdentity));
Space space = spaceService.getSpaceById(news.getSpaceId());
if (space != null) {
news.setCanSchedule(canScheduleNews(space, currentIdentity, news));
}
news.setTargets(newsTargetingService.getTargetsByNews(news));
ExoSocialActivity activity = null;
try {
Expand Down Expand Up @@ -573,6 +577,10 @@ public List<News> getNews(NewsFilter filter, Identity currentIdentity) throws Ex
news.setCanEdit(canEditNews(news, currentIdentity.getUserId()));
news.setCanDelete(canDeleteNews(currentIdentity, news));
news.setCanPublish(NewsUtils.canPublishNews(news.getSpaceId(), currentIdentity));
Space space = spaceService.getSpaceById(news.getSpaceId());
if (space != null) {
news.setCanSchedule(canScheduleNews(space, currentIdentity, news));
}
});
return newsList;
}
Expand Down Expand Up @@ -733,7 +741,7 @@ public News getNewsByActivityIdAndLang(String activityId, Identity currentIdenti
@Override
public News scheduleNews(News news, Identity currentIdentity, String newsObjectType) throws Exception {
Space space = news.getSpaceId() == null ? null : spaceService.getSpaceById(news.getSpaceId());
if (!canScheduleNews(space, currentIdentity)) {
if (!canScheduleNews(space, currentIdentity, news)) {
throw new IllegalArgumentException("User " + currentIdentity.getUserId() + " is not authorized to schedule news");
}
if (newsObjectType.equalsIgnoreCase(NewsObjectType.DRAFT.name())) {
Expand Down Expand Up @@ -787,8 +795,10 @@ public List<NewsESSearchResult> search(org.exoplatform.social.core.identity.mode
* {@inheritDoc}
*/
@Override
public boolean canScheduleNews(Space space, Identity currentIdentity) {
return spaceService.isManager(space, currentIdentity.getUserId())
public boolean canScheduleNews(Space space, Identity currentIdentity, News article) {
boolean isArticleAuthor = article.getAuthor() != null && article.getAuthor().equals(currentIdentity.getUserId());
boolean spaceMemberCanSchedule = isArticleAuthor && spaceService.isMember(space, currentIdentity.getUserId());
return spaceMemberCanSchedule || spaceService.isManager(space, currentIdentity.getUserId())
|| spaceService.isRedactor(space, currentIdentity.getUserId())
|| NewsUtils.canPublishNews(space.getId(), currentIdentity);
}
Expand Down Expand Up @@ -817,7 +827,7 @@ public boolean canViewNews(News news, String authenticatedUser) {
return false;
}
if (StringUtils.equals(news.getPublicationState(), STAGED)
&& !canScheduleNews(space, NewsUtils.getUserIdentity(authenticatedUser))) {
&& !canScheduleNews(space, NewsUtils.getUserIdentity(authenticatedUser), news)) {
return false;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
:editor-icon="editorIcon"
:publication-params="{
spaceId: spaceId,
canPublish: canScheduleArticle,
canPublish: canPublishArticle,
canSchedule: true,
allowedTargets: allowedTargets
}"
:images-download-folder="'DRIVE_ROOT_NODE/News/images'"
Expand Down Expand Up @@ -120,6 +121,7 @@ export default {
editorTitleInputRef: 'articleTitle',
imagesURLs: new Map(),
canScheduleArticle: false,
canPublishArticle: false,
postKey: 1,
editorIcon: 'fas fa-newspaper',
articleId: null,
Expand Down Expand Up @@ -686,9 +688,12 @@ export default {
}
this.loading = false;
});
this.$newsServices.canScheduleNews(this.currentSpace.id).then(canScheduleArticle => {
this.$newsServices.canScheduleNews(this.currentSpace.id, this.article?.id).then(canScheduleArticle => {
this.canScheduleArticle = canScheduleArticle;
});
this.$newsServices.canPublishNews(this.currentSpace.id).then(canPublishArticle => {
this.canPublishArticle = canPublishArticle;
});
});
},
fillArticle(articleId, setData, lang) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default {
if (resp !== null && resp !== UNAUTHORIZED_CODE) {
this.news = resp;
this.showEditButton = this.news.canEdit;
this.showPublishButton = this.news.canPublish;
this.showPublishButton = this.news.canPublish || this.news?.canSchedule;
this.showDeleteButton = this.news.canDelete;
if (this.news.lang) {
this.addParamToUrl('lang', this.news.lang);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
:params="{
spaceId: spaceId,
allowedTargets: allowedTargets,
canPublish: news?.canPublish
canPublish: news?.canPublish,
canSchedule: news?.canSchedule
}"
:edit-mode="true"
@publish="publishArticle" />
Expand All @@ -87,7 +88,9 @@ export default {
news: {
type: Object,
required: false,
default: function() { return new Object(); }
default: () => {
return {};
}
},
newsId: {
type: String,
Expand Down Expand Up @@ -200,7 +203,7 @@ export default {
}
},
getSpaceById(spaceId) {
this.$spaceService.getSpaceById(spaceId, 'identity')
return this.$spaceService.getSpaceById(spaceId, 'identity')
.then((space) => {
if (space && space.identity && space.identity.id) {
this.currentSpace = space;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ export default {
return this.sharedTemplateParams && this.sharedTemplateParams.newsId;
},
showDeleteButton() {
return this.news && this.news.canDelete;
return this.news?.canDelete;
},
showEditButton() {
return this.news && this.news.canEdit;
return this.news?.canEdit;
},
showPublishButton() {
return this.news && this.news.canPublish;
return this.news?.canPublish || this.news?.canSchedule;
},
},
created() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function init(params) {
activityId: params.activityId,
newsType: params.newsType,
showEditButton: params.news.canEdit,
showPublishButton: params.news.canPublish,
showPublishButton: params.news.canPublish || params.news?.canSchedule,
showCopyLinkButton: true,
showDeleteButton: params.news.canDelete,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ export function canUserCreateNews(spaceId) {
}).then((resp) => resp && resp.ok && resp.json());
}

export function canScheduleNews(spaceId) {
return fetch(`${newsConstants.CONTENT_API}/contents/canScheduleNews/${eXo.env.portal.spaceId || spaceId}`, {
export function canScheduleNews(spaceId, articleId) {
return fetch(`${newsConstants.CONTENT_API}/contents/canScheduleNews/${eXo.env.portal.spaceId || spaceId}?articleId=${articleId}`, {
headers: {
'Content-Type': 'application/json'
},
Expand Down

0 comments on commit ecfb638

Please sign in to comment.