diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/invitation/mgt/EnrollmentTypeMail.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/invitation/mgt/EnrollmentTypeMail.java new file mode 100644 index 0000000000..86bec9ebff --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/invitation/mgt/EnrollmentTypeMail.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.device.mgt.common.invitation.mgt; + +import java.util.Properties; + +public class EnrollmentTypeMail { + private String template; + private String username; + private String recipient; + private Properties properties; + + public String getTemplate() { + return template; + } + + public void setTemplate(String template) { + this.template = template; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + public String getRecipient() { + return recipient; + } + public void setRecipient(String recipient) { + this.recipient = recipient; + } + + public Properties getProperties() { + return properties; + } + public void setProperties(Properties properties) { + this.properties = properties; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/invitation/mgt/UserMailAttributes.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/invitation/mgt/UserMailAttributes.java new file mode 100644 index 0000000000..147298a666 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/invitation/mgt/UserMailAttributes.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.device.mgt.common.invitation.mgt; + +public class UserMailAttributes { + private String username; + private String firstName; + private String email; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getUsernamePlaceholder() { + return "username"; + } + + public String getEmailPlaceholder() { + return "email"; + } + + public String getFirstNamePlaceholder() { + return "first-name"; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java index 8fc2e2804a..872b25205a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java @@ -121,6 +121,9 @@ public final class DeviceManagementConstants { public static final String USER_WELCOME_TEMPLATE = "user-welcome"; public static final String DEFAULT_ENROLLMENT_TEMPLATE = "default-enrollment-invitation"; public static final String ENROLLMENT_GUIDE_TEMPLATE = "enrollment-guide"; + public static final String DEVICE_ENROLLMENT_MAIL_KEY = "enrollment"; + public static final String TEMPLATE_NAME_PART_JOINER = "-"; + public static final String ENROLLMENT_TYPE_SPLITTER = "_"; } public static final class OperationAttributes { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java index bc9204eecb..564c9f073e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.otp.mgt.service; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.*; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,9 +29,6 @@ import io.entgra.device.mgt.core.device.mgt.common.exceptions.DBConnectionExcept import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.OTPManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; -import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation; -import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitationDetails; -import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.DeviceEnrollmentType; import io.entgra.device.mgt.core.device.mgt.common.otp.mgt.OTPEmailTypes; import io.entgra.device.mgt.core.device.mgt.common.otp.mgt.dto.OneTimePinDTO; import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService; @@ -47,13 +45,7 @@ import org.wso2.carbon.user.api.Tenant; import org.wso2.carbon.user.api.UserStoreException; import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.UUID; +import java.util.*; public class OTPManagementServiceImpl implements OTPManagementService { @@ -218,53 +210,12 @@ public class OTPManagementServiceImpl implements OTPManagementService { } } - @Override public void sendDeviceEnrollmentInvitationMail(DeviceEnrollmentInvitation deviceEnrollmentInvitation) - throws OTPManagementException { - DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(); - StringBuilder enrollmentSteps = new StringBuilder(); - DeviceEnrollmentInvitationDetails deviceEnrollmentInvitationDetails; - for (DeviceEnrollmentType deviceEnrollmentType : deviceEnrollmentInvitation.getDeviceEnrollmentTypes()) { - deviceEnrollmentInvitationDetails = dms.getDeviceEnrollmentInvitationDetails( - deviceEnrollmentType.getDeviceType()); - if (deviceEnrollmentInvitationDetails != null && - deviceEnrollmentInvitationDetails.getEnrollmentDetails() != null) { - for (String enrollmentType : deviceEnrollmentType.getEnrollmentType()) { - deviceEnrollmentInvitationDetails.getEnrollmentDetails().stream() - .filter(details -> enrollmentType.equals(details.getEnrollmentType())).findFirst() - .ifPresent(details -> enrollmentSteps.append(details.getEnrollmentSteps())); - } - } - } - Properties props = new Properties(); - props.setProperty("enrollment-steps", enrollmentSteps.toString()); - try { - ConnectionManagerUtil.beginDBTransaction(); - for (String username : deviceEnrollmentInvitation.getUsernames()) { - String emailAddress = DeviceManagerUtil.getUserClaimValue( - username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS); - props.setProperty("first-name", DeviceManagerUtil. - getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); - props.setProperty("username", username); - sendMail(props, emailAddress, DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE); - } - ConnectionManagerUtil.commitDBTransaction(); - } catch (UserStoreException e) { - String msg = "Error occurred while getting claim values to invite user"; - log.error(msg, e); - throw new OTPManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "Error occurred while getting database connection to add OPT data."; - log.error(msg, e); - throw new OTPManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "SQL Error occurred when adding OPT data to send device enrollment Invitation."; - log.error(msg, e); - throw new OTPManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } + throws OTPManagementException { + List enrollmentTypeMails = + getEnrollmentTypeMails(deviceEnrollmentInvitation.getDeviceEnrollmentTypes()); + sendEnrollmentTypeMails(deviceEnrollmentInvitation.getUsernames(), enrollmentTypeMails); } /** @@ -380,4 +331,103 @@ public class OTPManagementServiceImpl implements OTPManagementService { ConnectionManagerUtil.closeDBConnection(); } } + + /** + * Send enrollment type mails to users + * @param usernames List of usernames to send enrollment type mails + * @param enrollmentTypeMails List of enrollment types + * @throws OTPManagementException Throws when error occurred while sending emails + */ + private void sendEnrollmentTypeMails(List usernames, List enrollmentTypeMails) + throws OTPManagementException { + try { + ConnectionManagerUtil.beginDBTransaction(); + for (String username : usernames) { + populateUserAttributes(getUserMailAttributes(username), enrollmentTypeMails); + for (EnrollmentTypeMail enrollmentTypeMail : enrollmentTypeMails) { + sendMail(enrollmentTypeMail); + } + } + ConnectionManagerUtil.commitDBTransaction(); + } catch (UserStoreException e) { + String msg = "Error occurred while populating user attributes"; + log.error(msg, e); + throw new OTPManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while getting database connection to add OTP data."; + log.error(msg, e); + throw new OTPManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "SQL Error occurred when adding OPT data to send device enrollment Invitation."; + log.error(msg, e); + throw new OTPManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * Send enrollment type mail + * @param enrollmentTypeMail Data related to the enrollment mail + * @throws OTPManagementException Throws when error occurred while sending email + */ + private void sendMail(EnrollmentTypeMail enrollmentTypeMail) throws OTPManagementException { + sendMail(enrollmentTypeMail.getProperties(), enrollmentTypeMail.getRecipient(), enrollmentTypeMail.getTemplate()); + } + + /** + * Get user claims based on the username + * @param username Username + * @return {@link UserMailAttributes} + * @throws UserStoreException Throws when error occurred while retrieving user claims + */ + private UserMailAttributes getUserMailAttributes(String username) throws UserStoreException { + UserMailAttributes userMailAttributes = new UserMailAttributes(); + userMailAttributes.setEmail(DeviceManagerUtil.getUserClaimValue( + username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS)); + userMailAttributes.setFirstName(DeviceManagerUtil. + getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); + userMailAttributes.setUsername(username); + return userMailAttributes; + } + + private void populateUserAttributes(UserMailAttributes userMailAttributes, List enrollmentTypeMails) { + for (EnrollmentTypeMail enrollmentTypeMail : enrollmentTypeMails) { + Properties properties = new Properties(); + properties.setProperty(userMailAttributes.getEmailPlaceholder(), userMailAttributes.getEmail()); + properties.setProperty(userMailAttributes.getFirstNamePlaceholder(), userMailAttributes.getFirstName()); + properties.setProperty(userMailAttributes.getUsernamePlaceholder(), userMailAttributes.getUsername()); + enrollmentTypeMail.setProperties(properties); + enrollmentTypeMail.setUsername(userMailAttributes.getUsername()); + enrollmentTypeMail.setRecipient(userMailAttributes.getEmail()); + } + } + + /** + * Generate enrollment type mail + * @param deviceType Device type of the enrollment type + * @param enrollmentType Enrollment type + * @return {@link EnrollmentTypeMail} + */ + private EnrollmentTypeMail getEnrollmentTypeMail(String deviceType, String enrollmentType) { + EnrollmentTypeMail enrollmentTypeMail = new EnrollmentTypeMail(); + enrollmentTypeMail.setUsername(enrollmentTypeMail.getUsername()); + enrollmentTypeMail.setTemplate(String.join(DeviceManagementConstants.EmailAttributes.TEMPLATE_NAME_PART_JOINER, + deviceType.toLowerCase(), enrollmentType.toLowerCase(). + replace(DeviceManagementConstants.EmailAttributes.ENROLLMENT_TYPE_SPLITTER, + DeviceManagementConstants.EmailAttributes.TEMPLATE_NAME_PART_JOINER), + DeviceManagementConstants.EmailAttributes.DEVICE_ENROLLMENT_MAIL_KEY)); + return enrollmentTypeMail; + } + + private List getEnrollmentTypeMails(List deviceEnrollmentTypes) { + List enrollmentTypeMails = new ArrayList<>(); + for (DeviceEnrollmentType deviceEnrollmentType : deviceEnrollmentTypes) { + String deviceType = deviceEnrollmentType.getDeviceType(); + for (String enrollmentType : deviceEnrollmentType.getEnrollmentType()) { + enrollmentTypeMails.add(getEnrollmentTypeMail(deviceType, enrollmentType)); + } + } + return enrollmentTypeMails; + } } \ No newline at end of file diff --git a/features/transport-mgt/email-sender/io.entgra.device.mgt.core.email.sender.feature/src/main/resources/email/templates/user-registration.vm b/features/transport-mgt/email-sender/io.entgra.device.mgt.core.email.sender.feature/src/main/resources/email/templates/user-registration.vm index bcca3b19ca..1bdea39053 100644 --- a/features/transport-mgt/email-sender/io.entgra.device.mgt.core.email.sender.feature/src/main/resources/email/templates/user-registration.vm +++ b/features/transport-mgt/email-sender/io.entgra.device.mgt.core.email.sender.feature/src/main/resources/email/templates/user-registration.vm @@ -15,56 +15,440 @@ specific language governing permissions and limitations under the License. *# + - You have successfully been registered in Entgra IoT + You have been invited to enroll your device in Entgra UEM - - Entgra IoT Server - - -
-
-
-
- entgra -
-
-
-

- Hi $first-name, -

- -

- You have been registered in Entgra IoT and invited to enrol your device. - Click here to begin device enrolment.

- -

- Use following credentials to log in to Entgra IoT Device Management application. -

- -

- Username: $username -
- Password: $password -

- -

- Should you need assistance, please contact your administrator. -

- -

- Regards, -

- -

- Entgra IoT Administrator -

-
-
-
- + + + + + + + + + + Entgra UEM Server + + + + + + + + + + + +
+ +
+
+
+ + +
+
+ +
+ + + + + + + +
+ + + + +
+ +
+
+ +
+ +
+
+ + +
+
+
+
+
+
+ + +
+
+ +
+ + + + + + + +
+
+

Dear $first-name,

+
+

We have completed your Entgra UEM registration. You are now invited to proceed with the enrollment of your device.

+
+
+

Please click on the link below to initiate this process.

+
+
+ + + + + + +
+ + +
+ + + + + + +
+
+

Here are your login credentials:

+
+

Username: $username

+

Password: $password

+
+

If you require any assistance or have questions during the enrollment process, reach out to your designated administrator via the Entgra Support Portal.

+
+

Thank you.

+
+

Best wishes,

+

Entgra team

+
+
+ +
+ +
+
+ + +
+
+
+
+
+
+ + +
+
+ +
+ + + + + + + +
+ + + + + + +
+
+
+ + + + + + +
+ +
+ + + + + + +
+
+

Follow Entgra on social media

+
+
+ + + + + + +
+
+
+ + + + + + + + +
+ + Facebook + +
+ + + + + + + + +
+ + LinkedIn + +
+ + + + + + + + +
+ + X + +
+ + + + + + + + +
+ + YouTube + +
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
+ + + ]]>