| 1 5 package com.dotmarketing.portal.struts; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 import javax.portlet.ActionRequest; 11 import javax.portlet.ActionResponse; 12 import javax.portlet.PortletConfig; 13 import javax.portlet.RenderRequest; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpSession ; 16 import javax.servlet.jsp.PageContext ; 17 18 import org.apache.commons.beanutils.BeanUtils; 19 import org.apache.struts.action.ActionForm; 20 import org.apache.struts.action.ActionForward; 21 import org.apache.struts.action.ActionMapping; 22 23 import com.dotmarketing.beans.Host; 24 import com.dotmarketing.beans.Identifier; 25 import com.dotmarketing.beans.Inode; 26 import com.dotmarketing.beans.Permission; 27 import com.dotmarketing.beans.WebAsset; 28 import com.dotmarketing.cache.IdentifierCache; 29 import com.dotmarketing.cache.PermissionCache; 30 import com.dotmarketing.cms.factories.PublicCompanyFactory; 31 import com.dotmarketing.db.DotHibernate; 32 import com.dotmarketing.factories.HostFactory; 33 import com.dotmarketing.factories.IdentifierFactory; 34 import com.dotmarketing.factories.InodeFactory; 35 import com.dotmarketing.factories.PermissionFactory; 36 import com.dotmarketing.factories.PublishFactory; 37 import com.dotmarketing.factories.RoleFactory; 38 import com.dotmarketing.factories.WebAssetFactory; 39 import com.dotmarketing.portlets.containers.model.Container; 40 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 41 import com.dotmarketing.portlets.contentlet.model.Contentlet; 42 import com.dotmarketing.portlets.files.model.File; 43 import com.dotmarketing.portlets.folders.model.Folder; 44 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 45 import com.dotmarketing.portlets.links.model.Link; 46 import com.dotmarketing.portlets.structure.model.Structure; 47 import com.dotmarketing.portlets.templates.model.Template; 48 import com.dotmarketing.util.Config; 49 import com.dotmarketing.util.Logger; 50 import com.dotmarketing.util.UtilMethods; 51 import com.dotmarketing.util.WebKeys; 52 import com.liferay.portal.ejb.UserLocalManagerUtil; 53 import com.liferay.portal.language.LanguageUtil; 54 import com.liferay.portal.model.Company; 55 import com.liferay.portal.model.Role; 56 import com.liferay.portal.model.User; 57 import com.liferay.portal.struts.ActionException; 58 import com.liferay.portal.struts.PortletAction; 59 import com.liferay.portal.util.Constants; 60 import com.liferay.portlet.ActionRequestImpl; 61 import com.liferay.util.servlet.SessionMessages; 62 63 67 public class DotPortletAction extends PortletAction { 68 69 80 public void _deleteVersionWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKeyEdit) 81 throws Exception { 82 83 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 85 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 86 87 WebAsset webAsset = (WebAsset) req.getAttribute(webKeyEdit); 88 89 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 91 92 if (WebAssetFactory.deleteAssetVersion(webAsset)) { 94 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".delete"); 95 } else { 96 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".delete.live_or_working"); 98 } 99 100 } 101 102 113 public void _undeleteWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKeyEdit) 114 throws Exception { 115 116 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 118 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 119 120 WebAsset webAsset = (WebAsset) req.getAttribute(webKeyEdit); 121 122 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 124 125 WebAssetFactory.unDeleteAsset(webAsset); 126 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".undelete"); 127 128 } 129 130 141 public void _deleteWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKeyEdit) 142 throws Exception { 143 144 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 146 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 147 148 WebAsset webAsset = (WebAsset) req.getAttribute(webKeyEdit); 149 150 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 152 153 if (WebAssetFactory.deleteAsset(webAsset, user.getUserId())) { 154 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".delete"); 155 } else { 156 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".delete.locked"); 157 } 158 159 160 } 161 162 173 public void _unPublishWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKeyEdit) 174 throws Exception { 175 176 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 178 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 179 180 WebAsset webAsset = (WebAsset) req.getAttribute(webKeyEdit); 181 182 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 184 185 Folder parent = (Folder) InodeFactory.getParentOfClass(webAsset, Folder.class); 186 String userId = user.getUserId(); 188 if (webAsset.getInode() > 0) { 189 if (WebAssetFactory.unPublishAsset(webAsset, userId, parent)) { 191 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".unpublished"); 192 } else { 193 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".unpublish.notlive_or_locked"); 194 } 195 } 196 197 } 198 199 211 public void _getVersionsWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKeyEdit, 212 String webKeyVersions) throws Exception { 213 214 WebAsset webAsset = (WebAsset) req.getAttribute(webKeyEdit); 215 216 if (webAsset.getInode() > 0) { 217 req.setAttribute(webKeyVersions, WebAssetFactory.getAssetVersions(webAsset)); 219 } 220 221 } 222 223 234 public void _unLockWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKey) 235 throws Exception { 236 237 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 239 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 240 241 WebAsset webAsset = (WebAsset) req.getAttribute(webKey); 242 243 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_READ")); 245 246 if (webAsset.getInode() > 0) { 247 WebAssetFactory.unLockAsset(webAsset); 249 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".unlocked"); 250 } 251 252 httpReq.getSession().removeAttribute(WebKeys.CONTENTLET_RELATIONSHIPS_EDIT); 253 254 } 255 256 268 public void _moveWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, Class webAssetClass, 269 String webKey) throws Exception { 270 271 Logger.debug(this, "I'm moving the webasset"); 272 273 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 275 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 276 277 WebAsset webAsset = (WebAsset) req.getAttribute(webKey); 279 Identifier identifier = com.dotmarketing.factories.IdentifierFactory.getParentIdentifier(webAsset); 280 281 WebAsset workingWebAsset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, webAssetClass); 283 WebAsset liveWebAsset = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, webAssetClass); 285 286 String parentInode = req.getParameter("parent"); 288 289 if (parentInode != null && parentInode.length() != 0 && !parentInode.equals("0")) { 290 Folder parent = (Folder) InodeFactory.getInode(parentInode, Folder.class); 292 293 _checkCopyAndMovePermissions(webAsset, parent, user, httpReq, "move"); 295 296 Folder oldParent = (Folder) InodeFactory.getParentOfClass(workingWebAsset, Folder.class); 298 Logger.debug(this, "Old Parent Folder=" + oldParent.getInode()); 299 oldParent.deleteChild(workingWebAsset); 300 if ((liveWebAsset != null) && (liveWebAsset.getInode() > 0)) { 301 oldParent.deleteChild(liveWebAsset); 302 } 303 304 Logger.debug(this, "Parent Folder=" + parent.getInode()); 306 parent.addChild(workingWebAsset); 307 if ((liveWebAsset != null) && (liveWebAsset.getInode() > 0)) { 308 parent.addChild(liveWebAsset); 309 } 310 311 Host newHost = HostFactory.getParentHost(parent); 314 identifier.setHostInode(newHost.getInode()); 315 identifier.setURI(workingWebAsset.getURI(parent)); 316 InodeFactory.saveInode(identifier); 317 318 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".move"); 319 } 320 321 } 322 323 334 public void _publishWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String formWebKey) 335 throws Exception { 336 337 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 339 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 340 341 WebAsset webAsset = (WebAsset) req.getAttribute(formWebKey); 342 Logger.debug(this, "WEB ASSET " + webAsset.getType() + " TO PUBLISH=" + webAsset.getInode()); 343 344 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_PUBLISH")); 346 347 if (webAsset.getInode() > 0) { 348 PublishFactory.publishAsset(webAsset, httpReq); 350 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".published"); 351 } 352 353 } 354 355 367 public void _retrieveWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, Class myClass, 368 String webkey) throws Exception { 369 370 WebAsset webAsset = (WebAsset) InodeFactory.getInode(req.getParameter("inode"), myClass); 371 372 if (!(WebAssetFactory.isAbstractAsset(webAsset))) { 374 if (webAsset.getInode() > 0) 375 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, webAsset); 376 else { 377 Folder parentFolder = (Folder) InodeFactory.getInode(req.getParameter("parent"), Folder.class); 378 379 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, parentFolder); 381 } 382 } else { 383 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, webAsset); 384 } 385 386 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_READ")); 388 389 Logger.debug(this, "webAsset:" + webAsset.toString()); 390 Logger.debug(this, "webAsset:" + webAsset.getInode()); 391 req.setAttribute(webkey, webAsset); 392 393 req.setAttribute(WebKeys.VERSIONS_INODE_EDIT, webAsset); 395 396 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, webAsset); 398 399 } 400 401 416 protected Folder _editWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKey) 417 throws Exception { 418 419 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 421 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 422 423 WebAsset webAsset = (WebAsset) req.getAttribute(webKey); 424 425 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_READ")); 427 428 if (webAsset.getInode() > 0) { 429 430 boolean editAsset = WebAssetFactory.editAsset(webAsset, user.getUserId()); 432 if (!editAsset) { 433 434 try { 435 User userMod = UserLocalManagerUtil.getUserById(webAsset.getModUser()); 436 Company comp = PublicCompanyFactory.getDefaultCompany(); 437 String message = LanguageUtil.get(comp.getCompanyId(), user.getLocale(), "message." + webAsset.getType() + ".edit.locked"); 438 message += " (" + userMod.getEmailAddress() + ")"; 439 SessionMessages.add(httpReq, "custommessage", message); 440 } catch (Exception e) { 441 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".edit.locked"); 442 } 443 444 throw (new ActionException(WebKeys.EDIT_ASSET_EXCEPTION)); 445 } 446 } 447 448 Folder parentFolder = new Folder(); 449 450 if (!(WebAssetFactory.isAbstractAsset(webAsset))) { 451 if (webAsset.getInode() > 0) { 452 parentFolder = (Folder) com.dotmarketing.factories.InodeFactory.getParentOfClass(webAsset, Folder.class); 453 } else { 454 parentFolder = (Folder) com.dotmarketing.factories.InodeFactory.getInode(req.getParameter("parent"), Folder.class); 455 } 456 } 457 458 req.setAttribute(webKey, webAsset); 459 460 BeanUtils.copyProperties(form, req.getAttribute(webKey)); 461 462 return parentFolder; 463 } 464 465 480 protected WebAsset _getVersionBackWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, 481 Class className, String webKey) throws Exception { 482 483 WebAsset version = (WebAsset) InodeFactory.getInode(req.getParameter("inode_version"), className); 484 485 _checkUserPermissions(version, user, Config.getIntProperty("PERMISSION_WRITE")); 487 488 WebAsset workingAsset = (WebAsset) WebAssetFactory.getBackAssetVersion(version); 489 490 version = (WebAsset) InodeFactory.getInode(version.getInode(), version.getClass()); 491 492 req.setAttribute(webKey, version); 493 494 if (workingAsset instanceof Contentlet) { 495 ContentletFactory.reIndexContentlet((Contentlet) workingAsset); 497 } 498 499 return workingAsset; 500 501 } 502 503 518 protected void _viewWebAssets(RenderRequest req, User user, Class className, String tableName, String countWebKey, String viewWebKey, 519 String queryWebKey, String showDeletedWebKey) throws Exception { 520 521 com.liferay.portlet.RenderRequestImpl reqImpl = (com.liferay.portlet.RenderRequestImpl) req; 522 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 523 HttpSession session = httpReq.getSession(); 525 526 String hostId = (String ) session.getAttribute(WebKeys.SEARCH_HOST_ID); 527 if (req.getParameter("host_id") != null) 528 hostId = req.getParameter("host_id"); 529 if (hostId != null) 530 session.setAttribute(WebKeys.SEARCH_HOST_ID, hostId); 531 532 String query = req.getParameter("query"); 533 String resetQuery = req.getParameter("resetQuery"); 534 535 List l = new java.util.ArrayList (); 536 537 try { 538 Role[] roles = (Role[]) RoleFactory.getAllRolesForUser(user.getUserId()).toArray(new Role[0]); 540 Logger.debug(this, "Inside _viewWebAssets Roles=" + roles.length); 541 542 String condition = ""; 543 String showDeleted = req.getParameter("showDeleted"); 544 545 if ((showDeleted == null) && (resetQuery == null)) { 546 showDeleted = (String ) session.getAttribute(showDeletedWebKey); 547 } 548 if ((showDeleted != null) && (showDeleted.equals("true"))) { 549 condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue(); 550 session.setAttribute(showDeletedWebKey, "true"); 551 } else { 552 condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" 553 + com.dotmarketing.db.DbConnectionFactory.getDBFalse(); 554 session.setAttribute(showDeletedWebKey, "false"); 555 } 556 String orderby = req.getParameter("orderby"); 557 if (!UtilMethods.isSet(orderby)) { 558 orderby = "title"; 559 } 560 561 int pageNumber = 1; 562 563 if (UtilMethods.isSet(req.getParameter("pageNumber"))) { 564 pageNumber = Integer.parseInt(req.getParameter("pageNumber")); 565 } 566 567 int limit = com.dotmarketing.util.Config.getIntProperty("PER_PAGE"); 568 569 int offset = (pageNumber - 1) * limit; 570 571 if ((query == null) && (resetQuery == null)) { 572 query = (String ) session.getAttribute(queryWebKey); 573 } 574 session.setAttribute(queryWebKey, query); 575 576 int count = 0; 577 578 if (((query != null) && (query.length() != 0)) || (hostId != null && !hostId.equals("0"))) { 580 if (query == null) 581 query = ""; 582 condition += " and (lower(" + tableName + "_condition.title) " + "like '%" + query.toLowerCase() + "%' "; 583 if ("file_asset".equals(tableName)) { 584 condition += " or lower(" + tableName + "_condition.file_name) like '%" + query.toLowerCase() + "%' "; 585 } 586 condition += " or lower(" + tableName + "_condition.friendly_name) like '%" + query.toLowerCase() + "%')"; 587 Logger.debug(this, "Getting Assets based on condition=" + condition); 588 limit = 0; 589 if (hostId == null || hostId.equals("0")) { 590 l = WebAssetFactory.getAssetsAndPermissionsPerRoleAndCondition(roles, condition, limit, offset, orderby, className, tableName); 591 count = WebAssetFactory.getCountAssetsPerConditionWithPermission(condition, className, roles); 592 } else { 593 long hostIdL = Long.parseLong(hostId); 594 l = WebAssetFactory.getAssetsAndPermissionsPerRoleAndCondition(hostIdL, roles, condition, limit, offset, orderby, className, 595 tableName); 596 count = WebAssetFactory.getCountAssetsPerConditionWithPermission(hostIdL, condition, className, roles); 597 } 598 } else { 599 Logger.debug(this, "Getting all Assets=" + condition); 600 l = WebAssetFactory.getAssetsAndPermissionsPerRoleAndCondition(roles, condition, limit, offset, orderby, className, tableName); 601 count = WebAssetFactory.getCountAssetsPerConditionWithPermission(condition, className, roles); 602 603 } 604 605 req.setAttribute(countWebKey, new Integer (count)); 606 req.setAttribute(viewWebKey, l); 607 } catch (Exception e) { 608 req.setAttribute(viewWebKey, l); 609 Logger.error(this, "Exception e =" + e.getMessage(), e); 610 throw new Exception (e.getMessage()); 611 } 612 613 } 614 615 622 protected void _handleException(Exception e, ActionRequest req) { 623 Logger.warn(this, e.toString(), e); 624 DotHibernate.rollbackTransaction(); 625 req.setAttribute(PageContext.EXCEPTION, e); 626 setForward(req, Constants.COMMON_ERROR); 627 } 628 629 636 protected void _handleException(Exception e, ActionRequest req, boolean showStackTrace) { 637 if (!showStackTrace) 638 Logger.info(this, e.toString()); 639 else 640 Logger.warn(this, e.toString(), e); 641 DotHibernate.rollbackTransaction(); 642 req.setAttribute(PageContext.EXCEPTION, e); 643 setForward(req, Constants.COMMON_ERROR); 644 } 645 646 653 protected ActionForward _handleException(Exception e, RenderRequest req, ActionMapping mapping) { 654 Logger.warn(this, e.toString(), e); 655 DotHibernate.rollbackTransaction(); 656 req.setAttribute(PageContext.EXCEPTION, e); 657 return mapping.findForward(Constants.COMMON_ERROR); 658 } 659 660 669 protected void _sendToReferral(ActionRequest req, ActionResponse res, String referer) throws Exception { 670 671 String redirect = req.getParameter("redirect"); 672 673 if (UtilMethods.isSet(referer)) { 674 Logger.debug(this, "\n\nGoing to redirect to referer: " + referer); 675 res.sendRedirect(referer); 676 } else if (UtilMethods.isSet(redirect)) { 677 Logger.debug(this, "\n\nGoing to redirect to redirect : " + redirect); 678 res.sendRedirect(redirect); 679 } 680 Logger.debug(this, "End of _sendToReferral"); 681 } 682 683 689 protected User _getUser(ActionRequest req) { 690 691 User user = null; 693 try { 694 user = com.liferay.portal.util.PortalUtil.getUser(req); 695 } catch (Exception e) { 696 _handleException(e, req); 697 } 698 return user; 699 700 } 701 702 708 protected User _getUser(RenderRequest req) { 709 710 User user = null; 712 try { 713 user = com.liferay.portal.util.PortalUtil.getUser(req); 714 } catch (Exception e) { 715 req.setAttribute(PageContext.EXCEPTION, e); 716 return null; 717 } 718 return user; 719 720 } 721 722 public String getForward(ActionRequest req) { 725 return getForward(req, null); 726 } 727 728 public String getForward(ActionRequest req, String defaultValue) { 729 String forward = (String ) req.getAttribute(com.liferay.portal.util.WebKeys.PORTLET_STRUTS_FORWARD); 730 731 if (forward == null) { 732 return defaultValue; 733 } else { 734 return forward; 735 } 736 } 737 738 public void _lockWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user, String webKey) 739 throws Exception { 740 741 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 743 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 744 745 WebAsset webAsset = (WebAsset) req.getAttribute(webKey); 746 747 if (webAsset.getInode() > 0) { 748 WebAssetFactory.editAsset(webAsset, user.getUserId()); 750 SessionMessages.add(httpReq, "message", "message." + webAsset.getType() + ".locked"); 751 } 752 753 if (webAsset instanceof Contentlet) { 754 ContentletFactory.reIndexContentlet((Contentlet) webAsset); 756 } 757 758 } 759 760 public void _applyPermissions(ActionRequest req, String webKey) { 761 Inode inode = (Inode) req.getAttribute(webKey); 762 _applyPermissions(req, inode); 763 } 764 765 @SuppressWarnings ("unchecked") 766 public void _applyPermissions(ActionRequest req, Inode inode) { 767 String permission_not_changed = req.getParameter(WebKeys.PERMISSION_NOT_CHANGED); 769 String permission_is_new = req.getParameter(WebKeys.PERMISSION_IS_NEW); 770 if ((UtilMethods.isSet(permission_not_changed) && Boolean.valueOf(permission_not_changed).booleanValue()) 773 || (UtilMethods.isSet(permission_is_new) && Boolean.valueOf(permission_is_new).booleanValue())) { 774 775 Inode origInode = inode; 776 777 if(inode instanceof WebAsset){ 778 Identifier id = new Identifier(); 779 if (inode.getIdentifier() > 0) { 780 id.setInode(inode.getIdentifier()); 781 } else { 782 id.setInode(IdentifierCache.getIdentifierByInodeFromCache(inode.getInode()).getInode()); 783 } 784 785 inode = id; 786 787 } 788 789 Permission permission = null; 790 791 java.util.List <Permission> currentPerms = PermissionFactory.getPermissions(inode); 792 for(Permission perm : currentPerms){ 793 PermissionFactory.deletePermission(perm); 794 } 795 796 String [] readPermissions = req.getParameterValues("read"); 798 if (readPermissions != null) { 799 for (int k = 0; k < readPermissions.length; k++) { 800 801 Logger.debug(this, "_applyPermissions: Saving Read Permission for=" + readPermissions[k]); 802 803 permission = new Permission(inode.getInode(), Long.parseLong(readPermissions[k]), Config.getIntProperty("PERMISSION_READ")); 804 PermissionFactory.savePermission(permission); 805 } 806 } 807 808 String [] writePermissions = req.getParameterValues("write"); 810 if (writePermissions != null) { 811 for (int k = 0; k < writePermissions.length; k++) { 812 813 Logger.debug(this, "_applyPermissions: Saving Write Permission for=" + writePermissions[k]); 814 815 permission = new Permission(inode.getInode(), Long.parseLong(writePermissions[k]), Config.getIntProperty("PERMISSION_WRITE")); 816 PermissionFactory.savePermission(permission); 817 } 818 } 819 820 String [] publishPermissions = req.getParameterValues("publish"); 822 if (publishPermissions != null) { 823 for (int k = 0; k < publishPermissions.length; k++) { 824 825 Logger.debug(this, "_applyPermissions: Saving Publish Permission for=" + publishPermissions[k]); 826 827 permission = new Permission(inode.getInode(), Long.parseLong(publishPermissions[k]), Config.getIntProperty("PERMISSION_PUBLISH")); 828 PermissionFactory.savePermission(permission); 829 } 830 } 831 832 if (origInode instanceof Folder) { 834 Folder f = (Folder) origInode; 835 PermissionCache.addToPermissionCache(f); 836 } else if (origInode instanceof Host) { 837 Host host = (Host) origInode; 838 PermissionCache.addToPermissionCache(host); 839 } else if (origInode instanceof WebAsset) { 840 WebAsset asset = (WebAsset) origInode; 841 PermissionCache.addToPermissionCache(asset); 842 } 843 844 if(origInode instanceof Contentlet){ 845 Contentlet content = (Contentlet) origInode; 846 ContentletFactory.reIndexContentlet(content); 847 } 848 849 if ((req.getParameter("applyChildren") != null) && (req.getParameter("applyChildren").equals("on")) 850 && (inode instanceof Folder || inode instanceof Host || inode instanceof Structure)) { 851 852 Logger.debug(this, "_applyPermissions: applying permissions to children"); 853 854 855 ApplyChildrenPermissionThread thread = new DotPortletAction().new ApplyChildrenPermissionThread(req,inode); 856 thread.start(); 857 858 } 859 } 860 } 861 862 protected void _checkUserPermissions(Inode webAsset, User user, int permission) throws ActionException { 863 if (webAsset.getInode() == 0) 865 return; 866 if (!PermissionFactory.doesUserHavePermission(webAsset, permission, user)) { 867 Logger.warn(this, "_checkUserPermissions: user does not have permissions ( " + permission + " ) over this asset: " + webAsset); 868 throw new ActionException(WebKeys.USER_PERMISSIONS_EXCEPTION); 869 } 870 } 871 872 protected void _checkPermissions(Inode webAsset, Folder parentFolder, User user, HttpServletRequest httpReq) throws Exception { 873 String subcmd = httpReq.getParameter("subcmd"); 874 boolean publish = (subcmd != null) && subcmd.equals(com.dotmarketing.util.Constants.PUBLISH); 875 876 try { 877 if (webAsset.getInode() > 0) { 878 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 879 if (publish) 880 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_PUBLISH")); 881 } else { 882 _checkUserPermissions(parentFolder, user, Config.getIntProperty("PERMISSION_WRITE")); 883 if (publish) 884 _checkUserPermissions(parentFolder, user, Config.getIntProperty("PERMISSION_PUBLISH")); 885 } 886 } catch (Exception ae) { 887 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 888 if (publish) 889 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save.and.publish"); 890 else 891 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save"); 892 } 893 throw ae; 894 } 895 896 } 897 898 protected void _checkPermissions(Inode webAsset, User user, HttpServletRequest httpReq) throws Exception { 899 String subcmd = httpReq.getParameter("subcmd"); 900 boolean publish = (subcmd != null) && subcmd.equals(com.dotmarketing.util.Constants.PUBLISH); 901 902 try { 903 if (webAsset.getInode() > 0) { 904 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 905 if (publish) 906 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_PUBLISH")); 907 } 908 } catch (Exception ae) { 909 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 910 if (publish) 911 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save.and.publish"); 912 else 913 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save"); 914 } 915 throw ae; 916 } 917 918 } 919 920 protected void _checkWritePermissions(Inode inode, User user, HttpServletRequest httpReq) throws Exception { 921 try { 922 _checkUserPermissions(inode, user, Config.getIntProperty("PERMISSION_WRITE")); 923 } catch (Exception ae) { 924 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 925 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save"); 926 } 927 throw ae; 928 } 929 930 } 931 932 protected void _checkDeletePermissions(Structure structure, User user, HttpServletRequest httpReq) throws Exception { 933 try { 934 _checkUserPermissions(structure, user, Config.getIntProperty("PERMISSION_WRITE")); 935 } catch (Exception ae) { 936 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 937 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.delete"); 938 } 939 throw ae; 940 } 941 942 } 943 944 protected void _checkCopyAndMovePermissions(Inode webAsset, Folder parentFolder, User user, HttpServletRequest httpReq, String action) 945 throws Exception { 946 947 try { 948 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 949 _checkUserPermissions(parentFolder, user, Config.getIntProperty("PERMISSION_WRITE")); 950 } catch (Exception ae) { 951 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 952 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to." + action); 953 } 954 throw ae; 955 } 956 } 957 958 protected void _checkCopyAndMovePermissions(Inode webAsset, User user, HttpServletRequest httpReq, String action) throws Exception { 959 960 try { 961 _checkUserPermissions(webAsset, user, Config.getIntProperty("PERMISSION_WRITE")); 962 } catch (Exception ae) { 963 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 964 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to." + action); 965 } 966 throw ae; 967 } 968 } 969 970 protected void _copyPermissions(Inode from, Inode to) throws ActionException { 971 PermissionFactory.copyPermissions(from, to); 972 } 973 974 private class ApplyChildrenPermissionThread extends Thread { 975 976 private Inode parentInode = null; 977 private ActionRequest req = null; 978 979 985 public ApplyChildrenPermissionThread(ActionRequest req, Inode inode) { 986 super("Apply Children Permission Thread"); 987 this.parentInode = inode; 988 this.req = req; 989 } 990 991 public void run() { 992 if (req == null || parentInode == null || parentInode.getInode() == 0) 993 return; 994 995 int counterTransaction = 0; 996 int commitEvery = com.dotmarketing.util.Constants.APPLY_CHILD_PERMISSION_THREAD_COMMIT; 997 998 DotHibernate.startTransaction(); 999 1000 List <Inode> inodeList = getChildren(parentInode); 1001 1002 for(Inode inode : inodeList){ 1003 try { 1004 _applyPermissionsToChild( req, inode); 1006 1007 if(counterTransaction >= commitEvery){ 1009 counterTransaction = 0; 1010 DotHibernate.commitTransaction(); 1011 DotHibernate.startTransaction(); 1012 } 1013 1014 counterTransaction++; 1015 Thread.sleep(com.dotmarketing.util.Constants.APPLY_CHILD_PERMISSION_THREAD_SLEEP); 1017 1018 }catch(Exception e){ 1019 Logger.debug(ApplyChildrenPermissionThread.class,e.getMessage()); 1020 } 1021 } 1022 1023 DotHibernate.commitTransaction(); 1024 1025 } 1026 1027 1032 public final void _applyPermissionsToChild(ActionRequest req, Inode inode) { 1033 String permission_not_changed = req.getParameter(WebKeys.PERMISSION_NOT_CHANGED); 1035 String permission_is_new = req.getParameter(WebKeys.PERMISSION_IS_NEW); 1036 if ((UtilMethods.isSet(permission_not_changed) && Boolean.valueOf(permission_not_changed).booleanValue()) 1039 || (UtilMethods.isSet(permission_is_new) && Boolean.valueOf(permission_is_new).booleanValue())) { 1040 Inode origInode = inode; 1041 1042 if(inode instanceof WebAsset){ 1043 Identifier id = new Identifier(); 1044 if (inode.getIdentifier() > 0) { 1045 id.setInode(inode.getIdentifier()); 1046 } else { 1047 id.setInode(IdentifierCache.getIdentifierByInodeFromCache(inode.getInode()).getInode()); 1048 } 1049 1050 inode = id; 1051 1052 } 1053 1054 Permission permission = null; 1055 1056 java.util.List <Permission> currentPerms = PermissionFactory.getPermissions(inode); 1057 for(Permission perm : currentPerms){ 1058 PermissionFactory.deletePermission(perm); 1059 } 1060 1061 String [] readPermissions = req.getParameterValues("read"); 1063 if (readPermissions != null) { 1064 for (int k = 0; k < readPermissions.length; k++) { 1065 1066 Logger.debug(this, "_applyChildPermissions: Saving Read Permission for=" + readPermissions[k]); 1067 1068 permission = new Permission(inode.getInode(), Long.parseLong(readPermissions[k]), Config.getIntProperty("PERMISSION_READ")); 1069 PermissionFactory.savePermission(permission); 1070 } 1071 } 1072 1073 String [] writePermissions = req.getParameterValues("write"); 1075 if (writePermissions != null) { 1076 for (int k = 0; k < writePermissions.length; k++) { 1077 1078 Logger.debug(this, "_applyChildPermissions: Saving Write Permission for=" + writePermissions[k]); 1079 1080 permission = new Permission(inode.getInode(), Long.parseLong(writePermissions[k]), Config.getIntProperty("PERMISSION_WRITE")); 1081 PermissionFactory.savePermission(permission); 1082 } 1083 } 1084 1085 String [] publishPermissions = req.getParameterValues("publish"); 1087 if (publishPermissions != null) { 1088 for (int k = 0; k < publishPermissions.length; k++) { 1089 1090 Logger.debug(this, "_applyChildPermissions: Saving Publish Permission for=" + publishPermissions[k]); 1091 1092 permission = new Permission(inode.getInode(), Long.parseLong(publishPermissions[k]), Config.getIntProperty("PERMISSION_PUBLISH")); 1093 PermissionFactory.savePermission(permission); 1094 } 1095 } 1096 1097 if (origInode instanceof Folder) { 1099 Folder f = (Folder) origInode; 1100 PermissionCache.addToPermissionCache(f); 1101 } else if (origInode instanceof Host) { 1102 Host host = (Host) origInode; 1103 PermissionCache.addToPermissionCache(host); 1104 } else if (origInode instanceof WebAsset) { 1105 WebAsset asset = (WebAsset) origInode; 1106 PermissionCache.addToPermissionCache(asset); 1107 } 1108 1109 if(origInode instanceof Contentlet){ 1110 Contentlet content = (Contentlet) origInode; 1111 ContentletFactory.reIndexContentlet(content); 1112 } 1113 1114 if ((req.getParameter("applyChildren") != null) && (req.getParameter("applyChildren").equals("on")) 1115 && (inode instanceof Folder || inode instanceof Host || inode instanceof Structure)) { 1116 1117 Logger.debug(this, "_applyChildPermissions: applying permissions to children"); 1118 1119 List <Inode> children = getChildren (inode); 1120 1121 for(Object child: children){ 1122 _applyPermissionsToChild(req, (Inode) child); 1123 } 1124 1125 } 1126 } 1127 } 1128 1129 @SuppressWarnings ("unchecked") 1130 private List <Inode> getChildren (Inode parent) { 1131 1132 java.util.List <Inode> children = new ArrayList <Inode>(); 1133 1134 if (parent instanceof Folder || parent instanceof Host) { 1135 children.addAll(InodeFactory.getChildrenClass(parent, Folder.class)); 1136 children.addAll(InodeFactory.getChildrenClass(parent, Contentlet.class)); 1137 children.addAll(InodeFactory.getChildrenClass(parent, Container.class)); 1138 children.addAll(InodeFactory.getChildrenClass(parent, Template.class)); 1139 children.addAll(InodeFactory.getChildrenClass(parent, HTMLPage.class)); 1140 children.addAll(InodeFactory.getChildrenClass(parent, Link.class)); 1141 children.addAll(InodeFactory.getChildrenClass(parent, File.class)); 1142 } else if (parent instanceof Structure) { 1143 children.addAll(InodeFactory.getInodesOfClassByCondition(Contentlet.class, "structure_inode = " + parent.getInode())); 1144 } 1145 1146 return children; 1147 1148 } 1149 1150 } 1151 1152} | Popular Tags |