diff --git a/src/main/java/com/viglet/shiohara/api/object/ShObjectAPI.java b/src/main/java/com/viglet/shiohara/api/object/ShObjectAPI.java index 7d16fa606..58dfe111c 100644 --- a/src/main/java/com/viglet/shiohara/api/object/ShObjectAPI.java +++ b/src/main/java/com/viglet/shiohara/api/object/ShObjectAPI.java @@ -47,7 +47,9 @@ import com.viglet.shiohara.api.folder.ShFolderPath; import com.viglet.shiohara.bean.ShFolderTinyBean; import com.viglet.shiohara.bean.ShPostTinyBean; -import com.viglet.shiohara.bean.ShSecurityBean; +import com.viglet.shiohara.bean.security.ShConsoleSecurityBean; +import com.viglet.shiohara.bean.security.ShPageSecurityBean; +import com.viglet.shiohara.bean.security.ShSecurityBean; import com.viglet.shiohara.persistence.model.auth.ShGroup; import com.viglet.shiohara.persistence.model.auth.ShUser; import com.viglet.shiohara.persistence.model.folder.ShFolder; @@ -157,8 +159,17 @@ public ShSecurityBean shObjectGroupsGet(@PathVariable String id) { List shObjects = new ArrayList<>(); shObjects.add(shObject); ShSecurityBean shSecurityBean = new ShSecurityBean(); - shSecurityBean.setShGroups(shObject.getShGroups()); - shSecurityBean.setShUsers(shObject.getShUsers()); + ShConsoleSecurityBean shConsoleSecurityBean = new ShConsoleSecurityBean(); + shConsoleSecurityBean.setShGroups(shObject.getShGroups()); + shConsoleSecurityBean.setShUsers(shObject.getShUsers()); + + ShPageSecurityBean shPageSecurityBean = new ShPageSecurityBean(); + shPageSecurityBean.setAllowGuestUser(shObject.isPageAllowGuestUser()); + shPageSecurityBean.setAllowRegisterUser(shObject.isPageAllowRegisterUser()); + shPageSecurityBean.setShGroups(shObject.getShPageGroups()); + + shSecurityBean.setConsole(shConsoleSecurityBean); + shSecurityBean.setPage(shPageSecurityBean); return shSecurityBean; } @@ -167,9 +178,18 @@ public ShSecurityBean shObjectGroupsGet(@PathVariable String id) { @JsonView({ ShJsonView.ShJsonViewObject.class }) public ShSecurityBean shObjectGroupsUpdate(@PathVariable String id, @RequestBody ShSecurityBean shSecurityBean) { ShObject shObject = shObjectRepository.findById(id).orElse(null); - if (shObject != null) { - shObject.setShGroups(shSecurityBean.getShGroups()); - shObject.setShUsers(shSecurityBean.getShUsers()); + if (shObject != null && shSecurityBean != null) { + if (shSecurityBean.getConsole() != null) { + shObject.setShGroups(shSecurityBean.getConsole().getShGroups()); + shObject.setShUsers(shSecurityBean.getConsole().getShUsers()); + } + + if (shSecurityBean.getPage() != null) { + shObject.setPageAllowGuestUser(shSecurityBean.getPage().isAllowGuestUser()); + shObject.setPageAllowRegisterUser(shSecurityBean.getPage().isAllowRegisterUser()); + shObject.setShPageGroups(shSecurityBean.getPage().getShGroups()); + } + shObjectRepository.saveAndFlush(shObject); } return shSecurityBean; @@ -307,7 +327,7 @@ private Set allowedFolders(ShUser shUser, ShObject shObject) { return folders; } else { Set shFolders = new HashSet<>(); - + for (ShGroup shGroup : shUser.getShGroups()) { shGroups.add(shGroup.getName()); } @@ -348,7 +368,7 @@ private List allowedPosts(ShUser shUser, ShObject shObject) { List shPosts = new ArrayList<>(); for (ShGroup shGroup : shUser.getShGroups()) { shGroups.add(shGroup.getName()); - } + } shUsers.add(shUser.getUsername()); for (ShPostTinyBean post : posts) if (shObjectRepository.countByIdAndShGroupsInOrIdAndShUsersInOrIdAndShGroupsIsNullAndShUsersIsNull( diff --git a/src/main/java/com/viglet/shiohara/bean/security/ShConsoleSecurityBean.java b/src/main/java/com/viglet/shiohara/bean/security/ShConsoleSecurityBean.java new file mode 100644 index 000000000..09b2fd63f --- /dev/null +++ b/src/main/java/com/viglet/shiohara/bean/security/ShConsoleSecurityBean.java @@ -0,0 +1,27 @@ +package com.viglet.shiohara.bean.security; + +import java.util.Set; + +public class ShConsoleSecurityBean { + + private Set shUsers; + + private Set shGroups; + + public Set getShUsers() { + return shUsers; + } + + public void setShUsers(Set shUsers) { + this.shUsers = shUsers; + } + + public Set getShGroups() { + return shGroups; + } + + public void setShGroups(Set shGroups) { + this.shGroups = shGroups; + } + +} diff --git a/src/main/java/com/viglet/shiohara/bean/security/ShPageSecurityBean.java b/src/main/java/com/viglet/shiohara/bean/security/ShPageSecurityBean.java new file mode 100644 index 000000000..64f435e93 --- /dev/null +++ b/src/main/java/com/viglet/shiohara/bean/security/ShPageSecurityBean.java @@ -0,0 +1,37 @@ +package com.viglet.shiohara.bean.security; + +import java.util.Set; + +public class ShPageSecurityBean { + + private boolean allowRegisterUser; + + private boolean allowGuestUser; + + private Set shGroups; + + public boolean isAllowRegisterUser() { + return allowRegisterUser; + } + + public void setAllowRegisterUser(boolean allowRegisterUser) { + this.allowRegisterUser = allowRegisterUser; + } + + public boolean isAllowGuestUser() { + return allowGuestUser; + } + + public void setAllowGuestUser(boolean allowGuestUser) { + this.allowGuestUser = allowGuestUser; + } + + public Set getShGroups() { + return shGroups; + } + + public void setShGroups(Set shGroups) { + this.shGroups = shGroups; + } + +} diff --git a/src/main/java/com/viglet/shiohara/bean/security/ShSecurityBean.java b/src/main/java/com/viglet/shiohara/bean/security/ShSecurityBean.java index b2cb7b6f1..209fd4383 100644 --- a/src/main/java/com/viglet/shiohara/bean/security/ShSecurityBean.java +++ b/src/main/java/com/viglet/shiohara/bean/security/ShSecurityBean.java @@ -1,26 +1,25 @@ -package com.viglet.shiohara.bean; - -import java.util.Set; +package com.viglet.shiohara.bean.security; public class ShSecurityBean { - private Set shUsers; - private Set shGroups; + private ShConsoleSecurityBean console; + + private ShPageSecurityBean page; - public Set getShUsers() { - return shUsers; + public ShConsoleSecurityBean getConsole() { + return console; } - public void setShUsers(Set shUsers) { - this.shUsers = shUsers; + public void setConsole(ShConsoleSecurityBean console) { + this.console = console; } - public Set getShGroups() { - return shGroups; + public ShPageSecurityBean getPage() { + return page; } - public void setShGroups(Set shGroups) { - this.shGroups = shGroups; + public void setPage(ShPageSecurityBean page) { + this.page = page; } } diff --git a/src/main/java/com/viglet/shiohara/sites/ShSitesContext.java b/src/main/java/com/viglet/shiohara/sites/ShSitesContext.java index 7212d4c2b..684d60d41 100644 --- a/src/main/java/com/viglet/shiohara/sites/ShSitesContext.java +++ b/src/main/java/com/viglet/shiohara/sites/ShSitesContext.java @@ -21,7 +21,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import javax.activation.MimetypesFileTypeMap; import javax.annotation.Resource; @@ -30,25 +32,18 @@ import javax.servlet.http.HttpSession; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.security.core.authority.AuthorityUtils; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; +import com.viglet.shiohara.persistence.model.auth.ShGroup; import com.viglet.shiohara.persistence.model.auth.ShUser; import com.viglet.shiohara.persistence.model.post.ShPost; import com.viglet.shiohara.persistence.model.site.ShSite; @@ -110,11 +105,22 @@ private void sitesFullGeneric(HttpServletRequest request, HttpServletResponse re ShSitesContextURL shSitesContextURL = shSitesContextURLProcess.getContextURL(request, response); String username = (String) session.getAttribute("shUsername"); + String[] groups = (String[]) session.getAttribute("shUserGroups"); if (username == null && shSitesContextURL.getInfo().isPageAllowGuestUser()) showPage = true; - else if (username != null && shSitesContextURL.getInfo().isPageAllowRegisterUser()) - showPage = true; + else if (username != null && shSitesContextURL.getInfo().isPageAllowRegisterUser()) { + String[] pageGroups = shSitesContextURL.getInfo().getShPageGroups(); + + if (pageGroups != null && pageGroups.length > 0) { + if (groups.length > 0) + for (String group : groups) + if (StringUtils.indexOfAny(group, pageGroups) >= 0) + showPage = true; + + } else + showPage = true; + } if (showPage) { if (shSitesContextURL.getInfo().getSiteId() != null) @@ -122,9 +128,17 @@ else if (username != null && shSitesContextURL.getInfo().isPageAllowRegisterUser else response.sendError(HttpServletResponse.SC_NOT_FOUND); } else { + if (username != null) { + if (shSitesContextURL.getInfo().isPageAllowGuestUser()) + response.sendError(HttpServletResponse.SC_NOT_FOUND); + else + response.sendError(HttpServletResponse.SC_FORBIDDEN); + } + else { String callback = this.getCurrentUrlFromRequest(request); session.setAttribute("shLoginCallBack", callback); response.sendRedirect("/login-page"); + } } } @@ -154,7 +168,17 @@ private void sitesLoginPagePost(HttpServletRequest request, HttpServletResponse if (shUser != null && passwordEncoder.matches(password, shUser.getPassword())) { String callback = (String) session.getAttribute("shLoginCallBack"); + + List groupList = new ArrayList<>(); + for (ShGroup group : shUser.getShGroups()) { + groupList.add(group.getName()); + } + + String[] groups = groupList.toArray(new String[groupList.size()]); + session.setAttribute("shUsername", username); + session.setAttribute("shUserGroups", groups); + if (callback != null) response.sendRedirect(callback); else diff --git a/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLInfo.java b/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLInfo.java index 84c45b617..1ad2293f9 100644 --- a/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLInfo.java +++ b/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLInfo.java @@ -35,6 +35,7 @@ public class ShSitesContextURLInfo implements Serializable { private boolean staticFile = false; private boolean pageAllowGuestUser = true; private boolean pageAllowRegisterUser = true; + private String[] shPageGroups = null; public String toString() { return String.format( @@ -138,6 +139,14 @@ public void setPageAllowRegisterUser(boolean pageAllowRegisterUser) { this.pageAllowRegisterUser = pageAllowRegisterUser; } + public String[] getShPageGroups() { + return shPageGroups; + } + + public void setShPageGroups(String[] shPageGroups) { + this.shPageGroups = shPageGroups; + } + public static long getSerialversionuid() { return serialVersionUID; } diff --git a/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcess.java b/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcess.java index c5cc0d5c3..043ccbc47 100644 --- a/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcess.java +++ b/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcess.java @@ -75,11 +75,10 @@ public ShSitesContextURL getContextURL(HttpServletRequest request, HttpServletRe if (request.getParameter("sh-format") != null) { String[] contexts = context.split("/"); contexts[3] = request.getParameter("sh-format"); - context = StringUtils.join(contexts, "/"); - shSitesContextURL.getInfo().setContextURLOriginal(context); - } else { - shSitesContextURL.getInfo().setContextURLOriginal(context); + context = StringUtils.join(contexts, "/"); } + + shSitesContextURL.getInfo().setContextURLOriginal(context); shSitesContextURL.getInfo().setContextURL(shSitesContextURL.getInfo().getContextURLOriginal()); } diff --git a/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcessCache.java b/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcessCache.java index 196c87c3c..d3a42aa49 100644 --- a/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcessCache.java +++ b/src/main/java/com/viglet/shiohara/sites/ShSitesContextURLProcessCache.java @@ -25,10 +25,12 @@ import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; +import com.viglet.shiohara.persistence.model.folder.ShFolder; import com.viglet.shiohara.persistence.model.object.ShObject; import com.viglet.shiohara.persistence.model.post.ShPost; import com.viglet.shiohara.persistence.repository.object.ShObjectRepository; import com.viglet.shiohara.post.type.ShSystemPostType; +import com.viglet.shiohara.utils.ShFolderUtils; @Component public class ShSitesContextURLProcessCache { @@ -37,6 +39,8 @@ public class ShSitesContextURLProcessCache { ShSitesContextURLProcess shSitesContextURLProcess; @Autowired ShObjectRepository shObjectRepository; + @Autowired + ShFolderUtils shFolderUtils; @Cacheable(value = "url", key = "{#shSitesContextURL.getInfo().getContextURL(), #shSitesContextURL.getInfo().getContextURLOriginal()}", sync = true) public ShSitesContextURLInfo detectContextURL(ShSitesContextURL shSitesContextURL) { @@ -63,12 +67,26 @@ public ShSitesContextURLInfo detectContextURL(ShSitesContextURL shSitesContextUR else shSitesContextURLInfo.setStaticFile(false); - shSitesContextURLInfo.setPageAllowGuestUser(shObject.isPageAllowGuestUser()); - shSitesContextURLInfo.setPageAllowRegisterUser(shObject.isPageAllowRegisterUser()); + if (shObject instanceof ShPost && shObject.getFurl().equals("index")) { + ShFolder shFolder = shFolderUtils.getParentFolder(shObject); + shSitesContextURLInfo.setPageAllowGuestUser(shFolder.isPageAllowGuestUser()); + shSitesContextURLInfo.setPageAllowRegisterUser(shFolder.isPageAllowRegisterUser()); + + shSitesContextURLInfo.setShPageGroups(shFolder.getShPageGroups() != null + ? shFolder.getShPageGroups().toArray(new String[shFolder.getShPageGroups().size()]) + : null); + } else { + shSitesContextURLInfo.setPageAllowGuestUser(shObject.isPageAllowGuestUser()); + shSitesContextURLInfo.setPageAllowRegisterUser(shObject.isPageAllowRegisterUser()); + shSitesContextURLInfo.setShPageGroups(shObject.getShPageGroups() != null + ? (String[]) shObject.getShPageGroups().toArray(new String[shObject.getShPageGroups().size()]) + : null); + } } else { shSitesContextURLInfo.setPageAllowGuestUser(true); shSitesContextURLInfo.setPageAllowRegisterUser(false); shSitesContextURLInfo.setStaticFile(false); + shSitesContextURLInfo.setShPageGroups(null); } if (logger.isDebugEnabled()) diff --git a/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheObject.java b/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheObject.java index 07e7c033b..75a73abaa 100644 --- a/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheObject.java +++ b/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheObject.java @@ -17,6 +17,7 @@ import com.viglet.shiohara.persistence.repository.object.ShObjectRepository; import com.viglet.shiohara.persistence.repository.post.ShPostRepository; import com.viglet.shiohara.sites.ShSitesContextURL; +import com.viglet.shiohara.sites.utils.ShSitesObjectUtils; import com.viglet.shiohara.utils.ShFolderUtils; @Component @@ -25,6 +26,8 @@ public class ShCacheObject { @Autowired ShCachePage shCachePage; @Autowired + ShCacheURL shCacheURL; + @Autowired ShCacheObject shCacheObject; @Autowired ShObjectRepository shObjectRepository; @@ -32,6 +35,8 @@ public class ShCacheObject { ShPostRepository shPostRepository; @Autowired ShFolderUtils shFolderUtils; + @Autowired + ShSitesObjectUtils shSitesObjectUtils; @Cacheable(value = "shObject", key = "#id", sync = true) public List cache(String id) { @@ -77,6 +82,23 @@ public void deleteDependency(String id) { if (logger.isDebugEnabled()) logger.debug("Deleting the page with id: " + id + " and URL: " + url); shCachePage.deleteCache(id, url); + + ShObject shObject = shObjectRepository.findById(id).orElse(null); + String contextURL = null; + if (shObject instanceof ShPost && shObject.getFurl().equals("index")) { + ShFolder shFolder = shFolderUtils.getParentFolder(shObject); + contextURL = shSitesObjectUtils.generateObjectLinkById(shFolder.getId()); + } else + contextURL = shSitesObjectUtils.generateObjectLinkById(id); + + /** + * If the URL doesn't end with slash,remove the slash of contextURL + */ + if (!url.endsWith("/")) + contextURL = contextURL.trim().replaceFirst(".$", ""); + + shCacheURL.deleteCache(contextURL, url); + } } diff --git a/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheURL.java b/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheURL.java new file mode 100644 index 000000000..0c86a7029 --- /dev/null +++ b/src/main/java/com/viglet/shiohara/sites/cache/component/ShCacheURL.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016-2019 Alexandre Oliveira + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.viglet.shiohara.sites.cache.component; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.stereotype.Component; + +import com.viglet.shiohara.sites.ShSitesContextURL; + +@Component +public class ShCacheURL { + @SuppressWarnings("unused") + private static final Log logger = LogFactory.getLog(ShCacheURL.class); + + @CacheEvict(value = "url", key = "{#contextURL, #contextURLOriginal}") + public void deleteCache(String contextURL, String contextURLOriginal) { + System.out.println(String.format("deleteURL Cache: %s %s",contextURL,contextURLOriginal)); + } +} diff --git a/src/main/resources/ui/js/src/feature/object/ShObjectPropertiesCtrl.js b/src/main/resources/ui/js/src/feature/object/ShObjectPropertiesCtrl.js index 5ed487707..e7cab3961 100644 --- a/src/main/resources/ui/js/src/feature/object/ShObjectPropertiesCtrl.js +++ b/src/main/resources/ui/js/src/feature/object/ShObjectPropertiesCtrl.js @@ -40,19 +40,28 @@ shioharaApp.controller('ShObjectPropertiesCtrl', [ $uibModalInstance.dismiss('cancel'); }; - $ctrl.addGroups = function () { - shObjectFactory.addGroups($ctrl.shSecurity.shGroups); + $ctrl.addConsoleGroups = function () { + shObjectFactory.addGroups($ctrl.shSecurity.console.shGroups); } - $ctrl.addUsers = function () { - shObjectFactory.addUsers($ctrl.shSecurity.shUsers); + $ctrl.addConsoleUsers = function () { + shObjectFactory.addUsers($ctrl.shSecurity.console.shUsers); } - $ctrl.removeGroup = function (index) { + $ctrl.removeConsoleGroup = function (index) { $ctrl.shSecurity.shGroups.splice(index, 1); } - $ctrl.removeUser = function (index) { + $ctrl.removeConsoleUser = function (index) { $ctrl.shSecurity.shUsers.splice(index, 1); } + + $ctrl.addPageGroups = function () { + shObjectFactory.addGroups($ctrl.shSecurity.page.shGroups); + } + + $ctrl.removePageGroup = function (index) { + $ctrl.shSecurity.page.shGroups.splice(index, 1); + } + }]); diff --git a/src/main/resources/ui/public/error/403.html b/src/main/resources/ui/public/error/403.html new file mode 100644 index 000000000..a8e5ea7c7 --- /dev/null +++ b/src/main/resources/ui/public/error/403.html @@ -0,0 +1,85 @@ + + + + +Forbidden + + + + +
+ +

403

+

+ Forbidden +

+ +

You do not have permission to access this page.

+ +
+ + \ No newline at end of file diff --git a/src/main/resources/ui/public/template/object/object-properties.html b/src/main/resources/ui/public/template/object/object-properties.html index bf97f61d8..045f21ad0 100644 --- a/src/main/resources/ui/public/template/object/object-properties.html +++ b/src/main/resources/ui/public/template/object/object-properties.html @@ -3,20 +3,20 @@