Thursday, October 31, 2013

Save Logged in User details in ADF Session Scope

In real project implementations, there is often a need to save user data in session so that you can access. In this post we will be looking at how to save this user information in session when the user logs in into the application. Thanks to my colleague and friend Mohan with whom I discussed the same.

Lets get started.

Created a simple ADF Web Application in developer.  We will be using HR Schema and Employee and Department table.



Created entity objects for Employee and Department and their up datable view objects.
Added a View Critera in EmployeeVO. This has employeeId equal to a bind variable.





Next Created EmployeeVOImpl.java class from java tab in EmployeeVO and added a method getUserDetails() in it. This method will server as model part which will get executed once when the employee logs in and it will fetch the employee details based on employeeId. It expects one paramter employeeId of type integer. We will call this method from view layer. Below is the code in EmployeeVoImpl



This basically apply view criteria to EmployeeVO and fetches all the attributes of the employee based on employeeId and return a Map . Also it saves userName in the session scope as this will be used very often.

Now expose this method to the client interface so that it is available in EmployeeVO data controls.

We are done with our mode part. Now we just need to call this with logged in employeeId.

So lets start with View Layer.

First we need to create a page which employee will see upon login into the application. So lets create a home.jspx page. This page as simple table in it. EmployeeVO is dragged as readonly table.

Now we need to add getUserDetails() method in the bindings of this page. Lets do it.



Now we need a place which will call this method with employeeId. Lets create a new bean. We will write logic to call getUserDetails() method with employeeId in this bean. We will write this logic in constructor of this bean. Below is the code.



In this logic, we just retrieved the Map returned by getUserDetails() method and add it to the session scope.
We register this bean in unbounded task flow adfc-config.xml in session scope.



Ok so we are all set - configured our bean in session scope and it has logic in its constructor to call the method which returns the map of things we need to set in session scope.

Now the Big question is when this constructor will get executed. Hmm.... for this we need to bind any UI component of the page with this bean. This is because, when we will bind any component, the bean will be loaded when the page is loaded.

I will bind the the table component with a property in the bean.



Now let do something to test UserName on the page. Added output text with value user name from session scope as below .



And one important thing which we forgot. Enable ADF Security and give grants to home.jspx page in jazn-data.xml to aunthicated-user role. Also add a user in security-relams in WLS console with name 101 which is employeeId.

Now lets run home.jspx and see. Login page comes up. Enter user as Employee Id 101 and password.





So here is it. It shows employee name of logged in employee. Similarly we can pull our any information about the employee form the Map saved in the session scope.


Happy Learning !!!


14 comments:

  1. One very important question about ADF Application users . After deployment how a " Admin of our customer " will create new role or users. Because adf allow us to make role in design mode. How we can dedicate a developer to make all users at run time . Please help. ndcshahid@gmail.com

    ReplyDelete
  2. This will be done in Weblogic admin console and security relams.

    There will be all application roles and users there. Or you can add new user and associate it with application roles.

    Please feel free to email me if you have any specific questions.

    Thanks
    Rohan

    ReplyDelete
  3. Hi Rohan
    How can I get only the record of the logged user ? In other term each employee can show only his record

    ReplyDelete
  4. Hi Elie,

    This can be done if you create your employeeVO with bindVariable/ViewCriteria as employeeName/Id.

    Get the employeeName/Id from the session and reexecute the VO with employeeName/ID as value to bindVariable. This will show only logged in user record.

    Hope this answers :)

    ReplyDelete
  5. Done, Thank you Rohan very much :)

    ReplyDelete
  6. Hi Rohan,
    I am a beginner in ADF and I am stuck with adf login using datas in my database with adf security. Could you help me by publishing a post on how to complete the same.

    Greeshma

    ReplyDelete
  7. Hi. I have been trying to maintain a single session for user in adf. What I have done is I have created a login page and now what I want is that if the user is already logged in once and tries to login again from another tab or browser then he should be prompted that You are already logged in or Please log out before login. Besides if the user still logs in then the session of previously opened login page should be terminated and the user should be allowed to login. I have posted the query on oracle forums and they have mentioned something about Oracle Access Manager, but I am unable to get it. Could you please help me with the same. A code/blog or video would be much appreciated.
    Thank you in advance.

    ReplyDelete
  8. Are you looking to earn cash from your websites/blogs by using popunder ads?
    In case you do, have you tried using Propeller Ads?

    ReplyDelete
  9. Hi Rohan,
    how to display user description after login instead of user name.

    ReplyDelete
  10. hi i have httpsession how i can get current user from session ?
    public void afterPhase(PhaseEvent phaseEvent) {
    FacesContext fc = phaseEvent.getFacesContext();
    HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
    UserData u = (UserData)session.getAttribute("user");
    boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true :false;
    if (!loginPage && u == null){
    NavigationHandler nh = fc.getApplication().getNavigationHandler();
    nh.handleNavigation(fc, null, "logout");
    }
    }

    ReplyDelete
  11. Hadoop Training Institute in Noida, The Hadoop certification has considerable advantages in addition to the great demand for trained professionals. A certified Hadoop professional gets the advantage in terms of payment package compared to other professionals working in the field without certification. When it comes to IJP, Hadoop certification is compatible to advance the ladder and accelerate your career. big data hadoop training in Noida, big data hadoop Training institutes in Noida, sector 2, oracle big data training,Oracle Training in Noida, big data boot camp, nosql database training, big data appliance training, exalytics training, exadata course, big data introduction, oracle course, oracle training, big data training in Noida, ibm big data hadoop training institutes noida
    Hadoop Training Institute in Noida

    CONTAC CIITN:

    B-12, Sector - 2, Noida, U.P
    State - Uttar Pradesh U.P
    Country - India
    Pin Code - 201301

    Phone - +917290926565
    Mon - Sun: 10am - 6pm

    ReplyDelete
  12. Nice post ! Thanks for sharing valuable information with us. Keep sharing..
    Big data hadoop online Training

    ReplyDelete
  13. I am very grateful for sharing such information. Call girls

    ReplyDelete