openejb-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thiago Veronezi <thi...@veronezi.org>
Subject Re: JUnit: Sessioncontext.isCallerInRole allways returns false
Date Thu, 03 Feb 2011 11:15:27 GMT
Hi nick,
This is the default behaviour of EJB.

According to the EJB3.1 specs:

17.3.4.1: Run-as
Establishing a run-as identity for an enterprise bean does not affect the
identities of its callers, which are the identities tested for permission to
access the methods of the enterprise bean. The run-as identity establishes
the identity the enterprise bean will use when it makes calls.

[]s,
Thiago.


On Thu, Feb 3, 2011 at 3:24 AM, ndegraev <nick.degraeve@telenet.be> wrote:

>
> I need to write a session bean that somewhere in the code checks if the
> current user has some role(s).
>
> To unittest my EJB3 I'm trying out OpenEJB. I followed the example about
> testing security but if I test in my code for the role with
> SessionContect.isCallerInRole() it always returns false.
>
> Why doesn't it work?
>
> I've written some code to illustrate.
>
> My local interface:
>
> @Local
> public interface MyBean {
>
>    boolean doSomething();
>
> }
>
> My EJB:
>
> @Stateless
> public class MyBeanImpl implements MyBean {
>
>    @Resource
>    private SessionContext sessionContext;
>
>    @Override
>    public boolean doSomething() {
>        return this.sessionContext.isCallerInRole("role1");
>    }
>
> }
>
> My test:
>
> public class MyBeanTest {
>
>    private Context context;
>
>    @Before
>    public void setUp() throws Exception {
>        final Properties properties = new Properties();
>        properties.put(Context.INITIAL_CONTEXT_FACTORY,
> "org.apache.openejb.client.LocalInitialContextFactory");
>
>        this.context = new InitialContext(properties);
>    }
>
>    @Test
>    public void test1() throws Exception {
>        final Caller roleBean = (Caller)
> this.context.lookup("RoleBeanLocal");
>        roleBean.call(new Callable() {
>
>            @Override
>            public Object call() throws Exception {
>                final MyBean myBean = (MyBean)
> MyBeanTest.this.context.lookup("MyBeanImplLocal");
>                Assert.assertTrue(myBean.doSomething()); // FAILURE
>                return null;
>            }
>        });
>    }
>
>    @Test
>    public void test2() throws Exception {
>        final Caller role2Bean = (Caller)
> this.context.lookup("Role2BeanLocal");
>        role2Bean.call(new Callable() {
>
>            @Override
>            public Object call() throws Exception {
>                final MyBean myBean = (MyBean)
> MyBeanTest.this.context.lookup("MyBeanImplLocal");
>                Assert.assertFalse(myBean.doSomething());
>                return null;
>            }
>        });
>    }
>
>    public static interface Caller {
>
>        <V> V call(Callable<V> callable) throws Exception;
>
>    }
>
>    @Stateless
>    @RunAs("role1")
>    public static class RoleBean implements Caller {
>
>        @Override
>        public <V> V call(final Callable<V> callable) throws Exception {
>            return callable.call();
>        }
>
>    }
>
>    @Stateless
>    @RunAs("role2")
>    public static class Role2Bean implements Caller {
>
>        @Override
>        public <V> V call(final Callable<V> callable) throws Exception {
>            return callable.call();
>        }
>
>    }
> }
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/JUnit-Sessioncontext-isCallerInRole-allways-returns-false-tp3257408p3257408.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message