[APIM] Send emails to users upon self-sign up aproval
When using the self sign up of WSO2 API manager, the user needs to wait for the admin to approve their signup request. However when the admin approves/rejects the request, users may like to receive an email with the status of the approval. This can be achieved by the extension points provided by APIM. In this article we will consider how this can be achieved,
First step is to set up the user sign up work flow as described in the documentation[1]. After following the documentation, you should be able to self sign up and attach a workflow to the to the self sign up process. After self signup, you can login to the admin portal of APIM and approve or reject a request to sign up.
This portal can be address through https://HOSTNAME:PORT/admin for example https://10.100.9.174:9443/admin
Next we need to extend from UserSignUpWSWorkflowExecutor of APIM and write your own logic to send out an email. In this case, we have overriden the complete method of UserSignUpWSWorkflowExecutor. In the default implementation, when admin approves or rejects the request and the comple method is called to either delete the user or grant him permission to login. Since this logic is still needed, super.comple() method is first called. Afterwards, the using javax.mail, the mail is constructed as bellow,
Next step is to find the email of the user that this message needs to be delivered to and for this, it is possible use UserStoreManager class as bellow,
You can find the complete code, in github[2] and clone this and compile the code. After compiling, copy the file/target/custom-usersignup-workflow-executor-1.0.0.jar to /repository/components/lib directory
Startup the APIM server and go to the management console(https://:9443/carbon)
Click on browse under resource and go to the following resource/_system/governance/apimgt/applicationdata/workflow-extensions.xml
Add the following section[3] to the workflow-extensions.xml
Comment out org.wso2.carbon.apimgt.impl.workflow.UserSignUpSimpleWorkflowExecutor tag in workflow-extensions.xml.
Now when you sign up and approve, an email should come to the email address of the newly signed up user's provided email.
[1]. https://docs.wso2.com/display/AM210/Adding+a+User+Signup+Workflow
[2]. https://github.com/inoshperera/workflow-executor
[3]. https://github.com/inoshperera/workflow-executor/blob/master/wf.xml#L36-L43
First step is to set up the user sign up work flow as described in the documentation[1]. After following the documentation, you should be able to self sign up and attach a workflow to the to the self sign up process. After self signup, you can login to the admin portal of APIM and approve or reject a request to sign up.
This portal can be address through https://HOSTNAME
Next we need to extend from UserSignUpWSWorkflowExecutor of APIM and write your own logic to send out an email. In this case, we have overriden the complete method of UserSignUpWSWorkflowExecutor. In the default implementation, when admin approves or rejects the request and the comple method is called to either delete the user or grant him permission to login. Since this logic is still needed, super.comple() method is first called. Afterwards, the using javax.mail, the mail is constructed as bellow,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException { | |
super.complete(workflowDTO); | |
Properties props = new Properties(); | |
props.put(MAIL_SMTP_AUTH, "true"); | |
props.put(MAIL_SMTP_STAR_TTLS_ENABLE, "true"); | |
props.put(MAIL_SMTP_HOST, "smtp.gmail.com"); | |
props.put(MAIL_SMTP_PORT, "587"); | |
Session session = Session.getInstance(props, new javax.mail.Authenticator() { | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(emailAddress, emailPassword); | |
} | |
}); | |
try { | |
Message message = new MimeMessage(session); | |
message.setFrom(new InternetAddress(emailAddress)); | |
String email = getUserEmail(workflowDTO); | |
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); | |
message.setSubject("My Company User Sign-up Service"); | |
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) { | |
message.setText("Your request to sign-up with <Company name> has been approved by the admin. Your " + | |
"username is "+ MultitenantUtils.getTenantAwareUsername(workflowDTO.getWorkflowReference()) + | |
". You can now login with your credentials provided to the API store."); | |
} else { | |
message.setText("Your request to sign-up with <Company name> has been declined by the admin. " + | |
"Please contact the admin for more details."); | |
} | |
Transport.send(message); | |
} catch (AddressException e) { | |
String msg = "Error while converting the email address."; | |
log.error(msg); | |
throw new WorkflowException(e.getMessage()); | |
} catch (MessagingException e) { | |
String msg = "Error while sending the sign-up update mail to user: " + workflowDTO.getWorkflowReference(); | |
log.error(msg); | |
throw new WorkflowException(e.getMessage()); | |
} | |
return new GeneralWorkflowResponse(); | |
} |
Next step is to find the email of the user that this message needs to be delivered to and for this, it is possible use UserStoreManager class as bellow,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String getUserEmail(WorkflowDTO workflowDTO) throws WorkflowException { | |
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); | |
ctx.setUsername(workflowDTO.getWorkflowReference()); | |
RealmService realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); | |
if (realmService == null) { | |
String msg = "RealmService is not initialized"; | |
log.error(msg); | |
throw new WorkflowException(msg); | |
} | |
String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(workflowDTO.getWorkflowReference()); | |
try { | |
UserRealm userRealm = realmService.getTenantUserRealm(workflowDTO.getTenantId()); | |
UserStoreManager userStoreManager = userRealm.getUserStoreManager(); | |
return userStoreManager.getUserClaimValue(tenantAwareUserName, EMAIL_CLAIM, null); | |
} catch (UserStoreException e) { | |
String msg = "Error while getting email address of user: " + workflowDTO.getWorkflowReference(); | |
log.error(msg); | |
throw new WorkflowException(e.getMessage()); | |
} | |
} |
You can find the complete code, in github[2] and clone this and compile the code. After compiling, copy the file
Startup the APIM server and go to the management console(https://
Click on browse under resource and go to the following resource/_system/governance/apimgt/applicationdata/workflow-extensions.xml
Add the following section[3] to the workflow-extensions.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserSignUp executor="com.wso2.custom.workflow.CustomUserSignUpWSWorkflowExecutor"> | |
<property name="serviceEndpoint">http://localhost:9765/services/UserSignupProcess/</property> | |
<property name="username">admin</property> | |
<property name="password">admin</property> | |
<property name="callbackURL">https://localhost:8243/services/WorkflowCallbackService</property> | |
<property name="emailAddress">senders_email_address_here</property> | |
<property name="emailPassword">senders_email_password_here</property> | |
</UserSignUp> |
Comment out org.wso2.carbon.apimgt.impl.workflow.UserSignUpSimpleWorkflowExecutor tag in workflow-extensions.xml.
Now when you sign up and approve, an email should come to the email address of the newly signed up user's provided email.
[1]. https://docs.wso2.com/display/AM210/Adding+a+User+Signup+Workflow
[2]. https://github.com/inoshperera/workflow-executor
[3]. https://github.com/inoshperera/workflow-executor/blob/master/wf.xml#L36-L43
Interesting Article. Hoping that you will continue posting an article having a useful information. IOS Development
ReplyDelete