I'd like to send an e-mail through APEX whenever a request is denied. I want to send it to the user(s) who made the request through the system. This user was authenticated via LDAP (Active Directory) in another application when he sent that request. Therefore, his e-mail address can be found within his AD profile he authenticates with. All the requests are listed in a report in another application (which uses the same LDAP Authentication Scheme) where they can be reviewed, then either denied or accepted. When the deny button is clicked, it updates the request's status in the database and sends an e-mail to an hardcoded e-mail address. I'd like to send it to the user who made the request.
Is it possible to use the current LDAP session (since I'm logged in the app via LDAP, I take for granted that there must be a valid LDAP session in use), or do I need to connect again to the server? If it's the latter, then how do I do that? I can't just hardcode my own credentials in the function, that would be nuts.
I thought I could use a script like this and call it in a page process, but I know I'm missing something.
create or replace function Get_Mail(p_user in varchar2)
return varchar2
is
l_attrs dbms_ldap.string_collection;
l_message dbms_ldap.MESSAGE;
l_entry dbms_ldap.MESSAGE;
l_vals dbms_ldap.string_collection;
l_user varchar2(256);
l_user2 varchar2(256);
l_mail varchar2(256);
l_ldap_server varchar2(256) := '****';
l_domain varchar2(256) := '****';
l_ldap_port number := 389;
l_retval pls_integer;
l_session dbms_ldap.session;
l_username varchar2(256) := NULL;
l_password varchar2(256) := NULL;
begin
dbms_ldap.use_exception := TRUE;
l_user2 := p_user||'@'||l_domain;
l_user := l_username||'@'||l_domain;
l_session := dbms_ldap.init (l_ldap_server, l_ldap_port);
l_retval := dbms_ldap.simple_bind_s (l_session, l_user, l_password);
l_attrs(1) := 'email';
l_retval := dbms_ldap.search_s (ld => l_session, base => '****', scope => dbms_ldap.scope_subtree,
filter =>'&(userPrincipalName='|| l_user2 || ')(objectClass=user)', attrs => l_attrs, attronly => 0, res => l_message);
l_entry := dbms_ldap.first_entry (ld => l_session, msg => l_message);
l_vals := dbms_ldap.get_values (ld => l_session, ldapentry => l_entry, attr => l_attrs(1));
l_mail := l_vals(1);
return l_mail;
exception
when others then
begin
dbms_output.put_line (' Erreur #' || TO_CHAR (SQLCODE));
dbms_output.put_line (' Message: ' || SQLERRM);
l_mail := NULL;
return l_mail;
end;
end Get_Mail;
Any ideas?
Best regards,
Mathieu