03.05.2015 Views

IBM WebSphere V5.0 Security - CGISecurity

IBM WebSphere V5.0 Security - CGISecurity

IBM WebSphere V5.0 Security - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

►<br />

Boolean isCallerInRole(String roleName)<br />

The isCallerInRole method allows the developer to make additional checks<br />

on the authorization rights of a user which are not possible, or more difficult,<br />

to perform through the deployment descriptor of the EJB.<br />

EJBContext ejbContext;<br />

...<br />

if (ejbContext.isCallerInRole(““))<br />

// Perform some fuction<br />

else<br />

// Throw a security exception<br />

The isCallerInRole(String role) method returns true if the user is in the<br />

specified role, and false if it is not. The role name specified in the method is<br />

really a security role reference, not a role. If the security role reference is not<br />

defined for the EJB, the method will return null.<br />

Sample usage of security methods<br />

The following example is a modified code snippet from the ITSOBank sample<br />

application. You can find similar code in the TransferBean.java in the<br />

transferBranch2Customer() method. For more details, check the comments in<br />

the source below, or in the original sample application.<br />

Example 8-1 Sample code using the EJB security methods<br />

// getting the environment variables for restricted role<br />

// and for maximum transferable amount<br />

restrictedRole=(String)environment.lookup("RestrictedRole");<br />

maxEJBTransferAmount=(Integer)environment.lookup("MaxEJBTransferAmount");<br />

// checking if the user is restricted to a certain amount of transfer<br />

if(mySessionCtx.isCallerInRole(restrictedRole) &&<br />

transferAmount>maxEJBTransferAmount.intValue()) {<br />

// the user cannot transfer the requested amount<br />

return false;<br />

}<br />

// get the caller principal, then the user name<br />

java.security.Principal callerPrincipal=mySessionCtx.getCallerPrincipal();<br />

String callerName =callerPrincipal.getName();<br />

// print out the user information about the EJB method invocation<br />

System.out.println("... method was invoked on the Transfer EJB by:<br />

"+callerName);<br />

With the security methods, the EJB will not let the user in a restricted role to<br />

submit a transfer greater than the maximum transferable amount.<br />

Chapter 8. Programmatic security 181

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!