]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SimpleArgeoUser.java
Introduce Commons Modeshape
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / SimpleArgeoUser.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;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.argeo.ArgeoException;
26
27 /**
28 * Read-write implementation of an Argeo user. Typically initialized with a
29 * generic instance (read-only9 in order to modify a user.
30 */
31 public class SimpleArgeoUser implements ArgeoUser, Serializable {
32 private static final long serialVersionUID = 1L;
33
34 private String username;
35 private String password;
36 private Map<String, UserNature> userNatures = new HashMap<String, UserNature>();
37 private List<String> roles = new ArrayList<String>();
38
39 public SimpleArgeoUser() {
40
41 }
42
43 public SimpleArgeoUser(ArgeoUser argeoUser) {
44 username = argeoUser.getUsername();
45 password = argeoUser.getPassword();
46 userNatures = new HashMap<String, UserNature>(
47 argeoUser.getUserNatures());
48 roles = new ArrayList<String>(argeoUser.getRoles());
49 }
50
51 public Map<String, UserNature> getUserNatures() {
52 return userNatures;
53 }
54
55 public void updateUserNatures(Map<String, UserNature> userNaturesData) {
56 updateUserNaturesWithCheck(userNatures, userNaturesData);
57 }
58
59 public static void updateUserNaturesWithCheck(
60 Map<String, UserNature> userNatures,
61 Map<String, UserNature> userNaturesData) {
62 // checks consistency
63 if (userNatures.size() != userNaturesData.size())
64 throw new ArgeoException(
65 "It is forbidden to add or remove user natures via this method");
66
67 for (String type : userNatures.keySet()) {
68 if (!userNaturesData.containsKey(type))
69 throw new ArgeoException(
70 "Could not find a user nature of type " + type);
71 }
72
73 // for (int i = 0; i < userNatures.size(); i++) {
74 // String type = userNatures.get(i).getType();
75 // boolean found = false;
76 // for (int j = 0; j < userNatures.size(); j++) {
77 // String newType = userNaturesData.get(j).getType();
78 // if (type.equals(newType))
79 // found = true;
80 // }
81 // if (!found)
82 // throw new ArgeoException(
83 // "Could not find a user nature of type " + type);
84 // }
85
86 for (String key : userNatures.keySet()) {
87 userNatures.put(key, userNaturesData.get(key));
88 }
89 }
90
91 @Override
92 public String toString() {
93 return username;
94 }
95
96 public List<String> getRoles() {
97 return roles;
98 }
99
100 public String getUsername() {
101 return username;
102 }
103
104 public void setUsername(String username) {
105 this.username = username;
106 }
107
108 public void setUserNatures(Map<String, UserNature> userNatures) {
109 this.userNatures = userNatures;
110 }
111
112 public void setRoles(List<String> roles) {
113 this.roles = roles;
114 }
115
116 public String getPassword() {
117 return password;
118 }
119
120 public void setPassword(String password) {
121 this.password = password;
122 }
123 }