KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > base > security > SecurityRolesBase


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SecurityRolesBase.java 864 2006-07-13 11:31:08Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.base.security;
26
27 import java.security.Principal JavaDoc;
28
29 import javax.annotation.Resource;
30 import javax.annotation.security.DeclareRoles;
31 import javax.annotation.security.DenyAll;
32 import javax.annotation.security.PermitAll;
33 import javax.annotation.security.RolesAllowed;
34 import javax.ejb.EJB JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36
37 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.security.ItfEJBContextMethods;
38
39 /**
40  * Containes different types of polices. In this class, the methods do nothing,
41  * but has different types of polices.
42  * @author Gisele Pinheiro Souza
43  * @author Eduardo Studzinski Estima de Castro
44  */

45 @DeclareRoles({"mainrole", "secondaryrole"})
46 public class SecurityRolesBase implements ItfSecurityRoles {
47
48     /**
49      * The context used to test the security methods in the EJBContext
50      * interface.
51      */

52     @Resource
53     private SessionContext JavaDoc sessionContext;
54
55     /**
56      * Bean used to test the getCallerPrincipal.
57      */

58     @EJB JavaDoc
59     private ItfEJBContextMethods bean;
60
61     /**
62      * Method with the police permiteAll.
63      */

64     @PermitAll
65     public void permitAllAttribute() {
66
67     }
68
69     /**
70      * Method with the polcie denyAll.
71      */

72     @DenyAll
73     public void denyAllAttribute() {
74
75     }
76
77     /**
78      * Method that can be accessed only by the mainrole and the secondaryrole.
79      */

80     @RolesAllowed(value = {"mainrole", "secondaryrole"})
81     public void permitTwoRoles() {
82
83     }
84
85     /**
86      * Method that can be accessed only by the mainrole.
87      */

88     @RolesAllowed(value = {"mainrole"})
89     public void permitOneRole() {
90
91     }
92
93     /**
94      * Calls a method in other bean that returns the callerPrincipal. It
95      * compares its caller with the caller returned. The caller for the two
96      * methods must be the same.
97      * @return true if the bean caller and the bean callee are called by the
98      * same role, false otherwise.
99      */

100     @PermitAll
101     public boolean testCallerPrincipal() {
102         Principal JavaDoc principalCaller = sessionContext.getCallerPrincipal();
103         return principalCaller.equals(bean.getCallerPrincipal());
104     }
105
106     /**
107      * Returns the bean caller principal.
108      * @return the caller principal.
109      */

110     public Principal JavaDoc getCallerPrincipal() {
111         return sessionContext.getCallerPrincipal();
112     }
113
114     /**
115      * Compares the role in the parameter with the caller in role.
116      * @param role the role name.
117      * @return true if the caller has the role in the parameter, false
118      * otherwise.
119      */

120     public boolean isCallerinRole(final String JavaDoc role) {
121         return sessionContext.isCallerInRole(role);
122     }
123
124 }
125
Popular Tags