You can subscribe to this list here.
2006 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(4) |
Nov
(39) |
Dec
(10) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2007 |
Jan
(19) |
Feb
(150) |
Mar
(10) |
Apr
|
May
(8) |
Jun
(11) |
Jul
(27) |
Aug
(52) |
Sep
(35) |
Oct
(30) |
Nov
(18) |
Dec
(4) |
2008 |
Jan
(76) |
Feb
(121) |
Mar
(39) |
Apr
(55) |
May
(18) |
Jun
(49) |
Jul
(32) |
Aug
(4) |
Sep
(10) |
Oct
|
Nov
(3) |
Dec
(33) |
2009 |
Jan
(19) |
Feb
(87) |
Mar
(69) |
Apr
(38) |
May
(47) |
Jun
(20) |
Jul
(5) |
Aug
(76) |
Sep
(145) |
Oct
(34) |
Nov
(8) |
Dec
(68) |
2010 |
Jan
(150) |
Feb
(379) |
Mar
(191) |
Apr
(100) |
May
(525) |
Jun
(269) |
Jul
(127) |
Aug
(190) |
Sep
(190) |
Oct
(29) |
Nov
(147) |
Dec
(83) |
2011 |
Jan
(188) |
Feb
(81) |
Mar
(43) |
Apr
(97) |
May
(63) |
Jun
(129) |
Jul
(17) |
Aug
(124) |
Sep
(6) |
Oct
(20) |
Nov
(67) |
Dec
(23) |
2012 |
Jan
(6) |
Feb
(14) |
Mar
(181) |
Apr
(64) |
May
(102) |
Jun
(47) |
Jul
(26) |
Aug
(3) |
Sep
(1) |
Oct
(14) |
Nov
(13) |
Dec
(23) |
2013 |
Jan
(4) |
Feb
(14) |
Mar
(18) |
Apr
(14) |
May
(27) |
Jun
(27) |
Jul
(5) |
Aug
(2) |
Sep
(74) |
Oct
(79) |
Nov
(21) |
Dec
(97) |
2014 |
Jan
(6) |
Feb
(3) |
Mar
(8) |
Apr
|
May
(5) |
Jun
|
Jul
(9) |
Aug
(6) |
Sep
(3) |
Oct
(10) |
Nov
(6) |
Dec
|
2015 |
Jan
|
Feb
|
Mar
(1) |
Apr
(25) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2016 |
Jan
|
Feb
|
Mar
|
Apr
(5) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: <fc...@us...> - 2008-02-20 16:56:27
|
Revision: 670 http://openutils.svn.sourceforge.net/openutils/?rev=670&view=rev Author: fcarone Date: 2008-02-20 08:56:29 -0800 (Wed, 20 Feb 2008) Log Message: ----------- [maven-release-plugin] copy for tag openutils-hibernate-security-0.0.3 Added Paths: ----------- tags/openutils-hibernate-security-0.0.3/ tags/openutils-hibernate-security-0.0.3/pom.xml tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/utils/ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/services/SecurityRuleManager.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/services/impl/SecurityRuleManagerImpl.java tags/openutils-hibernate-security-0.0.3/src/test/java/it/openutils/hibernate/security/ tags/openutils-hibernate-security-0.0.3/src/test/resources/SecurityIntegrationTest-load.xml tags/openutils-hibernate-security-0.0.3/src/test/resources/spring-dao.xml tags/openutils-hibernate-security-0.0.3/src/test/resources/spring-managers.xml tags/openutils-hibernate-security-0.0.3/src/test/resources/spring-security.xml Removed Paths: ------------- tags/openutils-hibernate-security-0.0.3/pom.xml tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/services/SecurityRuleManager.java tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/services/impl/SecurityRuleManagerImpl.java tags/openutils-hibernate-security-0.0.3/src/test/java/it/openutils/hibernate/security/ tags/openutils-hibernate-security-0.0.3/src/test/resources/SecurityIntegrationTest-load.xml tags/openutils-hibernate-security-0.0.3/src/test/resources/spring-dao.xml tags/openutils-hibernate-security-0.0.3/src/test/resources/spring-managers.xml tags/openutils-hibernate-security-0.0.3/src/test/resources/spring-security.xml Copied: tags/openutils-hibernate-security-0.0.3 (from rev 645, trunk/openutils-hibernate-security) Deleted: tags/openutils-hibernate-security-0.0.3/pom.xml =================================================================== --- trunk/openutils-hibernate-security/pom.xml 2008-02-19 18:28:34 UTC (rev 645) +++ tags/openutils-hibernate-security-0.0.3/pom.xml 2008-02-20 16:56:29 UTC (rev 670) @@ -1,225 +0,0 @@ -<?xml version="1.0"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <parent> - <artifactId>openutils</artifactId> - <groupId>net.sourceforge.openutils</groupId> - <version>7</version> - <relativePath>..</relativePath> - </parent> - <modelVersion>4.0.0</modelVersion> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-hibernate-security</artifactId> - <name>openutils hibernate security</name> - <version>0.0.3-SNAPSHOT</version> - <description>Hibernate Security classes</description> - <properties> - <spring.version>2.5.1</spring.version> - <aspectj.version>1.5.3</aspectj.version> - </properties> - <dependencies> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-core</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-context</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-aop</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-orm</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <!-- already imported cglib-nodep by spring --> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>aspectj</groupId> - <artifactId>aspectjrt</artifactId> - <version>${aspectj.version}</version> - </dependency> - <dependency> - <groupId>aspectj</groupId> - <artifactId>aspectjweaver</artifactId> - <version>${aspectj.version}</version> - </dependency> - <dependency> - <groupId>cglib</groupId> - <artifactId>cglib-nodep</artifactId> - <version>2.1_3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm</artifactId> - <version>2.2.3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm-attrs</artifactId> - <version>2.2.3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm-commons</artifactId> - <version>2.2.3</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-usermanagement</artifactId> - <version>2.0</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-usermanagement-dataobjects</artifactId> - <version>2.0</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-bshd5</artifactId> - <version>2.0.1</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-testing-junit</artifactId> - <version>2.0.2</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-testing</artifactId> - <version>2.0.3</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.4.1</version> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate</artifactId> - <version>3.2.5.ga</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-annotations</artifactId> - <version>3.2.1.ga</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>commons-dbcp</groupId> - <artifactId>commons-dbcp</artifactId> - <version>1.2.1</version> - <scope>test</scope> - <exclusions> - <exclusion> - <artifactId>xerces</artifactId> - <groupId>xerces</groupId> - </exclusion> - <exclusion> - <artifactId>xml-apis</artifactId> - <groupId>xml-apis</groupId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.2</version> - </dependency> - <dependency> - <groupId>org.acegisecurity</groupId> - <artifactId>acegi-security</artifactId> - <version>1.0.6</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <artifactId>spring-remoting</artifactId> - <groupId>org.springframework</groupId> - </exclusion> - <exclusion> - <artifactId>spring-jdbc</artifactId> - <groupId>org.springframework</groupId> - </exclusion> - <exclusion> - <artifactId>spring-support</artifactId> - <groupId>org.springframework</groupId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>hsqldb</groupId> - <artifactId>hsqldb</artifactId> - <version>1.8.0.7</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.4</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>dbunit</groupId> - <artifactId>dbunit</artifactId> - <version>2.1</version> - <scope>test</scope> - </dependency> - </dependencies> -</project> Copied: tags/openutils-hibernate-security-0.0.3/pom.xml (from rev 669, trunk/openutils-hibernate-security/pom.xml) =================================================================== --- tags/openutils-hibernate-security-0.0.3/pom.xml (rev 0) +++ tags/openutils-hibernate-security-0.0.3/pom.xml 2008-02-20 16:56:29 UTC (rev 670) @@ -0,0 +1,229 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <artifactId>openutils</artifactId> + <groupId>net.sourceforge.openutils</groupId> + <version>7</version> + <relativePath>..</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-hibernate-security</artifactId> + <name>openutils hibernate security</name> + <version>0.0.3</version> + <description>Hibernate Security classes</description> + <properties> + <spring.version>2.5.1</spring.version> + <aspectj.version>1.5.3</aspectj.version> + </properties> + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-aop</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-orm</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <!-- already imported cglib-nodep by spring --> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>aspectj</groupId> + <artifactId>aspectjrt</artifactId> + <version>${aspectj.version}</version> + </dependency> + <dependency> + <groupId>aspectj</groupId> + <artifactId>aspectjweaver</artifactId> + <version>${aspectj.version}</version> + </dependency> + <dependency> + <groupId>cglib</groupId> + <artifactId>cglib-nodep</artifactId> + <version>2.1_3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm</artifactId> + <version>2.2.3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm-attrs</artifactId> + <version>2.2.3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm-commons</artifactId> + <version>2.2.3</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-usermanagement</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-usermanagement-dataobjects</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-bshd5</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-testing-junit</artifactId> + <version>2.0.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-testing</artifactId> + <version>2.0.3</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.4.1</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate</artifactId> + <version>3.2.5.ga</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-annotations</artifactId> + <version>3.2.1.ga</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>commons-dbcp</groupId> + <artifactId>commons-dbcp</artifactId> + <version>1.2.1</version> + <scope>test</scope> + <exclusions> + <exclusion> + <artifactId>xerces</artifactId> + <groupId>xerces</groupId> + </exclusion> + <exclusion> + <artifactId>xml-apis</artifactId> + <groupId>xml-apis</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.2</version> + </dependency> + <dependency> + <groupId>org.acegisecurity</groupId> + <artifactId>acegi-security</artifactId> + <version>1.0.6</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <artifactId>spring-remoting</artifactId> + <groupId>org.springframework</groupId> + </exclusion> + <exclusion> + <artifactId>spring-jdbc</artifactId> + <groupId>org.springframework</groupId> + </exclusion> + <exclusion> + <artifactId>spring-support</artifactId> + <groupId>org.springframework</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <version>1.8.0.7</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.4</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>dbunit</groupId> + <artifactId>dbunit</artifactId> + <version>2.1</version> + <scope>test</scope> + </dependency> + </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-hibernate-security-0.0.3</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-hibernate-security-0.0.3</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-hibernate-security-0.0.3</url> + </scm> +</project> \ No newline at end of file Deleted: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-19 18:28:34 UTC (rev 645) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-20 16:56:29 UTC (rev 670) @@ -1,155 +0,0 @@ -/* - * Copyright Openmind http://www.openmindonline.it - * - * 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 it.openutils.hibernate.security.aop; - -import it.openutils.hibernate.security.dataobject.SecurityRule; -import it.openutils.hibernate.security.services.SecurityRuleManager; - -import java.util.ArrayList; -import java.util.List; - -import org.acegisecurity.Authentication; -import org.acegisecurity.GrantedAuthority; -import org.acegisecurity.context.SecurityContextHolder; -import org.apache.commons.lang.StringUtils; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.hibernate.Filter; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * @author fcarone - * @version $Id: $ - */ -@Aspect -public class AOPSecurity -{ - - /** - * Logger. - */ - private Logger log = LoggerFactory.getLogger(AOPSecurity.class); - - private SecurityRuleManager securityRuleManager; - - private List<String> securedDAOs; - - private boolean enabled; - - /** - * @param pjp The proceeding joinpoint - * @param filter The entity we are going to filter - * @param additionalCriteria The additional criteria list, cannot be null - * @return The execution invocation result - * @throws Throwable Any exception occurring in the invoked method - */ - @Around("execution(* it.openutils.dao.hibernate.*.*(Object, .., java.util.List<org.hibernate.criterion.Criterion>)) && " - + // - " args(filter, .., additionalCriteria)") - public Object applySecurityRules(ProceedingJoinPoint pjp, Object filter, List<Criterion> additionalCriteria) - throws Throwable - { - if (!enabled) - { - log.debug("DAO security disabled, proceeding."); - return pjp.proceed(); - } - - if (!securedDAOs.contains(pjp.getTarget().getClass().getCanonicalName())) - { - log.debug("The intercepted DAO {} is not secured, proceeding.", pjp.getTarget().toString()); - return pjp.proceed(); - } - - log.debug("applying security rules for {} with criteria {}", filter.toString(), additionalCriteria); - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication == null) - { - throw new SecurityException("Authentication is not valid"); - } - GrantedAuthority[] authorities = authentication.getAuthorities(); - List<String> roles = new ArrayList<String>(); - for (int i = 0; i < authorities.length; i++) - { - roles.add(authorities[i].getAuthority()); - } - - String entity = filter.getClass().getCanonicalName(); - List<SecurityRule> rules = securityRuleManager.getRulesForRoles(entity, roles); - - if (rules.isEmpty()) - { - if (log.isWarnEnabled()) - { - String grantedRoles = StringUtils.EMPTY; - for (int i = 0; i < authorities.length; i++) - { - grantedRoles += authorities[i].getAuthority() + " "; - } - log.warn("Access is denied on " + entity + ", for user {} with roles {}", SecurityContextHolder - .getContext() - .getAuthentication() - .getPrincipal() - .toString(), grantedRoles); - } - throw new SecurityException("Access denied"); - } - - Filter hibernateFilter = securityRuleManager.getEntityFilterFromRules(entity, rules); - - Criterion sqlCriterion = Restrictions.sqlRestriction(hibernateFilter - .getFilterDefinition() - .getDefaultFilterCondition()); - - log.debug("Adding sql restriction: {}", sqlCriterion.toString()); - additionalCriteria.add(sqlCriterion); - - Object result = pjp.proceed(); - return result; - } - - /** - * Sets the securityRuleManager. - * @param securityRuleManager the securityRuleManager to set - */ - public void setSecurityRuleManager(SecurityRuleManager securityRuleManager) - { - this.securityRuleManager = securityRuleManager; - } - - /** - * Sets the securedDAOs. - * @param securedDAOs the securedDAOs to set - */ - public void setSecuredDAOs(List<String> securedDAOs) - { - this.securedDAOs = securedDAOs; - } - - /** - * Sets the enabled. - * @param enabled the enabled to set - */ - public void setEnabled(boolean enabled) - { - this.enabled = enabled; - } -} Copied: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java (from rev 656, trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java) =================================================================== --- tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java (rev 0) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-20 16:56:29 UTC (rev 670) @@ -0,0 +1,154 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.hibernate.security.aop; + +import it.openutils.hibernate.security.dataobject.SecurityRule; +import it.openutils.hibernate.security.services.SecurityRuleManager; + +import java.util.ArrayList; +import java.util.List; + +import org.acegisecurity.Authentication; +import org.acegisecurity.GrantedAuthority; +import org.acegisecurity.context.SecurityContextHolder; +import org.apache.commons.lang.StringUtils; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.hibernate.Filter; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fcarone + * @version $Id: $ + */ +@Aspect +public class AOPSecurity +{ + + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(AOPSecurity.class); + + private SecurityRuleManager securityRuleManager; + + private List<String> securedDAOs; + + private boolean enabled; + + /** + * @param pjp The proceeding joinpoint + * @param filter The entity we are going to filter + * @param additionalCriteria The additional criteria list, cannot be null + * @return The execution invocation result + * @throws Throwable Any exception occurring in the invoked method + */ + @Around("execution(* it.openutils.dao.hibernate.*.*(Object, .., java.util.List<org.hibernate.criterion.Criterion>)) && " + + // + " args(filter, .., additionalCriteria)") + public Object applySecurityRules(ProceedingJoinPoint pjp, Object filter, List<Criterion> additionalCriteria) + throws Throwable + { + if (!enabled) + { + log.debug("DAO security disabled, proceeding."); + return pjp.proceed(); + } + + if (!securedDAOs.contains(pjp.getTarget().getClass().getCanonicalName())) + { + log.debug("The intercepted DAO {} is not secured, proceeding.", pjp.getTarget().toString()); + return pjp.proceed(); + } + + log.debug("applying security rules for {} with criteria {}", filter.toString(), additionalCriteria); + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null) + { + throw new SecurityException("Authentication is not valid"); + } + GrantedAuthority[] authorities = authentication.getAuthorities(); + List<String> roles = new ArrayList<String>(); + for (int i = 0; i < authorities.length; i++) + { + roles.add(authorities[i].getAuthority()); + } + + String entity = filter.getClass().getCanonicalName(); + List<SecurityRule> rules = securityRuleManager.getRulesForRoles(entity, roles); + + if (rules.isEmpty()) + { + if (log.isWarnEnabled()) + { + String grantedRoles = StringUtils.EMPTY; + for (int i = 0; i < authorities.length; i++) + { + grantedRoles += authorities[i].getAuthority() + " "; + } + log.warn( + "No rules found. Access is denied on " + entity + ", for user {} with roles {}", + SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString(), + grantedRoles); + } + throw new SecurityException("Access denied"); + } + + Filter hibernateFilter = securityRuleManager.getEntityFilterFromRules(entity, rules); + + Criterion sqlCriterion = Restrictions.sqlRestriction(hibernateFilter + .getFilterDefinition() + .getDefaultFilterCondition()); + + log.debug("Adding sql restriction: {}", sqlCriterion.toString()); + additionalCriteria.add(sqlCriterion); + + Object result = pjp.proceed(); + return result; + } + + /** + * Sets the securityRuleManager. + * @param securityRuleManager the securityRuleManager to set + */ + public void setSecurityRuleManager(SecurityRuleManager securityRuleManager) + { + this.securityRuleManager = securityRuleManager; + } + + /** + * Sets the securedDAOs. + * @param securedDAOs the securedDAOs to set + */ + public void setSecuredDAOs(List<String> securedDAOs) + { + this.securedDAOs = securedDAOs; + } + + /** + * Sets the enabled. + * @param enabled the enabled to set + */ + public void setEnabled(boolean enabled) + { + this.enabled = enabled; + } +} Deleted: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-19 18:28:34 UTC (rev 645) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 16:56:29 UTC (rev 670) @@ -1,215 +0,0 @@ -/* - * Copyright Openmind http://www.openmindonline.it - * - * 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 it.openutils.hibernate.security.filter; - -import it.openutils.hibernate.security.dataobject.SecurityRule; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Table; - -import org.apache.commons.lang.StringUtils; -import org.hibernate.Filter; -import org.hibernate.HibernateException; -import org.hibernate.engine.FilterDefinition; - - -/** - * @author fcarone - * @version $Id: $ - */ -public class JavaBeanFilter implements Filter -{ - - private FilterDefinition filterDefinition; - - /** - * @param bean The bean to set rules for - * @param securityRules The list of {@link SecurityRule}s to apply. - * @throws ClassNotFoundException If the bean class has not been found - * @throws InstantiationException If the bean doesn't contain the no-arg constructor - * @throws IllegalAccessException If the bean properties cannot be accessed - * @throws SecurityException If the bean class cannot be accessed - * @throws NoSuchFieldException If the property contained in the security rule refers to a bean non-existent field - */ - @SuppressWarnings("unchecked") - public JavaBeanFilter(String bean, List<SecurityRule> securityRules) - throws ClassNotFoundException, - InstantiationException, - IllegalAccessException, - SecurityException, - NoSuchFieldException - { - Class< ? extends Object> beanClass = Class.forName(bean, true, this.getClass().getClassLoader()); - - if (!beanClass.isAnnotationPresent(Entity.class)) - { - throw new IllegalArgumentException("Class " + bean + " must contain the @Entity annotation."); - } - if (!(beanClass.isAnnotationPresent(Table.class) || beanClass - .isAnnotationPresent(org.hibernate.annotations.Table.class))) - { - throw new IllegalArgumentException("Class " + bean + " must contain the @Table annotation."); - } - - Map<String, String> propertyColumnMap = new HashMap<String, String>(); - - StringBuffer filterDefCondition = new StringBuffer(); - String filterName = StringUtils.EMPTY; - - Map<String, List<SecurityRule>> roleRuleMap = new LinkedHashMap<String, List<SecurityRule>>(); - for (SecurityRule securityRule : securityRules) - { - if (!roleRuleMap.containsKey(securityRule.getRole())) - { - roleRuleMap.put(securityRule.getRole(), new ArrayList<SecurityRule>()); - } - roleRuleMap.get(securityRule.getRole()).add(securityRule); - } - - for (Map.Entry<String, List<SecurityRule>> entry : roleRuleMap.entrySet()) - { - filterName += entry.getKey(); - - List<SecurityRule> rules = entry.getValue(); - if (!StringUtils.isEmpty(filterDefCondition.toString())) - { - filterDefCondition.append(" OR "); - } - filterDefCondition.append("("); - StringBuffer subFilterCond = new StringBuffer(); - for (SecurityRule securityRule : rules) - { - String property = securityRule.getProperty(); - filterName += property; - - Field field = beanClass.getDeclaredField(property); - - // @todo: annotations may also be defined on getters/setters... - propertyColumnMap.put(property, field.getAnnotation(Column.class).name()); - if (!StringUtils.isEmpty(subFilterCond.toString())) - { - subFilterCond.append(" AND "); - } - String modifier = null; - String startQuote = null; - String endQuote = null; - - if (field.getType().isAssignableFrom(String.class)) - { - startQuote = "\'"; - endQuote = startQuote; - } - else if (field.getType().getSuperclass().isAssignableFrom(Number.class)) - { - startQuote = StringUtils.EMPTY; - endQuote = StringUtils.EMPTY; - } - - switch (securityRule.getModifier()) - { - case EQUALS : - modifier = " = "; - break; - - case NOT : - modifier = " != "; - break; - - default : - throw new IllegalArgumentException("Modifier " + securityRule.getModifier() + "not recognized."); - } - subFilterCond.append(field.getAnnotation(Column.class).name()); - subFilterCond.append(modifier); - if (StringUtils.isNotEmpty(startQuote)) - { - subFilterCond.append(startQuote); - } - subFilterCond.append(securityRule.getValue()); - if (StringUtils.isNotEmpty(endQuote)) - { - subFilterCond.append(endQuote); - } - - filterName += securityRule.getValue(); - } - filterDefCondition.append(subFilterCond); - filterDefCondition.append(")"); - } - - // filtername is unique, but untraceable - this.filterDefinition = new FilterDefinition(Integer.toString(filterName.hashCode()), filterDefCondition - .toString(), new HashMap()); - } - - /** - * {@inheritDoc} - */ - public FilterDefinition getFilterDefinition() - { - return this.filterDefinition; - } - - /** - * {@inheritDoc} - */ - public String getName() - { - return this.filterDefinition.getFilterName(); - } - - /** - * {@inheritDoc} - */ - public Filter setParameter(String name, Object value) - { - return this; - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings("unchecked") - public Filter setParameterList(String name, Collection values) - { - return this; - } - - /** - * {@inheritDoc} - */ - public Filter setParameterList(String name, Object[] values) - { - return this; - } - - /** - * {@inheritDoc} - */ - public void validate() throws HibernateException - { - // - } - -} Copied: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java (from rev 659, trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java) =================================================================== --- tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java (rev 0) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 16:56:29 UTC (rev 670) @@ -0,0 +1,258 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.dataobject.PermissionEnum; +import it.openutils.hibernate.security.dataobject.SecurityRule; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; +import org.hibernate.Filter; +import org.hibernate.HibernateException; +import org.hibernate.engine.FilterDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class JavaBeanFilter implements Filter +{ + + private FilterDefinition filterDefinition; + + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(JavaBeanFilter.class); + + + /** + * @param bean The bean to set rules for + * @param securityRules The list of {@link SecurityRule}s to apply. + * @throws ClassNotFoundException If the bean class has not been found + * @throws InstantiationException If the bean doesn't contain the no-arg constructor + * @throws IllegalAccessException If the bean properties cannot be accessed + * @throws SecurityException If the bean class cannot be accessed + * @throws NoSuchFieldException If the property contained in the security rule refers to a bean non-existent field + */ + @SuppressWarnings("unchecked") + public JavaBeanFilter(String bean, List<SecurityRule> securityRules) + throws ClassNotFoundException, + InstantiationException, + IllegalAccessException, + SecurityException, + NoSuchFieldException + { + Class< ? extends Object> beanClass = Class.forName(bean, true, this.getClass().getClassLoader()); + + if (!beanClass.isAnnotationPresent(Entity.class)) + { + throw new IllegalArgumentException("Class " + bean + " must contain the @Entity annotation."); + } + if (!(beanClass.isAnnotationPresent(Table.class) || beanClass + .isAnnotationPresent(org.hibernate.annotations.Table.class))) + { + throw new IllegalArgumentException("Class " + bean + " must contain the @Table annotation."); + } + + Map<String, String> propertyColumnMap = new HashMap<String, String>(); + + StringBuffer filterDefCondition = new StringBuffer(); + String filterName = StringUtils.EMPTY; + + Map<String, List<SecurityRule>> roleRuleMap = new LinkedHashMap<String, List<SecurityRule>>(); + for (SecurityRule securityRule : securityRules) + { + if (!roleRuleMap.containsKey(securityRule.getRole())) + { + roleRuleMap.put(securityRule.getRole(), new ArrayList<SecurityRule>()); + } + roleRuleMap.get(securityRule.getRole()).add(securityRule); + } + + for (Map.Entry<String, List<SecurityRule>> entry : roleRuleMap.entrySet()) + { + filterName += entry.getKey(); + + List<SecurityRule> rules = entry.getValue(); + if (rules == null || rules.isEmpty()) + { + log.debug("No rules defined for role {}", entry.getKey()); + continue; + } + + if (!rulesContainLoad(rules)) + { + log.debug("No LOAD rules defined for role {}", entry.getKey()); + continue; + } + + if (!StringUtils.isEmpty(filterDefCondition.toString())) + { + filterDefCondition.append(" OR "); + } + filterDefCondition.append("("); + StringBuffer subFilterCond = new StringBuffer(); + for (SecurityRule securityRule : rules) + { + if (!securityRule.getPermissions().contains(PermissionEnum.LOAD)) + { + log.debug("Skipping rule {} since it is not related to LOAD.", securityRule); + continue; + } + String property = securityRule.getProperty(); + filterName += property; + + Field field = beanClass.getDeclaredField(property); + + // @todo: annotations may also be defined on getters/setters... + propertyColumnMap.put(property, field.getAnnotation(Column.class).name()); + if (!StringUtils.isEmpty(subFilterCond.toString())) + { + subFilterCond.append(" AND "); + } + String modifier = null; + String startQuote = null; + String endQuote = null; + + if (String.class.isAssignableFrom(field.getType())) + { + startQuote = "\'"; + endQuote = startQuote; + } + else if (Number.class.isAssignableFrom(field.getType())) + { + startQuote = StringUtils.EMPTY; + endQuote = StringUtils.EMPTY; + } + + switch (securityRule.getModifier()) + { + case EQUALS : + modifier = " = "; + break; + + case NOT : + modifier = " != "; + break; + + default : + throw new IllegalArgumentException("Modifier " + securityRule.getModifier() + "not recognized."); + } + subFilterCond.append(field.getAnnotation(Column.class).name()); + subFilterCond.append(modifier); + if (StringUtils.isNotEmpty(startQuote)) + { + subFilterCond.append(startQuote); + } + subFilterCond.append(StringEscapeUtils.escapeSql(securityRule.getValue())); + if (StringUtils.isNotEmpty(endQuote)) + { + subFilterCond.append(endQuote); + } + + filterName += securityRule.getValue(); + } + filterDefCondition.append(subFilterCond); + filterDefCondition.append(")"); + } + + // filtername is unique, but untraceable + this.filterDefinition = new FilterDefinition(Integer.toString(filterName.hashCode()), filterDefCondition + .toString(), new HashMap()); + } + + /** + * @param rules + * @return + */ + private boolean rulesContainLoad(List<SecurityRule> rules) + { + for (SecurityRule rule : rules) + { + if (rule.getPermissions().contains(PermissionEnum.LOAD)) + { + return true; + } + } + return false; + } + + /** + * {@inheritDoc} + */ + public FilterDefinition getFilterDefinition() + { + return this.filterDefinition; + } + + /** + * {@inheritDoc} + */ + public String getName() + { + return this.filterDefinition.getFilterName(); + } + + /** + * {@inheritDoc} + */ + public Filter setParameter(String name, Object value) + { + return this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public Filter setParameterList(String name, Collection values) + { + return this; + } + + /** + * {@inheritDoc} + */ + public Filter setParameterList(String name, Object[] values) + { + return this; + } + + /** + * {@inheritDoc} + */ + public void validate() throws HibernateException + { + // + } + +} Deleted: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-19 18:28:34 UTC (rev 645) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 16:56:29 UTC (rev 670) @@ -1,222 +0,0 @@ -/* - * Copyright Openmind http://www.openmindonline.it - * - * 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 it.openutils.hibernate.security.filter; - -import it.openutils.hibernate.security.dataobject.ModifierEnum; -import it.openutils.hibernate.security.dataobject.PermissionEnum; -import it.openutils.hibernate.security.dataobject.SecurityRule; -import it.openutils.hibernate.security.services.SecurityRuleManager; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.acegisecurity.Authentication; -import org.acegisecurity.ConfigAttribute; -import org.acegisecurity.ConfigAttributeDefinition; -import org.acegisecurity.GrantedAuthority; -import org.acegisecurity.vote.AccessDecisionVoter; -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.aop.framework.ReflectiveMethodInvocation; - - -/** - * @author fcarone - * @version $Id: $ - */ -public class SecurityRuleAccessDecisionVoter implements AccessDecisionVoter -{ - - /** - * Logger. - */ - private static Logger log = LoggerFactory.getLogger(SecurityRuleAccessDecisionVoter.class); - - private SecurityRuleManager securityRuleManager; - - /** - * {@inheritDoc} - */ - public boolean supports(ConfigAttribute attribute) - { - log.debug("Evaluating attribute {}", attribute.getAttribute()); - - if (attribute.getAttribute() != null) - { - for (PermissionEnum permission : PermissionEnum.values()) - { - if (StringUtils.equals(permission.getValue(), attribute.getAttribute())) - { - log.debug("Support ok."); - return true; - } - } - } - log.debug("Not supporting attribute."); - return false; - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings("unchecked") - public boolean supports(Class clazz) - { - return true; - } - - /** - * {@inheritDoc} - */ - public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config) - { - List<String> roles = new ArrayList<String>(); - roles.addAll(getRolesFromAuthentication(authentication)); - - if (object instanceof ReflectiveMethodInvocation) - { - ReflectiveMethodInvocation methodInvocation = ((ReflectiveMethodInvocation) object); - for (Object argument : methodInvocation.getArguments()) - { - log.debug("Evaluating argument {}", argument); - List<SecurityRule> rules = securityRuleManager.getRulesForRoles(argument.getClass().getName(), roles); - boolean permissionMatches = checkPermissions(rules, config); - if (!permissionMatches) - { - return ACCESS_DENIED; - } - - boolean areRulesMatching = checkRules(rules, argument); - if (!areRulesMatching) - { - return ACCESS_DENIED; - } - } - } - - return ACCESS_GRANTED; - } - - /** - * @param authentication - * @return - */ - private Collection< ? extends String> getRolesFromAuthentication(Authentication authentication) - { - List<String> roles = new ArrayList<String>(); - for (GrantedAuthority authority : authentication.getAuthorities()) - { - log.debug("Granted authority for user {}: {}", authentication.getName(), authority.getAuthority()); - roles.add(authority.getAuthority()); - } - return roles; - } - - /** - * @param rules - * @param argument - * @return - */ - private boolean checkRules(List<SecurityRule> rules, Object argument) - { - log.debug("Evaluating rules."); - try - { - for (SecurityRule rule : rules) - { - String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); - if (rule.getModifier() == ModifierEnum.EQUALS) - { - if (StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - } - else if (rule.getModifier() == ModifierEnum.NOT) - { - if (!StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - } - else - { - throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); - } - } - } - catch (NoSuchMethodException e) - { - log.error("{}", e); - } - catch (IllegalAccessException e) - { - log.error("{}", e); - } - catch (InvocationTargetException e) - { - log.error("{}", e); - } - log.debug("No matching rules found."); - return false; - } - - /** - * @param rules - * @param config - * @return - */ - @SuppressWarnings("unchecked") - private boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) - { - log.debug("Evaluation permissions"); - Iterator iterator = config.getConfigAttributes(); - while (iterator.hasNext()) - { - String attribute = ((ConfigAttribute) iterator.next()).getAttribute(); - for (SecurityRule rule : rules) - { - for (PermissionEnum permission : rule.getPermissions()) - { - if (StringUtils.equals(permission.getValue(), attribute)) - { - log.debug("Matching permission: {}", permission.getValue()); - return true; - } - } - } - } - log.debug("No matching permissions found."); - return false; - } - - /** - * Sets the securityRuleManager. - * @param securityRuleManager the securityRuleManager to set - */ - public void setSecurityRuleManager(SecurityRuleManager securityRuleManager) - { - this.securityRuleManager = securityRuleManager; - } - -} Copied: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java (from rev 668, trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java) =================================================================== --- tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java (rev 0) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 16:56:29 UTC (rev 670) @@ -0,0 +1,87 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.dataobject.SecurityRule; +import it.openutils.hibernate.security.filter.utils.SecurityRuleUtils; +import it.openutils.hibernate.security.services.SecurityRuleManager; + +import java.util.List; + +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.vote.AccessDecisionVoter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.aop.framework.ReflectiveMethodInvocation; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class SecurityRuleAccessDecisionVoter extends SecurityRuleBaseHandler implements AccessDecisionVoter +{ + + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(SecurityRuleAccessDecisionVoter.class); + + private SecurityRuleManager securityRuleManager; + + private SecurityRuleUtils ruleUtils = new SecurityRuleUtils(); + + /** + * {@inheritDoc} + */ + public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config) + { + List<String> roles = ruleUtils.getRolesFromAuthentication(authentication); + + if (object instanceof ReflectiveMethodInvocation) + { + ReflectiveMethodInvocation methodInvocation = ((ReflectiveMethodInvocation) object); + for (Object argument : methodInvocation.getArguments()) + { + log.debug("Evaluating argument {}", argument); + List<SecurityRule> rules = securityRuleManager.getRulesForRoles(ruleUtils.getClassName(argument), roles); + boolean permissionMatches = ruleUtils.checkPermissions(rules, config); + if (!permissionMatches) + { + return ACCESS_DENIED; + } + + boolean areRulesMatching = ruleUtils.checkRules(rules, argument); + if (!areRulesMatching) + { + return ACCESS_DENIED; + } + } + } + + return ACCESS_GRANTED; + } + + /** + * Sets the securityRuleManager. + * @param securityRuleManager the securityRuleManager to set + */ + public void setSecurityRuleManager(SecurityRuleManager securityRuleManager) + { + this.securityRuleManager = securityRuleManager; + } +} Copied: tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java (from rev 667, trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java) =================================================================== --- tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java (rev 0) +++ tags/openutils-hibernate-security-0.0.3/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java 2008-02-20 16:56:29 UTC (rev 670) @@ -0,0 +1,70 @@ +/* + * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + */ +package it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.services.SecurityRuleManager; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.acegisecurity.AccessDeniedException; +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.afterinvocation.AfterInvocationProvider; +import org.apache.commons.collections.CollectionUtils; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class SecurityRuleAfterInvocationHandler extends SecurityRuleBaseHandler implements AfterInvocationProvider +{ + + private SecurityRuleManager securityRuleManager; + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config, + Object returnedObject) throws AccessDeniedException + { ... [truncated message content] |
From: <fc...@us...> - 2008-02-20 16:50:44
|
Revision: 669 http://openutils.svn.sourceforge.net/openutils/?rev=669&view=rev Author: fcarone Date: 2008-02-20 08:50:46 -0800 (Wed, 20 Feb 2008) Log Message: ----------- [maven-release-plugin] prepare release openutils-hibernate-security-0.0.3 Modified Paths: -------------- trunk/openutils-hibernate-security/pom.xml Modified: trunk/openutils-hibernate-security/pom.xml =================================================================== --- trunk/openutils-hibernate-security/pom.xml 2008-02-20 16:04:43 UTC (rev 668) +++ trunk/openutils-hibernate-security/pom.xml 2008-02-20 16:50:46 UTC (rev 669) @@ -1,225 +1,229 @@ -<?xml version="1.0"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <parent> - <artifactId>openutils</artifactId> - <groupId>net.sourceforge.openutils</groupId> - <version>7</version> - <relativePath>..</relativePath> - </parent> - <modelVersion>4.0.0</modelVersion> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-hibernate-security</artifactId> - <name>openutils hibernate security</name> - <version>0.0.3-SNAPSHOT</version> - <description>Hibernate Security classes</description> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <artifactId>openutils</artifactId> + <groupId>net.sourceforge.openutils</groupId> + <version>7</version> + <relativePath>..</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-hibernate-security</artifactId> + <name>openutils hibernate security</name> + <version>0.0.3</version> + <description>Hibernate Security classes</description> <properties> <spring.version>2.5.1</spring.version> <aspectj.version>1.5.3</aspectj.version> </properties> - <dependencies> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-core</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-context</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-aop</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-orm</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <!-- already imported cglib-nodep by spring --> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>aspectj</groupId> - <artifactId>aspectjrt</artifactId> - <version>${aspectj.version}</version> - </dependency> - <dependency> - <groupId>aspectj</groupId> - <artifactId>aspectjweaver</artifactId> - <version>${aspectj.version}</version> - </dependency> - <dependency> - <groupId>cglib</groupId> - <artifactId>cglib-nodep</artifactId> - <version>2.1_3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm</artifactId> - <version>2.2.3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm-attrs</artifactId> - <version>2.2.3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm-commons</artifactId> - <version>2.2.3</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-usermanagement</artifactId> - <version>2.0</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-usermanagement-dataobjects</artifactId> - <version>2.0</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-bshd5</artifactId> - <version>2.0.1</version> - </dependency> - <dependency> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils-testing-junit</artifactId> - <version>2.0.2</version> - <scope>test</scope> + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-aop</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-orm</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <!-- already imported cglib-nodep by spring --> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>aspectj</groupId> + <artifactId>aspectjrt</artifactId> + <version>${aspectj.version}</version> + </dependency> + <dependency> + <groupId>aspectj</groupId> + <artifactId>aspectjweaver</artifactId> + <version>${aspectj.version}</version> + </dependency> + <dependency> + <groupId>cglib</groupId> + <artifactId>cglib-nodep</artifactId> + <version>2.1_3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm</artifactId> + <version>2.2.3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm-attrs</artifactId> + <version>2.2.3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm-commons</artifactId> + <version>2.2.3</version> + </dependency> + <dependency> <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-usermanagement</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-usermanagement-dataobjects</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-bshd5</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils-testing-junit</artifactId> + <version>2.0.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>net.sourceforge.openutils</groupId> <artifactId>openutils-testing</artifactId> <version>2.0.3</version> <scope>test</scope> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.4.1</version> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate</artifactId> - <version>3.2.5.ga</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-annotations</artifactId> - <version>3.2.1.ga</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>commons-dbcp</groupId> - <artifactId>commons-dbcp</artifactId> - <version>1.2.1</version> - <scope>test</scope> - <exclusions> - <exclusion> - <artifactId>xerces</artifactId> - <groupId>xerces</groupId> - </exclusion> - <exclusion> - <artifactId>xml-apis</artifactId> - <groupId>xml-apis</groupId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.2</version> - </dependency> - <dependency> - <groupId>org.acegisecurity</groupId> - <artifactId>acegi-security</artifactId> - <version>1.0.6</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <artifactId>spring-remoting</artifactId> - <groupId>org.springframework</groupId> - </exclusion> - <exclusion> - <artifactId>spring-jdbc</artifactId> - <groupId>org.springframework</groupId> - </exclusion> - <exclusion> - <artifactId>spring-support</artifactId> - <groupId>org.springframework</groupId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>hsqldb</groupId> - <artifactId>hsqldb</artifactId> - <version>1.8.0.7</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.4</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>dbunit</groupId> - <artifactId>dbunit</artifactId> - <version>2.1</version> - <scope>test</scope> - </dependency> - </dependencies> -</project> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.4.1</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate</artifactId> + <version>3.2.5.ga</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-annotations</artifactId> + <version>3.2.1.ga</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>commons-dbcp</groupId> + <artifactId>commons-dbcp</artifactId> + <version>1.2.1</version> + <scope>test</scope> + <exclusions> + <exclusion> + <artifactId>xerces</artifactId> + <groupId>xerces</groupId> + </exclusion> + <exclusion> + <artifactId>xml-apis</artifactId> + <groupId>xml-apis</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.2</version> + </dependency> + <dependency> + <groupId>org.acegisecurity</groupId> + <artifactId>acegi-security</artifactId> + <version>1.0.6</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <artifactId>spring-remoting</artifactId> + <groupId>org.springframework</groupId> + </exclusion> + <exclusion> + <artifactId>spring-jdbc</artifactId> + <groupId>org.springframework</groupId> + </exclusion> + <exclusion> + <artifactId>spring-support</artifactId> + <groupId>org.springframework</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <version>1.8.0.7</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.4</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>dbunit</groupId> + <artifactId>dbunit</artifactId> + <version>2.1</version> + <scope>test</scope> + </dependency> + </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-hibernate-security-0.0.3</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-hibernate-security-0.0.3</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-hibernate-security-0.0.3</url> + </scm> +</project> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 16:04:49
|
Revision: 668 http://openutils.svn.sourceforge.net/openutils/?rev=668&view=rev Author: fcarone Date: 2008-02-20 08:04:43 -0800 (Wed, 20 Feb 2008) Log Message: ----------- SecurityRuleUtils moved to the utils package Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/SecurityRuleManager.java Added Paths: ----------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/utils/ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/utils/SecurityRuleUtils.java Removed Paths: ------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 15:55:57 UTC (rev 667) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 16:04:43 UTC (rev 668) @@ -16,6 +16,7 @@ package it.openutils.hibernate.security.filter; import it.openutils.hibernate.security.dataobject.SecurityRule; +import it.openutils.hibernate.security.filter.utils.SecurityRuleUtils; import it.openutils.hibernate.security.services.SecurityRuleManager; import java.util.List; Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java 2008-02-20 15:55:57 UTC (rev 667) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java 2008-02-20 16:04:43 UTC (rev 668) @@ -4,6 +4,7 @@ package it.openutils.hibernate.security.filter; import it.openutils.hibernate.security.dataobject.SecurityRule; +import it.openutils.hibernate.security.filter.utils.SecurityRuleUtils; import it.openutils.hibernate.security.services.SecurityRuleManager; import java.util.List; Deleted: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:55:57 UTC (rev 667) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 16:04:43 UTC (rev 668) @@ -1,144 +0,0 @@ -/* - * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it - */ -package it.openutils.hibernate.security.filter; - -import it.openutils.hibernate.security.dataobject.PermissionEnum; -import it.openutils.hibernate.security.dataobject.SecurityRule; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import org.acegisecurity.Authentication; -import org.acegisecurity.ConfigAttribute; -import org.acegisecurity.ConfigAttributeDefinition; -import org.acegisecurity.GrantedAuthority; -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * @author fcarone - * @version $Id: $ - */ -public class SecurityRuleUtils -{ - /** - * Logger. - */ - private Logger log = LoggerFactory.getLogger(SecurityRuleUtils.class); - - /** - * @param rules The list of rules to check - * @param config The config attribute to check the rules against - * @return True if any of the rules matches the given config attribute, false otherwise. - */ - @SuppressWarnings("unchecked") - public boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) - { - log.debug("Evaluation permissions"); - Iterator iterator = config.getConfigAttributes(); - while (iterator.hasNext()) - { - String attribute = ((ConfigAttribute) iterator.next()).getAttribute(); - for (SecurityRule rule : rules) - { - for (PermissionEnum permission : rule.getPermissions()) - { - if (StringUtils.equals(permission.getValue(), attribute)) - { - log.debug("Matching permission: {}", permission.getValue()); - return true; - } - } - } - } - log.debug("No matching permissions found."); - return false; - } - - /** - * @param rules The list of rules to check - * @param argument The object to check the rules against - * @return True if any of the rules matches the given object, false otherwise. - */ - public boolean checkRules(List<SecurityRule> rules, Object argument) - { - log.debug("Evaluating rules."); - try - { - for (SecurityRule rule : rules) - { - String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); - switch (rule.getModifier()) - { - case EQUALS: - if (StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - break; - case NOT: - if (!StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - break; - default: - throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); - } - } - } - catch (NoSuchMethodException e) - { - log.error("{}", e); - } - catch (IllegalAccessException e) - { - log.error("{}", e); - } - catch (InvocationTargetException e) - { - log.error("{}", e); - } - log.debug("No matching rules found."); - return false; - } - - - /** - * @param authentication The authentication method - * @return The collection of roles contained in the authentication - */ - public List<String> getRolesFromAuthentication(Authentication authentication) - { - List<String> roles = new ArrayList<String>(); - for (GrantedAuthority authority : authentication.getAuthorities()) - { - log.debug("Granted authority for user {}: {}", authentication.getName(), authority.getAuthority()); - roles.add(authority.getAuthority()); - } - return roles; - } - - - /** - * @param object The object to get the name from - * @return The object name - */ - public String getClassName(Object object) - { - // is this class a CGLib proxy? - if (StringUtils.contains(object.getClass().getName(), "$$")) - { - return StringUtils.substringBefore(object.getClass().getName(), "$$"); - } - return object.getClass().getName(); - } -} Copied: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/utils/SecurityRuleUtils.java (from rev 667, trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java) =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/utils/SecurityRuleUtils.java (rev 0) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/utils/SecurityRuleUtils.java 2008-02-20 16:04:43 UTC (rev 668) @@ -0,0 +1,144 @@ +/* + * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + */ +package it.openutils.hibernate.security.filter.utils; + +import it.openutils.hibernate.security.dataobject.PermissionEnum; +import it.openutils.hibernate.security.dataobject.SecurityRule; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttribute; +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.GrantedAuthority; +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class SecurityRuleUtils +{ + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(SecurityRuleUtils.class); + + /** + * @param rules The list of rules to check + * @param config The config attribute to check the rules against + * @return True if any of the rules matches the given config attribute, false otherwise. + */ + @SuppressWarnings("unchecked") + public boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) + { + log.debug("Evaluation permissions"); + Iterator iterator = config.getConfigAttributes(); + while (iterator.hasNext()) + { + String attribute = ((ConfigAttribute) iterator.next()).getAttribute(); + for (SecurityRule rule : rules) + { + for (PermissionEnum permission : rule.getPermissions()) + { + if (StringUtils.equals(permission.getValue(), attribute)) + { + log.debug("Matching permission: {}", permission.getValue()); + return true; + } + } + } + } + log.debug("No matching permissions found."); + return false; + } + + /** + * @param rules The list of rules to check + * @param argument The object to check the rules against + * @return True if any of the rules matches the given object, false otherwise. + */ + public boolean checkRules(List<SecurityRule> rules, Object argument) + { + log.debug("Evaluating rules."); + try + { + for (SecurityRule rule : rules) + { + String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); + switch (rule.getModifier()) + { + case EQUALS: + if (StringUtils.equals(objProperty, rule.getValue())) + { + log.debug("Matching rule found: {}", rule); + return true; + } + break; + case NOT: + if (!StringUtils.equals(objProperty, rule.getValue())) + { + log.debug("Matching rule found: {}", rule); + return true; + } + break; + default: + throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); + } + } + } + catch (NoSuchMethodException e) + { + log.error("{}", e); + } + catch (IllegalAccessException e) + { + log.error("{}", e); + } + catch (InvocationTargetException e) + { + log.error("{}", e); + } + log.debug("No matching rules found."); + return false; + } + + + /** + * @param authentication The authentication method + * @return The collection of roles contained in the authentication + */ + public List<String> getRolesFromAuthentication(Authentication authentication) + { + List<String> roles = new ArrayList<String>(); + for (GrantedAuthority authority : authentication.getAuthorities()) + { + log.debug("Granted authority for user {}: {}", authentication.getName(), authority.getAuthority()); + roles.add(authority.getAuthority()); + } + return roles; + } + + + /** + * @param object The object to get the name from + * @return The object name + */ + public String getClassName(Object object) + { + // is this class a CGLib proxy? + if (StringUtils.contains(object.getClass().getName(), "$$")) + { + return StringUtils.substringBefore(object.getClass().getName(), "$$"); + } + return object.getClass().getName(); + } +} Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/SecurityRuleManager.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/SecurityRuleManager.java 2008-02-20 15:55:57 UTC (rev 667) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/SecurityRuleManager.java 2008-02-20 16:04:43 UTC (rev 668) @@ -65,7 +65,7 @@ /** * @param entity The entity to apply rules for * @param rules The list of rules to apply - * @return + * @return The Hibernate Filter responding to the given criteria * @throws SecurityException * @throws ClassNotFoundException * @throws InstantiationException This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:55:52
|
Revision: 667 http://openutils.svn.sourceforge.net/openutils/?rev=667&view=rev Author: fcarone Date: 2008-02-20 07:55:57 -0800 (Wed, 20 Feb 2008) Log Message: ----------- Various enhancements Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java 2008-02-20 15:36:56 UTC (rev 666) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java 2008-02-20 15:55:57 UTC (rev 667) @@ -5,7 +5,6 @@ import it.openutils.hibernate.security.services.SecurityRuleManager; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java 2008-02-20 15:36:56 UTC (rev 666) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java 2008-02-20 15:55:57 UTC (rev 667) @@ -11,8 +11,6 @@ import org.acegisecurity.Authentication; import org.acegisecurity.ConfigAttributeDefinition; import org.apache.commons.collections.Predicate; -import org.apache.commons.lang.StringUtils; -import org.hibernate.proxy.HibernateProxy; /** @@ -61,7 +59,7 @@ { return false; } - return (ruleUtils.checkRules(rules, object) && ruleUtils.checkPermissions(rules, configAttribute)); + return (ruleUtils.checkPermissions(rules, configAttribute) && ruleUtils.checkRules(rules, object)); } Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:36:56 UTC (rev 666) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:55:57 UTC (rev 667) @@ -82,13 +82,14 @@ log.debug("Matching rule found: {}", rule); return true; } - + break; case NOT: if (!StringUtils.equals(objProperty, rule.getValue())) { log.debug("Matching rule found: {}", rule); return true; } + break; default: throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:37:18
|
Revision: 666 http://openutils.svn.sourceforge.net/openutils/?rev=666&view=rev Author: fcarone Date: 2008-02-20 07:36:56 -0800 (Wed, 20 Feb 2008) Log Message: ----------- Switch case is nicer for enums Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:36:09 UTC (rev 665) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:36:56 UTC (rev 666) @@ -3,7 +3,6 @@ */ package it.openutils.hibernate.security.filter; -import it.openutils.hibernate.security.dataobject.ModifierEnum; import it.openutils.hibernate.security.dataobject.PermissionEnum; import it.openutils.hibernate.security.dataobject.SecurityRule; @@ -75,26 +74,24 @@ for (SecurityRule rule : rules) { String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); - if (rule.getModifier().equals(ModifierEnum.EQUALS)) + switch (rule.getModifier()) { - if (StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } + case EQUALS: + if (StringUtils.equals(objProperty, rule.getValue())) + { + log.debug("Matching rule found: {}", rule); + return true; + } + + case NOT: + if (!StringUtils.equals(objProperty, rule.getValue())) + { + log.debug("Matching rule found: {}", rule); + return true; + } + default: + throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); } - else if (rule.getModifier().equals(ModifierEnum.NOT)) - { - if (!StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - } - else - { - throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); - } } } catch (NoSuchMethodException e) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:36:18
|
Revision: 665 http://openutils.svn.sourceforge.net/openutils/?rev=665&view=rev Author: fcarone Date: 2008-02-20 07:36:09 -0800 (Wed, 20 Feb 2008) Log Message: ----------- New testcase for update added Modified Paths: -------------- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 15:30:58 UTC (rev 664) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 15:36:09 UTC (rev 665) @@ -141,6 +141,24 @@ /** * @throws Exception Any exception */ + @Test(expected = AccessDeniedException.class) + public void testUpdateNoGo2() throws Exception + { + authenticate("UserUno", "password"); + DummyDataobject filter = new DummyDataobject(); + List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); + Assert.assertNotNull(dummyObjects); + DummyDataobject ddo = dummyObjects.get(0); + + ddo.setStringValue("ModifiedValue"); + ddo.setIntValue(3); + + dummyObjectManager.update(ddo); + } + + /** + * @throws Exception Any exception + */ @Test public void testDeleteOk() throws Exception { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:30:56
|
Revision: 664 http://openutils.svn.sourceforge.net/openutils/?rev=664&view=rev Author: fcarone Date: 2008-02-20 07:30:58 -0800 (Wed, 20 Feb 2008) Log Message: ----------- More tests added for new features. Modified Paths: -------------- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManager.java trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml trunk/openutils-hibernate-security/src/test/resources/spring-security.xml Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 15:30:24 UTC (rev 663) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 15:30:58 UTC (rev 664) @@ -267,9 +267,41 @@ ((AOPSecurity) applicationContext.getBean("securityAspect")).setEnabled(true); } + /** + * @throws Exception Any exception + */ + @Test + public void testFindAll() throws Exception + { + authenticate("UserUno", "password"); + List<DummyDataobject> result = dummyObjectManager.findAll(); + Assert.assertEquals(1, result.size()); + Assert.assertEquals(1, result.get(0).getIntValue().intValue()); + } + /** + * @throws Exception Any exception + */ + @Test + public void testLoadOk() throws Exception + { + authenticate("UserUno", "password"); + DummyDataobject result = dummyObjectManager.load(1L); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.getIntValue().intValue()); + } /** + * @throws Exception Any exception + */ + @Test(expected = AccessDeniedException.class) + public void testLoadNonOk() throws Exception + { + authenticate("UserUno", "password"); + dummyObjectManager.load(0L); + } + + /** * Sets the securedObject. * @param securedObject the securedObject to set */ Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManager.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManager.java 2008-02-20 15:30:24 UTC (rev 663) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManager.java 2008-02-20 15:30:58 UTC (rev 664) @@ -3,7 +3,9 @@ */ package it.openutils.hibernate.security.apptest; +import java.util.List; + /** * @author fcarone * @version $Id: $ @@ -16,4 +18,8 @@ public void delete(DummyDataobject ddo); + public List<DummyDataobject> findAll(); + + DummyDataobject load(Long id); + } Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java 2008-02-20 15:30:24 UTC (rev 663) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java 2008-02-20 15:30:58 UTC (rev 664) @@ -3,7 +3,9 @@ */ package it.openutils.hibernate.security.apptest; +import java.util.List; + /** * @author fcarone * @version $Id: $ @@ -11,6 +13,8 @@ public class DummyObjectManagerImpl implements DummyObjectManager { + private DummyDAO dummyDAO; + /** * {@inheritDoc} */ @@ -38,4 +42,30 @@ } + /** + * {@inheritDoc} + */ + public DummyDataobject load(Long id) + { + return dummyDAO.load(id); + } + + /** + * {@inheritDoc} + */ + public List<DummyDataobject> findAll() + { + return dummyDAO.findAll(); + } + + + /** + * Sets the dummyDAO. + * @param dummyDAO the dummyDAO to set + */ + public void setDummyDAO(DummyDAO dummyDAO) + { + this.dummyDAO = dummyDAO; + } + } Modified: trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml 2008-02-20 15:30:24 UTC (rev 663) +++ trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml 2008-02-20 15:30:58 UTC (rev 664) @@ -2,6 +2,7 @@ xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> + <bean id="securityRuleManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> @@ -15,6 +16,7 @@ <bean class="it.openutils.hibernate.security.services.impl.SecurityRuleManagerImpl" autowire="byType"></bean> </property> </bean> + <bean id="dummyObjectManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> @@ -25,9 +27,12 @@ </props> </property> <property name="target"> - <bean class="it.openutils.hibernate.security.apptest.DummyObjectManagerImpl" /> + <bean class="it.openutils.hibernate.security.apptest.DummyObjectManagerImpl"> + <property name="dummyDAO" ref="dummyDAO" /> + </bean> </property> </bean> + <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> Modified: trunk/openutils-hibernate-security/src/test/resources/spring-security.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/spring-security.xml 2008-02-20 15:30:24 UTC (rev 663) +++ trunk/openutils-hibernate-security/src/test/resources/spring-security.xml 2008-02-20 15:30:58 UTC (rev 664) @@ -33,10 +33,16 @@ <bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter" /> + <bean id="authenticatedVoter" class="org.acegisecurity.vote.AuthenticatedVoter" /> + <bean id="securityRuleVoter" class="it.openutils.hibernate.security.filter.SecurityRuleAccessDecisionVoter"> <property name="securityRuleManager" ref="securityRuleManager" /> </bean> + <bean id="securityRuleAfterInvocationHandler" class="it.openutils.hibernate.security.filter.SecurityRuleAfterInvocationHandler"> + <property name="securityRuleManager" ref="securityRuleManager" /> + </bean> + <bean id="accessDecisionManager" class="org.acegisecurity.vote.UnanimousBased"> <property name="allowIfAllAbstainDecisions" value="false" /> <property name="decisionVoters"> @@ -47,7 +53,25 @@ </property> </bean> - <bean id="securityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"> + <bean id="afterAccessDecisionManager" class="org.acegisecurity.vote.UnanimousBased"> + <property name="allowIfAllAbstainDecisions" value="false" /> + <property name="decisionVoters"> + <list> + <ref bean="authenticatedVoter" /> + </list> + </property> + </bean> + + + <bean id="afterInvocationManager" class="org.acegisecurity.afterinvocation.AfterInvocationProviderManager"> + <property name="providers"> + <list> + <ref bean="securityRuleAfterInvocationHandler" /> + </list> + </property> + </bean> + + <bean id="beforeSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"> <property name="authenticationManager" ref="authenticationManager" /> <property name="accessDecisionManager" ref="accessDecisionManager" /> <property name="objectDefinitionSource"> @@ -59,6 +83,18 @@ </property> </bean> + <bean id="afterSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"> + <property name="accessDecisionManager" ref="afterAccessDecisionManager" /> + <property name="authenticationManager" ref="authenticationManager" /> + <property name="afterInvocationManager" ref="afterInvocationManager" /> + <property name="objectDefinitionSource"> + <value> + it.openutils.hibernate.security.apptest.DummyObjectManager.findAll=LOAD,IS_AUTHENTICATED_ANONYMOUSLY + it.openutils.hibernate.security.apptest.DummyObjectManager.load=LOAD,IS_AUTHENTICATED_ANONYMOUSLY + </value> + </property> + </bean> + <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> @@ -67,7 +103,8 @@ </property> <property name="interceptorNames"> <list> - <value>securityInterceptor</value> + <value>beforeSecurityInterceptor</value> + <value>afterSecurityInterceptor</value> </list> </property> </bean> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:30:19
|
Revision: 663 http://openutils.svn.sourceforge.net/openutils/?rev=663&view=rev Author: fcarone Date: 2008-02-20 07:30:24 -0800 (Wed, 20 Feb 2008) Log Message: ----------- After invocation handler based on security rules added Added Paths: ----------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java Added: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java (rev 0) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAfterInvocationHandler.java 2008-02-20 15:30:24 UTC (rev 663) @@ -0,0 +1,71 @@ +/* + * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + */ +package it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.services.SecurityRuleManager; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.acegisecurity.AccessDeniedException; +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.afterinvocation.AfterInvocationProvider; +import org.apache.commons.collections.CollectionUtils; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class SecurityRuleAfterInvocationHandler extends SecurityRuleBaseHandler implements AfterInvocationProvider +{ + + private SecurityRuleManager securityRuleManager; + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config, + Object returnedObject) throws AccessDeniedException + { + if (returnedObject == null) + { + return null; + } + SecurityRulePredicate predicate = new SecurityRulePredicate(authentication, config, securityRuleManager); + + if (returnedObject instanceof Collection) + { + CollectionUtils.filter((Collection) returnedObject, predicate); + } + else if (returnedObject.getClass().isArray()) + { + Object[] objectArray = (Object[]) returnedObject; + List<Object> objectArrayList = Arrays.asList(objectArray); + CollectionUtils.filter(objectArrayList, predicate); + } + else + { + if (!predicate.evaluate(returnedObject)) + { + throw new AccessDeniedException("Access denied"); + } + } + return returnedObject; + } + + /** + * Sets the securityRuleManager. + * @param securityRuleManager the securityRuleManager to set + */ + public void setSecurityRuleManager(SecurityRuleManager securityRuleManager) + { + this.securityRuleManager = securityRuleManager; + } + +} Added: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java (rev 0) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRulePredicate.java 2008-02-20 15:30:24 UTC (rev 663) @@ -0,0 +1,68 @@ +/* + * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + */ +package it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.dataobject.SecurityRule; +import it.openutils.hibernate.security.services.SecurityRuleManager; + +import java.util.List; + +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttributeDefinition; +import org.apache.commons.collections.Predicate; +import org.apache.commons.lang.StringUtils; +import org.hibernate.proxy.HibernateProxy; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class SecurityRulePredicate implements Predicate +{ + + private Authentication authentication; + + private SecurityRuleManager securityRuleManager; + + private ConfigAttributeDefinition configAttribute; + + private SecurityRuleUtils ruleUtils = new SecurityRuleUtils(); + + /** + * @param authentication The current authentication + * @param configAttribute The config attribute of the current method invocation interceptor + * @param securityRuleManager The security rule manager to retrieve rules from + */ + public SecurityRulePredicate( + Authentication authentication, + ConfigAttributeDefinition configAttribute, + SecurityRuleManager securityRuleManager) + { + this.authentication = authentication; + this.securityRuleManager = securityRuleManager; + this.configAttribute = configAttribute; + } + + /** + * {@inheritDoc} + * If we return false here, the element will be removed from the original collection. + */ + @SuppressWarnings("unchecked") + public boolean evaluate(Object object) + { + List<String> roles = ruleUtils.getRolesFromAuthentication(authentication); + List<SecurityRule> rules = securityRuleManager.getRulesForRoles(ruleUtils.getClassName(object), roles); + + // @todo: this should be configurable + // denyAll by default + if (rules == null || rules.isEmpty()) + { + return false; + } + return (ruleUtils.checkRules(rules, object) && ruleUtils.checkPermissions(rules, configAttribute)); + } + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
Revision: 662 http://openutils.svn.sourceforge.net/openutils/?rev=662&view=rev Author: fcarone Date: 2008-02-20 07:29:19 -0800 (Wed, 20 Feb 2008) Log Message: ----------- use getClassName to get the class name Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 15:28:47 UTC (rev 661) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 15:29:19 UTC (rev 662) @@ -57,7 +57,7 @@ for (Object argument : methodInvocation.getArguments()) { log.debug("Evaluating argument {}", argument); - List<SecurityRule> rules = securityRuleManager.getRulesForRoles(argument.getClass().getName(), roles); + List<SecurityRule> rules = securityRuleManager.getRulesForRoles(ruleUtils.getClassName(argument), roles); boolean permissionMatches = ruleUtils.checkPermissions(rules, config); if (!permissionMatches) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:28:40
|
Revision: 661 http://openutils.svn.sourceforge.net/openutils/?rev=661&view=rev Author: fcarone Date: 2008-02-20 07:28:47 -0800 (Wed, 20 Feb 2008) Log Message: ----------- assume acegi is not giving us null attribute values Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java 2008-02-20 15:27:43 UTC (rev 660) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java 2008-02-20 15:28:47 UTC (rev 661) @@ -3,20 +3,9 @@ */ package it.openutils.hibernate.security.filter; -import it.openutils.hibernate.security.dataobject.ModifierEnum; import it.openutils.hibernate.security.dataobject.PermissionEnum; -import it.openutils.hibernate.security.dataobject.SecurityRule; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import org.acegisecurity.Authentication; import org.acegisecurity.ConfigAttribute; -import org.acegisecurity.ConfigAttributeDefinition; -import org.acegisecurity.GrantedAuthority; -import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,15 +30,12 @@ { log.debug("Evaluating attribute {}", attribute.getAttribute()); - if (attribute.getAttribute() != null) + for (PermissionEnum permission : PermissionEnum.values()) { - for (PermissionEnum permission : PermissionEnum.values()) + if (StringUtils.equals(permission.getValue(), attribute.getAttribute())) { - if (StringUtils.equals(permission.getValue(), attribute.getAttribute())) - { - log.debug("Support ok."); - return true; - } + log.debug("Support ok."); + return true; } } log.debug("Not supporting attribute."); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:27:37
|
Revision: 660 http://openutils.svn.sourceforge.net/openutils/?rev=660&view=rev Author: fcarone Date: 2008-02-20 07:27:43 -0800 (Wed, 20 Feb 2008) Log Message: ----------- getClassName utility method added Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:26:27 UTC (rev 659) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 15:27:43 UTC (rev 660) @@ -39,7 +39,7 @@ * @return True if any of the rules matches the given config attribute, false otherwise. */ @SuppressWarnings("unchecked") - protected boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) + public boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) { log.debug("Evaluation permissions"); Iterator iterator = config.getConfigAttributes(); @@ -67,7 +67,7 @@ * @param argument The object to check the rules against * @return True if any of the rules matches the given object, false otherwise. */ - protected boolean checkRules(List<SecurityRule> rules, Object argument) + public boolean checkRules(List<SecurityRule> rules, Object argument) { log.debug("Evaluating rules."); try @@ -118,7 +118,7 @@ * @param authentication The authentication method * @return The collection of roles contained in the authentication */ - protected List<String> getRolesFromAuthentication(Authentication authentication) + public List<String> getRolesFromAuthentication(Authentication authentication) { List<String> roles = new ArrayList<String>(); for (GrantedAuthority authority : authentication.getAuthorities()) @@ -128,4 +128,19 @@ } return roles; } + + + /** + * @param object The object to get the name from + * @return The object name + */ + public String getClassName(Object object) + { + // is this class a CGLib proxy? + if (StringUtils.contains(object.getClass().getName(), "$$")) + { + return StringUtils.substringBefore(object.getClass().getName(), "$$"); + } + return object.getClass().getName(); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 15:26:24
|
Revision: 659 http://openutils.svn.sourceforge.net/openutils/?rev=659&view=rev Author: fcarone Date: 2008-02-20 07:26:27 -0800 (Wed, 20 Feb 2008) Log Message: ----------- isAssignableFrom leftvalue and rightvalue exchanged Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 14:09:10 UTC (rev 658) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 15:26:27 UTC (rev 659) @@ -143,7 +143,7 @@ String startQuote = null; String endQuote = null; - if (field.getType().isAssignableFrom(String.class)) + if (String.class.isAssignableFrom(field.getType())) { startQuote = "\'"; endQuote = startQuote; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 14:09:09
|
Revision: 658 http://openutils.svn.sourceforge.net/openutils/?rev=658&view=rev Author: fcarone Date: 2008-02-20 06:09:10 -0800 (Wed, 20 Feb 2008) Log Message: ----------- Some (heavy) refactoring Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml Added Paths: ----------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 10:38:55 UTC (rev 657) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 14:09:10 UTC (rev 658) @@ -32,7 +32,6 @@ import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; -import org.dbunit.dataset.csv.handlers.EscapeHandler; import org.hibernate.Filter; import org.hibernate.HibernateException; import org.hibernate.engine.FilterDefinition; @@ -149,7 +148,7 @@ startQuote = "\'"; endQuote = startQuote; } - else if (field.getType().getSuperclass().isAssignableFrom(Number.class)) + else if (Number.class.isAssignableFrom(field.getType())) { startQuote = StringUtils.EMPTY; endQuote = StringUtils.EMPTY; Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 10:38:55 UTC (rev 657) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 14:09:10 UTC (rev 658) @@ -15,24 +15,14 @@ */ package it.openutils.hibernate.security.filter; -import it.openutils.hibernate.security.dataobject.ModifierEnum; -import it.openutils.hibernate.security.dataobject.PermissionEnum; import it.openutils.hibernate.security.dataobject.SecurityRule; import it.openutils.hibernate.security.services.SecurityRuleManager; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; import java.util.List; import org.acegisecurity.Authentication; -import org.acegisecurity.ConfigAttribute; import org.acegisecurity.ConfigAttributeDefinition; -import org.acegisecurity.GrantedAuthority; import org.acegisecurity.vote.AccessDecisionVoter; -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.framework.ReflectiveMethodInvocation; @@ -42,7 +32,7 @@ * @author fcarone * @version $Id: $ */ -public class SecurityRuleAccessDecisionVoter implements AccessDecisionVoter +public class SecurityRuleAccessDecisionVoter extends SecurityRuleBaseHandler implements AccessDecisionVoter { /** @@ -52,44 +42,14 @@ private SecurityRuleManager securityRuleManager; - /** - * {@inheritDoc} - */ - public boolean supports(ConfigAttribute attribute) - { - log.debug("Evaluating attribute {}", attribute.getAttribute()); + private SecurityRuleUtils ruleUtils = new SecurityRuleUtils(); - if (attribute.getAttribute() != null) - { - for (PermissionEnum permission : PermissionEnum.values()) - { - if (StringUtils.equals(permission.getValue(), attribute.getAttribute())) - { - log.debug("Support ok."); - return true; - } - } - } - log.debug("Not supporting attribute."); - return false; - } - /** * {@inheritDoc} */ - @SuppressWarnings("unchecked") - public boolean supports(Class clazz) - { - return true; - } - - /** - * {@inheritDoc} - */ public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config) { - List<String> roles = new ArrayList<String>(); - roles.addAll(getRolesFromAuthentication(authentication)); + List<String> roles = ruleUtils.getRolesFromAuthentication(authentication); if (object instanceof ReflectiveMethodInvocation) { @@ -98,13 +58,13 @@ { log.debug("Evaluating argument {}", argument); List<SecurityRule> rules = securityRuleManager.getRulesForRoles(argument.getClass().getName(), roles); - boolean permissionMatches = checkPermissions(rules, config); + boolean permissionMatches = ruleUtils.checkPermissions(rules, config); if (!permissionMatches) { return ACCESS_DENIED; } - boolean areRulesMatching = checkRules(rules, argument); + boolean areRulesMatching = ruleUtils.checkRules(rules, argument); if (!areRulesMatching) { return ACCESS_DENIED; @@ -116,101 +76,6 @@ } /** - * @param authentication - * @return - */ - private Collection< ? extends String> getRolesFromAuthentication(Authentication authentication) - { - List<String> roles = new ArrayList<String>(); - for (GrantedAuthority authority : authentication.getAuthorities()) - { - log.debug("Granted authority for user {}: {}", authentication.getName(), authority.getAuthority()); - roles.add(authority.getAuthority()); - } - return roles; - } - - /** - * @param rules - * @param argument - * @return - */ - private boolean checkRules(List<SecurityRule> rules, Object argument) - { - log.debug("Evaluating rules."); - try - { - for (SecurityRule rule : rules) - { - String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); - if (rule.getModifier().equals(ModifierEnum.EQUALS)) - { - if (StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - } - else if (rule.getModifier().equals(ModifierEnum.NOT)) - { - if (!StringUtils.equals(objProperty, rule.getValue())) - { - log.debug("Matching rule found: {}", rule); - return true; - } - } - else - { - throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); - } - } - } - catch (NoSuchMethodException e) - { - log.error("{}", e); - } - catch (IllegalAccessException e) - { - log.error("{}", e); - } - catch (InvocationTargetException e) - { - log.error("{}", e); - } - log.debug("No matching rules found."); - return false; - } - - /** - * @param rules - * @param config - * @return - */ - @SuppressWarnings("unchecked") - private boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) - { - log.debug("Evaluation permissions"); - Iterator iterator = config.getConfigAttributes(); - while (iterator.hasNext()) - { - String attribute = ((ConfigAttribute) iterator.next()).getAttribute(); - for (SecurityRule rule : rules) - { - for (PermissionEnum permission : rule.getPermissions()) - { - if (StringUtils.equals(permission.getValue(), attribute)) - { - log.debug("Matching permission: {}", permission.getValue()); - return true; - } - } - } - } - log.debug("No matching permissions found."); - return false; - } - - /** * Sets the securityRuleManager. * @param securityRuleManager the securityRuleManager to set */ @@ -218,5 +83,4 @@ { this.securityRuleManager = securityRuleManager; } - } Added: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java (rev 0) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleBaseHandler.java 2008-02-20 14:09:10 UTC (rev 658) @@ -0,0 +1,68 @@ +/* + * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + */ +package it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.dataobject.ModifierEnum; +import it.openutils.hibernate.security.dataobject.PermissionEnum; +import it.openutils.hibernate.security.dataobject.SecurityRule; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttribute; +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.GrantedAuthority; +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fcarone + * @version $Id: $ + */ +public abstract class SecurityRuleBaseHandler +{ + + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(SecurityRuleBaseHandler.class); + + /** + * {@inheritDoc} + */ + public boolean supports(ConfigAttribute attribute) + { + log.debug("Evaluating attribute {}", attribute.getAttribute()); + + if (attribute.getAttribute() != null) + { + for (PermissionEnum permission : PermissionEnum.values()) + { + if (StringUtils.equals(permission.getValue(), attribute.getAttribute())) + { + log.debug("Support ok."); + return true; + } + } + } + log.debug("Not supporting attribute."); + return false; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public boolean supports(Class clazz) + { + return true; + } + +} Added: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java (rev 0) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleUtils.java 2008-02-20 14:09:10 UTC (rev 658) @@ -0,0 +1,131 @@ +/* + * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + */ +package it.openutils.hibernate.security.filter; + +import it.openutils.hibernate.security.dataobject.ModifierEnum; +import it.openutils.hibernate.security.dataobject.PermissionEnum; +import it.openutils.hibernate.security.dataobject.SecurityRule; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.acegisecurity.Authentication; +import org.acegisecurity.ConfigAttribute; +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.GrantedAuthority; +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fcarone + * @version $Id: $ + */ +public class SecurityRuleUtils +{ + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(SecurityRuleUtils.class); + + /** + * @param rules The list of rules to check + * @param config The config attribute to check the rules against + * @return True if any of the rules matches the given config attribute, false otherwise. + */ + @SuppressWarnings("unchecked") + protected boolean checkPermissions(List<SecurityRule> rules, ConfigAttributeDefinition config) + { + log.debug("Evaluation permissions"); + Iterator iterator = config.getConfigAttributes(); + while (iterator.hasNext()) + { + String attribute = ((ConfigAttribute) iterator.next()).getAttribute(); + for (SecurityRule rule : rules) + { + for (PermissionEnum permission : rule.getPermissions()) + { + if (StringUtils.equals(permission.getValue(), attribute)) + { + log.debug("Matching permission: {}", permission.getValue()); + return true; + } + } + } + } + log.debug("No matching permissions found."); + return false; + } + + /** + * @param rules The list of rules to check + * @param argument The object to check the rules against + * @return True if any of the rules matches the given object, false otherwise. + */ + protected boolean checkRules(List<SecurityRule> rules, Object argument) + { + log.debug("Evaluating rules."); + try + { + for (SecurityRule rule : rules) + { + String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); + if (rule.getModifier().equals(ModifierEnum.EQUALS)) + { + if (StringUtils.equals(objProperty, rule.getValue())) + { + log.debug("Matching rule found: {}", rule); + return true; + } + } + else if (rule.getModifier().equals(ModifierEnum.NOT)) + { + if (!StringUtils.equals(objProperty, rule.getValue())) + { + log.debug("Matching rule found: {}", rule); + return true; + } + } + else + { + throw new RuntimeException("Modifier " + rule.getModifier() + " is not recognized"); + } + } + } + catch (NoSuchMethodException e) + { + log.error("{}", e); + } + catch (IllegalAccessException e) + { + log.error("{}", e); + } + catch (InvocationTargetException e) + { + log.error("{}", e); + } + log.debug("No matching rules found."); + return false; + } + + + /** + * @param authentication The authentication method + * @return The collection of roles contained in the authentication + */ + protected List<String> getRolesFromAuthentication(Authentication authentication) + { + List<String> roles = new ArrayList<String>(); + for (GrantedAuthority authority : authentication.getAuthorities()) + { + log.debug("Granted authority for user {}: {}", authentication.getName(), authority.getAuthority()); + roles.add(authority.getAuthority()); + } + return roles; + } +} Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 10:38:55 UTC (rev 657) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 14:09:10 UTC (rev 658) @@ -67,6 +67,9 @@ SecurityContextHolder.setContext(sci); } + /** + * Init this test class with the dummyDAO DAO (with AOP enabled) + */ @Before public void initDummyDAO() { @@ -245,7 +248,7 @@ DummyDataobject filter = new DummyDataobject(); List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); Assert.assertNotNull(dummyObjects); - Assert.assertEquals(2, dummyObjects.size()); + Assert.assertEquals(3, dummyObjects.size()); } /** @@ -259,10 +262,13 @@ DummyDataobject filter = new DummyDataobject(); List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); Assert.assertNotNull(dummyObjects); - Assert.assertEquals(2, dummyObjects.size()); + Assert.assertEquals(3, dummyObjects.size()); + + ((AOPSecurity) applicationContext.getBean("securityAspect")).setEnabled(true); } + /** * Sets the securedObject. * @param securedObject the securedObject to set Modified: trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml 2008-02-20 10:38:55 UTC (rev 657) +++ trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml 2008-02-20 14:09:10 UTC (rev 658) @@ -25,6 +25,7 @@ <value>Verdi</value> <value>ve...@ex...</value> </row> + <!-- UserTre has no access to any data --> <row> <value>UserTre</value> <!-- sha1 for 'password' --> @@ -181,6 +182,11 @@ <value>1</value> <value>DEF</value> </row> + <row> + <value>2</value> + <value>3</value> + <value>GHI</value> + </row> </table> </dataset> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 10:48:43
|
Revision: 656 http://openutils.svn.sourceforge.net/openutils/?rev=656&view=rev Author: fcarone Date: 2008-02-20 02:38:11 -0800 (Wed, 20 Feb 2008) Log Message: ----------- code formatted Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-20 10:27:15 UTC (rev 655) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-20 10:38:11 UTC (rev 656) @@ -104,11 +104,10 @@ { grantedRoles += authorities[i].getAuthority() + " "; } - log.warn("No rules found. Access is denied on " + entity + ", for user {} with roles {}", SecurityContextHolder - .getContext() - .getAuthentication() - .getPrincipal() - .toString(), grantedRoles); + log.warn( + "No rules found. Access is denied on " + entity + ", for user {} with roles {}", + SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString(), + grantedRoles); } throw new SecurityException("Access denied"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
Revision: 657 http://openutils.svn.sourceforge.net/openutils/?rev=657&view=rev Author: fcarone Date: 2008-02-20 02:38:55 -0800 (Wed, 20 Feb 2008) Log Message: ----------- use equals for enum comparisons Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 10:38:11 UTC (rev 656) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/SecurityRuleAccessDecisionVoter.java 2008-02-20 10:38:55 UTC (rev 657) @@ -143,7 +143,7 @@ for (SecurityRule rule : rules) { String objProperty = BeanUtils.getSimpleProperty(argument, rule.getProperty()); - if (rule.getModifier() == ModifierEnum.EQUALS) + if (rule.getModifier().equals(ModifierEnum.EQUALS)) { if (StringUtils.equals(objProperty, rule.getValue())) { @@ -151,7 +151,7 @@ return true; } } - else if (rule.getModifier() == ModifierEnum.NOT) + else if (rule.getModifier().equals(ModifierEnum.NOT)) { if (!StringUtils.equals(objProperty, rule.getValue())) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 10:32:40
|
Revision: 652 http://openutils.svn.sourceforge.net/openutils/?rev=652&view=rev Author: fcarone Date: 2008-02-20 02:20:34 -0800 (Wed, 20 Feb 2008) Log Message: ----------- Better warning log when no rules are found. Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-20 10:02:28 UTC (rev 651) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/aop/AOPSecurity.java 2008-02-20 10:20:34 UTC (rev 652) @@ -104,7 +104,7 @@ { grantedRoles += authorities[i].getAuthority() + " "; } - log.warn("Access is denied on " + entity + ", for user {} with roles {}", SecurityContextHolder + log.warn("No rules found. Access is denied on " + entity + ", for user {} with roles {}", SecurityContextHolder .getContext() .getAuthentication() .getPrincipal() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 10:27:20
|
Revision: 655 http://openutils.svn.sourceforge.net/openutils/?rev=655&view=rev Author: fcarone Date: 2008-02-20 02:27:15 -0800 (Wed, 20 Feb 2008) Log Message: ----------- Escape SQL for filter value. Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 10:22:01 UTC (rev 654) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-20 10:27:15 UTC (rev 655) @@ -30,7 +30,9 @@ import javax.persistence.Entity; import javax.persistence.Table; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; +import org.dbunit.dataset.csv.handlers.EscapeHandler; import org.hibernate.Filter; import org.hibernate.HibernateException; import org.hibernate.engine.FilterDefinition; @@ -172,7 +174,7 @@ { subFilterCond.append(startQuote); } - subFilterCond.append(securityRule.getValue()); + subFilterCond.append(StringEscapeUtils.escapeSql(securityRule.getValue())); if (StringUtils.isNotEmpty(endQuote)) { subFilterCond.append(endQuote); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 10:22:03
|
Revision: 654 http://openutils.svn.sourceforge.net/openutils/?rev=654&view=rev Author: fcarone Date: 2008-02-20 02:22:01 -0800 (Wed, 20 Feb 2008) Log Message: ----------- More test data and a dao with aop disabled added Modified Paths: -------------- trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml trunk/openutils-hibernate-security/src/test/resources/spring-dao.xml Modified: trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml 2008-02-20 10:21:15 UTC (rev 653) +++ trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml 2008-02-20 10:22:01 UTC (rev 654) @@ -14,7 +14,7 @@ <value>1</value> <value>Gino</value> <value>Rossi</value> - <value>ro...@ba...</value> + <value>ro...@ex...</value> </row> <row> <value>UserDue</value> @@ -23,8 +23,17 @@ <value>1</value> <value>Mario</value> <value>Verdi</value> - <value>ve...@ba...</value> + <value>ve...@ex...</value> </row> + <row> + <value>UserTre</value> + <!-- sha1 for 'password' --> + <value>5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8</value> + <value>1</value> + <value>Luca</value> + <value>Bianchi</value> + <value>ve...@ex...</value> + </row> </table> <table name="APP_GROUP"> <column>ID_GROUP</column> @@ -40,6 +49,11 @@ <value>descrizione</value> <value>1</value> </row> + <row> + <value>GRUPPOTRE</value> + <value>descrizione</value> + <value>1</value> + </row> </table> <table name="APP_ROLE"> <column>ID_ROLE</column> @@ -56,6 +70,10 @@ <value>RoleTre</value> <value>descr3</value> </row> + <row> + <value>RoleQuattro</value> + <value>descr4</value> + </row> </table> <table name="APP_USER_GROUP"> <column>ID_USER</column> @@ -68,6 +86,10 @@ <value>UserDue</value> <value>GRUPPODUE</value> </row> + <row> + <value>UserTre</value> + <value>GRUPPOTRE</value> + </row> </table> <table name="APP_GROUP_ROLE"> <column>ID_GROUP</column> @@ -84,8 +106,11 @@ <value>GRUPPODUE</value> <value>RoleTre</value> </row> + <row> + <value>GRUPPOTRE</value> + <value>RoleQuattro</value> + </row> </table> - <table name="SECURITY_RULE"> <column>ID_SECURITY_RULE</column> <column>ROLE</column> Modified: trunk/openutils-hibernate-security/src/test/resources/spring-dao.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/spring-dao.xml 2008-02-20 10:21:15 UTC (rev 653) +++ trunk/openutils-hibernate-security/src/test/resources/spring-dao.xml 2008-02-20 10:22:01 UTC (rev 654) @@ -24,4 +24,12 @@ </bean> </property> </bean> + <bean id="dummyDAONoAop" parent="txProxyTemplate"> + <property name="target"> + <bean class="it.openutils.hibernate.security.apptest.DummyDaoImpl"> + <property name="sessionFactory" ref="sessionFactory" /> + <property name="aopenabled" value="false" /> + </bean> + </property> + </bean> </beans> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-20 10:21:28
|
Revision: 653 http://openutils.svn.sourceforge.net/openutils/?rev=653&view=rev Author: fcarone Date: 2008-02-20 02:21:15 -0800 (Wed, 20 Feb 2008) Log Message: ----------- More tests added Modified Paths: -------------- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 10:20:34 UTC (rev 652) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-20 10:21:15 UTC (rev 653) @@ -1,6 +1,7 @@ package it.openutils.hibernate.security; import it.openutils.hibernate.example.FilterMetadata; +import it.openutils.hibernate.security.aop.AOPSecurity; import it.openutils.hibernate.security.apptest.DummyDAO; import it.openutils.hibernate.security.apptest.DummyDataobject; import it.openutils.hibernate.security.apptest.DummyObjectManager; @@ -21,6 +22,7 @@ import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Order; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; @@ -48,7 +50,6 @@ public class SecurityIntegrationTest extends AbstractDbUnitJunitSpringContextTests { - @Autowired private DummyDAO securedObject; @Autowired @@ -66,6 +67,12 @@ SecurityContextHolder.setContext(sci); } + @Before + public void initDummyDAO() + { + securedObject = (DummyDAO) applicationContext.getBean("dummyDAO"); + } + /** * @throws Exception Any exception */ @@ -207,6 +214,56 @@ } /** + * @throws Exception Any exception + */ + @Test(expected = SecurityException.class) + public void testFindFilteredWithoutAuthentication() throws Exception + { + SecurityContextHolder.clearContext(); + DummyDataobject filter = new DummyDataobject(); + securedObject.findFiltered(filter); + } + + /** + * @throws Exception Any exception + */ + @Test(expected = SecurityException.class) + public void testRoleWithoutRules() throws Exception + { + authenticate("UserTre", "password"); + DummyDataobject filter = new DummyDataobject(); + securedObject.findFiltered(filter); + } + + /** + * @throws Exception Any exception + */ + @Test + public void testFindFilteredWithDAOAOPDisabled() throws Exception + { + securedObject = (DummyDAO) applicationContext.getBean("dummyDAONoAop"); + DummyDataobject filter = new DummyDataobject(); + List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); + Assert.assertNotNull(dummyObjects); + Assert.assertEquals(2, dummyObjects.size()); + } + + /** + * @throws Exception Any exception + */ + @Test + public void testFindFilteredWithSecurityAspectDisabled() throws Exception + { + ((AOPSecurity) applicationContext.getBean("securityAspect")).setEnabled(false); + + DummyDataobject filter = new DummyDataobject(); + List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); + Assert.assertNotNull(dummyObjects); + Assert.assertEquals(2, dummyObjects.size()); + } + + + /** * Sets the securedObject. * @param securedObject the securedObject to set */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2008-02-20 10:07:22
|
Revision: 648 http://openutils.svn.sourceforge.net/openutils/?rev=648&view=rev Author: fgiust Date: 2008-02-20 02:01:32 -0800 (Wed, 20 Feb 2008) Log Message: ----------- reworked AlternateSMTPAppender Modified Paths: -------------- trunk/openutils-log4j/pom.xml trunk/openutils-log4j/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java trunk/openutils-log4j/src/main/java/it/openutils/log4j/FilteredPatternLayout.java Added Paths: ----------- trunk/openutils-log4j/src/site/changes/ trunk/openutils-log4j/src/site/changes/changes.xml Modified: trunk/openutils-log4j/pom.xml =================================================================== --- trunk/openutils-log4j/pom.xml 2008-02-19 18:31:24 UTC (rev 647) +++ trunk/openutils-log4j/pom.xml 2008-02-20 10:01:32 UTC (rev 648) @@ -32,6 +32,11 @@ <version>1.2.14</version> </dependency> <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.3</version> + </dependency> + <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> Modified: trunk/openutils-log4j/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java =================================================================== --- trunk/openutils-log4j/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java 2008-02-19 18:31:24 UTC (rev 647) +++ trunk/openutils-log4j/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java 2008-02-20 10:01:32 UTC (rev 648) @@ -36,7 +36,10 @@ import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; +import javax.mail.internet.MimeUtility; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.AppenderSkeleton; import org.apache.log4j.Layout; import org.apache.log4j.Level; @@ -65,21 +68,21 @@ * <param name="SMTPHost" value="localhost" /> * <param name="Timeout" value="180" /> * <param name="Subject" value="[EXAMPLE] %m" /> - * <layout class="org.apache.log4j.PatternLayout"> + * <layout class="it.openutils.log4j.FilteredPatternLayout"> * <param name="ConversionPattern" value="%-5p %c %d{dd.MM.yyyy HH:mm:ss} -- %m%n" /> + * <param name="Header" + * value=" + * =================================== + * Myapp (production environment) + * Date: %d{dd.MM.yyyy HH:mm:ss} + * =================================== + * " /> * </layout> - * <param name="Header" - * value=" - * =================================== - * Myapp (production environment) - * Date: %d{dd.MM.yyyy HH:mm:ss} - * =================================== - * " /> * </appender> * </pre> * * @author Fabrizio Giustina - * @version $Id$ + * @version $Id: $ */ public class AlternateSMTPAppender extends AppenderSkeleton { @@ -94,7 +97,7 @@ private boolean locationInfo; - private Timer timer = new Timer(); + private Timer timer = new Timer("log4j mail appender", true); private TimerTask timerTask; @@ -137,6 +140,10 @@ public AlternateSMTPAppender() { this(new DefaultEvaluator()); + + // force loading this class + MimeBodyPart.class.getName(); + MimeUtility.class.getName(); } /** @@ -337,13 +344,12 @@ String t = layout.getHeader(); if (t != null) { + t = StringUtils.replace(t, "%o", Integer.toString(lea.getCount())); + t = StringUtils.replace(t, "%n", Layout.LINE_SEP); sbuf.append(t); + sbuf.append("\n"); } - sbuf.append("\nNumber of occurences: "); - sbuf.append(lea.getCount()); - sbuf.append("\n\n"); - LoggingEvent event = lea.getLoggingEvent(); if (this.subjectLayout != null) @@ -377,6 +383,7 @@ t = layout.getFooter(); if (t != null) { + t = StringUtils.replace(t, "%n", Layout.LINE_SEP); sbuf.append(t); } part.setContent(sbuf.toString(), layout.getContentType()); @@ -631,6 +638,12 @@ Object otherLem = other.loggingEvent.getMessage(); String[] otherThstr = other.loggingEvent.getThrowableStrRep(); + int length = Math.min(otherThstr.length, thstr.length); + length = Math.min(10, length); + + otherThstr = (String[]) ArrayUtils.subarray(otherThstr, 0, length); + String[] thisThstr = (String[]) ArrayUtils.subarray(thstr, 0, length); + if (lem == null) { if (otherLem != null) @@ -643,7 +656,7 @@ { return false; } - if (!Arrays.equals(thstr, otherThstr)) + if (!Arrays.equals(thisThstr, otherThstr)) { return false; } @@ -660,4 +673,4 @@ { return event.getLevel().isGreaterOrEqual(Level.ERROR); } -} +} \ No newline at end of file Modified: trunk/openutils-log4j/src/main/java/it/openutils/log4j/FilteredPatternLayout.java =================================================================== --- trunk/openutils-log4j/src/main/java/it/openutils/log4j/FilteredPatternLayout.java 2008-02-19 18:31:24 UTC (rev 647) +++ trunk/openutils-log4j/src/main/java/it/openutils/log4j/FilteredPatternLayout.java 2008-02-20 10:01:32 UTC (rev 648) @@ -40,20 +40,24 @@ * </pre> * * @author Fabrizio Giustina - * @version $Id$ + * @version $Id: FilteredPatternLayout.java 8589 2008-02-10 18:01:57Z fgiust $ */ public class FilteredPatternLayout extends PatternLayout { /** - * Line separator for stacktrace frames. + * Holds the list of filtered frames. */ - private static String lineSeparator = "\n"; + private Set<String> filteredFrames = new HashSet<String>(); + private String header; + + private String footer; + /** - * Holds the list of filtered frames. + * Line separator for stacktrace frames. */ - private Set<String> filteredFrames = new HashSet<String>(); + private static String lineSeparator = "\n"; static { @@ -68,16 +72,54 @@ } /** - * {@inheritDoc} + * Returns the header. + * @return the header */ @Override + public String getHeader() + { + return header; + } + + /** + * Sets the header. + * @param header the header to set + */ + public void setHeader(String header) + { + this.header = header; + } + + /** + * Returns the footer. + * @return the footer + */ + @Override + public String getFooter() + { + return footer; + } + + /** + * Sets the footer. + * @param footer the footer to set + */ + public void setFooter(String footer) + { + this.footer = footer; + } + + /** + * @see org.apache.log4j.Layout#ignoresThrowable() + */ + @Override public boolean ignoresThrowable() { return false; } /** - * {@inheritDoc} + * @see org.apache.log4j.PatternLayout#format(org.apache.log4j.spi.LoggingEvent) */ @Override public String format(LoggingEvent event) @@ -142,4 +184,5 @@ } return false; } + } Added: trunk/openutils-log4j/src/site/changes/changes.xml =================================================================== --- trunk/openutils-log4j/src/site/changes/changes.xml (rev 0) +++ trunk/openutils-log4j/src/site/changes/changes.xml 2008-02-20 10:01:32 UTC (rev 648) @@ -0,0 +1,18 @@ +<?xml version="1.0"?> +<!-- + "type" attribute can be: add, remove, update or fix. +--> +<document> + <properties> + <title>Changes</title> + <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> + </properties> + <body> + <release version="2.0" date="2008-02-20" description="2.0.0"> + <action type="add" dev="fgiust"> + new it.openutils.log4j.AlternateSMTPAppender that aggregates similar log messages. + </action> + <action type="update" dev="fgiust">commons-lang dependency added</action> + </release> + </body> +</document> \ No newline at end of file Property changes on: trunk/openutils-log4j/src/site/changes/changes.xml ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2008-02-20 10:07:22
|
Revision: 649 http://openutils.svn.sourceforge.net/openutils/?rev=649&view=rev Author: fgiust Date: 2008-02-20 02:02:04 -0800 (Wed, 20 Feb 2008) Log Message: ----------- [maven-release-plugin] prepare release openutils-log4j-2.0 Modified Paths: -------------- trunk/openutils-log4j/pom.xml Modified: trunk/openutils-log4j/pom.xml =================================================================== --- trunk/openutils-log4j/pom.xml 2008-02-20 10:01:32 UTC (rev 648) +++ trunk/openutils-log4j/pom.xml 2008-02-20 10:02:04 UTC (rev 649) @@ -1,6 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.sourceforge.openutils</groupId> @@ -10,7 +9,7 @@ </parent> <artifactId>openutils-log4j</artifactId> <packaging>jar</packaging> - <version>2.0-SNAPSHOT</version> + <version>2.0</version> <name>openutils for Log4j</name> <description>openutils log4j extensions</description> <build> @@ -55,4 +54,10 @@ <scope>test</scope> </dependency> </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-log4j-2.0</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-log4j-2.0</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-log4j-2.0</url> + </scm> </project> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2008-02-20 10:07:14
|
Revision: 650 http://openutils.svn.sourceforge.net/openutils/?rev=650&view=rev Author: fgiust Date: 2008-02-20 02:02:19 -0800 (Wed, 20 Feb 2008) Log Message: ----------- [maven-release-plugin] copy for tag openutils-log4j-2.0 Added Paths: ----------- tags/openutils-log4j-2.0/ tags/openutils-log4j-2.0/pom.xml tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java tags/openutils-log4j-2.0/src/site/changes/ Removed Paths: ------------- tags/openutils-log4j-2.0/pom.xml tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java Copied: tags/openutils-log4j-2.0 (from rev 594, trunk/openutils-log4j) Deleted: tags/openutils-log4j-2.0/pom.xml =================================================================== --- trunk/openutils-log4j/pom.xml 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-log4j-2.0/pom.xml 2008-02-20 10:02:19 UTC (rev 650) @@ -1,53 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils</artifactId> - <version>7</version> - <relativePath>..</relativePath> - </parent> - <artifactId>openutils-log4j</artifactId> - <packaging>jar</packaging> - <version>2.0-SNAPSHOT</version> - <name>openutils for Log4j</name> - <description>openutils log4j extensions</description> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <configuration> - <source>1.5</source> - <target>1.5</target> - </configuration> - </plugin> - </plugins> - </build> - <dependencies> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.14</version> - </dependency> - <dependency> - <groupId>javax.mail</groupId> - <artifactId>mail</artifactId> - <version>1.4</version> - <optional>true</optional> - </dependency> - <dependency> - <groupId>javax.servlet</groupId> - <artifactId>servlet-api</artifactId> - <version>2.4</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.0</version> - <scope>test</scope> - </dependency> - </dependencies> -</project> Copied: tags/openutils-log4j-2.0/pom.xml (from rev 649, trunk/openutils-log4j/pom.xml) =================================================================== --- tags/openutils-log4j-2.0/pom.xml (rev 0) +++ tags/openutils-log4j-2.0/pom.xml 2008-02-20 10:02:19 UTC (rev 650) @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>7</version> + <relativePath>..</relativePath> + </parent> + <artifactId>openutils-log4j</artifactId> + <packaging>jar</packaging> + <version>2.0</version> + <name>openutils for Log4j</name> + <description>openutils log4j extensions</description> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.5</source> + <target>1.5</target> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.14</version> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.3</version> + </dependency> + <dependency> + <groupId>javax.mail</groupId> + <artifactId>mail</artifactId> + <version>1.4</version> + <optional>true</optional> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.4</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.0</version> + <scope>test</scope> + </dependency> + </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-log4j-2.0</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-log4j-2.0</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-log4j-2.0</url> + </scm> +</project> Deleted: tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java =================================================================== --- trunk/openutils-log4j/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java 2008-02-20 10:02:19 UTC (rev 650) @@ -1,663 +0,0 @@ -/* - * Copyright Openmind http://www.openmindonline.it - * - * 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 it.openutils.log4j; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Properties; -import java.util.Timer; -import java.util.TimerTask; - -import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.Multipart; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; - -import org.apache.log4j.AppenderSkeleton; -import org.apache.log4j.Layout; -import org.apache.log4j.Level; -import org.apache.log4j.PatternLayout; -import org.apache.log4j.helpers.LogLog; -import org.apache.log4j.helpers.OptionConverter; -import org.apache.log4j.spi.ErrorCode; -import org.apache.log4j.spi.LoggingEvent; -import org.apache.log4j.spi.TriggeringEventEvaluator; - - -/** - * An alternative of <code>org.apache.log4j.net.SMTPAppender</code> with few differences: - * <ul> - * <li>allow customizing the mail subject using a pattern</li> - * <li>can be configured with a timeout (in seconds), it will only send messages after this timeout</li> - * <li>it will send a mail for every single message (bufferSize is not supported), but it will aggregate any identical - * log event received during the timeout. Identical events are log with same message and same stack trace</li> - * </ul> - * - * <pre> - * <appender name="mail" class="it.openutils.log4j.AlternateSMTPAppender"> - * <param name="Threshold" value="ERROR" /> - * <param name="To" value="lo...@ex..." /> - * <param name="From" value="in...@ex..." /> - * <param name="SMTPHost" value="localhost" /> - * <param name="Timeout" value="180" /> - * <param name="Subject" value="[EXAMPLE] %m" /> - * <layout class="org.apache.log4j.PatternLayout"> - * <param name="ConversionPattern" value="%-5p %c %d{dd.MM.yyyy HH:mm:ss} -- %m%n" /> - * </layout> - * <param name="Header" - * value=" - * =================================== - * Myapp (production environment) - * Date: %d{dd.MM.yyyy HH:mm:ss} - * =================================== - * " /> - * </appender> - * </pre> - * - * @author Fabrizio Giustina - * @version $Id$ - */ -public class AlternateSMTPAppender extends AppenderSkeleton -{ - - private String to; - - private String from; - - private Layout subjectLayout; - - private String smtpHost; - - private boolean locationInfo; - - private Timer timer = new Timer(); - - private TimerTask timerTask; - - private int timeout; - - protected Map<LoggingEventAggregator, LoggingEventAggregator> events = new LinkedHashMap<LoggingEventAggregator, LoggingEventAggregator>() - { - - /** - * - */ - private static final long serialVersionUID = 1L; - - /** - * {@inheritDoc} - */ - @Override - public LoggingEventAggregator put(LoggingEventAggregator key, LoggingEventAggregator value) - { - LoggingEventAggregator lea = this.get(key); - if (lea != null) - { - lea.incrementCount(); - return lea; - } - - return super.put(key, value); - } - - }; - - protected Message msg; - - protected TriggeringEventEvaluator evaluator; - - /** - * The default constructor will instantiate the appender with a {@link TriggeringEventEvaluator} that will trigger - * on events with level ERROR or higher. - */ - public AlternateSMTPAppender() - { - this(new DefaultEvaluator()); - } - - /** - * Use <code>evaluator</code> passed as parameter as the {@link TriggeringEventEvaluator} for this SMTPAppender. - */ - public AlternateSMTPAppender(TriggeringEventEvaluator evaluator) - { - this.evaluator = evaluator; - } - - /** - * Activate the specified options, such as the smtp host, the recipient, from, etc. - */ - @Override - public void activateOptions() - { - Properties props = new Properties(System.getProperties()); - if (smtpHost != null) - { - props.put("mail.smtp.host", smtpHost); - } - - Session session = Session.getInstance(props, null); - // session.setDebug(true); - msg = new MimeMessage(session); - - try - { - if (from != null) - { - msg.setFrom(getAddress(from)); - } - else - { - msg.setFrom(); - } - - msg.setRecipients(Message.RecipientType.TO, parseAddress(to)); - } - catch (MessagingException e) - { - LogLog.error("Could not activate SMTPAppender options.", e); - } - } - - /** - * Perform SMTPAppender specific appending actions, mainly adding the event to a cyclic buffer and checking if the - * event triggers an e-mail to be sent. - */ - @Override - public void append(LoggingEvent event) - { - - if (!checkEntryConditions()) - { - return; - } - - event.getThreadName(); - event.getNDC(); - if (locationInfo) - { - event.getLocationInformation(); - } - - LoggingEventAggregator leg = new LoggingEventAggregator(event); - - events.put(leg, leg); - - if (evaluator.isTriggeringEvent(event)) - { - if (timeout == 0) - { - // send immediately - sendBuffer(events.values()); - return; - } - if (timerTask == null) - { - - timerTask = new TimerTask() - { - - @Override - public void run() - { - Collection<LoggingEventAggregator> le; - synchronized (events) - { - le = new ArrayList<LoggingEventAggregator>(events.values()); - events.clear(); - timerTask = null; - } - - sendBuffer(le); - } - }; - - this.timer.schedule(this.timerTask, this.timeout * 1000L); - - } - } - } - - /** - * This method determines if there is a sense in attempting to append. - * <p> - * It checks whether there is a set output target and also if there is a set layout. If these checks fail, then the - * boolean value <code>false</code> is returned. - */ - protected boolean checkEntryConditions() - { - if (this.msg == null) - { - errorHandler.error("Message object not configured."); - return false; - } - - if (this.evaluator == null) - { - errorHandler.error("No TriggeringEventEvaluator is set for appender [" + name + "]."); - return false; - } - - if (this.layout == null) - { - errorHandler.error("No layout set for appender named [" + name + "]."); - return false; - } - return true; - } - - @Override - public synchronized void close() - { - this.closed = true; - } - - InternetAddress getAddress(String addressStr) - { - try - { - return new InternetAddress(addressStr); - } - catch (AddressException e) - { - errorHandler.error("Could not parse address [" + addressStr + "].", e, ErrorCode.ADDRESS_PARSE_FAILURE); - return null; - } - } - - InternetAddress[] parseAddress(String addressStr) - { - try - { - return InternetAddress.parse(addressStr, true); - } - catch (AddressException e) - { - errorHandler.error("Could not parse address [" + addressStr + "].", e, ErrorCode.ADDRESS_PARSE_FAILURE); - return null; - } - } - - /** - * Returns value of the <b>To</b> option. - */ - public String getTo() - { - return to; - } - - /** - * The <code>SMTPAppender</code> requires a {@link org.apache.log4j.Layout layout}. - */ - @Override - public boolean requiresLayout() - { - return true; - } - - /** - * Send the contents of the cyclic buffer as an e-mail message. - */ - protected void sendBuffer(Collection<LoggingEventAggregator> eventsCollection) - { - - // Note: this code already owns the monitor for this - // appender. This frees us from needing to synchronize on 'cb'. - try - { - - for (LoggingEventAggregator lea : eventsCollection) - { - MimeBodyPart part = new MimeBodyPart(); - - StringBuffer sbuf = new StringBuffer(); - String t = layout.getHeader(); - if (t != null) - { - sbuf.append(t); - } - - sbuf.append("\nNumber of occurences: "); - sbuf.append(lea.getCount()); - sbuf.append("\n\n"); - - LoggingEvent event = lea.getLoggingEvent(); - - if (this.subjectLayout != null) - { - String subject = this.subjectLayout.format(event); - - if (subject != null) - { - subject = subject.trim(); - if (subject.indexOf("\n") > 0) - { - subject = subject.substring(0, subject.indexOf("\n")); - } - } - - this.msg.setSubject(subject); - } - - sbuf.append(layout.format(event)); - if (layout.ignoresThrowable()) - { - String[] s = event.getThrowableStrRep(); - if (s != null) - { - for (String element : s) - { - sbuf.append(element); - } - } - } - t = layout.getFooter(); - if (t != null) - { - sbuf.append(t); - } - part.setContent(sbuf.toString(), layout.getContentType()); - - Multipart mp = new MimeMultipart(); - mp.addBodyPart(part); - msg.setContent(mp); - - msg.setSentDate(new Date()); - Transport.send(msg); - } - - } - catch (Exception e) - { - LogLog.error("Error occured while sending e-mail notification.", e); - } - - } - - /** - * Returns value of the <b>EvaluatorClass</b> option. - */ - public String getEvaluatorClass() - { - return evaluator == null ? null : evaluator.getClass().getName(); - } - - /** - * Returns value of the <b>From</b> option. - */ - public String getFrom() - { - return from; - } - - /** - * Returns value of the <b>Subject</b> option. - */ - public String getSubject() - { - return subjectLayout.toString(); - } - - /** - * The <b>From</b> option takes a string value which should be a e-mail address of the sender. - */ - public void setFrom(String from) - { - this.from = from; - } - - /** - * The <b>Subject</b> option takes a string value which should be a the subject of the e-mail message. - */ - public void setSubject(String subjectPattern) - { - this.subjectLayout = new PatternLayout(subjectPattern); - } - - /** - * This option is ignored! - */ - @Deprecated - public void setBufferSize(int bufferSize) - { - // kept as deprecated - LogLog.warn("BufferSize property is deprecated for " + getClass().getName()); - } - - /** - * The <b>SMTPHost</b> option takes a string value which should be a the host name of the SMTP server that will - * send the e-mail message. - */ - public void setSMTPHost(String smtpHost) - { - this.smtpHost = smtpHost; - } - - /** - * Returns value of the <b>SMTPHost</b> option. - */ - public String getSMTPHost() - { - return smtpHost; - } - - /** - * The <b>To</b> option takes a string value which should be a comma separated list of e-mail address of the - * recipients. - */ - public void setTo(String to) - { - this.to = to; - } - - /** - * Returns value of the <b>BufferSize</b> option. - */ - public int getBufferSize() - { - return 0; - } - - /** - * The <b>EvaluatorClass</b> option takes a string value representing the name of the class implementing the {@link - * TriggeringEventEvaluator} interface. A corresponding object will be instantiated and assigned as the triggering - * event evaluator for the SMTPAppender. - */ - public void setEvaluatorClass(String value) - { - evaluator = (TriggeringEventEvaluator) OptionConverter.instantiateByClassName( - value, - TriggeringEventEvaluator.class, - evaluator); - } - - /** - * @param value - */ - public void setEvaluator(TriggeringEventEvaluator value) - { - evaluator = value; - } - - /** - * The <b>LocationInfo</b> option takes a boolean value. By default, it is set to false which means there will be - * no effort to extract the location information related to the event. As a result, the layout that formats the - * events as they are sent out in an e-mail is likely to place the wrong location information (if present in the - * format). - * <p> - * Location information extraction is comparatively very slow and should be avoided unless performance is not a - * concern. - */ - public void setLocationInfo(boolean locationInfo) - { - this.locationInfo = locationInfo; - } - - /** - * Returns value of the <b>LocationInfo</b> option. - */ - public boolean getLocationInfo() - { - return locationInfo; - } - - /** - * Returns the timeout. - * @return the timeout - */ - public int getTimeout() - { - return timeout; - } - - /** - * Sets the timeout. - * @param timeout the timeout to set - */ - public void setTimeout(int timeout) - { - this.timeout = timeout; - } -} - - -class LoggingEventAggregator -{ - - private LoggingEvent loggingEvent; - - private int count; - - public LoggingEventAggregator(LoggingEvent loggingEvent) - { - this.loggingEvent = loggingEvent; - this.count = 1; - } - - /** - * Returns the loggingEvent. - * @return the loggingEvent - */ - public LoggingEvent getLoggingEvent() - { - return loggingEvent; - } - - /** - * Returns the count. - * @return the count - */ - public int getCount() - { - return count; - } - - /** - * Sets the count. - * @param count the count to set - */ - public void setCount(int count) - { - this.count = count; - } - - public void incrementCount() - { - count++; - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() - { - final int prime = 31; - int result = 1; - - Object lem = loggingEvent.getMessage(); - String[] thstr = loggingEvent.getThrowableStrRep(); - result = prime * result + ((lem == null) ? 0 : lem.hashCode()); - result = prime * result + Arrays.hashCode(thstr); - return result; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object obj) - { - if (this == obj) - { - return true; - } - if (obj == null) - { - return false; - } - if (getClass() != obj.getClass()) - { - return false; - } - final LoggingEventAggregator other = (LoggingEventAggregator) obj; - - Object lem = loggingEvent.getMessage(); - String[] thstr = loggingEvent.getThrowableStrRep(); - - Object otherLem = other.loggingEvent.getMessage(); - String[] otherThstr = other.loggingEvent.getThrowableStrRep(); - - if (lem == null) - { - if (otherLem != null) - { - return false; - } - } - - else if (!lem.equals(otherLem)) - { - return false; - } - if (!Arrays.equals(thstr, otherThstr)) - { - return false; - } - return true; - } - -} - - -class DefaultEvaluator implements TriggeringEventEvaluator -{ - - public boolean isTriggeringEvent(LoggingEvent event) - { - return event.getLevel().isGreaterOrEqual(Level.ERROR); - } -} Copied: tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java (from rev 648, trunk/openutils-log4j/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java) =================================================================== --- tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java (rev 0) +++ tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/AlternateSMTPAppender.java 2008-02-20 10:02:19 UTC (rev 650) @@ -0,0 +1,676 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.log4j; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Timer; +import java.util.TimerTask; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.mail.internet.MimeUtility; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.AppenderSkeleton; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.PatternLayout; +import org.apache.log4j.helpers.LogLog; +import org.apache.log4j.helpers.OptionConverter; +import org.apache.log4j.spi.ErrorCode; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.spi.TriggeringEventEvaluator; + + +/** + * An alternative of <code>org.apache.log4j.net.SMTPAppender</code> with few differences: + * <ul> + * <li>allow customizing the mail subject using a pattern</li> + * <li>can be configured with a timeout (in seconds), it will only send messages after this timeout</li> + * <li>it will send a mail for every single message (bufferSize is not supported), but it will aggregate any identical + * log event received during the timeout. Identical events are log with same message and same stack trace</li> + * </ul> + * + * <pre> + * <appender name="mail" class="it.openutils.log4j.AlternateSMTPAppender"> + * <param name="Threshold" value="ERROR" /> + * <param name="To" value="lo...@ex..." /> + * <param name="From" value="in...@ex..." /> + * <param name="SMTPHost" value="localhost" /> + * <param name="Timeout" value="180" /> + * <param name="Subject" value="[EXAMPLE] %m" /> + * <layout class="it.openutils.log4j.FilteredPatternLayout"> + * <param name="ConversionPattern" value="%-5p %c %d{dd.MM.yyyy HH:mm:ss} -- %m%n" /> + * <param name="Header" + * value=" + * =================================== + * Myapp (production environment) + * Date: %d{dd.MM.yyyy HH:mm:ss} + * =================================== + * " /> + * </layout> + * </appender> + * </pre> + * + * @author Fabrizio Giustina + * @version $Id: $ + */ +public class AlternateSMTPAppender extends AppenderSkeleton +{ + + private String to; + + private String from; + + private Layout subjectLayout; + + private String smtpHost; + + private boolean locationInfo; + + private Timer timer = new Timer("log4j mail appender", true); + + private TimerTask timerTask; + + private int timeout; + + protected Map<LoggingEventAggregator, LoggingEventAggregator> events = new LinkedHashMap<LoggingEventAggregator, LoggingEventAggregator>() + { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * {@inheritDoc} + */ + @Override + public LoggingEventAggregator put(LoggingEventAggregator key, LoggingEventAggregator value) + { + LoggingEventAggregator lea = this.get(key); + if (lea != null) + { + lea.incrementCount(); + return lea; + } + + return super.put(key, value); + } + + }; + + protected Message msg; + + protected TriggeringEventEvaluator evaluator; + + /** + * The default constructor will instantiate the appender with a {@link TriggeringEventEvaluator} that will trigger + * on events with level ERROR or higher. + */ + public AlternateSMTPAppender() + { + this(new DefaultEvaluator()); + + // force loading this class + MimeBodyPart.class.getName(); + MimeUtility.class.getName(); + } + + /** + * Use <code>evaluator</code> passed as parameter as the {@link TriggeringEventEvaluator} for this SMTPAppender. + */ + public AlternateSMTPAppender(TriggeringEventEvaluator evaluator) + { + this.evaluator = evaluator; + } + + /** + * Activate the specified options, such as the smtp host, the recipient, from, etc. + */ + @Override + public void activateOptions() + { + Properties props = new Properties(System.getProperties()); + if (smtpHost != null) + { + props.put("mail.smtp.host", smtpHost); + } + + Session session = Session.getInstance(props, null); + // session.setDebug(true); + msg = new MimeMessage(session); + + try + { + if (from != null) + { + msg.setFrom(getAddress(from)); + } + else + { + msg.setFrom(); + } + + msg.setRecipients(Message.RecipientType.TO, parseAddress(to)); + } + catch (MessagingException e) + { + LogLog.error("Could not activate SMTPAppender options.", e); + } + } + + /** + * Perform SMTPAppender specific appending actions, mainly adding the event to a cyclic buffer and checking if the + * event triggers an e-mail to be sent. + */ + @Override + public void append(LoggingEvent event) + { + + if (!checkEntryConditions()) + { + return; + } + + event.getThreadName(); + event.getNDC(); + if (locationInfo) + { + event.getLocationInformation(); + } + + LoggingEventAggregator leg = new LoggingEventAggregator(event); + + events.put(leg, leg); + + if (evaluator.isTriggeringEvent(event)) + { + if (timeout == 0) + { + // send immediately + sendBuffer(events.values()); + return; + } + if (timerTask == null) + { + + timerTask = new TimerTask() + { + + @Override + public void run() + { + Collection<LoggingEventAggregator> le; + synchronized (events) + { + le = new ArrayList<LoggingEventAggregator>(events.values()); + events.clear(); + timerTask = null; + } + + sendBuffer(le); + } + }; + + this.timer.schedule(this.timerTask, this.timeout * 1000L); + + } + } + } + + /** + * This method determines if there is a sense in attempting to append. + * <p> + * It checks whether there is a set output target and also if there is a set layout. If these checks fail, then the + * boolean value <code>false</code> is returned. + */ + protected boolean checkEntryConditions() + { + if (this.msg == null) + { + errorHandler.error("Message object not configured."); + return false; + } + + if (this.evaluator == null) + { + errorHandler.error("No TriggeringEventEvaluator is set for appender [" + name + "]."); + return false; + } + + if (this.layout == null) + { + errorHandler.error("No layout set for appender named [" + name + "]."); + return false; + } + return true; + } + + @Override + public synchronized void close() + { + this.closed = true; + } + + InternetAddress getAddress(String addressStr) + { + try + { + return new InternetAddress(addressStr); + } + catch (AddressException e) + { + errorHandler.error("Could not parse address [" + addressStr + "].", e, ErrorCode.ADDRESS_PARSE_FAILURE); + return null; + } + } + + InternetAddress[] parseAddress(String addressStr) + { + try + { + return InternetAddress.parse(addressStr, true); + } + catch (AddressException e) + { + errorHandler.error("Could not parse address [" + addressStr + "].", e, ErrorCode.ADDRESS_PARSE_FAILURE); + return null; + } + } + + /** + * Returns value of the <b>To</b> option. + */ + public String getTo() + { + return to; + } + + /** + * The <code>SMTPAppender</code> requires a {@link org.apache.log4j.Layout layout}. + */ + @Override + public boolean requiresLayout() + { + return true; + } + + /** + * Send the contents of the cyclic buffer as an e-mail message. + */ + protected void sendBuffer(Collection<LoggingEventAggregator> eventsCollection) + { + + // Note: this code already owns the monitor for this + // appender. This frees us from needing to synchronize on 'cb'. + try + { + + for (LoggingEventAggregator lea : eventsCollection) + { + MimeBodyPart part = new MimeBodyPart(); + + StringBuffer sbuf = new StringBuffer(); + String t = layout.getHeader(); + if (t != null) + { + t = StringUtils.replace(t, "%o", Integer.toString(lea.getCount())); + t = StringUtils.replace(t, "%n", Layout.LINE_SEP); + sbuf.append(t); + sbuf.append("\n"); + } + + LoggingEvent event = lea.getLoggingEvent(); + + if (this.subjectLayout != null) + { + String subject = this.subjectLayout.format(event); + + if (subject != null) + { + subject = subject.trim(); + if (subject.indexOf("\n") > 0) + { + subject = subject.substring(0, subject.indexOf("\n")); + } + } + + this.msg.setSubject(subject); + } + + sbuf.append(layout.format(event)); + if (layout.ignoresThrowable()) + { + String[] s = event.getThrowableStrRep(); + if (s != null) + { + for (String element : s) + { + sbuf.append(element); + } + } + } + t = layout.getFooter(); + if (t != null) + { + t = StringUtils.replace(t, "%n", Layout.LINE_SEP); + sbuf.append(t); + } + part.setContent(sbuf.toString(), layout.getContentType()); + + Multipart mp = new MimeMultipart(); + mp.addBodyPart(part); + msg.setContent(mp); + + msg.setSentDate(new Date()); + Transport.send(msg); + } + + } + catch (Exception e) + { + LogLog.error("Error occured while sending e-mail notification.", e); + } + + } + + /** + * Returns value of the <b>EvaluatorClass</b> option. + */ + public String getEvaluatorClass() + { + return evaluator == null ? null : evaluator.getClass().getName(); + } + + /** + * Returns value of the <b>From</b> option. + */ + public String getFrom() + { + return from; + } + + /** + * Returns value of the <b>Subject</b> option. + */ + public String getSubject() + { + return subjectLayout.toString(); + } + + /** + * The <b>From</b> option takes a string value which should be a e-mail address of the sender. + */ + public void setFrom(String from) + { + this.from = from; + } + + /** + * The <b>Subject</b> option takes a string value which should be a the subject of the e-mail message. + */ + public void setSubject(String subjectPattern) + { + this.subjectLayout = new PatternLayout(subjectPattern); + } + + /** + * This option is ignored! + */ + @Deprecated + public void setBufferSize(int bufferSize) + { + // kept as deprecated + LogLog.warn("BufferSize property is deprecated for " + getClass().getName()); + } + + /** + * The <b>SMTPHost</b> option takes a string value which should be a the host name of the SMTP server that will + * send the e-mail message. + */ + public void setSMTPHost(String smtpHost) + { + this.smtpHost = smtpHost; + } + + /** + * Returns value of the <b>SMTPHost</b> option. + */ + public String getSMTPHost() + { + return smtpHost; + } + + /** + * The <b>To</b> option takes a string value which should be a comma separated list of e-mail address of the + * recipients. + */ + public void setTo(String to) + { + this.to = to; + } + + /** + * Returns value of the <b>BufferSize</b> option. + */ + public int getBufferSize() + { + return 0; + } + + /** + * The <b>EvaluatorClass</b> option takes a string value representing the name of the class implementing the {@link + * TriggeringEventEvaluator} interface. A corresponding object will be instantiated and assigned as the triggering + * event evaluator for the SMTPAppender. + */ + public void setEvaluatorClass(String value) + { + evaluator = (TriggeringEventEvaluator) OptionConverter.instantiateByClassName( + value, + TriggeringEventEvaluator.class, + evaluator); + } + + /** + * @param value + */ + public void setEvaluator(TriggeringEventEvaluator value) + { + evaluator = value; + } + + /** + * The <b>LocationInfo</b> option takes a boolean value. By default, it is set to false which means there will be + * no effort to extract the location information related to the event. As a result, the layout that formats the + * events as they are sent out in an e-mail is likely to place the wrong location information (if present in the + * format). + * <p> + * Location information extraction is comparatively very slow and should be avoided unless performance is not a + * concern. + */ + public void setLocationInfo(boolean locationInfo) + { + this.locationInfo = locationInfo; + } + + /** + * Returns value of the <b>LocationInfo</b> option. + */ + public boolean getLocationInfo() + { + return locationInfo; + } + + /** + * Returns the timeout. + * @return the timeout + */ + public int getTimeout() + { + return timeout; + } + + /** + * Sets the timeout. + * @param timeout the timeout to set + */ + public void setTimeout(int timeout) + { + this.timeout = timeout; + } +} + + +class LoggingEventAggregator +{ + + private LoggingEvent loggingEvent; + + private int count; + + public LoggingEventAggregator(LoggingEvent loggingEvent) + { + this.loggingEvent = loggingEvent; + this.count = 1; + } + + /** + * Returns the loggingEvent. + * @return the loggingEvent + */ + public LoggingEvent getLoggingEvent() + { + return loggingEvent; + } + + /** + * Returns the count. + * @return the count + */ + public int getCount() + { + return count; + } + + /** + * Sets the count. + * @param count the count to set + */ + public void setCount(int count) + { + this.count = count; + } + + public void incrementCount() + { + count++; + } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + + Object lem = loggingEvent.getMessage(); + String[] thstr = loggingEvent.getThrowableStrRep(); + result = prime * result + ((lem == null) ? 0 : lem.hashCode()); + result = prime * result + Arrays.hashCode(thstr); + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + final LoggingEventAggregator other = (LoggingEventAggregator) obj; + + Object lem = loggingEvent.getMessage(); + String[] thstr = loggingEvent.getThrowableStrRep(); + + Object otherLem = other.loggingEvent.getMessage(); + String[] otherThstr = other.loggingEvent.getThrowableStrRep(); + + int length = Math.min(otherThstr.length, thstr.length); + length = Math.min(10, length); + + otherThstr = (String[]) ArrayUtils.subarray(otherThstr, 0, length); + String[] thisThstr = (String[]) ArrayUtils.subarray(thstr, 0, length); + + if (lem == null) + { + if (otherLem != null) + { + return false; + } + } + + else if (!lem.equals(otherLem)) + { + return false; + } + if (!Arrays.equals(thisThstr, otherThstr)) + { + return false; + } + return true; + } + +} + + +class DefaultEvaluator implements TriggeringEventEvaluator +{ + + public boolean isTriggeringEvent(LoggingEvent event) + { + return event.getLevel().isGreaterOrEqual(Level.ERROR); + } +} \ No newline at end of file Deleted: tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java =================================================================== --- trunk/openutils-log4j/src/main/java/it/openutils/log4j/FilteredPatternLayout.java 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java 2008-02-20 10:02:19 UTC (rev 650) @@ -1,145 +0,0 @@ -/* - * Copyright 2005 Fabrizio Giustina. - * - * 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 it.openutils.log4j; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -import org.apache.log4j.PatternLayout; -import org.apache.log4j.spi.LoggingEvent; -import org.apache.log4j.spi.ThrowableInformation; - - -/** - * An extension of <code>org.apache.log4j.PatternLayout</code> which strips out from stack traces a list of configured - * entries. Sample configuration: - * - * <pre> - * <appender name="console" class="org.apache.log4j.ConsoleAppender"> - * <layout class="it.openutils.log4j.FilteredPatternLayout"> - * <param name="ConversionPattern" value="%-5p %c %F(%M:%L) %d{dd.MM.yyyy HH:mm:ss} %m%n" /> - * <param name="Filter" value="org.apache.catalina" /> - * <param name="Filter" value="sun.reflect" /> - * <param name="Filter" value="javax.servlet.http" /> - * </layout> - * </appender> - * </pre> - * - * @author Fabrizio Giustina - * @version $Id$ - */ -public class FilteredPatternLayout extends PatternLayout -{ - - /** - * Line separator for stacktrace frames. - */ - private static String lineSeparator = "\n"; - - /** - * Holds the list of filtered frames. - */ - private Set<String> filteredFrames = new HashSet<String>(); - - static - { - try - { - lineSeparator = System.getProperty("line.separator"); - } - catch (SecurityException ex) - { - // ignore - } - } - - /** - * {@inheritDoc} - */ - @Override - public boolean ignoresThrowable() - { - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public String format(LoggingEvent event) - { - - ThrowableInformation throwableInformation = event.getThrowableInformation(); - - if (throwableInformation == null) - { - return super.format(event); - } - - return super.format(event) + getFilteredStacktrace(throwableInformation); - } - - /** - * Adds a new filtered frame. Any stack frame starting with <code>"at "</code> + <code>filter</code> will not be - * written to the log. - * @param filter a class name or package name to be filtered - */ - public void setFilter(String filter) - { - filteredFrames.add("at " + filter); - } - - private String getFilteredStacktrace(ThrowableInformation throwableInformation) - { - StringBuffer buffer = new StringBuffer(); - - String[] s = throwableInformation.getThrowableStrRep(); - - for (int j = 0; j < s.length; j++) - { - String string = s[j]; - - if (startsWithAFilteredPAttern(string)) - { - continue; - } - buffer.append(string); - buffer.append(lineSeparator); - } - - return buffer.toString(); - } - - /** - * Check if the given string starts with any of the filtered patterns. - * @param string checked String - * @return <code>true</code> if the begininning of the string matches a filtered pattern, <code>false</code> - * otherwise - */ - private boolean startsWithAFilteredPAttern(String string) - { - Iterator<String> iterator = filteredFrames.iterator(); - while (iterator.hasNext()) - { - if (string.trim().startsWith(iterator.next())) - { - return true; - } - } - return false; - } -} Copied: tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java (from rev 648, trunk/openutils-log4j/src/main/java/it/openutils/log4j/FilteredPatternLayout.java) =================================================================== --- tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java (rev 0) +++ tags/openutils-log4j-2.0/src/main/java/it/openutils/log4j/FilteredPatternLayout.java 2008-02-20 10:02:19 UTC (rev 650) @@ -0,0 +1,188 @@ +/* + * Copyright 2005 Fabrizio Giustina. + * + * 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 it.openutils.log4j; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.apache.log4j.PatternLayout; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.spi.ThrowableInformation; + + +/** + * An extension of <code>org.apache.log4j.PatternLayout</code> which strips out from stack traces a list of configured + * entries. Sample configuration: + * + * <pre> + * <appender name="console" class="org.apache.log4j.ConsoleAppender"> + * <layout class="it.openutils.log4j.FilteredPatternLayout"> + * <param name="ConversionPattern" value="%-5p %c %F(%M:%L) %d{dd.MM.yyyy HH:mm:ss} %m%n" /> + * <param name="Filter" value="org.apache.catalina" /> + * <param name="Filter" value="sun.reflect" /> + * <param name="Filter" value="javax.servlet.http" /> + * </layout> + * </appender> + * </pre> + * + * @author Fabrizio Giustina + * @version $Id: FilteredPatternLayout.java 8589 2008-02-10 18:01:57Z fgiust $ + */ +public class FilteredPatternLayout extends PatternLayout +{ + + /** + * Holds the list of filtered frames. + */ + private Set<String> filteredFrames = new HashSet<String>(); + + private String header; + + private String footer; + + /** + * Line separator for stacktrace frames. + */ + private static String lineSeparator = "\n"; + + static + { + try + { + lineSeparator = System.getProperty("line.separator"); + } + catch (SecurityException ex) + { + // ignore + } + } + + /** + * Returns the header. + * @return the header + */ + @Override + public String getHeader() + { + return header; + } + + /** + * Sets the header. + * @param header the header to set + */ + public void setHeader(String header) + { + this.header = header; + } + + /** + * Returns the footer. + * @return the footer + */ + @Override + public String getFooter() + { + return footer; + } + + /** + * Sets the footer. + * @param footer the footer to set + */ + public void setFooter(String footer) + { + this.footer = footer; + } + + /** + * @see org.apache.log4j.Layout#ignoresThrowable() + */ + @Override + public boolean ignoresThrowable() + { + return false; + } + + /** + * @see org.apache.log4j.PatternLayout#format(org.apache.log4j.spi.LoggingEvent) + */ + @Override + public String format(LoggingEvent event) + { + + ThrowableInformation throwableInformation = event.getThrowableInformation(); + + if (throwableInformation == null) + { + return super.format(event); + } + + return super.format(event) + getFilteredStacktrace(throwableInformation); + } + + /** + * Adds a new filtered frame. Any stack frame starting with <code>"at "</code> + <code>filter</code> will not be + * written to the log. + * @param filter a class name or package name to be filtered + */ + public void setFilter(String filter) + { + filteredFrames.add("at " + filter); + } + + private String getFilteredStacktrace(ThrowableInformation throwableInformation) + { + StringBuffer buffer = new StringBuffer(); + + String[] s = throwableInformation.getThrowableStrRep(); + + for (int j = 0; j < s.length; j++) + { + String string = s[j]; + + if (startsWithAFilteredPAttern(string)) + { + continue; + } + buffer.append(string); + buffer.append(lineSeparator); + } + + return buffer.toString(); + } + + /** + * Check if the given string starts with any of the filtered patterns. + * @param string checked String + * @return <code>true</code> if the begininning of the string matches a filtered pattern, <code>false</code> + * otherwise + */ + private boolean startsWithAFilteredPAttern(String string) + { + Iterator<String> iterator = filteredFrames.iterator(); + while (iterator.hasNext()) + { + if (string.trim().startsWith(iterator.next())) + { + return true; + } + } + return false; + } + +} Copied: tags/openutils-log4j-2.0/src/site/changes (from rev 648, trunk/openutils-log4j/src/site/changes) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2008-02-20 10:07:09
|
Revision: 651 http://openutils.svn.sourceforge.net/openutils/?rev=651&view=rev Author: fgiust Date: 2008-02-20 02:02:28 -0800 (Wed, 20 Feb 2008) Log Message: ----------- [maven-release-plugin] prepare for next development iteration Modified Paths: -------------- trunk/openutils-log4j/pom.xml Modified: trunk/openutils-log4j/pom.xml =================================================================== --- trunk/openutils-log4j/pom.xml 2008-02-20 10:02:19 UTC (rev 650) +++ trunk/openutils-log4j/pom.xml 2008-02-20 10:02:28 UTC (rev 651) @@ -9,7 +9,7 @@ </parent> <artifactId>openutils-log4j</artifactId> <packaging>jar</packaging> - <version>2.0</version> + <version>2.0.1-SNAPSHOT</version> <name>openutils for Log4j</name> <description>openutils log4j extensions</description> <build> @@ -54,10 +54,4 @@ <scope>test</scope> </dependency> </dependencies> - - <scm> - <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-log4j-2.0</connection> - <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-log4j-2.0</developerConnection> - <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-log4j-2.0</url> - </scm> </project> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-19 18:32:08
|
Revision: 647 http://openutils.svn.sourceforge.net/openutils/?rev=647&view=rev Author: fcarone Date: 2008-02-19 10:31:24 -0800 (Tue, 19 Feb 2008) Log Message: ----------- More tests and various fixes: - javabeanfilter is only used for loading, so ignore all rules with permissions != LOAD Modified Paths: -------------- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/impl/SecurityRuleManagerImpl.java trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/filter/JavaBeanFilterTest.java trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/filter/JavaBeanFilter.java 2008-02-19 18:31:24 UTC (rev 647) @@ -15,6 +15,7 @@ */ package it.openutils.hibernate.security.filter; +import it.openutils.hibernate.security.dataobject.PermissionEnum; import it.openutils.hibernate.security.dataobject.SecurityRule; import java.lang.reflect.Field; @@ -33,6 +34,8 @@ import org.hibernate.Filter; import org.hibernate.HibernateException; import org.hibernate.engine.FilterDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -45,6 +48,12 @@ private FilterDefinition filterDefinition; /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(JavaBeanFilter.class); + + + /** * @param bean The bean to set rules for * @param securityRules The list of {@link SecurityRule}s to apply. * @throws ClassNotFoundException If the bean class has not been found @@ -93,6 +102,18 @@ filterName += entry.getKey(); List<SecurityRule> rules = entry.getValue(); + if (rules == null || rules.isEmpty()) + { + log.debug("No rules defined for role {}", entry.getKey()); + continue; + } + + if (!rulesContainLoad(rules)) + { + log.debug("No LOAD rules defined for role {}", entry.getKey()); + continue; + } + if (!StringUtils.isEmpty(filterDefCondition.toString())) { filterDefCondition.append(" OR "); @@ -101,6 +122,11 @@ StringBuffer subFilterCond = new StringBuffer(); for (SecurityRule securityRule : rules) { + if (!securityRule.getPermissions().contains(PermissionEnum.LOAD)) + { + log.debug("Skipping rule {} since it is not related to LOAD.", securityRule); + continue; + } String property = securityRule.getProperty(); filterName += property; @@ -164,6 +190,22 @@ } /** + * @param rules + * @return + */ + private boolean rulesContainLoad(List<SecurityRule> rules) + { + for (SecurityRule rule : rules) + { + if (rule.getPermissions().contains(PermissionEnum.LOAD)) + { + return true; + } + } + return false; + } + + /** * {@inheritDoc} */ public FilterDefinition getFilterDefinition() Modified: trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/impl/SecurityRuleManagerImpl.java =================================================================== --- trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/impl/SecurityRuleManagerImpl.java 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/main/java/it/openutils/hibernate/security/services/impl/SecurityRuleManagerImpl.java 2008-02-19 18:31:24 UTC (rev 647) @@ -12,8 +12,6 @@ import java.util.List; import org.hibernate.Filter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** @@ -22,12 +20,6 @@ */ public class SecurityRuleManagerImpl implements SecurityRuleManager { - - /** - * Logger. - */ - private Logger log = LoggerFactory.getLogger(SecurityRuleManagerImpl.class); - private SecurityRuleDAO securityRuleDAO; /** @@ -61,7 +53,6 @@ public Filter getEntityFilterFromRules(String entity, List<SecurityRule> rules) throws SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException { - // @todo: check rules consistency with the gentity return new JavaBeanFilter(entity, rules); } Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/SecurityIntegrationTest.java 2008-02-19 18:31:24 UTC (rev 647) @@ -86,10 +86,12 @@ new ArrayList<Criterion>()); Assert.assertNotNull(dummyObjects); Assert.assertEquals(1, dummyObjects.size()); + Assert.assertEquals(1, dummyObjects.get(0).getIntValue().intValue()); dummyObjects = securedObject.findFiltered(filter); Assert.assertNotNull(dummyObjects); Assert.assertEquals(1, dummyObjects.size()); + Assert.assertEquals(1, dummyObjects.get(0).getIntValue().intValue()); } /** @@ -182,11 +184,29 @@ List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); Assert.assertNotNull(dummyObjects); DummyDataobject ddo = dummyObjects.get(0); + ddo.setIntValue(0); dummyObjectManager.create(ddo); } /** + * @throws Exception Any exception + */ + @Test + public void testCreateWithIntValueNull() throws Exception + { + authenticate("UserDue", "password"); + DummyDataobject filter = new DummyDataobject(); + List<DummyDataobject> dummyObjects = securedObject.findFiltered(filter); + Assert.assertNotNull(dummyObjects); + DummyDataobject ddo = dummyObjects.get(0); + + ddo.setIntValue(null); + + dummyObjectManager.create(ddo); + } + + /** * Sets the securedObject. * @param securedObject the securedObject to set */ @@ -195,7 +215,6 @@ this.securedObject = securedObject; } - /** * Sets the authenticationProvider. * @param authenticationProvider the authenticationProvider to set @@ -205,7 +224,6 @@ this.authenticationProvider = authenticationProvider; } - /** * Sets the dummyObjectManager. * @param dummyObjectManager the dummyObjectManager to set Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/apptest/DummyObjectManagerImpl.java 2008-02-19 18:31:24 UTC (rev 647) @@ -11,8 +11,6 @@ public class DummyObjectManagerImpl implements DummyObjectManager { - private DummyDAO dummyDAO; - /** * {@inheritDoc} */ @@ -40,14 +38,4 @@ } - - /** - * Sets the dummyDAO. - * @param dummyDAO the dummyDAO to set - */ - public void setDummyDAO(DummyDAO dummyDAO) - { - this.dummyDAO = dummyDAO; - } - } Modified: trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/filter/JavaBeanFilterTest.java =================================================================== --- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/filter/JavaBeanFilterTest.java 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/filter/JavaBeanFilterTest.java 2008-02-19 18:31:24 UTC (rev 647) @@ -33,38 +33,62 @@ SecurityRule rule = new SecurityRule(); rule.setEntity("it.openutils.hibernate.security.dataobject.SecurityRule"); rule.setModifier(ModifierEnum.EQUALS); - List<PermissionEnum> permissions = new ArrayList<PermissionEnum>(); - permissions.add(PermissionEnum.CREATE); - rule.setPermissions(permissions); + List<PermissionEnum> permissionsRole1 = new ArrayList<PermissionEnum>(); + permissionsRole1.add(PermissionEnum.CREATE); + rule.setPermissions(permissionsRole1); rule.setProperty("entity"); rule.setRole("ROLE_1"); rule.setValue("VALUE"); SecurityRule rule2 = new SecurityRule(); rule2.setEntity("it.openutils.hibernate.security.dataobject.SecurityRule"); - rule2.setModifier(ModifierEnum.NOT); - rule2.setPermissions(permissions); + rule2.setModifier(ModifierEnum.EQUALS); + List<PermissionEnum> permissionsRole2 = new ArrayList<PermissionEnum>(); + permissionsRole2.add(PermissionEnum.LOAD); + rule2.setPermissions(permissionsRole2); rule2.setProperty("value"); rule2.setRole("ROLE_1"); rule2.setValue("VALUE2"); + SecurityRule rule2b = new SecurityRule(); + rule2b.setEntity("it.openutils.hibernate.security.dataobject.SecurityRule"); + rule2b.setModifier(ModifierEnum.EQUALS); + rule2b.setPermissions(permissionsRole2); + rule2b.setProperty("id"); + rule2b.setRole("ROLE_1"); + rule2b.setValue("4"); + SecurityRule rule3 = new SecurityRule(); rule3.setEntity("it.openutils.hibernate.security.dataobject.SecurityRule"); rule3.setModifier(ModifierEnum.NOT); - rule3.setPermissions(permissions); + List<PermissionEnum> permissionsRole3 = new ArrayList<PermissionEnum>(); + permissionsRole3.add(PermissionEnum.LOAD); + rule3.setPermissions(permissionsRole3); rule3.setProperty("value"); rule3.setRole("ROLE_2"); rule3.setValue("VALUE3"); rules.add(rule); rules.add(rule2); + rules.add(rule2b); rules.add(rule3); JavaBeanFilter filter = new JavaBeanFilter("it.openutils.hibernate.security.dataobject.SecurityRule", rules); Assert.assertNotNull(filter); - Assert.assertEquals("(ENTITY = 'VALUE' AND VALUE != 'VALUE2') OR (VALUE != 'VALUE3')", filter + Assert.assertEquals("(VALUE = 'VALUE2' AND ID_SECURITY_RULE = 4) OR (VALUE != 'VALUE3')", filter .getFilterDefinition() .getDefaultFilterCondition()); } + /** + * @throws Exception Any exception + */ + @Test(expected = IllegalArgumentException.class) + public void testFilterConstrutorFail() throws Exception + { + new JavaBeanFilter( + "it.openutils.hibernate.security.filter.JavaBeanFilter", + new ArrayList<SecurityRule>()); + } + } Modified: trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/test/resources/SecurityIntegrationTest-load.xml 2008-02-19 18:31:24 UTC (rev 647) @@ -115,7 +115,7 @@ <value>it.openutils.hibernate.security.apptest.DummyDataobject</value> <value>intValue</value> <value>1</value> - <value>EQUALS</value> + <value>NOT</value> </row> </table> <table name="SECRULE_PERMISSION"> Modified: trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml =================================================================== --- trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml 2008-02-19 18:29:31 UTC (rev 646) +++ trunk/openutils-hibernate-security/src/test/resources/spring-managers.xml 2008-02-19 18:31:24 UTC (rev 647) @@ -25,9 +25,7 @@ </props> </property> <property name="target"> - <bean class="it.openutils.hibernate.security.apptest.DummyObjectManagerImpl"> - <property name="dummyDAO" ref="dummyDAO" /> - </bean> + <bean class="it.openutils.hibernate.security.apptest.DummyObjectManagerImpl" /> </property> </bean> <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-19 18:29:33
|
Revision: 646 http://openutils.svn.sourceforge.net/openutils/?rev=646&view=rev Author: fcarone Date: 2008-02-19 10:29:31 -0800 (Tue, 19 Feb 2008) Log Message: ----------- Remove empty test Removed Paths: ------------- trunk/openutils-hibernate-security/src/test/java/it/openutils/hibernate/security/services/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |