1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
47
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
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
96
97 static class InitPhaseSecuritySupportImpl extends SecuritySupportImpl {
98
99
100
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
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
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 }