]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OsJcrUserAdminService.java
4ad2ad16d54566523bf7f008a1b3bf029791a6ae
[lgpl/argeo-commons.git] / OsJcrUserAdminService.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.internal.useradmin;
17
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22
23 import javax.jcr.Node;
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.Session;
27
28 import org.argeo.ArgeoException;
29 import org.argeo.cms.internal.auth.OsJcrAuthenticationProvider;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.jcr.UserJcrUtils;
32 import org.argeo.security.UserAdminService;
33 import org.argeo.security.jcr.JcrUserDetails;
34 import org.springframework.dao.DataAccessException;
35 import org.springframework.security.core.userdetails.User;
36 import org.springframework.security.core.userdetails.UserDetails;
37 import org.springframework.security.core.userdetails.UsernameNotFoundException;
38
39 /**
40 * Dummy user service to be used when running as a single OS user (typically
41 * desktop). TODO integrate with JCR user / groups
42 */
43 public class OsJcrUserAdminService implements UserAdminService {
44 private Repository repository;
45
46 /** In memory roles provided by applications. */
47 private List<String> roles = new ArrayList<String>();
48
49 // private Session adminSession;
50
51 public void init() {
52 // try {
53 // adminSession = repository.login();
54 // } catch (RepositoryException e) {
55 // throw new ArgeoException("Cannot initialize", e);
56 // }
57 }
58
59 public void destroy() {
60 // JcrUtils.logoutQuietly(adminSession);
61 }
62
63 /** <b>Unsupported</b> */
64 public void createUser(UserDetails user) {
65 throw new UnsupportedOperationException();
66 }
67
68 /** Does nothing */
69 public void updateUser(UserDetails user) {
70
71 }
72
73 /** <b>Unsupported</b> */
74 public void deleteUser(String username) {
75 throw new UnsupportedOperationException();
76 }
77
78 /** <b>Unsupported</b> */
79 public void changePassword(String oldPassword, String newPassword) {
80 throw new UnsupportedOperationException();
81 }
82
83 public boolean userExists(String username) {
84 if (getSPropertyUsername().equals(username))
85 return true;
86 else
87 return false;
88 }
89
90 public UserDetails loadUserByUsername(String username)
91 throws UsernameNotFoundException, DataAccessException {
92 if (getSPropertyUsername().equals(username)) {
93 UserDetails userDetails;
94 if (repository != null) {
95 Session adminSession = null;
96 try {
97 adminSession = repository.login();
98 Node userProfile = UserJcrUtils.getUserProfile(
99 adminSession, username);
100 userDetails = new JcrUserDetails(userProfile, "",
101 OsJcrAuthenticationProvider.getBaseAuthorities());
102 } catch (RepositoryException e) {
103 throw new ArgeoException(
104 "Cannot retrieve user profile for " + username, e);
105 } finally {
106 JcrUtils.logoutQuietly(adminSession);
107 }
108 } else {
109 userDetails = new User(username, "", true, true, true, true,
110 OsJcrAuthenticationProvider.getBaseAuthorities());
111 }
112 return userDetails;
113 } else {
114 throw new UnsupportedOperationException();
115 }
116 }
117
118 protected final String getSPropertyUsername() {
119 return System.getProperty("user.name");
120 }
121
122 public Set<String> listUsers() {
123 Set<String> set = new HashSet<String>();
124 set.add(getSPropertyUsername());
125 return set;
126 }
127
128 public Set<String> listUsersInRole(String role) {
129 Set<String> set = new HashSet<String>();
130 set.add(getSPropertyUsername());
131 return set;
132 }
133
134 /** Does nothing */
135 public void synchronize() {
136 }
137
138 /** <b>Unsupported</b> */
139 public void newRole(String role) {
140 roles.add(role);
141 }
142
143 public Set<String> listEditableRoles() {
144 return new HashSet<String>(roles);
145 }
146
147 /** <b>Unsupported</b> */
148 public void deleteRole(String role) {
149 roles.remove(role);
150 }
151
152 public void setRepository(Repository repository) {
153 this.repository = repository;
154 }
155 }