]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/OsJcrUserAdminService.java
Merge https://www.argeo.org/bugzilla/show_bug.cgi?id=141
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / jcr / 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.security.jcr;
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.jcr.JcrUtils;
30 import org.argeo.jcr.UserJcrUtils;
31 import org.argeo.security.UserAdminService;
32 import org.springframework.dao.DataAccessException;
33 import org.springframework.security.userdetails.User;
34 import org.springframework.security.userdetails.UserDetails;
35 import org.springframework.security.userdetails.UsernameNotFoundException;
36
37 /**
38 * Dummy user service to be used when running as a single OS user (typically
39 * desktop). TODO integrate with JCR user / groups
40 */
41 public class OsJcrUserAdminService implements UserAdminService {
42 private Repository repository;
43
44 /** In memory roles provided by applications. */
45 private List<String> roles = new ArrayList<String>();
46
47 // private Session adminSession;
48
49 public void init() {
50 // try {
51 // adminSession = repository.login();
52 // } catch (RepositoryException e) {
53 // throw new ArgeoException("Cannot initialize", e);
54 // }
55 }
56
57 public void destroy() {
58 // JcrUtils.logoutQuietly(adminSession);
59 }
60
61 /** <b>Unsupported</b> */
62 public void createUser(UserDetails user) {
63 throw new UnsupportedOperationException();
64 }
65
66 /** Does nothing */
67 public void updateUser(UserDetails user) {
68
69 }
70
71 /** <b>Unsupported</b> */
72 public void deleteUser(String username) {
73 throw new UnsupportedOperationException();
74 }
75
76 /** <b>Unsupported</b> */
77 public void changePassword(String oldPassword, String newPassword) {
78 throw new UnsupportedOperationException();
79 }
80
81 public boolean userExists(String username) {
82 if (getSPropertyUsername().equals(username))
83 return true;
84 else
85 return false;
86 }
87
88 public UserDetails loadUserByUsername(String username)
89 throws UsernameNotFoundException, DataAccessException {
90 if (getSPropertyUsername().equals(username)) {
91 UserDetails userDetails;
92 if (repository != null) {
93 Session adminSession = null;
94 try {
95 adminSession = repository.login();
96 Node userProfile = UserJcrUtils.getUserProfile(
97 adminSession, username);
98 userDetails = new JcrUserDetails(userProfile, "",
99 OsJcrAuthenticationProvider.getBaseAuthorities());
100 } catch (RepositoryException e) {
101 throw new ArgeoException(
102 "Cannot retrieve user profile for " + username, e);
103 } finally {
104 JcrUtils.logoutQuietly(adminSession);
105 }
106 } else {
107 userDetails = new User(username, "", true, true, true, true,
108 OsJcrAuthenticationProvider.getBaseAuthorities());
109 }
110 return userDetails;
111 } else {
112 throw new UnsupportedOperationException();
113 }
114 }
115
116 protected final String getSPropertyUsername() {
117 return System.getProperty("user.name");
118 }
119
120 public Set<String> listUsers() {
121 Set<String> set = new HashSet<String>();
122 set.add(getSPropertyUsername());
123 return set;
124 }
125
126 public Set<String> listUsersInRole(String role) {
127 Set<String> set = new HashSet<String>();
128 set.add(getSPropertyUsername());
129 return set;
130 }
131
132 /** Does nothing */
133 public void synchronize() {
134 }
135
136 /** <b>Unsupported</b> */
137 public void newRole(String role) {
138 roles.add(role);
139 }
140
141 public Set<String> listEditableRoles() {
142 return new HashSet<String>(roles);
143 }
144
145 /** <b>Unsupported</b> */
146 public void deleteRole(String role) {
147 roles.remove(role);
148 }
149
150 public void setRepository(Repository repository) {
151 this.repository = repository;
152 }
153 }