Thursday, December 1, 2011

Extending Security layer in ADF applications.


By default the security is not enabled in the ADF applications, but once you enable the security the jazn-data.xml gets created in the application, where you can add user , roles and also grant permission etc.  ADF security can be extended and and accessed from the custom code and creation of user roles etc can be done through code as well. Below given code snippet depicts the same.

ADFContext adfC  = ADFContext.getCurrent();

SecurityContext sec = adfC.getSecurityContext();
boolean userAuth = sec.isAuthenticated();
if(userAuth) {
 //perform some logic
    }
    String userName = sec.getUserName();
    String[] roles = sec.getUserRoles();
    for(String r: roles) {
      System.out.println("Roles: " + r);
    }

    UserManager usermanage = new UserManager();
    User user = new User();
    user.setUsername("user2");
    Credential cr = new Credential();
    cr.setProperty(cr.PASSWORD_ATTRIBUTE, "abhinav1");
    usermanage.addUser(user, cr);
    usermanage.createUser();  
    RoleManager rolem = new RoleManager();
    MyPrincipal mypr = new MyPrincipal(user.getUsername());
    Role role;
    role = new Role();
    role.setName("GoogleAdmin");
    role.addMember(mypr);
    rolem.addRole(role);
    rolem.createRole();

Also, you would need to import following classes:


import oracle.adf.share.ADFContext;
import oracle.adf.share.security.PermissionEvaluator;
import oracle.adf.share.security.SecurityContext;
import oracle.adf.share.security.SecurityEnv;
import oracle.adf.share.security.credentialstore.Credential;
import oracle.adf.share.security.identitymanagement.Role;
import oracle.adf.share.security.identitymanagement.RoleManager;
import oracle.adf.share.security.identitymanagement.User;
import oracle.adf.share.security.identitymanagement.UserManager;
import oracle.adf.share.security.identitymanagement.UserProfile;

Tuesday, November 29, 2011

Fixing the focus on the field, post validation on tab out event.

Recently I got a query on how to keep the focus on a text field after the field has been validated on tab out event occurrence. There are two ways to achieve the same as described below. The second one has been preferred by few people.  But I would anyways describe both of them.

1.Declare the validator method in the managed bean (autosubmit set to true, immediate set to true) also apply client listener on the input text. Replicate the validation done in the managed bean in the javascript method and use AdfFocusUtils.focusElementDelayed()  method to lock the focus.

2.In the value change listener if you are not throwing a ValidationException then you might also try calling the javascript method (having use AdfFocusUtils.focusElementDelayed()  method to lock the focus) using the ExtendedRenderKitService to achieve it.

Event handling for single record in ADF Table.

Scenario: The scenario is, that in the ADF table, when there is a single record, then it comes auto selected and no click event happens there after on the table row.

Solution approach: To use adf component to specifically handle the row selection event over the auto selected row.

Solution:
Add a client listener to the adf table tag by dragging and dropping the clientListener component from the Operations in the Component Palette. Add the method name (of the javascript method which will call the popup in turn). Also give the event name as click. After this add the below code snippet to call the popup defined on the jspx page.

<af:resource type="javascript">
        function showPopup()
       {
          var popupId = "p1";
          var popup = AdfPage.PAGE.findComponentByAbsoluteId(popupId);
          popup.show();
        }
</af:resource>

Also, this solution could be used to call the server listner, by calling the javascript method using the clientListener as described above. then use the AdfCustomEvent.queue() method to call the serverListener (again a component from the Operations in the Component Palette), which in turn declares the managed bean method that needs to be called.