X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=security%2Fruntime%2Forg.argeo.security.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2FSimpleArgeoUser.java;h=e736da8f4edfdb430e43117a03bede536aaa143b;hb=c95922edc1d65ef4ef568d66e29ab0bd679693ef;hp=a11081cbd2fe58888a76233812edd7730147f1f2;hpb=f516f257ad75279a2d14ab3d39e041637f10219d;p=lgpl%2Fargeo-commons.git diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SimpleArgeoUser.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SimpleArgeoUser.java index a11081cbd..e736da8f4 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SimpleArgeoUser.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SimpleArgeoUser.java @@ -1,15 +1,35 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.security; import java.io.Serializable; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; + +import org.argeo.ArgeoException; public class SimpleArgeoUser implements ArgeoUser, Serializable { private static final long serialVersionUID = 1L; private String username; private String password; - private List userNatures = new ArrayList(); + private Map userNatures = new HashMap(); private List roles = new ArrayList(); public SimpleArgeoUser() { @@ -19,16 +39,54 @@ public class SimpleArgeoUser implements ArgeoUser, Serializable { public SimpleArgeoUser(ArgeoUser argeoUser) { username = argeoUser.getUsername(); password = argeoUser.getPassword(); - userNatures = new ArrayList(argeoUser.getUserNatures()); + userNatures = new HashMap( + argeoUser.getUserNatures()); roles = new ArrayList(argeoUser.getRoles()); } - public List getUserNatures() { + public Map getUserNatures() { return userNatures; } - public void updateUserNatures(List userNaturesData) { - UserNature.updateUserNaturesWithCheck(userNatures, userNaturesData); + public void updateUserNatures(Map userNaturesData) { + updateUserNaturesWithCheck(userNatures, userNaturesData); + } + + public static void updateUserNaturesWithCheck( + Map userNatures, + Map userNaturesData) { + // checks consistency + if (userNatures.size() != userNaturesData.size()) + throw new ArgeoException( + "It is forbidden to add or remove user natures via this method"); + + for (String type : userNatures.keySet()) { + if (!userNaturesData.containsKey(type)) + throw new ArgeoException( + "Could not find a user nature of type " + type); + } + + // for (int i = 0; i < userNatures.size(); i++) { + // String type = userNatures.get(i).getType(); + // boolean found = false; + // for (int j = 0; j < userNatures.size(); j++) { + // String newType = userNaturesData.get(j).getType(); + // if (type.equals(newType)) + // found = true; + // } + // if (!found) + // throw new ArgeoException( + // "Could not find a user nature of type " + type); + // } + + for (String key : userNatures.keySet()) { + userNatures.put(key, userNaturesData.get(key)); + } + } + + @Override + public String toString() { + return username; } public List getRoles() { @@ -43,7 +101,7 @@ public class SimpleArgeoUser implements ArgeoUser, Serializable { this.username = username; } - public void setUserNatures(List userNatures) { + public void setUserNatures(Map userNatures) { this.userNatures = userNatures; }