View Javadoc

1   /**
2    * This file Copyright (c) 2003-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.cms.security;
35  
36  import info.magnolia.cms.util.ObservationUtil;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.repository.RepositoryConstants;
39  
40  import javax.jcr.Node;
41  import javax.jcr.RepositoryException;
42  import javax.jcr.Session;
43  import javax.jcr.observation.EventIterator;
44  import javax.jcr.observation.EventListener;
45  
46  import org.apache.commons.lang.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * Responsible to handle system users like anonymous and superuser.
53   * @author philipp
54   * @version $Id$
55   */
56  public class SystemUserManager extends MgnlUserManager {
57  
58      /**
59       * Logger.
60       */
61      private static Logger log = LoggerFactory.getLogger(SystemUserManager.class);
62  
63      /**
64       * kept as static for performance reasons on live instance. reinitialized on any modification event on anonymous
65       * role
66       */
67      private User anonymousUser;
68  
69      public SystemUserManager() {
70  
71          EventListener anonymousListener = new EventListener() {
72  
73              @Override
74              public void onEvent(EventIterator events) {
75                  anonymousUser = null;
76                  log.debug("Anonymous user reloaded");
77              }
78  
79          };
80  
81          final String anonymousUserPath = "/" + Realm.REALM_SYSTEM.getName() + "/" + UserManager.ANONYMOUS_USER;
82          ObservationUtil.registerChangeListener(
83                  RepositoryConstants.USERS,
84                  anonymousUserPath,
85                  true,
86                  "mgnl:user",
87                  anonymousListener);
88  
89          ObservationUtil.registerChangeListener(
90                  RepositoryConstants.USER_GROUPS,
91                  "/",
92                  true,
93                  "mgnl:group",
94                  anonymousListener);
95  
96          ObservationUtil.registerDeferredChangeListener(
97                  RepositoryConstants.USER_ROLES,
98                  "/",
99                  true,
100                 "mgnl:role",
101                 anonymousListener,
102                 1000,
103                 5000);
104     }
105 
106     @Override
107     public String getRealmName() {
108         String name = super.getRealmName();
109         // attempt to fix: MAGNOLIA-1839
110         if (StringUtils.isEmpty(name)) {
111             log.error("realm of system user manager is not set!");
112             return Realm.REALM_SYSTEM.getName();
113         }
114         return name;
115     }
116 
117     @Override
118     public User getSystemUser() {
119         return getOrCreateUser(UserManager.SYSTEM_USER, UserManager.SYSTEM_PSWD);
120     }
121 
122     @Override
123     public User getAnonymousUser() {
124         if (anonymousUser == null) {
125             // see MAGNOLIA-2029
126             anonymousUser = getRequiredSystemUser(UserManager.ANONYMOUS_USER, UserManager.ANONYMOUS_USER);
127         }
128         return anonymousUser;
129     }
130 
131     /**
132      * Load a system user from the repository, but don't try to create it if missing.
133      */
134     private User getRequiredSystemUser(final String username, String password) {
135         return MgnlContext.doInSystemContext(new SilentSessionOp<User>(getRepositoryName()) {
136 
137             @Override
138             public User doExec(Session session) throws RepositoryException {
139                 User user = null;
140                 Node node;
141                 try {
142                     node = session.getNode("/" + Realm.REALM_SYSTEM.getName() + "/" + username);
143                 }
144                 catch (RepositoryException e) {
145                     log.error("Error caught while loading the system user "
146                             + username
147                             + ": "
148                             + e.getClass().getName()
149                             + ": "
150                             + e.getMessage(), e);
151                     return null;
152                 }
153                 if (node == null) {
154                     log.error("User not found: {}.", username);
155                     return null;
156                 }
157 
158                 user = newUserInstance(node);
159                 return user;
160             }});
161     }
162 
163     protected User getOrCreateUser(String userName, String password) {
164         User user = getUser(userName);
165         if (user == null) {
166             log.error(
167                     "Failed to get system user [{}], will try to create new system user with default password",
168                     userName);
169             user = this.createUser(userName, password);
170         }
171         return user;
172     }
173 }