Commit 50fd636a authored by Matija Obreza's avatar Matija Obreza
Browse files

Added support for adding/editing global articles

parent 52b9bdaf
......@@ -67,11 +67,12 @@ public interface ContentService {
*/
ActivityPost createActivityPost(String title, String body);
Article updateArticle(long id, String title, String body);
Article updateArticle(EntityId entity, String slug, String title, String body, Locale locale);
Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, Locale locale);
Article updateArticle(long id, String slug, String title, String body);
Article createGlobalArticle(String slug, Locale locale, String title, String body);
}
......@@ -89,6 +89,42 @@ public class ContentServiceImpl implements ContentService {
return articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz), id, slug, locale.getLanguage());
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Article updateArticle(long id, String slug, String title, String body) {
Article article = articleRepository.findOne(id);
article.setSlug(slug);
article.setTitle(htmlSanitizer.sanitize(title));
article.setBody(htmlSanitizer.sanitize(body));
articleRepository.save(article);
return article;
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Article createGlobalArticle(String slug, Locale locale, String title, String body) {
Article article = getGlobalArticle(slug, locale);
if (article != null) {
throw new RuntimeException("Article exists");
}
article = new Article();
article.setClassPk(ensureClassPK(Article.class));
article.setLang(locale.getLanguage());
article.setSlug(slug);
article.setTitle(htmlSanitizer.sanitize(title));
article.setBody(htmlSanitizer.sanitize(body));
article.setPostDate(GregorianCalendar.getInstance());
articleRepository.save(article);
return article;
}
/**
* Creates or updates an article
*
......@@ -120,24 +156,13 @@ public class ContentServiceImpl implements ContentService {
article.setPostDate(GregorianCalendar.getInstance());
article.setSlug(slug);
}
article.setBody(body);
article.setTitle(title);
article.setBody(htmlSanitizer.sanitize(body));
article.setTitle(htmlSanitizer.sanitize(title));
articleRepository.save(article);
return article;
}
@Override
@Transactional(readOnly=false)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Article updateArticle(long id, String title, String body) {
Article article = articleRepository.findOne(id);
article.setTitle(title);
article.setBody(body);
articleRepository.save(article);
return article;
}
public ClassPK getClassPk(Class<?> clazz) {
return classPkRepository.findByClassName(clazz.getName());
}
......
......@@ -20,6 +20,7 @@ import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.ContentService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -40,6 +41,9 @@ public class ArticleController extends BaseController {
Article article = contentService.getGlobalArticle(slug, getLocale());
if (article == null) {
if (hasRole("ADMINISTRATOR")) {
return "redirect:/content/" + slug + "/edit";
}
throw new ResourceNotFoundException();
}
model.addAttribute("title", article.getTitle());
......@@ -48,10 +52,48 @@ public class ArticleController extends BaseController {
return "/content/article";
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping("{url}/edit")
public String edit(ModelMap model, @PathVariable(value = "url") String slug) {
_logger.debug("Editing article " + slug);
Article article = contentService.getGlobalArticle(slug, getLocale());
if (article == null) {
article = new Article();
article.setSlug(slug);
article.setLang(getLocale().getLanguage());
}
model.addAttribute("article", article);
return "/content/article-edit";
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/save-article", method = { RequestMethod.POST })
public String createNewGlobalArticle(ModelMap model, @RequestParam("slug") String slug, @RequestParam("title") String title,
@RequestParam("body") String body) {
contentService.createGlobalArticle(slug, getLocale(), title, body);
return "redirect:/content/" + slug;
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/save-article", params = { "id" }, method = { RequestMethod.POST })
public String saveExistingGlobalArticle(ModelMap model, @RequestParam("id") long id, @RequestParam("slug") String slug,
@RequestParam("title") String title, @RequestParam("body") String body) {
contentService.updateArticle(id, slug, title, body);
return "redirect:/content/" + slug;
}
@RequestMapping(value = "/blurp/update-blurp", method = { RequestMethod.POST })
public String updateBlurp(ModelMap model, @RequestParam("id") long id, @RequestParam(required = false, value = "title") String title,
@RequestParam("body") String body) {
contentService.updateArticle(id, title, body);
contentService.updateArticle(id, null, title, body);
return "redirect:/";
}
......
......@@ -14,7 +14,6 @@
* limitations under the License.
**/
package org.genesys2.server.servlet.controller;
import java.util.Locale;
......@@ -27,39 +26,51 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public abstract class BaseController {
protected Logger _logger = LoggerFactory.getLogger(getClass());
protected static final String ANONYMOUS_USER = "anonymousUser";
protected Logger _logger = LoggerFactory.getLogger(getClass());
protected static final String ANONYMOUS_USER = "anonymousUser";
protected static final String EXCEPTION_NOT_AUTHORIZED = "User is not authorized.";
protected static final String EXCEPTION_NOT_ORGANIZATION_MEMBER = "User is not a member of organization.";
@Autowired
protected MessageSource messageSource;
protected static final String EXCEPTION_NOT_AUTHORIZED = "User is not authorized.";
protected static final String EXCEPTION_NOT_ORGANIZATION_MEMBER = "User is not a member of organization.";
protected HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
@Autowired
protected MessageSource messageSource;
protected Locale getLocale() {
return LocaleContextHolder.getLocale();
}
protected HttpServletRequest getRequest() {
return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
}
protected boolean isAuthenticated() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !ANONYMOUS_USER.equals(authentication.getName());
}
protected Locale getLocale() {
return LocaleContextHolder.getLocale();
}
protected boolean isAuthenticated(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !ANONYMOUS_USER.equals(authentication.getName());
}
protected boolean hasRole(String role) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
for (GrantedAuthority grantedRole : authentication.getAuthorities()) {
if (grantedRole.getAuthority().equals(role))
return true;
}
}
return false;
}
//logs exception and returns it's message
protected String simpleExceptionHandler(Throwable th){
_logger.error(th.getMessage(), th);
return th.getMessage();
}
// logs exception and returns it's message
protected String simpleExceptionHandler(Throwable th) {
_logger.error(th.getMessage(), th);
return th.getMessage();
}
}
<!DOCTYPE html>
<%@include file="/WEB-INF/jsp/init.jsp"%>
<html>
<head>
<title>${title}</title>
</head>
<body>
<h1>
<spring:message code="article.edit-article" />
</h1>
<form role="form" class="" action="<c:url value="/content/save-article" />" method="post">
<c:if test="${article.id ne null}">
<input type="hidden" name="id" value="${article.id}" />
</c:if>
<div class="form-group">
<label for="article-slug" class="control-label"><spring:message code="article.slug" /></label>
<div class="controls">
<input type="text" id="article-slug" name="slug" value="<c:out value="${article.slug}" />" class="span9 form-control required" />
</div>
</div>
<div class="form-group">
<label for="article-title" class="control-label"><spring:message code="article.title" /></label>
<div class="controls">
<input type="text" id="article-title" name="title" value="<c:out value="${article.title}" />" class="span9 form-control required" />
</div>
</div>
<div class="form-group">
<label for="article-body" class="control-label"><spring:message code="article.body" /></label>
<div class="controls">
<textarea id="article-body" name="body" class="span9 required form-control html-editor">
<c:out value="${article.body}" escapeXml="false" />
</textarea>
</div>
</div>
<input type="submit" value="<spring:message code="save"/>" class="btn btn-primary" />
<c:if test="${article.id ne null}">
<a href="<c:url value="/content/${article.slug}" />" class="btn btn-default">Cancel</a>
</c:if>
<c:if test="${article.id eq null}">
<a href="<c:url value="/" />" class="btn btn-default">Cancel</a>
</c:if>
</form>
<script type="text/javascript">
jQuery(document).ready(function() {
tinyMCE.init({
selector : ".html-editor",
menubar : false,
statusbar : false,
height : 200,
directionality : document.dir
});
});
</script>
</body>
</html>
\ No newline at end of file
......@@ -14,6 +14,10 @@
</c:if>
<div class="">
<security:authorize access="hasRole('ADMINISTRATOR')">
<a href="<c:url value="/content/${article.slug}/edit" />" class="close">Edit</a>
</security:authorize>
<c:out value="${article.body}" escapeXml="false" />
</div>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment