0001: /*
0002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
0003: *
0004: * This file is part of TransferCM.
0005: *
0006: * TransferCM is free software; you can redistribute it and/or modify it under the
0007: * terms of the GNU General Public License as published by the Free Software
0008: * Foundation; either version 2 of the License, or (at your option) any later
0009: * version.
0010: *
0011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
0012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
0013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
0014: * details.
0015: *
0016: * You should have received a copy of the GNU General Public License along with
0017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
0018: * Fifth Floor, Boston, MA 02110-1301 USA
0019: */
0020:
0021: package com.methodhead.transfer;
0022:
0023: import java.io.IOException;
0024: import java.util.List;
0025: import java.util.ArrayList;
0026: import java.util.Iterator;
0027: import java.net.MalformedURLException;
0028: import com.methodhead.auth.AuthUser;
0029: import com.methodhead.property.PropertyPolicy;
0030: import com.methodhead.event.EventPolicy;
0031: import com.methodhead.sitecontext.SiteContext;
0032: import com.methodhead.sitecontext.SiteContextPolicy;
0033: import com.methodhead.aikp.IntKey;
0034: import javax.servlet.http.HttpServletRequest;
0035: import javax.servlet.http.Cookie;
0036: import javax.servlet.ServletContext;
0037: import java.io.File;
0038: import com.methodhead.util.OperationContext;
0039: import org.apache.struts.util.MessageResources;
0040: import org.apache.struts.Globals;
0041: import org.apache.struts.upload.FormFile;
0042: import com.methodhead.tree.FoldingTreeNode;
0043: import com.methodhead.shim.ShimPolicy;
0044: import com.methodhead.shim.Page;
0045: import com.methodhead.shim.Template;
0046: import com.methodhead.shim.TextModule;
0047: import com.methodhead.shim.NavModule;
0048: import com.methodhead.shim.IncludeModule;
0049: import com.methodhead.shim.Thumbnailer;
0050: import com.methodhead.shim.ShimGlobals;
0051: import com.methodhead.shim.SiteMap;
0052: import com.methodhead.shim.Link;
0053: import com.methodhead.shim.ShimUtils;
0054: import com.methodhead.res.ResPolicy;
0055: import com.methodhead.res.FileManager;
0056: import com.methodhead.res.FileTree;
0057: import com.methodhead.res.ResException;
0058: import com.methodhead.util.ServletUtils;
0059: import com.methodhead.reg.RegPolicy;
0060: import com.methodhead.auth.AuthPolicy;
0061: import com.methodhead.reg.User;
0062: import com.methodhead.mail.Mail;
0063: import com.methodhead.property.Property;
0064: import java.util.Enumeration;
0065: import java.util.zip.ZipFile;
0066: import java.util.zip.ZipEntry;
0067: import java.util.zip.ZipException;
0068: import org.apache.struts.util.LabelValueBean;
0069: import org.apache.struts.util.RequestUtils;
0070: import org.apache.struts.action.ActionForm;
0071: import org.apache.commons.lang.exception.ExceptionUtils;
0072: import org.apache.commons.lang.StringUtils;
0073:
0074: public class DefaultTransferPolicy implements AuthPolicy,
0075: PropertyPolicy, EventPolicy, ShimPolicy, SiteContextPolicy,
0076: ResPolicy, RegPolicy, TransferPolicy {
0077:
0078: // constructors /////////////////////////////////////////////////////////////
0079:
0080: // constants ////////////////////////////////////////////////////////////////
0081:
0082: public static final String ROLE_SYSADMIN = "ROLE_SYSADMIN";
0083: public static final String ROLE_SITEADMIN = "ROLE_SITEADMIN";
0084: public static final String ROLE_WEBMASTER = "ROLE_WEBMASTER";
0085:
0086: public static final String PROPERTY_MAILFROM = "com.methodhead.transfer.MailFrom";
0087:
0088: // classes //////////////////////////////////////////////////////////////////
0089:
0090: // methods //////////////////////////////////////////////////////////////////
0091:
0092: /**
0093: * Returns true if <code>user</code> is at least a web master for the current
0094: * site, or if <code>user</code> is a system administrator for the default
0095: * context.
0096: */
0097: private boolean isAtLeastWebmaster(OperationContext op, User user) {
0098:
0099: //
0100: // site admin or webmaster for current site?
0101: //
0102: SiteContext siteContext = SiteContext.getContext(op.request);
0103:
0104: if (siteContext != null) {
0105: if (user.hasRole(siteContext, ROLE_SITEADMIN)
0106: || user.hasRole(siteContext, ROLE_WEBMASTER)) {
0107:
0108: return true;
0109: }
0110: }
0111:
0112: //
0113: // sys admin for default context?
0114: //
0115: siteContext = SiteContext.getDefaultContext();
0116:
0117: if (user.hasRole(siteContext, ROLE_SYSADMIN)) {
0118: return true;
0119: }
0120:
0121: return false;
0122: }
0123:
0124: /**
0125: * Returns true if <code>user</code> is at least a site admin for the current
0126: * site, or if <code>user</code> is a system administrator for the default
0127: * context.
0128: */
0129: private boolean isAtLeastSiteAdmin(OperationContext op, User user) {
0130:
0131: //
0132: // site admin for current site?
0133: //
0134: SiteContext siteContext = SiteContext.getContext(op.request);
0135:
0136: if (siteContext != null) {
0137: if (user.hasRole(siteContext, ROLE_SITEADMIN)) {
0138:
0139: return true;
0140: }
0141: }
0142:
0143: //
0144: // sys admin for default context?
0145: //
0146: siteContext = SiteContext.getDefaultContext();
0147:
0148: if (user.hasRole(siteContext, ROLE_SYSADMIN)) {
0149: return true;
0150: }
0151:
0152: return false;
0153: }
0154:
0155: /**
0156: * Returns true if <code>user</code> is at least a system administrator for
0157: * the default context.
0158: */
0159: private boolean isAtLeastSysAdmin(OperationContext op, User user) {
0160:
0161: //
0162: // sys admin for default context?
0163: //
0164: SiteContext siteContext = SiteContext.getDefaultContext();
0165:
0166: if (user.hasRole(siteContext, ROLE_SYSADMIN)) {
0167: return true;
0168: }
0169:
0170: return false;
0171: }
0172:
0173: // mhf policies /////////////////////////////////////////////////////////////
0174:
0175: public AuthUser newUser() {
0176: return new User();
0177: }
0178:
0179: public boolean autoLogin(AuthUser authUser,
0180: HttpServletRequest request, ActionForm form) {
0181:
0182: User user = (User) authUser;
0183:
0184: //
0185: // is there a siteid cookie?
0186: //
0187: Cookie[] cookies = request.getCookies();
0188:
0189: if (cookies != null) {
0190: for (int i = 0; i < cookies.length; i++) {
0191: if (cookies[i].getName().equals("siteid")) {
0192:
0193: //
0194: // attempt to load a site context
0195: //
0196: SiteContext context = new SiteContext();
0197: context.load(new IntKey(cookies[i].getValue()));
0198:
0199: //
0200: // make sure the user is either a sysadmin or associated with this
0201: // context
0202: //
0203: if (!user.hasRole(SiteContext.getDefaultContext(),
0204: DefaultTransferPolicy.ROLE_SYSADMIN)
0205: && !user
0206: .hasRole(
0207: context,
0208: DefaultTransferPolicy.ROLE_SYSADMIN)
0209: && !user
0210: .hasRole(
0211: context,
0212: DefaultTransferPolicy.ROLE_WEBMASTER)) {
0213:
0214: return false;
0215: }
0216:
0217: //
0218: // set up the session
0219: //
0220: ShimUtils.setUpShimSession(request, context);
0221:
0222: return true;
0223: }
0224: }
0225: }
0226:
0227: //
0228: // no siteid cookie; make sure the user is a SYSADMIN
0229: //
0230: return false;
0231: }
0232:
0233: public String isListEventsAuthorized(OperationContext op) {
0234:
0235: User user = (User) op.user;
0236:
0237: if (isAtLeastWebmaster(op, user)) {
0238: return null;
0239: }
0240:
0241: return "transfer.auth.unauthorizedHome";
0242: }
0243:
0244: public String isListPropertiesAuthorized(OperationContext op) {
0245:
0246: User user = (User) op.user;
0247:
0248: if (isAtLeastWebmaster(op, user)) {
0249: return null;
0250: }
0251:
0252: return "transfer.auth.unauthorizedHome";
0253: }
0254:
0255: public String isSetPropertyFormAuthorized(OperationContext op) {
0256:
0257: User user = (User) op.user;
0258:
0259: if (isAtLeastWebmaster(op, user)) {
0260: return null;
0261: }
0262:
0263: return "transfer.auth.unauthorizedHome";
0264: }
0265:
0266: public String isSetPropertyAuthorized(OperationContext op) {
0267:
0268: User user = (User) op.user;
0269:
0270: if (isAtLeastWebmaster(op, user)) {
0271: return null;
0272: }
0273:
0274: return "transfer.auth.unauthorizedHome";
0275: }
0276:
0277: // shim policy //////////////////////////////////////////////////////////////
0278:
0279: public Page newPage() {
0280: return new Page();
0281: }
0282:
0283: public Template newTemplate() {
0284: return new Template();
0285: }
0286:
0287: public TextModule newTextModule() {
0288: return new TextModule();
0289: }
0290:
0291: public NavModule newNavModule() {
0292: return new NavModule();
0293: }
0294:
0295: public IncludeModule newIncludeModule() {
0296: return new IncludeModule();
0297: }
0298:
0299: public Thumbnailer newThumbnailer() {
0300: return new Thumbnailer();
0301: }
0302:
0303: public boolean isMappingAuthorized(AuthUser user, String path) {
0304: return true;
0305: }
0306:
0307: public String isHomeAuthorized(OperationContext op) {
0308:
0309: User user = (User) op.user;
0310:
0311: if (isAtLeastWebmaster(op, user)) {
0312: return null;
0313: }
0314:
0315: return "transfer.auth.unauthorizedHome";
0316: }
0317:
0318: public String isSiteMapAuthorized(OperationContext op) {
0319:
0320: User user = (User) op.user;
0321:
0322: if (isAtLeastWebmaster(op, user)) {
0323: return null;
0324: }
0325:
0326: return "transfer.auth.unauthorizedSiteMap";
0327: }
0328:
0329: public String isLinkAuthorized(OperationContext op) {
0330:
0331: User user = (User) op.user;
0332:
0333: if (isAtLeastWebmaster(op, user)) {
0334: return null;
0335: }
0336:
0337: return "transfer.auth.unauthorizedSiteMap"; // message applies to this action too
0338: }
0339:
0340: public String isHtmlFragmentDeleteAuthorized(OperationContext op) {
0341:
0342: User user = (User) op.user;
0343:
0344: if (isAtLeastWebmaster(op, user)) {
0345: return null;
0346: }
0347:
0348: return "shim.auth.unauthorizedHtmlFragmentDelete";
0349: }
0350:
0351: public String isHtmlFragmentListAuthorized(OperationContext op) {
0352:
0353: User user = (User) op.user;
0354:
0355: if (isAtLeastWebmaster(op, user)) {
0356: return null;
0357: }
0358:
0359: return "shim.auth.unauthorizedHtmlFragmentList";
0360: }
0361:
0362: public String isHtmlFragmentSaveAuthorized(OperationContext op) {
0363:
0364: User user = (User) op.user;
0365:
0366: if (isAtLeastWebmaster(op, user)) {
0367: return null;
0368: }
0369:
0370: return "shim.auth.unauthorizedHtmlFragmentSave";
0371: }
0372:
0373: public String isHtmlFragmentSaveNewAuthorized(OperationContext op) {
0374:
0375: User user = (User) op.user;
0376:
0377: if (isAtLeastWebmaster(op, user)) {
0378: return null;
0379: }
0380:
0381: return "shim.auth.unauthorizedHtmlFragmentSaveNew";
0382: }
0383:
0384: public String isHtmlFragmentNewAuthorized(OperationContext op) {
0385:
0386: User user = (User) op.user;
0387:
0388: if (isAtLeastWebmaster(op, user)) {
0389: return null;
0390: }
0391:
0392: return "shim.auth.unauthorizedHtmlFragmentNew";
0393: }
0394:
0395: public String isHtmlFragmentEditAuthorized(OperationContext op) {
0396:
0397: User user = (User) op.user;
0398:
0399: if (isAtLeastWebmaster(op, user)) {
0400: return null;
0401: }
0402:
0403: return "shim.auth.unauthorizedHtmlFragmentEdit";
0404: }
0405:
0406: public String isEditorListImagesAuthorized(OperationContext op) {
0407:
0408: User user = (User) op.user;
0409:
0410: if (isAtLeastWebmaster(op, user)) {
0411: return null;
0412: }
0413:
0414: return "shim.auth.unauthorizedEditorListImages";
0415: }
0416:
0417: public String isEditorPickImageAuthorized(OperationContext op) {
0418:
0419: User user = (User) op.user;
0420:
0421: if (isAtLeastWebmaster(op, user)) {
0422: return null;
0423: }
0424:
0425: return "shim.auth.unauthorizedEditorPickImage";
0426: }
0427:
0428: public String isEditorDisplayFilesAuthorized(OperationContext op) {
0429:
0430: User user = (User) op.user;
0431:
0432: if (isAtLeastWebmaster(op, user)) {
0433: return null;
0434: }
0435:
0436: return "shim.auth.unauthorizedEditorListFiles";
0437: }
0438:
0439: public String isEditorPickFileAuthorized(OperationContext op) {
0440:
0441: User user = (User) op.user;
0442:
0443: if (isAtLeastWebmaster(op, user)) {
0444: return null;
0445: }
0446:
0447: return "shim.auth.unauthorizedEditorPickFile";
0448: }
0449:
0450: public String isEditorPickPageAuthorized(OperationContext op) {
0451:
0452: User user = (User) op.user;
0453:
0454: if (isAtLeastWebmaster(op, user)) {
0455: return null;
0456: }
0457:
0458: return "shim.auth.unauthorizedEditorPickPage";
0459: }
0460:
0461: public String isEditorListPagesAuthorized(OperationContext op) {
0462:
0463: User user = (User) op.user;
0464:
0465: if (isAtLeastWebmaster(op, user)) {
0466: return null;
0467: }
0468:
0469: return "shim.auth.unauthorizedEditorListPages";
0470: }
0471:
0472: public String isConfigureTextModuleAuthorized(OperationContext op) {
0473:
0474: User user = (User) op.user;
0475:
0476: if (isAtLeastWebmaster(op, user)) {
0477: return null;
0478: }
0479:
0480: return "shim.auth.unauthorizedConfigureTextModule";
0481: }
0482:
0483: public String isConfigureNavModuleAuthorized(OperationContext op) {
0484:
0485: User user = (User) op.user;
0486:
0487: if (isAtLeastWebmaster(op, user)) {
0488: return null;
0489: }
0490:
0491: return "shim.auth.unauthorizedConfigureNavModule";
0492: }
0493:
0494: public String isConfigureIncludeModuleAuthorized(OperationContext op) {
0495:
0496: User user = (User) op.user;
0497:
0498: if (isAtLeastWebmaster(op, user)) {
0499: return null;
0500: }
0501:
0502: return "shim.auth.unauthorizedConfigureIncludeModule";
0503: }
0504:
0505: public String isNewPageFormAuthorized(OperationContext op) {
0506:
0507: User user = (User) op.user;
0508:
0509: if (isAtLeastWebmaster(op, user)) {
0510: return null;
0511: }
0512:
0513: return "shim.auth.unauthorizedNewPageForm";
0514: }
0515:
0516: public String isConfigurePageAuthorized(OperationContext op) {
0517:
0518: User user = (User) op.user;
0519:
0520: if (isAtLeastWebmaster(op, user)) {
0521: return null;
0522: }
0523:
0524: return "shim.auth.unauthorizedConfigurePage";
0525: }
0526:
0527: public String isConfigurePageFormAuthorized(OperationContext op) {
0528:
0529: User user = (User) op.user;
0530:
0531: if (isAtLeastWebmaster(op, user)) {
0532: return null;
0533: }
0534:
0535: return "shim.auth.unauthorizedConfigurePageForm";
0536: }
0537:
0538: public String isEditPageAuthorized(OperationContext op) {
0539:
0540: User user = (User) op.user;
0541:
0542: if (isAtLeastWebmaster(op, user)) {
0543: return null;
0544: }
0545:
0546: return "shim.auth.unauthorizedEditPage";
0547: }
0548:
0549: public String isConfigureModuleAuthorized(OperationContext op) {
0550:
0551: User user = (User) op.user;
0552:
0553: if (isAtLeastWebmaster(op, user)) {
0554: return null;
0555: }
0556:
0557: return "shim.auth.unauthorizedConfigureModule";
0558: }
0559:
0560: public String isEditPanelAuthorized(OperationContext op) {
0561:
0562: User user = (User) op.user;
0563:
0564: if (isAtLeastWebmaster(op, user)) {
0565: return null;
0566: }
0567:
0568: return "shim.auth.unauthorizedEditPanel";
0569: }
0570:
0571: public String isSavePanelAuthorized(OperationContext op) {
0572:
0573: User user = (User) op.user;
0574:
0575: if (isAtLeastWebmaster(op, user)) {
0576: return null;
0577: }
0578:
0579: return "shim.auth.unauthorizedSavePanel";
0580: }
0581:
0582: public String isDeletePageFormAuthorized(OperationContext op) {
0583:
0584: User user = (User) op.user;
0585:
0586: if (isAtLeastWebmaster(op, user)) {
0587: return null;
0588: }
0589:
0590: return "shim.auth.unauthorizedDeletePageForm";
0591: }
0592:
0593: public String isDeletePageAuthorized(OperationContext op) {
0594:
0595: User user = (User) op.user;
0596:
0597: if (isAtLeastWebmaster(op, user)) {
0598: return null;
0599: }
0600:
0601: return "shim.auth.unauthorizedDeletePage";
0602: }
0603:
0604: public String isMovePageFormAuthorized(OperationContext op) {
0605:
0606: User user = (User) op.user;
0607:
0608: if (isAtLeastWebmaster(op, user)) {
0609: return null;
0610: }
0611:
0612: return "shim.auth.unauthorizedMovePageForm";
0613: }
0614:
0615: public String isMovePageAuthorized(OperationContext op) {
0616:
0617: User user = (User) op.user;
0618:
0619: if (isAtLeastWebmaster(op, user)) {
0620: return null;
0621: }
0622:
0623: return "shim.auth.unauthorizedMovePage";
0624: }
0625:
0626: public String isConfigurePanelFormAuthorized(OperationContext op) {
0627:
0628: User user = (User) op.user;
0629:
0630: if (isAtLeastWebmaster(op, user)) {
0631: return null;
0632: }
0633:
0634: return "shim.auth.unauthorizedConfigurePanelForm";
0635: }
0636:
0637: public String isConfigurePanelAuthorized(OperationContext op) {
0638:
0639: User user = (User) op.user;
0640:
0641: if (isAtLeastWebmaster(op, user)) {
0642: return null;
0643: }
0644:
0645: return "shim.auth.unauthorizedConfigurePanel";
0646: }
0647:
0648: public String isSwitchAuthorized(OperationContext op) {
0649:
0650: User user = (User) op.user;
0651:
0652: if (isAtLeastSysAdmin(op, user)) {
0653: return null;
0654: }
0655:
0656: return "shim.auth.unauthorizedSwitch";
0657: }
0658:
0659: public void setUpMenu(OperationContext op) {
0660:
0661: User user = (User) op.user;
0662: FoldingTreeNode main = new FoldingTreeNode();
0663: FoldingTreeNode item = null;
0664: FoldingTreeNode subitem = null;
0665:
0666: MessageResources resources = (MessageResources) op.request
0667: .getSession().getServletContext().getAttribute(
0668: Globals.MESSAGES_KEY);
0669:
0670: //
0671: // transfer menu
0672: //
0673: FoldingTreeNode menu = new FoldingTreeNode();
0674: menu.setLabel(resources.getMessage("transfer.menu.transfer"));
0675: menu.setUrl("");
0676:
0677: try {
0678: item = new FoldingTreeNode();
0679: item
0680: .setLabel(resources
0681: .getMessage("transfer.menu.profile"));
0682: item.setUrl(RequestUtils.absoluteURL(op.request,
0683: "/profileForm.do").toString());
0684: menu.add(item);
0685:
0686: item = new FoldingTreeNode();
0687: item.setLabel(resources
0688: .getMessage("transfer.menu.properties"));
0689: item.setUrl(RequestUtils.absoluteURL(op.request,
0690: "/listProperties.do").toString());
0691: menu.add(item);
0692:
0693: item = new FoldingTreeNode();
0694: item.setLabel(resources.getMessage("transfer.menu.events"));
0695: item.setUrl(RequestUtils.absoluteURL(op.request,
0696: "/listEvents.do").toString());
0697: menu.add(item);
0698:
0699: item = new FoldingTreeNode();
0700: item.setLabel(resources.getMessage("transfer.menu.about"));
0701: item.setUrl(RequestUtils.absoluteURL(op.request,
0702: "/about.do").toString());
0703: menu.add(item);
0704:
0705: item = new FoldingTreeNode();
0706: item.setLabel(resources.getMessage("shim.menu.logout"));
0707: item.setUrl(RequestUtils.absoluteURL(op.request,
0708: "/logout.do").toString());
0709: menu.add(item);
0710:
0711: main.add(menu);
0712:
0713: //
0714: // site menu
0715: //
0716: if (SiteContext.getContext(op.request) != null) {
0717: menu = new FoldingTreeNode();
0718: menu.setLabel(resources.getMessage("shim.menu.site"));
0719: menu.setUrl("");
0720:
0721: item = new FoldingTreeNode();
0722: item.setLabel(resources
0723: .getMessage("transfer.menu.home"));
0724: item.setUrl(RequestUtils.absoluteURL(op.request,
0725: "/home.do").toString());
0726: menu.add(item);
0727:
0728: item = new FoldingTreeNode();
0729: item.setLabel(resources
0730: .getMessage("transfer.menu.sitemap"));
0731: item.setUrl(RequestUtils.absoluteURL(op.request,
0732: "/siteMap.do").toString());
0733: menu.add(item);
0734:
0735: main.add(menu);
0736: }
0737:
0738: //
0739: // system menu
0740: //
0741: if (isAtLeastSysAdmin(op, user)) {
0742: menu = new FoldingTreeNode();
0743: menu.setLabel(resources
0744: .getMessage("transfer.menu.system"));
0745: menu.setUrl("");
0746:
0747: item = new FoldingTreeNode();
0748: item.setLabel(resources
0749: .getMessage("transfer.menu.sites"));
0750: item.setUrl("");
0751: menu.add(item);
0752:
0753: subitem = new FoldingTreeNode();
0754: subitem.setLabel(resources
0755: .getMessage("shim.menu.newsite"));
0756: subitem.setUrl(RequestUtils.absoluteURL(op.request,
0757: "/siteContext.do?action=new").toString());
0758: item.add(subitem);
0759:
0760: subitem = new FoldingTreeNode();
0761: subitem
0762: .setLabel(resources
0763: .getMessage("shim.menu.list"));
0764: subitem.setUrl(RequestUtils.absoluteURL(op.request,
0765: "/siteContext.do?action=list").toString());
0766: item.add(subitem);
0767:
0768: item = new FoldingTreeNode();
0769: item.setLabel(resources
0770: .getMessage("transfer.menu.users"));
0771: item.setUrl("");
0772: menu.add(item);
0773:
0774: subitem = new FoldingTreeNode();
0775: subitem.setLabel(resources
0776: .getMessage("transfer.menu.newUser"));
0777: subitem.setUrl(RequestUtils.absoluteURL(op.request,
0778: "/user.do?action=new").toString());
0779: item.add(subitem);
0780:
0781: subitem = new FoldingTreeNode();
0782: subitem.setLabel(resources
0783: .getMessage("transfer.menu.listUsers"));
0784: subitem.setUrl(RequestUtils.absoluteURL(op.request,
0785: "/user.do?action=list").toString());
0786: item.add(subitem);
0787:
0788: main.add(menu);
0789: }
0790:
0791: if (SiteContext.getContext(op.request) != null) {
0792: //
0793: // page menu
0794: //
0795: menu = new FoldingTreeNode();
0796: menu.setLabel(resources.getMessage("shim.menu.page"));
0797: menu.setUrl("");
0798:
0799: item = new FoldingTreeNode();
0800: item.setLabel(resources.getMessage("shim.menu.new"));
0801: item.setUrl(RequestUtils.absoluteURL(op.request,
0802: "/newPageForm.do").toString());
0803: menu.add(item);
0804:
0805: item = new FoldingTreeNode();
0806: item.setLabel(resources
0807: .getMessage("shim.menu.fragments"));
0808: item.setUrl(RequestUtils.absoluteURL(op.request,
0809: "/htmlFragment.do?action=list").toString());
0810: menu.add(item);
0811:
0812: main.add(menu);
0813:
0814: //
0815: // files menu
0816: //
0817: menu = new FoldingTreeNode();
0818: menu.setLabel(resources
0819: .getMessage("transfer.menu.files"));
0820: menu.setUrl("");
0821:
0822: item = new FoldingTreeNode();
0823: item.setLabel(resources
0824: .getMessage("transfer.menu.listFiles"));
0825: item.setUrl(RequestUtils.absoluteURL(op.request,
0826: "/listFiles.do").toString());
0827: menu.add(item);
0828:
0829: item = new FoldingTreeNode();
0830: item.setLabel(resources
0831: .getMessage("transfer.menu.upload"));
0832: item.setUrl(RequestUtils.absoluteURL(op.request,
0833: "/uploadFileForm.do").toString());
0834: menu.add(item);
0835:
0836: main.add(menu);
0837:
0838: //
0839: // extensions menus
0840: //
0841: SiteExtension siteExtension = new SiteExtension();
0842: List extensions = siteExtension
0843: .loadAllForSiteContext(SiteContext
0844: .getContext(op.request));
0845:
0846: if (!extensions.isEmpty()) {
0847: menu = new FoldingTreeNode();
0848: menu.setLabel(resources
0849: .getMessage("transfer.menu.extensions"));
0850: menu.setUrl("");
0851:
0852: main.add(menu);
0853:
0854: for (Iterator iter = extensions.iterator(); iter
0855: .hasNext();) {
0856: siteExtension = (SiteExtension) iter.next();
0857: Extension extension = siteExtension
0858: .instantiateExtension(siteExtension
0859: .getString("class_name"));
0860: extension.setUpMenu(op, menu, resources);
0861: }
0862: }
0863: }
0864: } catch (MalformedURLException e) {
0865: throw new RuntimeException("Exception while building menu."
0866: + ExceptionUtils.getStackTrace(e));
0867: }
0868:
0869: op.request.setAttribute(ShimGlobals.MENU_KEY, main);
0870: }
0871:
0872: public void setUpEditorMenu(OperationContext op, Page page) {
0873:
0874: User user = (User) op.user;
0875:
0876: MessageResources resources = (MessageResources) op.request
0877: .getAttribute(Globals.MESSAGES_KEY);
0878:
0879: SiteMap siteMap = ShimUtils.getSiteMap(op.request);
0880:
0881: Link link = siteMap.find(page.getInt("id"));
0882:
0883: FoldingTreeNode menu = null;
0884: FoldingTreeNode item = null;
0885: FoldingTreeNode subitem = null;
0886:
0887: //
0888: // get the base menu
0889: //
0890: setUpMenu(op);
0891: FoldingTreeNode main = (FoldingTreeNode) op.request
0892: .getAttribute(ShimGlobals.MENU_KEY);
0893: FoldingTreeNode pageMenu = (FoldingTreeNode) main.getChildAt(2);
0894:
0895: //
0896: // update new page menu
0897: //
0898: if (isAtLeastSysAdmin(op, user)) {
0899: menu = (FoldingTreeNode) main.getChildAt(3);
0900: } else {
0901: menu = (FoldingTreeNode) main.getChildAt(2);
0902: }
0903:
0904: item = (FoldingTreeNode) menu.getChildAt(0);
0905:
0906: try {
0907: if (link != siteMap.getRoot()) {
0908: subitem = new FoldingTreeNode();
0909: subitem.setLabel(resources
0910: .getMessage("shim.menu.before"));
0911: subitem.setUrl(RequestUtils.absoluteURL(
0912: op.request,
0913: "/newPageForm.do?destid=" + page.getInt("id")
0914: + "&position=before").toString());
0915: item.add(subitem);
0916:
0917: subitem = new FoldingTreeNode();
0918: subitem.setLabel(resources
0919: .getMessage("shim.menu.after"));
0920: subitem.setUrl(RequestUtils.absoluteURL(
0921: op.request,
0922: "/newPageForm.do?destid=" + page.getInt("id")
0923: + "&position=after").toString());
0924: item.add(subitem);
0925: }
0926:
0927: subitem = new FoldingTreeNode();
0928: subitem.setLabel(resources.getMessage("shim.menu.under"));
0929: subitem.setUrl(RequestUtils.absoluteURL(
0930: op.request,
0931: "/newPageForm.do?destid=" + page.getInt("id")
0932: + "&position=under").toString());
0933: item.add(subitem);
0934:
0935: item = new FoldingTreeNode();
0936: item.setLabel(resources.getMessage("shim.menu.properties"));
0937: item.setUrl(RequestUtils.absoluteURL(op.request,
0938: "/configurePageForm.do?id=" + page.get("id"))
0939: .toString());
0940: menu.add(item);
0941:
0942: if (link != siteMap.getRoot()) {
0943: item = new FoldingTreeNode();
0944: item.setLabel(resources.getMessage("shim.menu.move"));
0945: item.setUrl(RequestUtils.absoluteURL(op.request,
0946: "/movePageForm.do?id=" + page.get("id"))
0947: .toString());
0948: menu.add(item);
0949: }
0950:
0951: item = new FoldingTreeNode();
0952: item.setLabel(resources.getMessage("shim.menu.delete"));
0953: item.setUrl(RequestUtils.absoluteURL(op.request,
0954: "/deletePage.do?id=" + page.get("id")).toString());
0955: menu.add(item);
0956: } catch (MalformedURLException e) {
0957: throw new RuntimeException("Exception while building menu."
0958: + ExceptionUtils.getStackTrace(e));
0959: }
0960: }
0961:
0962: // mhf policy ///////////////////////////////////////////////////////////////
0963:
0964: public String isSiteContextDeleteAuthorized(OperationContext op) {
0965:
0966: User user = (User) op.user;
0967:
0968: if (isAtLeastSysAdmin(op, user)) {
0969: return null;
0970: }
0971:
0972: return "sitecontext.auth.unauthorizedSiteContextDelete";
0973: }
0974:
0975: public String isSiteContextSaveNewAuthorized(OperationContext op) {
0976:
0977: User user = (User) op.user;
0978:
0979: if (isAtLeastSysAdmin(op, user)) {
0980: return null;
0981: }
0982:
0983: return "sitecontext.auth.unauthorizedSiteContextSaveNew";
0984: }
0985:
0986: public String isSiteContextListAuthorized(OperationContext op) {
0987:
0988: User user = (User) op.user;
0989:
0990: if (isAtLeastSysAdmin(op, user)) {
0991: return null;
0992: }
0993:
0994: return "sitecontext.auth.unauthorizedSiteContextList";
0995: }
0996:
0997: public String isSiteContextEditAuthorized(OperationContext op) {
0998:
0999: User user = (User) op.user;
1000:
1001: if (isAtLeastSysAdmin(op, user)) {
1002: return null;
1003: }
1004:
1005: return "sitecontext.auth.unauthorizedSiteContextEdit";
1006: }
1007:
1008: public String isSiteContextNewAuthorized(OperationContext op) {
1009:
1010: User user = (User) op.user;
1011:
1012: if (isAtLeastSysAdmin(op, user)) {
1013: return null;
1014: }
1015:
1016: return "sitecontext.auth.unauthorizedSiteContextNew";
1017: }
1018:
1019: public String isSiteContextSaveAuthorized(OperationContext op) {
1020:
1021: User user = (User) op.user;
1022:
1023: if (isAtLeastSysAdmin(op, user)) {
1024: return null;
1025: }
1026:
1027: return "sitecontext.auth.unauthorizedSiteContextSave";
1028: }
1029:
1030: // res policy ///////////////////////////////////////////////////////////////
1031:
1032: public FileManager newFileManager() {
1033: return new FileManager();
1034: }
1035:
1036: /**
1037: * Initializes the file manager, adding the app context root.
1038: */
1039: public void initFileManager(HttpServletRequest request,
1040: FileManager fileManager) {
1041:
1042: SiteContext siteContext = SiteContext.getContext(request);
1043: File f = null;
1044:
1045: f = ServletUtils.getRealFile(request, "/"
1046: + siteContext.getInt("id"));
1047: fileManager.addDirectory("Public", f);
1048:
1049: f = ServletUtils.getRealFile(request, "/WEB-INF/resources/"
1050: + siteContext.getInt("id"));
1051: fileManager.addDirectory("Private", f);
1052: }
1053:
1054: public FileTree newFileTree() {
1055: return new FileTree();
1056: }
1057:
1058: public String isFileMoveAuthorized(OperationContext op) {
1059:
1060: User user = (User) op.user;
1061:
1062: String movetoname = (String) op.form.get("movetoname");
1063:
1064: if (movetoname.toLowerCase().endsWith(".jsp")) {
1065: if (isAtLeastSiteAdmin(op, user)) {
1066: return null;
1067: } else {
1068: return "transfer.auth.unauthorizedCreateJsp";
1069: }
1070: }
1071:
1072: if (isAtLeastWebmaster(op, user)) {
1073: return null;
1074: }
1075:
1076: return "res.auth.unauthorizedFileMove";
1077: }
1078:
1079: public String isFileCopyAuthorized(OperationContext op) {
1080:
1081: User user = (User) op.user;
1082:
1083: String copytoname = (String) op.form.get("copytoname");
1084:
1085: if (copytoname.toLowerCase().endsWith(".jsp")) {
1086: if (isAtLeastSiteAdmin(op, user)) {
1087: return null;
1088: } else {
1089: return "transfer.auth.unauthorizedCreateJsp";
1090: }
1091: }
1092:
1093: if (isAtLeastWebmaster(op, user)) {
1094: return null;
1095: }
1096:
1097: return "res.auth.unauthorizedFileCopy";
1098: }
1099:
1100: public String isFileDeleteAuthorized(OperationContext op) {
1101:
1102: User user = (User) op.user;
1103:
1104: if (isAtLeastWebmaster(op, user)) {
1105: return null;
1106: }
1107:
1108: return "res.auth.unauthorizedFileDelete";
1109: }
1110:
1111: public String isFileEditAuthorized(OperationContext op) {
1112:
1113: User user = (User) op.user;
1114:
1115: List files = (List) op.form.get("files");
1116: FoldingTreeNode node = (FoldingTreeNode) files.get(0);
1117: File file = (File) node.getUserObject();
1118:
1119: if (file.getName().toLowerCase().endsWith(".jsp")) {
1120:
1121: if (isAtLeastSiteAdmin(op, user)) {
1122: return null;
1123: } else {
1124: return "transfer.auth.unauthorizedEditJsp";
1125: }
1126: }
1127:
1128: if (isAtLeastWebmaster(op, user)) {
1129: return null;
1130: }
1131:
1132: return "res.auth.unauthorizedFileEdit";
1133: }
1134:
1135: public String isFileUnzipAuthorized(
1136: OperationContext op ) {
1137:
1138: User user = ( User )op.user;
1139:
1140: if ( isAtLeastSiteAdmin( op, user ) ) {
1141: return null;
1142: }
1143:
1144: //
1145: // check if zip file contains jsps
1146: //
1147: if ( user.hasRole(
1148: SiteContext.getContext( op.request ), ROLE_WEBMASTER ) ) {
1149:
1150: List files = ( List )op.form.get( "files" );
1151: FoldingTreeNode node = ( FoldingTreeNode )files.get( 0 );
1152: File file = ( File )node.getUserObject();
1153:
1154: try {
1155: ZipFile zf = new ZipFile( file );
1156:
1157: for ( Enumeration enum = zf.entries(); enum.hasMoreElements(); ) {
1158: ZipEntry ze = ( ZipEntry )enum.nextElement();
1159:
1160: if ( ze.getName().toLowerCase().endsWith( ".jsp" ) ) {
1161: return "transfer.auth.unauthorizedFileUnzipContainsJsp";
1162: }
1163: }
1164: }
1165: catch ( ZipException e ) {
1166: throw new RuntimeException(
1167: "Unexpected ZipException:" + ExceptionUtils.getStackTrace( e ) );
1168: }
1169: catch ( IOException e ) {
1170: throw new RuntimeException(
1171: "Unexpected IOException:" + ExceptionUtils.getStackTrace( e ) );
1172: }
1173:
1174: return null;
1175: }
1176:
1177: return "res.auth.unauthorizedFileUnzip";
1178: }
1179:
1180: public String isFileListAuthorized(OperationContext op) {
1181:
1182: User user = (User) op.user;
1183:
1184: if (isAtLeastWebmaster(op, user)) {
1185: return null;
1186: }
1187:
1188: return "res.auth.unauthorizedFileList";
1189: }
1190:
1191: public String isFileManageAuthorized(OperationContext op) {
1192:
1193: User user = (User) op.user;
1194:
1195: if (isAtLeastWebmaster(op, user)) {
1196: return null;
1197: }
1198:
1199: return "res.auth.unauthorizedFileManage";
1200: }
1201:
1202: public String isFileUploadFormAuthorized(OperationContext op) {
1203:
1204: User user = (User) op.user;
1205:
1206: if (isAtLeastWebmaster(op, user)) {
1207: return null;
1208: }
1209:
1210: return "res.auth.unauthorizedFileUpload";
1211: }
1212:
1213: public String isFileUploadAuthorized(OperationContext op) {
1214:
1215: User user = (User) op.user;
1216:
1217: if (isAtLeastSiteAdmin(op, user)) {
1218: return null;
1219: }
1220:
1221: if (user.hasRole(SiteContext.getContext(op.request),
1222: ROLE_WEBMASTER)) {
1223:
1224: FormFile file = (FormFile) op.form.get("file");
1225: if (file.getFileName().toLowerCase().endsWith(".jsp"))
1226: return "transfer.auth.unauthorizedFileUploadJsp";
1227: else
1228: return null;
1229: }
1230:
1231: return "res.auth.unauthorizedFileUpload";
1232: }
1233:
1234: public String isFileCreateFormAuthorized(OperationContext op) {
1235:
1236: User user = (User) op.user;
1237:
1238: if (isAtLeastWebmaster(op, user)) {
1239: return null;
1240: }
1241:
1242: return "res.auth.unauthorizedFileCreate";
1243: }
1244:
1245: public String isFileCreateAuthorized(OperationContext op) {
1246:
1247: User user = (User) op.user;
1248:
1249: if (isAtLeastSiteAdmin(op, user)) {
1250: return null;
1251: }
1252:
1253: if (user.hasRole(SiteContext.getContext(op.request),
1254: ROLE_WEBMASTER)) {
1255: String name = (String) op.form.get("name");
1256: if (name.toLowerCase().endsWith(".jsp"))
1257: return "transfer.auth.unauthorizedCreateJsp";
1258: else
1259: return null;
1260: }
1261:
1262: return "res.auth.unauthorizedFileCreate";
1263: }
1264:
1265: // reg policies /////////////////////////////////////////////////////////////
1266:
1267: public User newRegUser() {
1268: return new User();
1269: }
1270:
1271: public List getRoleOptions(OperationContext op) {
1272: List options = new ArrayList();
1273: options.add(new LabelValueBean("reg.role.sysadmin",
1274: ROLE_SYSADMIN));
1275: options.add(new LabelValueBean("reg.role.siteadmin",
1276: ROLE_SITEADMIN));
1277: options.add(new LabelValueBean("reg.role.webmaster",
1278: ROLE_WEBMASTER));
1279: return options;
1280: }
1281:
1282: public void sendPassword(User user, String password,
1283: OperationContext op) {
1284:
1285: String from = Property.getProperty(SiteContext
1286: .getDefaultContext(), PROPERTY_MAILFROM,
1287: "support@methodhead.com");
1288:
1289: //
1290: // send user password via email
1291: //
1292: Mail.send(
1293: new String[] { user.getContact().getString("email") },
1294: from, "Your TransferCM Password",
1295: "Your password is:\n\n " + password);
1296: }
1297:
1298: public List getAuthorizedSites(OperationContext op) {
1299:
1300: throw new RuntimeException("Not implemented");
1301: }
1302:
1303: public String isListUsersAuthorized(OperationContext op) {
1304:
1305: User user = (User) op.user;
1306:
1307: if (isAtLeastSysAdmin(op, user)) {
1308: return null;
1309: }
1310:
1311: return "reg.auth.unauthorizedListUsers";
1312: }
1313:
1314: public String isDeleteUserAuthorized(OperationContext op) {
1315:
1316: User user = (User) op.user;
1317:
1318: if (isAtLeastSysAdmin(op, user)) {
1319: return null;
1320: }
1321:
1322: return "reg.auth.unauthorizedDeleteUser";
1323: }
1324:
1325: public String isSaveUserAuthorized(OperationContext op) {
1326:
1327: User user = (User) op.user;
1328:
1329: if (isAtLeastSysAdmin(op, user)) {
1330: return null;
1331: }
1332:
1333: return "reg.auth.unauthorizedSaveUser";
1334: }
1335:
1336: public String isSaveNewUserAuthorized(OperationContext op) {
1337:
1338: User user = (User) op.user;
1339:
1340: if (isAtLeastSysAdmin(op, user)) {
1341: return null;
1342: }
1343:
1344: return "reg.auth.unauthorizedSaveNewUser";
1345: }
1346:
1347: public String isEditUserAuthorized(OperationContext op) {
1348:
1349: User user = (User) op.user;
1350:
1351: if (isAtLeastSysAdmin(op, user)) {
1352: return null;
1353: }
1354:
1355: return "reg.auth.unauthorizedEditUser";
1356: }
1357:
1358: public String isNewUserAuthorized(OperationContext op) {
1359:
1360: User user = (User) op.user;
1361:
1362: if (isAtLeastSysAdmin(op, user)) {
1363: return null;
1364: }
1365:
1366: return "reg.auth.unauthorizedNewUser";
1367: }
1368:
1369: public String isRolesFormAuthorized(OperationContext op) {
1370:
1371: User user = (User) op.user;
1372:
1373: if (isAtLeastSysAdmin(op, user)) {
1374: return null;
1375: }
1376:
1377: return "reg.auth.unauthorizedRoles";
1378: }
1379:
1380: public String isRolesAuthorized(OperationContext op) {
1381:
1382: User user = (User) op.user;
1383:
1384: if (isAtLeastSysAdmin(op, user)) {
1385: return null;
1386: }
1387:
1388: return "reg.auth.unauthorizedRoles";
1389: }
1390:
1391: // transfer policy //////////////////////////////////////////////////////////
1392:
1393: public SiteExtension newSiteExtension() {
1394: return new SiteExtension();
1395: }
1396:
1397: public String isInitExtensionAuthorized(OperationContext op) {
1398:
1399: User user = (User) op.user;
1400:
1401: if (isAtLeastSysAdmin(op, user)) {
1402: return null;
1403: }
1404:
1405: return "reg.auth.unauthorizedInitExtension";
1406: }
1407:
1408: public String isDestroyExtensionAuthorized(OperationContext op) {
1409:
1410: User user = (User) op.user;
1411:
1412: if (isAtLeastSysAdmin(op, user)) {
1413: return null;
1414: }
1415:
1416: return "reg.auth.unauthorizedInitExtension";
1417: }
1418:
1419: public String isDisableExtensionAuthorized(OperationContext op) {
1420:
1421: User user = (User) op.user;
1422:
1423: if (isAtLeastSysAdmin(op, user)) {
1424: return null;
1425: }
1426:
1427: return "reg.auth.unauthorizedDisableExtension";
1428: }
1429:
1430: public String isEnableExtensionAuthorized(OperationContext op) {
1431:
1432: User user = (User) op.user;
1433:
1434: if (isAtLeastSysAdmin(op, user)) {
1435: return null;
1436: }
1437:
1438: return "reg.auth.unauthorizedEnableExtension";
1439: }
1440:
1441: public String isAboutAuthorized(OperationContext op) {
1442:
1443: User user = (User) op.user;
1444:
1445: if (isAtLeastWebmaster(op, user)) {
1446: return null;
1447: }
1448:
1449: return "reg.auth.unauthorizedAbout";
1450: }
1451:
1452: // properties ///////////////////////////////////////////////////////////////
1453:
1454: // attributes ///////////////////////////////////////////////////////////////
1455: }
|