@ -18,20 +18,12 @@
package io.entgra.device.mgt.core.device.mgt.core.otp.mgt.service ;
package io.entgra.device.mgt.core.device.mgt.core.otp.mgt.service ;
import com.google.gson.Gson ;
import com.google.gson.Gson ;
import org.apache.commons.lang.StringUtils ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.wso2.carbon.context.CarbonContext ;
import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationManagementException ;
import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationManagementException ;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.BadRequestException ;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.* ;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DBConnectionException ;
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.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.invitation.mgt.DeviceEnrollmentType ;
import io.entgra.device.mgt.core.device.mgt.common.otp.mgt.OTPEmailTypes ;
import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.EnrollmentTypeMail ;
import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.UserMailAttributes ;
import io.entgra.device.mgt.core.device.mgt.common.otp.mgt.dto.OneTimePinDTO ;
import io.entgra.device.mgt.core.device.mgt.common.otp.mgt.dto.OneTimePinDTO ;
import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService ;
import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService ;
import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants ;
import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants ;
@ -40,20 +32,16 @@ import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.dao.OTPManagementDAO;
import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.dao.OTPManagementDAOFactory ;
import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.dao.OTPManagementDAOFactory ;
import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.exception.OTPManagementDAOException ;
import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.exception.OTPManagementDAOException ;
import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.util.ConnectionManagerUtil ;
import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.util.ConnectionManagerUtil ;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService ;
import io.entgra.device.mgt.core.device.mgt.core.service.EmailMetaInfo ;
import io.entgra.device.mgt.core.device.mgt.core.service.EmailMetaInfo ;
import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil ;
import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil ;
import org.apache.commons.lang.StringUtils ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.wso2.carbon.user.api.Tenant ;
import org.wso2.carbon.user.api.Tenant ;
import org.wso2.carbon.user.api.UserStoreException ;
import org.wso2.carbon.user.api.UserStoreException ;
import java.sql.Timestamp ;
import java.sql.Timestamp ;
import java.util.ArrayList ;
import java.util.* ;
import java.util.Calendar ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import java.util.Properties ;
import java.util.UUID ;
public class OTPManagementServiceImpl implements OTPManagementService {
public class OTPManagementServiceImpl implements OTPManagementService {
@ -218,53 +206,12 @@ public class OTPManagementServiceImpl implements OTPManagementService {
}
}
}
}
@Override
@Override
public void sendDeviceEnrollmentInvitationMail ( DeviceEnrollmentInvitation deviceEnrollmentInvitation )
public void sendDeviceEnrollmentInvitationMail ( DeviceEnrollmentInvitation deviceEnrollmentInvitation )
throws OTPManagementException {
throws OTPManagementException {
DeviceManagementProviderService dms = DeviceManagementDataHolder . getInstance ( ) . getDeviceManagementProvider ( ) ;
List < EnrollmentTypeMail > enrollmentTypeMails =
StringBuilder enrollmentSteps = new StringBuilder ( ) ;
getEnrollmentTypeMails ( deviceEnrollmentInvitation . getDeviceEnrollmentTypes ( ) ) ;
DeviceEnrollmentInvitationDetails deviceEnrollmentInvitationDetails ;
sendEnrollmentTypeMails ( deviceEnrollmentInvitation . getUsernames ( ) , enrollmentTypeMails ) ;
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 ( ) ;
}
}
}
/ * *
/ * *
@ -296,7 +243,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
throw new OTPManagementException ( msg , e ) ;
throw new OTPManagementException ( msg , e ) ;
} catch ( OTPManagementDAOException e ) {
} catch ( OTPManagementDAOException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred while saving the OTP data for given email" ;
String msg = "Error occurred while saving the OTP data for given email" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new OTPManagementException ( msg , e ) ;
throw new OTPManagementException ( msg , e ) ;
} finally {
} finally {
@ -312,7 +259,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
* @return { @link OneTimePinDTO }
* @return { @link OneTimePinDTO }
* @throws OTPManagementException if error occurred while getting OTP data for given OTP in DB
* @throws OTPManagementException if error occurred while getting OTP data for given OTP in DB
* /
* /
private OneTimePinDTO getOTPDataByToken ( String oneTimeToken ) throws OTPManagementException {
private OneTimePinDTO getOTPDataByToken ( String oneTimeToken ) throws OTPManagementException {
try {
try {
ConnectionManagerUtil . openDBConnection ( ) ;
ConnectionManagerUtil . openDBConnection ( ) ;
return otpManagementDAO . getOTPDataByToken ( oneTimeToken ) ;
return otpManagementDAO . getOTPDataByToken ( oneTimeToken ) ;
@ -333,7 +280,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
* If OTP expired , resend the user verifying mail with renewed OTP
* If OTP expired , resend the user verifying mail with renewed OTP
* @param props Mail body properties
* @param props Mail body properties
* @param mailAddress Mail Address of the User
* @param mailAddress Mail Address of the User
* @param template Mail template to be used
* @param template Mail template to be used
* @throws OTPManagementException if error occurred while resend the user verifying mail
* @throws OTPManagementException if error occurred while resend the user verifying mail
* /
* /
private void sendMail ( Properties props , String mailAddress , String template ) throws OTPManagementException {
private void sendMail ( Properties props , String mailAddress , String template ) throws OTPManagementException {
@ -355,7 +302,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
/ * *
/ * *
* Renew the OTP
* Renew the OTP
* @param oneTimePinDTO { @link OneTimePinDTO }
* @param oneTimePinDTO { @link OneTimePinDTO }
* @param renewedOTP Renewed OTP
* @param renewedOTP Renewed OTP
* @throws OTPManagementException if error occurred while renew the OTP
* @throws OTPManagementException if error occurred while renew the OTP
* /
* /
private void renewOTP ( OneTimePinDTO oneTimePinDTO , String renewedOTP ) throws OTPManagementException {
private void renewOTP ( OneTimePinDTO oneTimePinDTO , String renewedOTP ) throws OTPManagementException {
@ -380,4 +327,113 @@ public class OTPManagementServiceImpl implements OTPManagementService {
ConnectionManagerUtil . closeDBConnection ( ) ;
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 < String > usernames , List < EnrollmentTypeMail > 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 ;
}
/ * *
* Populate enrollment type mails with provided user attributes
* @param userMailAttributes User attributes
* @param enrollmentTypeMails Enrollment type mails
* /
private void populateUserAttributes ( UserMailAttributes userMailAttributes , List < EnrollmentTypeMail > 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 ;
}
/ * *
* Generate enrollment type mails from device enrollment types
* @param deviceEnrollmentTypes List of device enrollment types
* @return List of enrollment type mails
* /
private List < EnrollmentTypeMail > getEnrollmentTypeMails ( List < DeviceEnrollmentType > deviceEnrollmentTypes ) {
List < EnrollmentTypeMail > enrollmentTypeMails = new ArrayList < > ( ) ;
for ( DeviceEnrollmentType deviceEnrollmentType : deviceEnrollmentTypes ) {
String deviceType = deviceEnrollmentType . getDeviceType ( ) ;
for ( String enrollmentType : deviceEnrollmentType . getEnrollmentType ( ) ) {
enrollmentTypeMails . add ( getEnrollmentTypeMail ( deviceType , enrollmentType ) ) ;
}
}
return enrollmentTypeMails ;
}
}
}