View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.exchangesimple.pages;
35  
36  import info.magnolia.cms.i18n.Messages;
37  import info.magnolia.cms.i18n.MessagesManager;
38  import info.magnolia.cms.security.AccessDeniedException;
39  import info.magnolia.cms.security.AccessManager;
40  import info.magnolia.cms.security.MgnlKeyPair;
41  import info.magnolia.cms.security.Permission;
42  import info.magnolia.cms.security.SecurityUtil;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.module.ModuleRegistry;
45  import info.magnolia.module.admininterface.TemplatedMVCHandler;
46  import info.magnolia.module.exchangesimple.ExchangeSimpleModule;
47  
48  import javax.servlet.ServletException;
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * Activation UI.
54   *
55   * @version $Id$
56   */
57  public class ActivationPage extends TemplatedMVCHandler {
58  
59      private int keyLength;
60  
61      private boolean generateKeys;
62  
63      private boolean success;
64  
65      private MgnlKeyPair keyPair;
66  
67      /**
68       * @param name
69       * @param request
70       * @param response
71       */
72      public ActivationPage(String name, HttpServletRequest request, HttpServletResponse response) {
73          super(name, request, response);
74          keyLength = ModuleRegistry.Factory.getInstance().getModuleInstance(ExchangeSimpleModule.class).getActivationKeyLength();
75      }
76  
77      /**
78       * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#getCommand()
79       */
80      @Override
81      public String getCommand() {
82          if (this.generateKeys) {
83              return "generateKeys";
84          }
85          return super.getCommand();
86      }
87  
88      /**
89       * Actually perform backup.
90       */
91      public String generateKeys() throws Exception {
92          if (!checkPermissions(request, "config", "/", Permission.WRITE)) {
93              throw new ServletException(new AccessDeniedException(
94              "Write permission needed to generate new key. Please try again after logging in with appropriate permissions."));
95          }
96  
97          keyPair = SecurityUtil.generateKeyPair(keyLength);
98          SecurityUtil.updateKeys(keyPair);
99          this.success = true;
100 
101         // TODO: distribute key to public instances
102 
103         return this.show();
104     }
105 
106     //need to retrieve the public key directly (proxy used by SecurityUtil might not be updated yet)
107     public String getCurrentPublicKey() {
108         if (keyPair != null) {
109             return keyPair.getPublicKey();
110         }
111         return SecurityUtil.getPublicKey();
112     }
113 
114     /**
115      * Uses access manager to authorize this request.
116      *
117      * @param request
118      *            HttpServletRequest as received by the service method
119      * @return boolean true if read access is granted
120      */
121     protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
122             long permissionType) {
123 
124         AccessManager accessManager = MgnlContext.getAccessManager(repository);
125         if (accessManager != null) {
126             if (!accessManager.isGranted(basePath, permissionType)) {
127                 return false;
128             }
129         }
130         return true;
131     }
132 
133     public Messages getMessages() {
134         return MessagesManager.getMessages("info.magnolia.module.exchangesimple.messages");
135     }
136 
137     public int getKeyLength() {
138         return keyLength;
139     }
140 
141     public void setKeyLength(int keyLength) {
142         this.keyLength = keyLength;
143     }
144 
145     public boolean isGenerateKeys() {
146         return generateKeys;
147     }
148 
149     public void setGenerateKeys(boolean generateKeys) {
150         this.generateKeys = generateKeys;
151     }
152 
153     public boolean isSuccess() {
154         return success;
155     }
156 
157     public void setSuccess(boolean success) {
158         this.success = success;
159     }
160 
161 }