Search code examples
oracle-databaseoracle-apexoracle-apex-5

Assign first page in Oracle APEX 5 based on login


I'm currently building a recruiting application to replace one currently in use.

I want to be able to set the first page a user sees based on login. I'm currently using Application Express Authentication, but I also have a logins table that contains office, user name and user type, so when the user name in :APP_USER. matches the user name in the login table, it will pull the user type.

So if a recruiter logs in I want them to go to page 1, which is a recruiting dashboard, but if a salesmen logs in I want them to go to page 20, which is a sales dashboard.

How can I accomplish this?

Thanks in advance!


Solution

  • Personally, I don't like altering the login mechanism. I certainly don't like performing code based on the username before the user has actually authenticated. The example by @trent isn't quite that yet since it has no actual selects, but I've seen people performing selects based on the username in order to retrieve some defaults or settings, only to be able to adjust the p_app_page parameter.

    Those parameters will -not- always redirect to the page's you want unless you have deep linking disabled. With deep linking enabled the user will always go to his requested page, not the one provided in p_app_page. It's up to you to determine how the application should behave. Respect deep links or always redirect to a certain page?

    Personally I prefer putting the redirect in the authentication scheme's post-authentication. In my opinion getting or setting user settings has no place on the login-page processes nor in the authentication code (it's meant to authenticate, not manipulate).

    In the post-auth code I'd put the select on type and alter FSP_AFTER_LOGIN_URL. Note though that this will completely disregard deep links. You can leave the ELSE out, but that'll only retain deep links for non-sales and recruiters, as they will always land on their dashboard page.

    DECLARE
      l_type persons.person_type%TYPE;
      l_url VARCHAR2(4000);
    BEGIN
      SELECT person_type
        INTO l_type
        FROM persons
       WHERE username = :P101_USERNAME;
    
      l_url := 
      CASE l_type
      WHEN 'SALES' THEN
        'f?p=' || V('APP_ALIAS') || ':SALES_DASH:' || V('APP_SESSION')
      WHEN 'RECRUITER' THEN
        'f?p=' || V('APP_ALIAS') || ':RECRUITER_DASH:' || V('APP_SESSION')
      ELSE  -- go to home page
        'f?p=' || V('APP_ALIAS') || ':HOME:' || V('APP_SESSION')
      END;
    
      APEX_UTIL.SET_SESSION_STATE('FSP_AFTER_LOGIN_URL', l_url); 
    END;