View Javadoc

1   /**
2    * This file Copyright (c) 2011 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      /**
66       * @param name
67       * @param request
68       * @param response
69       */
70      public ActivationPage(String name, HttpServletRequest request, HttpServletResponse response) {
71          super(name, request, response);
72          keyLength = ModuleRegistry.Factory.getInstance().getModuleInstance(ExchangeSimpleModule.class).getActivationKeyLength();
73      }
74  
75      /**
76       * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#getCommand()
77       */
78      @Override
79      public String getCommand() {
80          if (this.generateKeys) {
81              return "generateKeys";
82          }
83          return super.getCommand();
84      }
85  
86      /**
87       * Actually perform backup.
88       */
89      public String generateKeys() throws Exception {
90          if (!checkPermissions(request, "config", "/", Permission.WRITE)) {
91              throw new ServletException(new AccessDeniedException(
92              "Write permission needed to generate new key. Please try again after logging in with appropriate permissions."));
93          }
94  
95          MgnlKeyPair keyPair = SecurityUtil.generateKeyPair(keyLength);
96          SecurityUtil.updateKeys(keyPair);
97          this.success = true;
98  
99          // TODO: distribute key to public instances
100 
101         return this.show();
102     }
103 
104     public String getCurrentPublicKey() {
105         return SecurityUtil.getPublicKey();
106     }
107 
108     /**
109      * Uses access manager to authorize this request.
110      * 
111      * @param request
112      *            HttpServletRequest as received by the service method
113      * @return boolean true if read access is granted
114      */
115     protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
116             long permissionType) {
117 
118         AccessManager accessManager = MgnlContext.getAccessManager(repository);
119         if (accessManager != null) {
120             if (!accessManager.isGranted(basePath, permissionType)) {
121                 return false;
122             }
123         }
124         return true;
125     }
126 
127     public Messages getMessages() {
128         return MessagesManager.getMessages("info.magnolia.module.exchangesimple.messages");
129     }
130 
131     public int getKeyLength() {
132         return keyLength;
133     }
134 
135     public void setKeyLength(int keyLength) {
136         this.keyLength = keyLength;
137     }
138 
139     public boolean isGenerateKeys() {
140         return generateKeys;
141     }
142 
143     public void setGenerateKeys(boolean generateKeys) {
144         this.generateKeys = generateKeys;
145     }
146 
147     public boolean isSuccess() {
148         return success;
149     }
150 
151     public void setSuccess(boolean success) {
152         this.success = success;
153     }
154 
155 }