Merge branch 'master' into 'master'

Fixed QR generating issue in Cloud App

See merge request entgra/carbon-device-mgt!636
merge-requests/713/head
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.core.otp.mgt.exception.OTPManagementDAOException;
import java.util.List;
public interface OTPManagementDAO {
/**
* Save OTP token data and tenant details of registering user
* @param oneTimePinDTO OTPMailDTO
* @return Primary key of the newly adding data raw
* @param oneTimePinDTOList OTPMailDTO
* @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

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

@ -61,6 +61,8 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.ArrayList;
import java.util.Collections;
public class OTPManagementServiceImpl implements OTPManagementService {
@ -82,12 +84,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
otpWrapper.getUsername(), tenant, -1234);
try {
ConnectionManagerUtil.beginDBTransaction();
if (this.otpManagementDAO.addOTPData(oneTimePinDTO) == -1) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "OTP data saving failed. Please, contact Administrator";
log.error(msg);
throw new OTPManagementException(msg);
}
this.otpManagementDAO.addOTPData(Collections.singletonList(oneTimePinDTO));
Properties props = new Properties();
props.setProperty("first-name", tenant.getAdminFirstName());
props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
@ -98,12 +95,12 @@ public class OTPManagementServiceImpl implements OTPManagementService {
log.error(msg, e);
throw new OTPManagementException(msg, 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);
throw new OTPManagementException(msg, e);
} catch (OTPManagementDAOException e) {
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);
throw new OTPManagementException(msg, e);
} finally {
@ -219,24 +216,44 @@ public class OTPManagementServiceImpl implements OTPManagementService {
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
OneTimePinDTO oneTimePinDTO;
List<OneTimePinDTO> oneTimePinDTOList = new ArrayList<>();
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);
oneTimePinDTO = createOneTimePin(emailAddress, OTPEmailTypes.DEVICE_ENROLLMENT.toString(), username,
null, tenantId);
oneTimePinDTOList.add(oneTimePinDTO);
props.setProperty("first-name", DeviceManagerUtil.
getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME));
props.setProperty("username", username);
props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
sendMail(props, emailAddress, DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE);
}
this.otpManagementDAO.addOTPData(oneTimePinDTOList);
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);
} 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