View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 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.cms.security;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.objectfactory.ObservedComponentFactory;
38  import info.magnolia.repository.RepositoryConstants;
39  
40  import java.util.Collections;
41  
42  import javax.jcr.RepositoryException;
43  import javax.jcr.Session;
44  
45  /**
46   * Special ObservedComponentFactory used for providing a SecuritySupport already during the initialization phase - before the proper SecuritySupportImpl can been instantiated.
47   * Once the proper SecuritySupportImpl instantiated as configured in Jcr it will always be used instead of this one.
48   */
49  public class SecuritySupportObservedComponentFactory extends ObservedComponentFactory<SecuritySupport> {
50  
51      public static final String SECURITY_CONFIG_PATH = "/server/security";
52      public static final String SYSTEM_SUPERUSER_PATH = "/system/superuser";
53  
54      public SecuritySupportObservedComponentFactory() {
55          super(RepositoryConstants.CONFIG, SECURITY_CONFIG_PATH, SecuritySupport.class);
56      }
57  
58      @Override
59      protected Class[] getExposedInterfaces() {
60          // This impl has to return the interface to avoid running into ClassCastExceptions later, when the temporary InitPhaseSecuritySupportImpl is swapped for the proper SecuritySupportImpl.
61          return new Class[] {
62                  SecuritySupport.class
63          };
64      }
65  
66      @Override
67      protected void instantiateDefault() {
68          setObservedObject(new InitPhaseSecuritySupportImpl());
69      }
70  
71      @Override
72      public SecuritySupport getObservedObject() {
73          if (super.getObservedObject() instanceof InitPhaseSecuritySupportImpl) {
74              MgnlContext.doInSystemContext(new SilentSessionOp<Void>(RepositoryConstants.CONFIG) {
75  
76                  @Override
77                  public Void doExec(Session configSession) throws RepositoryException {
78                      if (!configSession.nodeExists(SECURITY_CONFIG_PATH)) {
79                          return null;
80                      }
81  
82                      Session usersSession = MgnlContext.getJCRSession(RepositoryConstants.USERS);
83                      if (usersSession.nodeExists(SYSTEM_SUPERUSER_PATH)) {
84                          reload();
85                      }
86  
87                      return null;
88                  }
89              });
90          }
91          return super.getObservedObject();
92      }
93  
94      /**
95       * SecuritySupport used during initialization phase.
96       */
97      static class InitPhaseSecuritySupportImpl extends SecuritySupportImpl {
98  
99          /**
100          * Default constructor - required for Cglib proxying.
101          */
102         public InitPhaseSecuritySupportImpl() {
103             super();
104         }
105 
106         @Override
107         public UserManager getUserManager(String realmName) {
108             return Realm.REALM_SYSTEM.getName().equals(realmName) ? new InitPhaseUserManager() : null;
109         }
110     }
111 
112     /**
113      * UserManager used during initialization phase.
114      */
115     static class InitPhaseUserManager extends SystemUserManager {
116         @Override
117         protected User getOrCreateUser(final String userName, final String ignoredPassword) {
118             return UserManager.SYSTEM_USER.equals(userName) ? new InitPhaseUser(userName) : null;
119         }
120     }
121 
122     /**
123      * User used during initialization phase.
124      */
125     static class InitPhaseUser extends MgnlUser {
126         private InitPhaseUser(final String name) {
127             super(name, Realm.REALM_SYSTEM.getName(), Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String, String>emptyMap());
128         }
129     }
130 
131 }