Index: topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaEntityAuthorizationImpl.java diff -u topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaEntityAuthorizationImpl.java:1.1 topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaEntityAuthorizationImpl.java:1.2 --- topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaEntityAuthorizationImpl.java:1.1 Wed Sep 13 08:45:10 2006 +++ topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaEntityAuthorizationImpl.java Thu Sep 14 13:41:59 2006 @@ -1,13 +1,164 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + package org.codelutin.topia.security.entities.authorization; +import static org.codelutin.topia.security.TopiaSecurityUtil.*; + import java.security.Principal; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; import java.util.Set; +import java.util.StringTokenizer; + +import org.codelutin.topia.security.jaas.TopiaPrincipal; public class TopiaEntityAuthorizationImpl extends TopiaEntityAuthorizationAbstract implements TopiaEntityAuthorization { - public TopiaEntityAuthorizationImpl(String topiaId, Set principals, String actions) { - // TODO Auto-generated constructor stub + private static final long serialVersionUID = 1L; + + public TopiaEntityAuthorizationImpl() { + principals = new HashMap>(); + } + + public TopiaEntityAuthorizationImpl(String id, String actions, Set principals) { + this.id = id; + this.actions = actionsString2Int(actions); + this.principals = new HashMap>(); + for (Principal principal : principals) { + addPrincipal(principal.getClass().getName(), principal.getName()); + } + } + + public void setActions(String actions) { + this.actions = actionsString2Int(actions); + } + + public void setPrincipals(String principals) { + principals = principals.replaceAll(" +", " ");// Un seul espace quand il y en a plusieurs + StringTokenizer tok = new StringTokenizer(principals, " ", false); + while (tok.hasMoreTokens()) { + String principalName = tok.nextToken(); + addPrincipal(TopiaPrincipal.class.getName(), principalName); + } + } + + public boolean implies(TopiaEntityAuthorization other) { + if (!impliesId(getId(), other.getId())) + return false; + if (!impliesActions(actions, other.getActions())) + return false; + return impliesPrincipal(principals, other.getPrincipals()); + } + + /** + * @param thisId + * un id + * @param thatId + * un autre Id + * @return vrai si les ids sont egaux + */ + private boolean impliesId(String thisId, String thatId) { + return (thisId.equals(thatId) || + "*".equals(thisId) || + (thatId.startsWith(thisId.substring(0, thisId.length()-1)) + && thisId.endsWith("#*"))); + } + + /** + * @param thisActions + * @param thatActions + * @return vrai si thisActions implique thatActions. C'est à dire que pour + * chaque action de that, this la contient également + */ + private boolean impliesActions(int thisActions, int thatActions) { + boolean result = true; + if ((thatActions & LOAD) == LOAD) { + result &= ((thisActions & LOAD) == LOAD); + } + if ((thatActions & CREATE) == CREATE) { + result &= ((thisActions & CREATE) == CREATE); + } + if ((thatActions & UPDATE) == UPDATE) { + result &= ((thisActions & UPDATE) == UPDATE); + } + if ((thatActions & DELETE) == DELETE) { + result &= ((thisActions & DELETE) == DELETE); + } + return result; + } + + /** + * @param thisPrincipals + * @param thatPrincipals + * @return vrai si thisPrincipals implique thatPrincipals. + */ + private boolean impliesPrincipal(Map> thisPrincipals, + Map> thatPrincipals) { + // this should never happen + if (thisPrincipals == null || thatPrincipals == null) + return false; + + if (thisPrincipals.size() == 0 || thatPrincipals.size() == 0) { + //FIXME Verifier ce test !!!!! + return true; + } + + boolean result = true; + for (String thisPrincipalClass : thisPrincipals.keySet()) { + if ("*".equals(thisPrincipalClass)) { + continue; + } + + Set thisPrincipalNames = thisPrincipals + .get(thisPrincipalClass); + Set thatPrincipalNames = thatPrincipals + .get(thisPrincipalClass); + + if (thatPrincipalNames != null // that contient bien le principal + // nécessaire ! + && (thisPrincipalNames.contains("*") || thatPrincipalNames + .containsAll(thisPrincipalNames))) { + // (this contient une étoile (accepte tous)) ou (that contient + // tout ce que contient this) + continue; + } + + result = false; + } + return result; + } + + protected void addPrincipal(String principalClass, String principalName) { + Set names = (Set) principals.get(principalClass); + if (names == null) { + principals.put(principalClass, names = new HashSet()); + } + if (principalName.trim().startsWith("[") && principalName.endsWith("]")) { + principalName = principalName.substring(1, principalName + .lastIndexOf("]")); + } + names.add(principalName); + // log.info("principal added : "+principalClass+" "+principalName); } } Index: topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAuthorizationImpl.java diff -u topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAuthorizationImpl.java:1.1 topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAuthorizationImpl.java:1.2 --- topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAuthorizationImpl.java:1.1 Wed Sep 13 08:45:10 2006 +++ topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAuthorizationImpl.java Thu Sep 14 13:41:59 2006 @@ -1,13 +1,29 @@ -package org.codelutin.topia.security.entities.authorization; +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ -public class TopiaAuthorizationImpl extends TopiaAuthorizationAbstract implements - TopiaAuthorization { +package org.codelutin.topia.security.entities.authorization; - public boolean implies(TopiaAuthorization authorization) { - // TODO Auto-generated method stub - return false; - } +public abstract class TopiaAuthorizationImpl extends TopiaAuthorizationAbstract implements + TopiaAuthorization { + public String getActionsAsString() { // TODO Auto-generated method stub return null; @@ -17,5 +33,4 @@ // TODO Auto-generated method stub return null; } - } Index: topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaLinkAuthorizationImpl.java diff -u /dev/null topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaLinkAuthorizationImpl.java:1.1 --- /dev/null Thu Sep 14 13:42:04 2006 +++ topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaLinkAuthorizationImpl.java Thu Sep 14 13:41:59 2006 @@ -0,0 +1,32 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +package org.codelutin.topia.security.entities.authorization; + +public class TopiaLinkAuthorizationImpl extends TopiaLinkAuthorizationAbstract + implements TopiaLinkAuthorization { + + private static final long serialVersionUID = 1L; + + public boolean implies(TopiaEntityAuthorization entityAuthorization) { + return false; + } + +} Index: topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAssociationAuthorizationImpl.java diff -u /dev/null topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAssociationAuthorizationImpl.java:1.1 --- /dev/null Thu Sep 14 13:42:04 2006 +++ topia-security/src/java/org/codelutin/topia/security/entities/authorization/TopiaAssociationAuthorizationImpl.java Thu Sep 14 13:41:59 2006 @@ -0,0 +1,34 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +package org.codelutin.topia.security.entities.authorization; + +public class TopiaAssociationAuthorizationImpl extends + TopiaAssociationAuthorizationAbstract implements + TopiaAssociationAuthorization { + + private static final long serialVersionUID = 1L; + + public boolean implies(TopiaEntityAuthorization entityAuthorization) { + // TODO Auto-generated method stub + return false; + } + +}