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;