How to use liferay inbuilt email templates programatically? - liferay

In my liferay application I am adding application users by using Expando programatically and not using liferay default User signup.
Now I want to send credentials to user to his email address using liferay default templates available from Control Panel - Portal Setting - Email Templates.
How can I trigger email using this liferay inbuilt template?
Any hint is appreciated.

Liferay uses template files(.tmpl) files to manage Email templates.
As per the Liferay Source,for User creation,given snippet is used to send out mail to users.
String body = PrefsPropsUtil.getContent(
user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY);
SubscriptionSender subscriptionSender = new SubscriptionSender();
subscriptionSender.setBody(body);
subscriptionSender.setCompanyId(user.getCompanyId());
subscriptionSender.setContextAttributes(
"[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", password,
"[$USER_SCREENNAME$]", user.getScreenName());
subscriptionSender.setFrom(fromAddress, fromName);
subscriptionSender.setHtmlFormat(true);
subscriptionSender.setMailId(
"user", user.getUserId(), System.currentTimeMillis(),
PwdGenerator.getPassword());
subscriptionSender.setServiceContext(serviceContext);
subscriptionSender.setSubject(subject);
subscriptionSender.setUserId(user.getUserId());
subscriptionSender.addRuntimeSubscribers(toAddress, toName);
subscriptionSender.flushNotificationsAsync();
This is part of service impl class UserLocalServiceImpl.
Here "PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY" is the path of the template for body content(default used by liferay).You can populate your custom data in the provided template.
Edit:
After you perform your custom logic,you can directly call
UserLocalServiceUtil.sendPassword(
long companyId, String emailAddress, String fromName,
String fromAddress, String subject, String body,
ServiceContext serviceContext)
method directly from custom class,which will take care of using liferay template as well as password management.

Related

Custom Post Login Action Hook in Liferay 7 using Blade

I am developing a liferay project where there are 2 sites, 1 for Admin and 1 for users. Admin and users are differentiated based on user groups. when an admin logs in he should be redirected to Admin page and when user logs in, they should be redirected to User page. How can we achieve this functionality in Liferay 7 using Blade or Hook plugin? Any help would be appreciated. Thanks!!!
You can find a plugin example here:
https://www.e-systems.tech/blog/-/blogs/liferay-7-how-to-get-a-session-object-after-login-
Basically, if you want a post login hook, this example should be enough.
To use liferay services, in the new style, you will need some references
private Portal portal;
private UserLocalService userLocalService;
#Reference
private void setUserLocalService(UserLocalService service){
this.userLocalService=service;
}
#Reference
private void setPortalUtil(Portal portal){
this.portal=portal;
}
now, to get user information and more data about the portal
final long companyId = portal.getCompanyId(request);
final HttpSession session = request.getSession();
This line should be useful to:
User user = (User)session.getAttribute(WebKeys.USER);
For redirection: use the redirect parameter in the URL.
or, this code would help..
Map params = new HashMap();
params.put("p_l_id", new String[] {"PRI.1.1"});
LastPath lastPath = new LastPath("/c", "/portal/layout", params);
ses.setAttribute(WebKeys.LAST_PATH, lastPath);
For this to work, you might also check the auth.forward.by.last.path property

Adding custom metadata field to DocuSign template

I need to be able to filter the templates that my organization has in docusign after I make a getTemplates API call. Right now we are filtering by folder name, but this solution is not ideal. Templates can easily get moved and the folders are created by the individual user not by organization, so every user in our organization would have to create the same folder structure.
Is there a way to add a custom data to a template? Ideally, this field would be returned with the JSON Response from DocuSign. Something like:
JSON Response from DocuSign
{
"envelopeTemplates": [
{
"allowMarkup": "sample string 30",
... other standard template properties ...
"customMetaData": {
"foo": "bar" // what ever the user wants to add here
}
}
]
}
Or does anyone have a better idea for filtering templates?
There are two possible ways to add the metadata to the template
TemplateCustom Fields let you specify metadata that gets associated at the Template level. This data isn't visible to recipients; it's just extra metadata that you can associate with an template and query later (via API). See here for more info.
Document Custom Fields let you specify metadata that gets associated at the Document level. This data isn't visible to recipients in any way; it's just extra metadata that you can associate with a specific Document and query later (via API). See here for more info.
It is possible to add custom data to a given template by adding a "Custom Field". See here how to Add Custom Document Fields to a Template Document

