]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DefaultUserAdminService.java
e823124d7c690bf387bdcf5ee6dc3ab1a00daa58
[lgpl/argeo-commons.git] / DefaultUserAdminService.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.security.core;
18
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.argeo.security.ArgeoUser;
25 import org.argeo.security.UserAdminDao;
26 import org.argeo.security.UserAdminService;
27 import org.argeo.security.nature.SimpleUserNature;
28
29 public class DefaultUserAdminService implements UserAdminService {
30 private String superUsername = "root";
31 private UserAdminDao userAdminDao;
32
33 public void newRole(String role) {
34 userAdminDao.createRole(role, getSuperUsername());
35 }
36
37 public void updateUserPassword(String username, String password) {
38 userAdminDao.updateUserPassword(username, password);
39 }
40
41 public void newUser(ArgeoUser user) {
42 // pre-process
43 SimpleUserNature simpleUserNature;
44 try {
45 simpleUserNature = SimpleUserNature
46 .findSimpleUserNature(user, null);
47 } catch (Exception e) {
48 simpleUserNature = new SimpleUserNature();
49 user.getUserNatures().put("simpleUserNature", simpleUserNature);
50 }
51
52 if (simpleUserNature.getLastName() == null
53 || simpleUserNature.getLastName().equals("")) {
54 // to prevent issue with sn in LDAP
55 simpleUserNature.setLastName("empty");
56 }
57
58 userAdminDao.createUser(user);
59 }
60
61
62
63 public void synchronize() {
64 // TODO Auto-generated method stub
65
66 }
67
68 public ArgeoUser getUser(String username) {
69 return userAdminDao.getUser(username);
70 }
71
72 public Boolean userExists(String username) {
73 return userAdminDao.userExists(username);
74 }
75
76 public void updateUser(ArgeoUser user) {
77 userAdminDao.updateUser(user);
78 }
79
80 public void deleteUser(String username) {
81 userAdminDao.deleteUser(username);
82
83 }
84
85 public void deleteRole(String role) {
86 userAdminDao.deleteRole(role);
87 }
88
89 public Set<ArgeoUser> listUsersInRole(String role) {
90 Set<ArgeoUser> lst = new HashSet<ArgeoUser>(
91 userAdminDao.listUsersInRole(role));
92 Iterator<ArgeoUser> it = lst.iterator();
93 while (it.hasNext()) {
94 if (it.next().getUsername().equals(getSuperUsername())) {
95 it.remove();
96 break;
97 }
98 }
99 return lst;
100 }
101
102 public Set<ArgeoUser> listUsers() {
103 return userAdminDao.listUsers();
104 }
105
106 public List<String> listUserRoles(String username) {
107 return getUser(username).getRoles();
108 }
109
110 public Set<String> listEditableRoles() {
111 return userAdminDao.listEditableRoles();
112 }
113
114 // TODO: expose it via the interface as well?
115 public String getSuperUsername() {
116 return superUsername;
117 }
118
119 public void setUserAdminDao(UserAdminDao userAdminDao) {
120 this.userAdminDao = userAdminDao;
121 }
122
123 }