Merge branch 'master' into 'master'

Fixed QR generating issue in Cloud App

See merge request entgra/carbon-device-mgt!636
4.x.x
Dharmakeerthi Lasantha 4 years ago
commit 3e6e55fa1e

@ -20,15 +20,16 @@ package org.wso2.carbon.device.mgt.core.otp.mgt.dao;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException; import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException;
import java.util.List;
public interface OTPManagementDAO { public interface OTPManagementDAO {
/** /**
* Save OTP token data and tenant details of registering user * Save OTP token data and tenant details of registering user
* @param oneTimePinDTO OTPMailDTO * @param oneTimePinDTOList OTPMailDTO
* @return Primary key of the newly adding data raw
* @throws OTPManagementDAOException if error occurred whule storing data * @throws OTPManagementDAOException if error occurred whule storing data
*/ */
int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException; void addOTPData(List<OneTimePinDTO> oneTimePinDTOList) throws OTPManagementDAOException;
/** /**
* Get OTP data for requesting One Time Token * Get OTP data for requesting One Time Token

@ -32,17 +32,20 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPManagementDAO { public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPManagementDAO {
private static final Log log = LogFactory.getLog(GenericOTPManagementDAOImpl.class); private static final Log log = LogFactory.getLog(GenericOTPManagementDAOImpl.class);
@Override @Override
public int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException { public void addOTPData(List<OneTimePinDTO> oneTimePinDTOList) throws OTPManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to create an OTP data entry"); log.debug("Request received in DAO Layer to create an OTP data entry");
log.debug("OTP Details : "); log.debug("OTP Details : ");
log.debug("OTP key : " + oneTimePinDTO.getOtpToken() + " Email : " + oneTimePinDTO.getEmail()); for(OneTimePinDTO oneTimePinDTO: oneTimePinDTOList){
log.debug("OTP key : " + oneTimePinDTO.getOtpToken() + " Email : " + oneTimePinDTO.getEmail());
}
} }
String sql = "INSERT INTO DM_OTP_DATA " String sql = "INSERT INTO DM_OTP_DATA "
@ -57,29 +60,25 @@ public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPM
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, oneTimePinDTO.getOtpToken()); for (OneTimePinDTO oneTimePinDTO : oneTimePinDTOList) {
stmt.setString(2, oneTimePinDTO.getEmail()); stmt.setString(1, oneTimePinDTO.getOtpToken());
stmt.setString(3, oneTimePinDTO.getEmailType()); stmt.setString(2, oneTimePinDTO.getEmail());
stmt.setString(4, oneTimePinDTO.getMetaInfo()); stmt.setString(3, oneTimePinDTO.getEmailType());
stmt.setTimestamp(5, timestamp); stmt.setString(4, oneTimePinDTO.getMetaInfo());
stmt.setInt(6, oneTimePinDTO.getTenantId()); stmt.setTimestamp(5, timestamp);
stmt.setString(7, oneTimePinDTO.getUsername()); stmt.setInt(6, oneTimePinDTO.getTenantId());
stmt.executeUpdate(); stmt.setString(7, oneTimePinDTO.getUsername());
try (ResultSet rs = stmt.getGeneratedKeys()) { stmt.addBatch();
if (rs.next()) {
return rs.getInt(1);
}
return -1;
} }
stmt.executeBatch();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to create an opt entry for email " String msg = "Error occurred while obtaining the DB connection to create an opt entry.";
+ oneTimePinDTO.getEmail();
log.error(msg, e); log.error(msg, e);
throw new OTPManagementDAOException(msg, e); throw new OTPManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while executing SQL to create an otp entry for email " + oneTimePinDTO.getEmail(); String msg = "Error occurred while executing SQL to create an otp entry";
log.error(msg, e); log.error(msg, e);
throw new OTPManagementDAOException(msg, e); throw new OTPManagementDAOException(msg, e);
} }

@ -61,6 +61,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import java.util.ArrayList;
import java.util.Collections;
public class OTPManagementServiceImpl implements OTPManagementService { public class OTPManagementServiceImpl implements OTPManagementService {
@ -82,12 +84,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
otpWrapper.getUsername(), tenant, -1234); otpWrapper.getUsername(), tenant, -1234);
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
if (this.otpManagementDAO.addOTPData(oneTimePinDTO) == -1) { this.otpManagementDAO.addOTPData(Collections.singletonList(oneTimePinDTO));
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "OTP data saving failed. Please, contact Administrator";
log.error(msg);
throw new OTPManagementException(msg);
}
Properties props = new Properties(); Properties props = new Properties();
props.setProperty("first-name", tenant.getAdminFirstName()); props.setProperty("first-name", tenant.getAdminFirstName());
props.setProperty("otp-token", oneTimePinDTO.getOtpToken()); props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
@ -98,12 +95,12 @@ public class OTPManagementServiceImpl implements OTPManagementService {
log.error(msg, e); log.error(msg, e);
throw new OTPManagementException(msg, e); throw new OTPManagementException(msg, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while getting database connection."; String msg = "Error occurred while getting database connection to add OPT data.";
log.error(msg, e); log.error(msg, e);
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. Email address: " + oneTimePinDTO.getEmail(); 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 {
@ -219,24 +216,44 @@ public class OTPManagementServiceImpl implements OTPManagementService {
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
OneTimePinDTO oneTimePinDTO; OneTimePinDTO oneTimePinDTO;
List<OneTimePinDTO> oneTimePinDTOList = new ArrayList<>();
Properties props = new Properties(); Properties props = new Properties();
props.setProperty("enrollment-steps", enrollmentSteps.toString()); props.setProperty("enrollment-steps", enrollmentSteps.toString());
try { try {
ConnectionManagerUtil.beginDBTransaction();
for (String username : deviceEnrollmentInvitation.getUsernames()) { for (String username : deviceEnrollmentInvitation.getUsernames()) {
String emailAddress = DeviceManagerUtil.getUserClaimValue( String emailAddress = DeviceManagerUtil.getUserClaimValue(
username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS); username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS);
oneTimePinDTO = createOneTimePin(emailAddress, OTPEmailTypes.DEVICE_ENROLLMENT.toString(), username, oneTimePinDTO = createOneTimePin(emailAddress, OTPEmailTypes.DEVICE_ENROLLMENT.toString(), username,
null, tenantId); null, tenantId);
oneTimePinDTOList.add(oneTimePinDTO);
props.setProperty("first-name", DeviceManagerUtil. props.setProperty("first-name", DeviceManagerUtil.
getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME));
props.setProperty("username", username); props.setProperty("username", username);
props.setProperty("otp-token", oneTimePinDTO.getOtpToken()); props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
sendMail(props, emailAddress, DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE); sendMail(props, emailAddress, DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE);
} }
this.otpManagementDAO.addOTPData(oneTimePinDTOList);
ConnectionManagerUtil.commitDBTransaction();
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while getting claim values to invite user"; String msg = "Error occurred while getting claim values to invite user";
log.error(msg, e); log.error(msg, e);
throw new OTPManagementException(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);
} catch (OTPManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred while saving the OTP data.";
log.error(msg, e);
throw new OTPManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }

Loading…
Cancel
Save