inosh-perera 9 years ago
commit ff6e316406

@ -31,6 +31,7 @@ public class EmailMessageProperties {
private String title; private String title;
private String password; private String password;
private String userName; private String userName;
private String domainName;
public String getUserName() { public String getUserName() {
return userName; return userName;
@ -40,6 +41,14 @@ public class EmailMessageProperties {
this.userName = userName; this.userName = userName;
} }
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
public String getMessageBody() { public String getMessageBody() {
return messageBody; return messageBody;
} }

@ -300,16 +300,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + " e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, " + "AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
"d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "e.TENANT_ID = ? AND e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; "AND t.ID = d.DEVICE_TYPE_ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); stmt.setString(2, username);
stmt.setString(3, username);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -609,7 +608,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
stmt.setString(index++, status.toString()); stmt.setString(index++, status.toString());
stmt.setInt(index, tenantId); stmt.setInt(index, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { while (rs.next()) {
enrolments.add(DeviceManagementDAOUtil.loadEnrolment(rs)); enrolments.add(DeviceManagementDAOUtil.loadEnrolment(rs));
} }
return enrolments; return enrolments;

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.email;
public final class EmailConstants { public final class EmailConstants {
public static final class EnrolmentEmailConstants { public static final class EnrolmentEmailConstants {
public static final String DOMAIN = "domain-name";
public static final String USERNAME = "user-name"; public static final String USERNAME = "user-name";
public static final String DOWNLOAD_URL = "downloadUrl"; public static final String DOWNLOAD_URL = "downloadUrl";
public static final String ENCODED_SCHEME = "UTF-8"; public static final String ENCODED_SCHEME = "UTF-8";

@ -97,7 +97,7 @@ public class OperationManagerImpl implements OperationManager {
List<EnrolmentInfo> enrolments; List<EnrolmentInfo> enrolments;
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
enrolments = deviceDAO.getEnrolmentsByStatus(deviceIds, EnrolmentInfo.Status.ACTIVE, tenantId); enrolments = deviceDAO.getEnrolmentsByStatus(authorizedDeviceList, EnrolmentInfo.Status.ACTIVE, tenantId);
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection the data " + throw new OperationManagementException("Error occurred while opening a connection the data " +
"source", e); "source", e);

@ -142,7 +142,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo();
if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { if (existingEnrolmentInfo != null && newEnrolmentInfo != null) {
//Get all the enrollments of current user for the same device //Get all the enrollments of current user for the same device
List<EnrolmentInfo> enrolmentInfos = this.getEnrollmentsOfUser(existingDevice.getId(), newEnrolmentInfo.getOwner()); List<EnrolmentInfo> enrolmentInfos = this.getEnrollmentsOfUser(existingDevice.getId(),
newEnrolmentInfo.getOwner());
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
//If the enrollments are same then we'll update the existing enrollment. //If the enrollments are same then we'll update the existing enrollment.
if (enrolmentInfo.equals(newEnrolmentInfo)) { if (enrolmentInfo.equals(newEnrolmentInfo)) {
@ -163,14 +164,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
existingEnrolmentInfo.setStatus(EnrolmentInfo.Status.REMOVED); existingEnrolmentInfo.setStatus(EnrolmentInfo.Status.REMOVED);
updateStatus = enrollmentDAO.updateEnrollment(existingEnrolmentInfo); updateStatus = enrollmentDAO.updateEnrollment(existingEnrolmentInfo);
} }
if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED.equals(existingEnrolmentInfo.getStatus())) { if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED.
enrolmentId = enrollmentDAO.addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); equals(existingEnrolmentInfo.getStatus())) {
enrolmentId = enrollmentDAO.
addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("An enrolment is successfully added with the id '" + enrolmentId + log.debug("An enrolment is successfully added with the id '" + enrolmentId +
"' associated with " + "the device identified by key '" + "' associated with " + "the device identified by key '" +
device.getDeviceIdentifier() + "', which belongs to " + "platform '" + device.getDeviceIdentifier() + "', which belongs to " + "platform '" +
device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() + "'"); device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() +
"'");
} }
status = true; status = true;
} else { } else {
@ -232,11 +236,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); DeviceType type = deviceTypeDAO.getDeviceType(device.getType());
deviceDAO.updateDevice(type.getId(), device, tenantId); deviceDAO.updateDevice(type.getId(), device, tenantId);
enrollmentDAO.updateEnrollment(device.getEnrolmentInfo()); enrollmentDAO.updateEnrollment(device.getEnrolmentInfo());
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
@ -257,8 +259,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
enrolmentInfos = enrollmentDAO.getEnrollmentsOfUser(deviceId, user, this.getTenantId()); enrolmentInfos = enrollmentDAO.getEnrollmentsOfUser(deviceId, user, this.getTenantId());
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the enrollment information device for id " + throw new DeviceManagementException("Error occurred while obtaining the enrollment information device for" +
"'" + deviceId + "' and user : " + user, e); "id '" + deviceId + "' and user : " + user, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -514,8 +516,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
for (NotificationMessages notificationMessage : notificationMessages) { for (NotificationMessages notificationMessage : notificationMessages) {
if (org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailNotifications.ENROL_NOTIFICATION_TYPE if (org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailNotifications.ENROL_NOTIFICATION_TYPE
.equals( .equals(notificationMessage.getType())) {
notificationMessage.getType())) {
messageHeader = notificationMessage.getHeader(); messageHeader = notificationMessage.getHeader();
messageBody = notificationMessage.getBody(); messageBody = notificationMessage.getBody();
messageFooter1 = notificationMessage.getFooterLine1(); messageFooter1 = notificationMessage.getFooterLine1();
@ -535,17 +536,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
EmailConfigurations emailConfig = EmailConfigurations emailConfig =
DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
getDeviceManagementConfigRepository().getEmailConfigurations(); getDeviceManagementConfigRepository().getEmailConfigurations();
emailMessageProperties.setEnrolmentUrl(emailConfig.getlBHostPortPrefix()+ emailConfig.getEnrollmentContextPath()); emailMessageProperties.setEnrolmentUrl(emailConfig.getlBHostPortPrefix() +
emailConfig.getEnrollmentContextPath());
messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}",
URLEncoder.encode(emailMessageProperties.getFirstName(), URLEncoder.encode(emailMessageProperties.getFirstName(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBody = messageBody.trim() + System.getProperty("line.separator") + messageBody = messageBody.trim() + System.getProperty("line.separator") + url.replaceAll("\\{"
System.getProperty("line.separator") + url.replaceAll("\\{"
+ EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}",
URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) messageBuilder.append(messageHeader).append(System.getProperty("line.separator"))
.append(System.getProperty("line.separator")); .append(System.getProperty("line.separator"));
messageBuilder.append(messageBody); messageBuilder.append(messageBody);
@ -553,7 +552,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
messageBuilder.append(messageFooter1.trim()) messageBuilder.append(messageFooter1.trim())
.append(System.getProperty("line.separator")).append(messageFooter2.trim()).append(System .append(System.getProperty("line.separator")).append(messageFooter2.trim()).append(System
.getProperty("line.separator")).append(messageFooter3.trim()); .getProperty("line.separator")).append(messageFooter3.trim());
} catch (IOException e) { } catch (IOException e) {
throw new DeviceManagementException("Error replacing tags in email template '" + throw new DeviceManagementException("Error replacing tags in email template '" +
emailMessageProperties.getSubject() + "'", e); emailMessageProperties.getSubject() + "'", e);
@ -576,8 +574,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
String subject = ""; String subject = "";
for (NotificationMessages notificationMessage : notificationMessages) { for (NotificationMessages notificationMessage : notificationMessages) {
if (org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailNotifications.USER_REGISTRATION_NOTIFICATION_TYPE. if (org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailNotifications.
equals(notificationMessage.getType())) { USER_REGISTRATION_NOTIFICATION_TYPE.equals(notificationMessage.getType())) {
messageHeader = notificationMessage.getHeader(); messageHeader = notificationMessage.getHeader();
messageBody = notificationMessage.getBody(); messageBody = notificationMessage.getBody();
messageFooter1 = notificationMessage.getFooterLine1(); messageFooter1 = notificationMessage.getFooterLine1();
@ -588,37 +586,33 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
break; break;
} }
} }
StringBuilder messageBuilder = new StringBuilder(); StringBuilder messageBuilder = new StringBuilder();
try { try {
// Reading the download url from the cdm-config.xml file // Reading the download url from the cdm-config.xml file
EmailConfigurations emailConfig = EmailConfigurations emailConfig =
DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
getDeviceManagementConfigRepository().getEmailConfigurations(); getDeviceManagementConfigRepository().getEmailConfigurations();
emailMessageProperties.setEnrolmentUrl(emailConfig.getlBHostPortPrefix()+ emailConfig.getEnrollmentContextPath()); emailMessageProperties.setEnrolmentUrl(emailConfig.getlBHostPortPrefix() +
emailConfig.getEnrollmentContextPath());
messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}",
URLEncoder.encode(emailMessageProperties.getFirstName(), URLEncoder.encode(emailMessageProperties.getFirstName(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants
.USERNAME .USERNAME
+ "\\}", + "\\}",
URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants
.ENCODED_SCHEME)); .ENCODED_SCHEME));
messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.DOMAIN
+ "\\}",
URLEncoder.encode(emailMessageProperties.getDomainName(), EmailConstants.EnrolmentEmailConstants
.ENCODED_SCHEME));
messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}", messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}",
URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants
.ENCODED_SCHEME)); .ENCODED_SCHEME));
messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{"
+ EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}",
URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBuilder.append(messageHeader).append(System.getProperty("line.separator")); messageBuilder.append(messageHeader).append(System.getProperty("line.separator"));
messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append(
messageFooter1.trim()); messageFooter1.trim());
@ -711,7 +705,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
deviceTypesInDatabase = deviceDAO.getDeviceTypes(); deviceTypesInDatabase = deviceDAO.getDeviceTypes();
Map<String, DeviceManagementService> registeredTypes = pluginRepository.getAllDeviceManagementServices(); Map<String, DeviceManagementService> registeredTypes = pluginRepository.getAllDeviceManagementServices();
DeviceType deviceType; DeviceType deviceType;
if (registeredTypes != null && deviceTypesInDatabase != null) { if (registeredTypes != null && deviceTypesInDatabase != null) {
for (int x = 0; x < deviceTypesInDatabase.size(); x++) { for (int x = 0; x < deviceTypesInDatabase.size(); x++) {
if (registeredTypes.get(deviceTypesInDatabase.get(x).getName()) != null) { if (registeredTypes.get(deviceTypesInDatabase.get(x).getName()) != null) {
@ -776,11 +769,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
EnrolmentInfo.Status status) throws DeviceManagementException { EnrolmentInfo.Status status) throws DeviceManagementException {
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); Device device = deviceDAO.getDevice(deviceId, tenantId);
boolean success = enrollmentDAO.setStatus(device.getId(), currentOwner, status, tenantId); boolean success = enrollmentDAO.setStatus(device.getId(), currentOwner, status, tenantId);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
return success; return success;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
@ -804,7 +795,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
dms.notifyOperationToDevices(operation, deviceIds); dms.notifyOperationToDevices(operation, deviceIds);
} }
} catch (DeviceManagementException deviceMgtEx) { } catch (DeviceManagementException deviceMgtEx) {
String errorMsg = "Error in notify operations to plugins for app installation:" + deviceMgtEx.getErrorMessage(); String errorMsg = "Error in notify operations to plugins for app installation:" +
deviceMgtEx.getErrorMessage();
log.error(errorMsg, deviceMgtEx); log.error(errorMsg, deviceMgtEx);
throw new DeviceManagementException(errorMsg, deviceMgtEx); throw new DeviceManagementException(errorMsg, deviceMgtEx);
} }
@ -1031,12 +1023,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException {
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(status); device.getEnrolmentInfo().setStatus(status);
deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId()); deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId());
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
@ -1083,7 +1073,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
for (Device device : allDevices) { for (Device device : allDevices) {
Device dmsDevice = this.getDeviceManager(device.getType()). Device dmsDevice = this.getDeviceManager(device.getType()).
getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));

@ -139,7 +139,9 @@ public class WebappAuthenticationValve extends CarbonTomcatValve {
msg = authenticationInfo.getMessage(); msg = authenticationInfo.getMessage();
response.setHeader("WWW-Authenticate", msg); response.setHeader("WWW-Authenticate", msg);
} }
log.error(msg + " , API : " + request.getRequestURI()); if (log.isDebugEnabled()) {
log.debug(msg + " , API : " + request.getRequestURI());
}
AuthenticationFrameworkUtil AuthenticationFrameworkUtil
.handleResponse(request, response, HttpServletResponse.SC_UNAUTHORIZED, .handleResponse(request, response, HttpServletResponse.SC_UNAUTHORIZED,
msg); msg);

@ -21,6 +21,7 @@
<Header>Dear {first-name},</Header> <Header>Dear {first-name},</Header>
<Body> <Body>
You have been registered to WSO2 MDM with following credentials. You have been registered to WSO2 MDM with following credentials.
Domain: {domain-name}
Username: {user-name} Username: {user-name}
Password: {password} Password: {password}
Below is the link to enroll. Below is the link to enroll.

Loading…
Cancel
Save