]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SimpleArgeoUser.java
94c7f9389f1d0dbdb81fa94c265ece4ae446310e
[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 Comparable<ArgeoUser> {
33 private static final long serialVersionUID = 1L;
34
35 private String username;
36 private String password;
37 private Map<String, UserNature> userNatures = new HashMap<String, UserNature>();
38 private List<String> roles = new ArrayList<String>();
39
40 public SimpleArgeoUser() {
41
42 }
43
44 public SimpleArgeoUser(ArgeoUser argeoUser) {
45 username = argeoUser.getUsername();
46 password = argeoUser.getPassword();
47 userNatures = new HashMap<String, UserNature>(
48 argeoUser.getUserNatures());
49 roles = new ArrayList<String>(argeoUser.getRoles());
50 }
51
52 public Map<String, UserNature> getUserNatures() {
53 return userNatures;
54 }
55
56 @Deprecated
57 public void updateUserNatures(Map<String, UserNature> userNaturesData) {
58 updateUserNaturesWithCheck(userNatures, userNaturesData);
59 }
60
61 @Deprecated
62 public static void updateUserNaturesWithCheck(
63 Map<String, UserNature> userNatures,
64 Map<String, UserNature> userNaturesData) {
65 // checks consistency
66 if (userNatures.size() != userNaturesData.size())
67 throw new ArgeoException(
68 "It is forbidden to add or remove user natures via this method");
69
70 for (String type : userNatures.keySet()) {
71 if (!userNaturesData.containsKey(type))
72 throw new ArgeoException(
73 "Could not find a user nature of type " + type);
74 }
75
76 for (String key : userNatures.keySet()) {
77 userNatures.put(key, userNaturesData.get(key));
78 }
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (!(obj instanceof ArgeoUser))
84 return false;
85 return ((ArgeoUser) obj).getUsername().equals(username);
86 }
87
88 public int compareTo(ArgeoUser o) {
89 return username.compareTo(o.getUsername());
90 }
91
92 @Override
93 public int hashCode() {
94 return username.hashCode();
95 }
96
97 @Override
98 public String toString() {
99 return username;
100 }
101
102 public List<String> getRoles() {
103 return roles;
104 }
105
106 public String getUsername() {
107 return username;
108 }
109
110 public void setUsername(String username) {
111 this.username = username;
112 }
113
114 @Deprecated
115 public void setUserNatures(Map<String, UserNature> userNatures) {
116 this.userNatures = userNatures;
117 }
118
119 public void setRoles(List<String> roles) {
120 this.roles = roles;
121 }
122
123 public String getPassword() {
124 return password;
125 }
126
127 public void setPassword(String password) {
128 this.password = password;
129 }
130 }