Liferay find User screenName from User Email

Is there any api to find out screen name by passing the email address as the input in liferay for LDAP configuration?
You'll need to get a request object, then do this
long companyID = PortalUtil.getCompanyId(request)
User user = UserLocalServiceUtil.getUserByEmailAddress(companyID, emailAddress);
String scrName = user.getScreenName();
EDIT : in your liferay control panel/portal/authentication/LDAP, there is field named 'user search filter'
use (cn=#screen_name#) if your users are logging with screenName
use (mail=#email_address#) if your users are logging with email address
This way you're supposed to import the users automatically.
Please update your question. According to your tags, it looked like you're searching for a Liferay API. If you wanted an LDAP API you should write it somewhere

Liferay PortalURL in scheduler

I am creating a Scheduler in liferay Portlet and once the scheduler job is complete i have to send an email.
In this email i need to send the Portal URL as a link.
<a href="http://localhost:8080"
Can any body help me on how i can get the portal URL in scheduler.
You can use the following:
Company company = CompanyLocalServiceUtil.getCompany(companyId);
String portalURL = PortalUtil.getPortalURL(company.getVirtualHost(),
PortalUtil.getPortalPort(), false);
In the last call, the last parameter must be true if you are using https.
If you don't have access to the companyId in your class but you know that you have only one company in the DB (which is the normal case unless you are in a hosting/multitenant scenario) then you can use the following:
Company company = CompanyLocalServiceUtil.getCompanies().get(0);

FBA on Sharepoint 2010

I have implemented FBA (Claim based Authentication) on Sharepoint 2010. Following are implemented.
Custom Login page
Custom Sign-in Page
Password recovery page (ForgetPassword.aspx)
In ForgetPassword page user is asked to enter their email address, they used while sign-in and in code behind I am using this email to get the UserName using the Membership.GetUserNameByEmail function and then passing this username to Membership.GetUser function to get the user credential to be send through mail.
But now the code throws as exception saying "The function is not implemented". I am wondering; I am not using any custom database for which I had to create a Custom Membership Provider. Then why I am getting this error. Let me know if anyone has any clue or faced similar problem. Thanks.
Regards,
Paddy
When FBA is configured for SharePoint 2010, two membership providers are defined in the web.config file - Microsoft.SharePoint.Administration.Claims.SPClaimsAuthMembershipProvider (usually named i) and System.Web.Security.SqlMembershipProvider (named FBAMembership in this case). Default membership provider must be set to the former (i.e. SharePoint claims one) in order for FBA authentication to work properly.
When the line containing Membership.GetUserNameByEmail(...) is executed, the default membership provider is used and as a result SPClaimsAuthMembershipProvider.GetUserNameByEmail is called. MSDN says that this method is reserved for internal use and is not intended to be used directly from your code and according to the comment in the Community Content section it throws NotImplementedException.
You need to retrieve an instance of the SqlMembershipProvider provider from the Membership.Providers collection and then call the GetUserNameByEmail method using this instance.
I use prefixes when configuring providers in the web.config file and the retrieve them like this:
string applicationNamePrefix = "fbaProvider_";
MembershipProvider fbaProvider;
foreach (MembershipProvider provider in Membership.Providers)
{
if (provider.ApplicationName.StartsWith(applicationNamePrefix, StringComparison.InvariantCultureIgnoreCase))
{
fbaProvider = provider;
}
}
throw new InvalidOperationException("Appropriate provider was not found.");

Resources