Commit da9f182739a4f7e50eee7f594b9b782dda23d010

Authored by Dancovich
2 parents 77a51743 1e5f442a
Exists in master

Merge remote-tracking branch 'origin/2.4.0' into 2.4.0

impl/core/src/main/java/br/gov/frameworkdemoiselle/security/RequiredPermissionInterceptor.java
... ... @@ -124,7 +124,12 @@ public class RequiredPermissionInterceptor implements Serializable {
124 124 * annotation or the class name itself
125 125 */
126 126 private String getResource(InvocationContext ic) {
127   - RequiredPermission requiredPermission = ic.getMethod().getAnnotation(RequiredPermission.class);
  127 + RequiredPermission requiredPermission;
  128 + requiredPermission = ic.getMethod().getAnnotation(RequiredPermission.class);
  129 +
  130 + if(requiredPermission == null){
  131 + requiredPermission = ic.getTarget().getClass().getAnnotation(RequiredPermission.class);
  132 + }
128 133  
129 134 if (requiredPermission == null || Strings.isEmpty(requiredPermission.resource())) {
130 135 if (ic.getTarget().getClass().getAnnotation(Name.class) == null) {
... ... @@ -147,8 +152,13 @@ public class RequiredPermissionInterceptor implements Serializable {
147 152 * annotation or the method's name itself
148 153 */
149 154 private String getOperation(InvocationContext ic) {
150   - RequiredPermission requiredPermission = ic.getMethod().getAnnotation(RequiredPermission.class);
151   -
  155 + RequiredPermission requiredPermission;
  156 + requiredPermission = ic.getMethod().getAnnotation(RequiredPermission.class);
  157 +
  158 + if(requiredPermission == null){
  159 + requiredPermission = ic.getTarget().getClass().getAnnotation(RequiredPermission.class);
  160 + }
  161 +
152 162 if (requiredPermission == null || Strings.isEmpty(requiredPermission.operation())) {
153 163 if (ic.getMethod().getAnnotation(Name.class) == null) {
154 164 return ic.getMethod().getName();
... ...
impl/core/src/test/java/security/authorization/disable/DisabledAuthorizationTest.java
... ... @@ -36,9 +36,7 @@
36 36 */
37 37 package security.authorization.disable;
38 38  
39   -import static org.junit.Assert.assertEquals;
40 39 import static org.junit.Assert.assertNull;
41   -import static org.junit.Assert.assertTrue;
42 40  
43 41 import javax.enterprise.context.RequestScoped;
44 42 import javax.enterprise.event.Observes;
... ... @@ -52,7 +50,6 @@ import org.jboss.shrinkwrap.api.spec.JavaArchive;
52 50 import org.junit.Test;
53 51 import org.junit.runner.RunWith;
54 52  
55   -import security.athentication.custom.CustomAuthenticator;
56 53 import security.authorization.custom.CustomAuthorizer;
57 54 import test.Tests;
58 55 import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful;
... ...
impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedMethods.java 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.loggedin;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.LoggedIn;
  40 +
  41 +public class DummyProtectedMethods {
  42 +
  43 + private String dummyAttrib = "Default";
  44 +
  45 + public String getDummyAttrib() {
  46 + return dummyAttrib;
  47 + }
  48 +
  49 + @LoggedIn
  50 + public void setDummyAttrib(String dummyAttrib) {
  51 + this.dummyAttrib = dummyAttrib;
  52 + }
  53 +
  54 +}
... ...
impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java
... ... @@ -37,9 +37,6 @@
37 37 package security.interceptor.loggedin;
38 38  
39 39 import static junit.framework.Assert.assertEquals;
40   -import static org.junit.Assert.assertEquals;
41   -import static org.junit.Assert.assertNotNull;
42   -import static org.junit.Assert.assertTrue;
43 40  
44 41 import javax.inject.Inject;
45 42  
... ... @@ -51,17 +48,14 @@ import org.junit.Before;
51 48 import org.junit.Test;
52 49 import org.junit.runner.RunWith;
53 50  
54   -import br.gov.frameworkdemoiselle.context.RequestContext;
  51 +import test.Tests;
55 52 import br.gov.frameworkdemoiselle.context.SessionContext;
56   -import br.gov.frameworkdemoiselle.security.AuthenticationException;
57 53 import br.gov.frameworkdemoiselle.security.NotLoggedInException;
58 54 import br.gov.frameworkdemoiselle.security.SecurityContext;
59 55 import br.gov.frameworkdemoiselle.util.Beans;
60 56 import br.gov.frameworkdemoiselle.util.NameQualifier;
61 57 import br.gov.frameworkdemoiselle.util.ResourceBundle;
62 58  
63   -import test.Tests;
64   -
65 59 @RunWith(Arquillian.class)
66 60 public class LoggedInInterceptorTest {
67 61  
... ... @@ -69,22 +63,26 @@ public class LoggedInInterceptorTest {
69 63 private DummyProtectedClass protectedClass;
70 64  
71 65 @Inject
  66 + private DummyProtectedMethods protectedMethods;
  67 +
  68 + @Inject
72 69 private SecurityContext context;
73 70  
74 71 @Deployment
75 72 public static JavaArchive createDeployment() {
76 73 JavaArchive deployment = Tests.createDeployment();
77 74 deployment.addClass(DummyProtectedClass.class);
  75 + deployment.addClass(DummyProtectedMethods.class);
78 76 deployment.addClass(CustomAuthenticator.class);
79 77 return deployment;
80 78 }
81 79  
82 80 @Before
83   - public void activeContext(){
  81 + public void activeContext() {
84 82 SessionContext ctx = Beans.getReference(SessionContext.class);
85 83 ctx.activate();
86 84 }
87   -
  85 +
88 86 @Test
89 87 public void callProtectedClassAttribNotLogged() {
90 88 try {
... ... @@ -98,11 +96,34 @@ public class LoggedInInterceptorTest {
98 96 @Test
99 97 public void callProtectedClassAttribLogged() {
100 98 context.login();
101   - protectedClass.getDummyAttrib();
  99 + protectedClass.setDummyAttrib("Test");
  100 + assertEquals("Test", protectedClass.getDummyAttrib());
  101 + }
  102 +
  103 + @Test
  104 + public void callUnprotectedMethod() {
  105 + assertEquals("Default", protectedMethods.getDummyAttrib());
  106 + }
  107 +
  108 + @Test
  109 + public void callProtectedMethodUnLogged() {
  110 + try {
  111 + protectedMethods.setDummyAttrib("Value Changed");
  112 + } catch (NotLoggedInException cause) {
  113 + assertEquals(Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-core-bundle"))
  114 + .getString("user-not-authenticated"), cause.getMessage());
  115 + }
  116 + }
  117 +
  118 + @Test
  119 + public void callProtectedMethodLogged() {
  120 + context.login();
  121 + protectedMethods.setDummyAttrib("Value Changed");
  122 + assertEquals("Value Changed", protectedMethods.getDummyAttrib());
102 123 }
103   -
  124 +
104 125 @After
105   - public void deactiveContext(){
  126 + public void deactiveContext() {
106 127 SessionContext ctx = Beans.getReference(SessionContext.class);
107 128 ctx.deactivate();
108 129 }
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/CustomAuthorizer.java 0 → 100644
... ... @@ -0,0 +1,55 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.Authorizer;
  40 +
  41 +public class CustomAuthorizer implements Authorizer {
  42 +
  43 + private static final long serialVersionUID = 1L;
  44 +
  45 + @Override
  46 + public boolean hasRole(String role) {
  47 + return "role".equals(role);
  48 + }
  49 +
  50 + @Override
  51 + public boolean hasPermission(String resource, String operation) {
  52 + return "resource".equals(resource) && "operation".equals(operation);
  53 + }
  54 +
  55 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/CustomAuthorizer2.java 0 → 100644
... ... @@ -0,0 +1,57 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.Authorizer;
  40 +
  41 +public class CustomAuthorizer2 implements Authorizer {
  42 +
  43 + private static final long serialVersionUID = 1L;
  44 +
  45 + @Override
  46 + public boolean hasRole(String role) {
  47 + return "role".equals(role);
  48 + }
  49 +
  50 + @Override
  51 + public boolean hasPermission(String resource, String operation) {
  52 + System.out.println("###" + resource + " " + operation + "###");
  53 + return "DummyProtectedClassAuthorizedWithoutParams$Proxy$_$$_WeldSubclass".equals(resource)
  54 + && ("setDummyAttrib".equals(operation) || "getDummyAttrib".equals(operation));
  55 + }
  56 +
  57 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/DummyProtectedClassAndMethod.java 0 → 100644
... ... @@ -0,0 +1,59 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.RequiredPermission;
  40 +
  41 +@RequiredPermission(resource = "resource false", operation = "operation false")
  42 +public class DummyProtectedClassAndMethod {
  43 +
  44 + private String dummyAttrib = "Default";
  45 +
  46 + public String getDummyAttrib() {
  47 + return dummyAttrib;
  48 + }
  49 +
  50 + public void setDummyAttribWithClassAuthorization(String dummyAttrib) {
  51 + this.dummyAttrib = dummyAttrib;
  52 + }
  53 +
  54 + @RequiredPermission(resource = "resource", operation = "operation")
  55 + public void setDummyAttribWithAuthorization(String dummyAttrib) {
  56 + this.dummyAttrib = dummyAttrib;
  57 + }
  58 +
  59 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/DummyProtectedClassAuthorized.java 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.RequiredPermission;
  40 +
  41 +@RequiredPermission(resource="resource", operation="operation")
  42 +public class DummyProtectedClassAuthorized {
  43 +
  44 + private String dummyAttrib;
  45 +
  46 + public String getDummyAttrib() {
  47 + return dummyAttrib;
  48 + }
  49 +
  50 + public void setDummyAttrib(String dummyAttrib) {
  51 + this.dummyAttrib = dummyAttrib;
  52 + }
  53 +
  54 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/DummyProtectedClassAuthorizedWithoutParams.java 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.RequiredPermission;
  40 +
  41 +@RequiredPermission
  42 +public class DummyProtectedClassAuthorizedWithoutParams {
  43 +
  44 + private String dummyAttrib;
  45 +
  46 + public String getDummyAttrib() {
  47 + return dummyAttrib;
  48 + }
  49 +
  50 + public void setDummyAttrib(String dummyAttrib) {
  51 + this.dummyAttrib = dummyAttrib;
  52 + }
  53 +
  54 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/DummyProtectedClassUnauthorized.java 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.RequiredPermission;
  40 +
  41 +@RequiredPermission(resource="resource false", operation="operation false")
  42 +public class DummyProtectedClassUnauthorized {
  43 +
  44 + private String dummyAttrib;
  45 +
  46 + public String getDummyAttrib() {
  47 + return dummyAttrib;
  48 + }
  49 +
  50 + public void setDummyAttrib(String dummyAttrib) {
  51 + this.dummyAttrib = dummyAttrib;
  52 + }
  53 +
  54 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/DummyProtectedMethods.java 0 → 100644
... ... @@ -0,0 +1,60 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import br.gov.frameworkdemoiselle.security.RequiredPermission;
  40 +
  41 +public class DummyProtectedMethods {
  42 +
  43 + private String dummyAttrib = "Default";
  44 +
  45 +
  46 + public String getDummyAttrib() {
  47 + return dummyAttrib;
  48 + }
  49 +
  50 + @RequiredPermission(resource="resource", operation="operation")
  51 + public void setDummyAttribAuthorized(String dummyAttrib) {
  52 + this.dummyAttrib = dummyAttrib;
  53 + }
  54 +
  55 + @RequiredPermission(resource="resource false", operation="operation false")
  56 + public void setDummyAttribUnauthorized(String dummyAttrib) {
  57 + this.dummyAttrib = dummyAttrib;
  58 + }
  59 +
  60 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/RequiredPermissionInterceptorTest.java 0 → 100644
... ... @@ -0,0 +1,143 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import static junit.framework.Assert.assertEquals;
  40 +import static org.junit.Assert.fail;
  41 +
  42 +import javax.inject.Inject;
  43 +
  44 +import org.jboss.arquillian.container.test.api.Deployment;
  45 +import org.jboss.arquillian.junit.Arquillian;
  46 +import org.jboss.shrinkwrap.api.spec.JavaArchive;
  47 +import org.junit.After;
  48 +import org.junit.Before;
  49 +import org.junit.Ignore;
  50 +import org.junit.Test;
  51 +import org.junit.runner.RunWith;
  52 +
  53 +import security.interceptor.loggedin.CustomAuthenticator;
  54 +import test.Tests;
  55 +import br.gov.frameworkdemoiselle.context.SessionContext;
  56 +import br.gov.frameworkdemoiselle.security.AuthorizationException;
  57 +import br.gov.frameworkdemoiselle.security.SecurityContext;
  58 +import br.gov.frameworkdemoiselle.util.Beans;
  59 +
  60 +@RunWith(Arquillian.class)
  61 +public class RequiredPermissionInterceptorTest {
  62 +
  63 + @Inject
  64 + private DummyProtectedClassAuthorized protectedClassAuthorized;
  65 +
  66 + @Inject
  67 + private DummyProtectedClassUnauthorized protectedClassUnAuthorized;
  68 +
  69 + @Inject
  70 + private DummyProtectedMethods protectedMethods;
  71 +
  72 + @Inject
  73 + private DummyProtectedClassAndMethod protectedClassAndMethod;
  74 +
  75 + @Inject
  76 + private SecurityContext securityContext;
  77 +
  78 + @Deployment
  79 + public static JavaArchive createDeployment() {
  80 + JavaArchive deployment = Tests.createDeployment();
  81 + deployment.addClass(DummyProtectedClassAuthorized.class);
  82 + deployment.addClass(DummyProtectedClassUnauthorized.class);
  83 + deployment.addClass(DummyProtectedMethods.class);
  84 + deployment.addClass(DummyProtectedClassAndMethod.class);
  85 + deployment.addClass(CustomAuthenticator.class);
  86 + deployment.addClass(CustomAuthorizer.class);
  87 + return deployment;
  88 + }
  89 +
  90 + @Before
  91 + public void activeContext() {
  92 + SessionContext sessionContext = Beans.getReference(SessionContext.class);
  93 + sessionContext.activate();
  94 +
  95 + securityContext.login();
  96 + }
  97 +
  98 + @Test(expected=AuthorizationException.class)
  99 + public void callProtectedClassAttribNotAuthorized() {
  100 + protectedClassUnAuthorized.getDummyAttrib();
  101 + }
  102 +
  103 + @Test
  104 + public void callProtectedClassAttribAuthorized() {
  105 + protectedClassAuthorized.setDummyAttrib("Test");
  106 + assertEquals("Test", protectedClassAuthorized.getDummyAttrib());
  107 + }
  108 +
  109 + @Test(expected=AuthorizationException.class)
  110 + public void callProtectedMethodNotAuthorized(){
  111 + protectedMethods.setDummyAttribUnauthorized("Not Authorized");
  112 + }
  113 +
  114 + @Test
  115 + public void callProtectedMethodAuthorized(){
  116 + protectedMethods.setDummyAttribAuthorized("Authorized");
  117 + assertEquals("Authorized", protectedMethods.getDummyAttrib());
  118 + }
  119 +
  120 + /**
  121 + * This test aim to verify the priority of method authorization over class authorization
  122 + */
  123 + @Test
  124 + public void callNotAnnotatedMethod(){
  125 + try{
  126 + protectedClassAndMethod.setDummyAttribWithClassAuthorization("Class not authorized");
  127 + fail();
  128 + }catch(AuthorizationException cause){
  129 + }
  130 +
  131 + protectedClassAndMethod.setDummyAttribWithAuthorization("Method authorized");
  132 + }
  133 +
  134 +
  135 +
  136 + @After
  137 + public void deactiveContext() {
  138 + securityContext.logout();
  139 +
  140 + SessionContext ctx = Beans.getReference(SessionContext.class);
  141 + ctx.deactivate();
  142 + }
  143 +}
... ...
impl/core/src/test/java/security/interceptor/requiredpermission/RequiredPermissionInterceptorTest2.java 0 → 100644
... ... @@ -0,0 +1,99 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.interceptor.requiredpermission;
  38 +
  39 +import static junit.framework.Assert.assertEquals;
  40 +import static org.junit.Assert.fail;
  41 +
  42 +import javax.inject.Inject;
  43 +
  44 +import org.jboss.arquillian.container.test.api.Deployment;
  45 +import org.jboss.arquillian.junit.Arquillian;
  46 +import org.jboss.shrinkwrap.api.spec.JavaArchive;
  47 +import org.junit.After;
  48 +import org.junit.Before;
  49 +import org.junit.Ignore;
  50 +import org.junit.Test;
  51 +import org.junit.runner.RunWith;
  52 +
  53 +import security.interceptor.loggedin.CustomAuthenticator;
  54 +import test.Tests;
  55 +import br.gov.frameworkdemoiselle.context.SessionContext;
  56 +import br.gov.frameworkdemoiselle.security.AuthorizationException;
  57 +import br.gov.frameworkdemoiselle.security.SecurityContext;
  58 +import br.gov.frameworkdemoiselle.util.Beans;
  59 +
  60 +@RunWith(Arquillian.class)
  61 +public class RequiredPermissionInterceptorTest2 {
  62 +
  63 + @Inject
  64 + private DummyProtectedClassAuthorizedWithoutParams protectedClassAuthorizedWithoutParams;
  65 +
  66 + @Inject
  67 + private SecurityContext securityContext;
  68 +
  69 + @Deployment
  70 + public static JavaArchive createDeployment() {
  71 + JavaArchive deployment = Tests.createDeployment();
  72 + deployment.addClass(DummyProtectedClassAuthorizedWithoutParams.class);
  73 + deployment.addClass(CustomAuthenticator.class);
  74 + deployment.addClass(CustomAuthorizer2.class);
  75 + return deployment;
  76 + }
  77 +
  78 + @Before
  79 + public void activeContext() {
  80 + SessionContext sessionContext = Beans.getReference(SessionContext.class);
  81 + sessionContext.activate();
  82 +
  83 + securityContext.login();
  84 + }
  85 +
  86 + @Test
  87 + public void callProtectedClassAttribNotAuthorized() {
  88 + protectedClassAuthorizedWithoutParams.setDummyAttrib("Test");
  89 + assertEquals("Test", protectedClassAuthorizedWithoutParams.getDummyAttrib());
  90 + }
  91 +
  92 + @After
  93 + public void deactiveContext() {
  94 + securityContext.logout();
  95 +
  96 + SessionContext ctx = Beans.getReference(SessionContext.class);
  97 + ctx.deactivate();
  98 + }
  99 +}
... ...