Sunday, 6 September 2009

Perform APEX administration tasks straight from PL/SQL

Recently I had to find a way to create new APEX users without having an admin manually creating them or having to create some sort of request page.
Now the easy way was to let the users do that themselves... Small hick-up though, you need to be an APEX Workspace administrator to create new end users. Meaning that 'normal' users had to be admin before being allowed to create new users.

Now in order to bypass this I just created a small batch that checks a simple table every now and then to see if new usernames where requested. A special administrator then automatically creates this user.

However in order to perform this task, I had to create a piece of PL/SQL code that would be able to run, connect to APEX without being integrated in a valid APEX session (from browser).  I used (a more complex version of) the following code to set the correct workspace, log in as the JOB_MANAGER APEX ADMIN and create the new user.

declare
  l_workspace VARCHAR2 := 'WORKSPACE';
  l_wks_id NUMBER;
begin
  -- find workspace security id
  l_wks_id := apex_util.find_security_group_id (p_workspace => l_workspace);
  dbms_output.put_line('Workspace id : ' || l_workspace);
  -- set workspace security id
  wwv_flow_api.set_security_group_id (l_wks_id);
  -- login user
  apex_custom_auth.login (p_uname       => 'JOB_MANAGER',
                          p_password    => 'welcome1',
                          p_session_id  => V('APP_SESSION'));
  -- create user
  apex_util.create_user (p_user_name       => 'username',
                         p_first_name      => 'first_name',
                         p_last_name       => 'last_name',
                         p_description     => 'description',
                         p_web_password    => 'welcome1',
                         p_email_address   => 'first.last@mail.com');
end;
/


A very short post but hope you guys find it useful.

No comments:

Post a Comment