From 66afe95fe9ecb0476cd916eb31c50c70c2b262d1 Mon Sep 17 00:00:00 2001 From: osh Date: Sun, 5 Mar 2023 14:49:34 +0530 Subject: [PATCH 1/9] Add api for self guide enrollment fixes https://roadmap.entgra.net/issues/9939 --- .../service/api/DeviceManagementService.java | 370 ++++++++++-------- .../impl/DeviceManagementServiceImpl.java | 41 ++ .../DeviceManagementProviderService.java | 4 + .../DeviceManagementProviderServiceImpl.java | 185 ++++++++- 4 files changed, 441 insertions(+), 159 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 3304be7c14..f4eb04cb42 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -54,6 +54,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; +import org.wso2.carbon.device.mgt.common.geo.service.Alert; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; @@ -66,6 +67,7 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationRequest; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationStatusBean; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; +import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import javax.validation.constraints.Size; @@ -553,12 +555,12 @@ public interface DeviceManagementService { required = false, defaultValue = "0") @QueryParam("offset") int offset, - @ApiParam( - name = "limit", - value = "Provide how many device details you require from the starting pagination index/offset.", - required = false, - defaultValue = "100") - @QueryParam("limit") int limit + @ApiParam( + name = "limit", + value = "Provide how many device details you require from the starting pagination index/offset.", + required = false, + defaultValue = "100") + @QueryParam("limit") int limit ); @GET @@ -802,6 +804,59 @@ public interface DeviceManagementService { @QueryParam("requireDeviceInfo") boolean requireDeviceInfo); + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.MULTIPART_FORM_DATA) + @Path("/enrollment/guide") + @ApiOperation( + consumes = MediaType.MULTIPART_FORM_DATA, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Sending Enrollment Mode chosen by customer", + notes = "Enrollment mode selected and path is sent as parameters", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully mailed the Enrollment Guide of customer.", + response = Device.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while sending mail of the Enrollment Guide.", + response = ErrorResponse.class) + }) + Response sendEnrollmentGuide( + @ApiParam( + name = "enrolmentGuide", + value = "The details of the enrolment path suggested.", + required = true) + MultipartFile enrolmentGuide); + @POST @Produces(MediaType.APPLICATION_JSON) @Path("/type/any/list") @@ -1399,15 +1454,15 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource was last modified. \n" + - "Used by caches, or in conditional requests.")}), + "Used by caches, or in conditional requests.")}), @ApiResponse( code = 304, message = "Not Modified. \n " + - "Empty body because the client already has the latest version of the requested resource.\n"), + "Empty body because the client already has the latest version of the requested resource.\n"), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -1425,7 +1480,7 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while getting the device details.", + "Server error occurred while getting the device details.", response = ErrorResponse.class) }) Response queryDevicesByProperties( @@ -1447,7 +1502,7 @@ public interface DeviceManagementService { name = "device property map", value = "properties by which devices need filtered", required = true) - PropertyMap map); + PropertyMap map); @GET @Produces(MediaType.APPLICATION_JSON) @@ -1873,140 +1928,141 @@ public interface DeviceManagementService { @Size(max = 45) String id); - @GET - @Produces(MediaType.APPLICATION_JSON) - @Path("/{type}/{id}/getstatushistory") - @ApiOperation( - produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "Get Device status history", - notes = "Get a list of status history associated with the device type and id", - tags = "Device Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") - }) - } - ) - @ApiResponses( - value = { - @ApiResponse( - code = 200, - message = "OK. \n Successfully fetched the status history of matching devices.", - response = List.class, - responseHeaders = { - @ResponseHeader( - name = "Content-Type", - description = "The content type of the body"), - @ResponseHeader( - name = "ETag", - description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), - @ResponseHeader( - name = "Last-Modified", - description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), - }), - @ApiResponse( - code = 304, - message = "Not Modified. Empty body because the client already has the latest version" + - " of the requested resource.\n"), - @ApiResponse( - code = 400, - message = "Bad Request. \n Invalid request or validation error.", - response = ErrorResponse.class), - @ApiResponse( - code = 404, - message = "Not Found. \n A device with the specified device type and id was not found.", - response = ErrorResponse.class), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n " + - "Server error occurred while retrieving the device details.", - response = ErrorResponse.class) - }) + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/{type}/{id}/getstatushistory") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get Device status history", + notes = "Get a list of status history associated with the device type and id", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the status history of matching devices.", + response = List.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version" + + " of the requested resource.\n"), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found. \n A device with the specified device type and id was not found.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving the device details.", + response = ErrorResponse.class) + }) Response getDeviceStatusHistory( - @ApiParam( - name = "type", - value = "The device type, such as ios, android, or windows.", - required = true) - @PathParam("type") - @Size(max = 45) - String type, - @ApiParam( - name = "id", - value = "Device ID.", - required = true) - @PathParam("id") - @Size(max = 45) - String id); - @GET - @Produces(MediaType.APPLICATION_JSON) - @Path("/{type}/{id}/getenrolmentstatushistory") - @ApiOperation( - produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "Get Device Current Enrolment status history", - notes = "Get a list of status history associated with the device type and id for the current enrolment", - tags = "Device Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") - }) - } - ) - @ApiResponses( - value = { - @ApiResponse( - code = 200, - message = "OK. \n Successfully fetched the status history of matching devices.", - response = List.class, - responseHeaders = { - @ResponseHeader( - name = "Content-Type", - description = "The content type of the body"), - @ResponseHeader( - name = "ETag", - description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), - @ResponseHeader( - name = "Last-Modified", - description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), - }), - @ApiResponse( - code = 304, - message = "Not Modified. Empty body because the client already has the latest version" + - " of the requested resource.\n"), - @ApiResponse( - code = 400, - message = "Bad Request. \n Invalid request or validation error.", - response = ErrorResponse.class), - @ApiResponse( - code = 404, - message = "Not Found. \n A device with the specified device type and id was not found.", - response = ErrorResponse.class), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n " + - "Server error occurred while retrieving the device details.", - response = ErrorResponse.class) - }) + @ApiParam( + name = "type", + value = "The device type, such as ios, android, or windows.", + required = true) + @PathParam("type") + @Size(max = 45) + String type, + @ApiParam( + name = "id", + value = "Device ID.", + required = true) + @PathParam("id") + @Size(max = 45) + String id); + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/{type}/{id}/getenrolmentstatushistory") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get Device Current Enrolment status history", + notes = "Get a list of status history associated with the device type and id for the current enrolment", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the status history of matching devices.", + response = List.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version" + + " of the requested resource.\n"), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found. \n A device with the specified device type and id was not found.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving the device details.", + response = ErrorResponse.class) + }) Response getCurrentEnrolmentDeviceStatusHistory( - @ApiParam( - name = "type", - value = "The device type, such as ios, android, or windows.", - required = true) - @PathParam("type") - @Size(max = 45) - String type, - @ApiParam( - name = "id", - value = "Device ID.", - required = true) - @PathParam("id") - @Size(max = 45) - String id); + @ApiParam( + name = "type", + value = "The device type, such as ios, android, or windows.", + required = true) + @PathParam("type") + @Size(max = 45) + String type, + @ApiParam( + name = "id", + value = "Device ID.", + required = true) + @PathParam("id") + @Size(max = 45) + String id); @PUT @Produces(MediaType.APPLICATION_JSON) @@ -2171,16 +2227,16 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 304, message = "Not Modified. Empty body because the client already has the latest version" + - " of the requested resource.\n"), + " of the requested resource.\n"), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -2192,7 +2248,7 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while retrieving the device details.", + "Server error occurred while retrieving the device details.", response = ErrorResponse.class) }) Response getDeviceCountByStatus( @@ -2240,16 +2296,16 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 304, message = "Not Modified. Empty body because the client already has the latest version" + - " of the requested resource.\n"), + " of the requested resource.\n"), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -2261,7 +2317,7 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while retrieving the device details.", + "Server error occurred while retrieving the device details.", response = ErrorResponse.class) }) Response getDeviceIdentifiersByStatus( @@ -2310,16 +2366,16 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource has been modified the last time.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 304, message = "Not Modified. Empty body because the client already has the latest " + - "version of the requested resource."), + "version of the requested resource."), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -2331,7 +2387,7 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while retrieving information requested device.", + "Server error occurred while retrieving information requested device.", response = ErrorResponse.class) }) Response bulkUpdateDeviceStatus( @@ -2642,11 +2698,11 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 500, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 3bcdeafbab..78c2280d4e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -68,6 +68,7 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; @@ -101,6 +102,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; @@ -130,6 +132,7 @@ import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import javax.ws.rs.Consumes; @@ -144,6 +147,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; +import java.io.IOException; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -816,6 +820,43 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.OK).entity(device).build(); } + @POST + @Path("/enrollment/guide") + @Override + public Response sendEnrollmentGuide(MultipartFile enrolmentGuide) { + if (log.isDebugEnabled()) { + log.debug("Sending enrollment invitation mail to existing user."); + } + DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); + try { +// Set recipients = new HashSet<>(); +// recipients.add(email); +// Properties props = new Properties(); +// props.setProperty("enrolment-guide", enrolmentGuide); +// +// EmailMetaInfo metaInfo = new EmailMetaInfo(recipients, props); +// dms.sendEnrolmentInvitation("enrolment-guide-template", metaInfo); + dms.sendEnrolmentGuide(enrolmentGuide); + return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while inviting user to enrol their device"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (ConfigurationManagementException e) { + String msg = "Error occurred while sending the email invitations. Mail server not configured."; + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (IOException e) { + e.printStackTrace(); + String msg = "testtttt."; + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } +// return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build(); + } + + @POST @Path("/type/any/list") @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 4e12d2cf1a..2be028d0a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -66,7 +66,9 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.sql.SQLException; import java.sql.Timestamp; import java.util.Collection; @@ -645,6 +647,8 @@ public interface DeviceManagementProviderService { void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException; + void sendEnrolmentGuide(MultipartFile enrolmentGuide) throws DeviceManagementException, ConfigurationManagementException, IOException; + void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException; FeatureManager getFeatureManager(String deviceType) throws DeviceTypeNotFoundException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 7bb7e7c627..28c79fa7a3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -38,6 +38,7 @@ package org.wso2.carbon.device.mgt.core.service; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import org.apache.commons.collections.map.SingletonMap; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -160,12 +161,13 @@ import org.wso2.carbon.stratos.common.beans.TenantInfoBean; import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import org.springframework.web.multipart.MultipartFile; +import javax.mail.*; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import java.io.IOException; -import java.io.StringWriter; +import java.io.*; import java.lang.reflect.Type; import java.sql.SQLException; import java.sql.Timestamp; @@ -185,6 +187,12 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMultipart; +import javax.ws.rs.core.MultivaluedMap; + public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -198,6 +206,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private final ApplicationDAO applicationDAO; private MetadataDAO metadataDAO; private final DeviceStatusDAO deviceStatusDAO; + int count = 0; public DeviceManagementProviderServiceImpl() { this.pluginRepository = new DeviceManagementPluginRepository(); @@ -1551,6 +1560,178 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + // save to somewhere + private void writeFile(byte[] content, String filename) throws IOException { + + File file = new File(filename); + + if (!file.exists()) { + file.createNewFile(); + } + + FileOutputStream fop = new FileOutputStream(file); + + fop.write(content); + fop.flush(); + fop.close(); + + } + + @Override + public void sendEnrolmentGuide(MultipartFile enrolmentGuide) throws DeviceManagementException, ConfigurationManagementException, IOException { + +// String fileName = ""; +// MultivaluedMap multivaluedMap = null; +// String fileName = null; +// InputStream inputStream = null; +// String uploadFilePath = null; +// +// Map> map = enrolmentGuide.getFormDataMap(); +// List inputParts = map.get("file"); +// +// for (InputPart inputPart : inputParts) { +// +// try { +// +// multivaluedMap = inputPart.getHeaders(); +// fileName = getFileName(multivaluedMap); +// +// //convert the uploaded file to inputstream +// InputStream inputStream = inputPart.getBody(InputStream.class,null); +// +// byte [] bytes = IOUtils.toByteArray(inputStream); +// +// //constructs upload file path +// fileName = UPLOADED_FILE_PATH + fileName; +// +// writeFile(bytes,fileName); +// +// System.out.println("Done"); +// +// } catch (IOException e) { +// e.printStackTrace(); +// } +// +// } + + // Recipient's email ID needs to be mentioned. + +// String charset = enrolmentGuide.getFormDataMap().get("_charset_").get(0).getBodyAsString(); +// File file = (File) enrolmentGuide.getFormDataMap().get("file").get(0); + String to = "oshani@entgra.io"; + + // Sender's email ID needs to be mentioned + String from = "oshsilva1996@gmail.com"; + + // Assuming you are sending email from through gmails smtp + String host = "smtp.gmail.com"; + + // Get system properties + Properties properties = System.getProperties(); + + // Setup mail server + properties.put("mail.smtp.host", host); + properties.put("mail.smtp.port", "465"); + properties.put("mail.smtp.ssl.enable", "true"); + properties.put("mail.smtp.auth", "true"); + + // Get the Session object.// and pass username and password + Session session = Session.getInstance(properties, new javax.mail.Authenticator() { + + protected PasswordAuthentication getPasswordAuthentication() { + + return new PasswordAuthentication("oshsilva1996@gmail.com", "layzvxhcxlwzkgwf"); + + } + + }); + + // Used to debug SMTP issues + session.setDebug(true); + + try { + // Create a default MimeMessage object. + MimeMessage message = new MimeMessage(session); + + // Set From: header field of the header. + message.setFrom(new InternetAddress(from)); + + // Set To: header field of the header. + message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); + +// // Set Subject: header field + message.setSubject("Enrollment Guide Triggered " + ++count); + +// message.setText(enrolmentGuide); + +// Multipart multipart = new MimeMultipart(); +// +// MimeBodyPart attachmentPart = new MimeBodyPart(); +// +// MimeBodyPart textPart = new MimeBodyPart(); +// +// try { +// +//// String charset = enrolmentGuide.getFormDataMap().get("_charset_").get(0).getBodyAsString(); +//// InputPart file = enrolmentGuide.getFormDataMap().get("file").get(0); +//// InputStream inputStream = file.getBody(InputStream.class, null); +//// BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset)); +//// String line; +//// StringBuilder content = new StringBuilder(); +//// while ((line = br.readLine()) != null) { +//// content.append(line); +//// } +//// +//// writeFile(content, "test"); +//// +//// File f =new File("H:\\pepipost_tutorials\\javaemail1.PNG"); +// +// attachmentPart.attachFile(file); +// textPart.setText("This is text"); +// multipart.addBodyPart(textPart); +// multipart.addBodyPart(attachmentPart); +// +// } catch (IOException e) { +// +// e.printStackTrace(); +// +// } +// +// message.setContent(multipart); + + System.out.println("sending..."); + // Send message + Transport.send(message); + System.out.println("Sent message successfully...."); + } catch (MessagingException mex) { + mex.printStackTrace(); + } +// +// try { +// // Create a default MimeMessage object. +// MimeMessage message = new MimeMessage(session); +// +// // Set From: header field of the header. +// message.setFrom(new InternetAddress(from)); +// +// // Set To: header field of the header. +// message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); +// +// // Set Subject: header field +// message.setSubject("Enrollment Guide Triggered " + ++count); +// +// // Now set the actual message +// message.setText("enrolmentGuide"); +// +// System.out.println("sending..."); +// // Send message +// Transport.send(message); +// System.out.println("Sent message successfully...."); +// } catch (MessagingException mex) { +// mex.printStackTrace(); +// } + } + @Override public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException { From 79a2d573fac310cef20301f7bbf297f177123717 Mon Sep 17 00:00:00 2001 From: osh Date: Thu, 9 Mar 2023 11:58:09 +0530 Subject: [PATCH 2/9] Fix build failure fixes https://roadmap.entgra.net/issues/9939 --- .../service/api/DeviceManagementService.java | 3 +- .../impl/DeviceManagementServiceImpl.java | 22 +--- .../DeviceManagementProviderService.java | 4 +- .../DeviceManagementProviderServiceImpl.java | 119 +----------------- 4 files changed, 8 insertions(+), 140 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index f4eb04cb42..2b9a2254f2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -67,7 +67,6 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationRequest; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationStatusBean; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; -import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import javax.validation.constraints.Size; @@ -855,7 +854,7 @@ public interface DeviceManagementService { name = "enrolmentGuide", value = "The details of the enrolment path suggested.", required = true) - MultipartFile enrolmentGuide); + String enrolmentGuide); @POST @Produces(MediaType.APPLICATION_JSON) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 78c2280d4e..80e70aaeff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -132,7 +132,6 @@ import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; -import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import javax.ws.rs.Consumes; @@ -823,37 +822,20 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @POST @Path("/enrollment/guide") @Override - public Response sendEnrollmentGuide(MultipartFile enrolmentGuide) { + public Response sendEnrollmentGuide(String enrolmentGuide) { if (log.isDebugEnabled()) { log.debug("Sending enrollment invitation mail to existing user."); } DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); try { -// Set recipients = new HashSet<>(); -// recipients.add(email); -// Properties props = new Properties(); -// props.setProperty("enrolment-guide", enrolmentGuide); -// -// EmailMetaInfo metaInfo = new EmailMetaInfo(recipients, props); -// dms.sendEnrolmentInvitation("enrolment-guide-template", metaInfo); dms.sendEnrolmentGuide(enrolmentGuide); return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build(); } catch (DeviceManagementException e) { - String msg = "Error occurred while inviting user to enrol their device"; + String msg = "Error occurred sending mail to group in enrollment guide"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); - } catch (ConfigurationManagementException e) { - String msg = "Error occurred while sending the email invitations. Mail server not configured."; - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); - } catch (IOException e) { - e.printStackTrace(); - String msg = "testtttt."; - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } -// return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 2be028d0a2..4ea3cd30de 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -66,8 +66,8 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster; -import org.springframework.web.multipart.MultipartFile; +import javax.mail.MessagingException; import java.io.IOException; import java.sql.SQLException; import java.sql.Timestamp; @@ -647,7 +647,7 @@ public interface DeviceManagementProviderService { void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException; - void sendEnrolmentGuide(MultipartFile enrolmentGuide) throws DeviceManagementException, ConfigurationManagementException, IOException; + void sendEnrolmentGuide(String enrolmentGuide) throws DeviceManagementException; void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 16b562b35b..8e38720f40 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -161,7 +161,6 @@ import org.wso2.carbon.stratos.common.beans.TenantInfoBean; import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import org.springframework.web.multipart.MultipartFile; import javax.mail.*; import javax.xml.bind.JAXBContext; @@ -1582,52 +1581,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void sendEnrolmentGuide(MultipartFile enrolmentGuide) throws DeviceManagementException, ConfigurationManagementException, IOException { - -// String fileName = ""; -// MultivaluedMap multivaluedMap = null; -// String fileName = null; -// InputStream inputStream = null; -// String uploadFilePath = null; -// -// Map> map = enrolmentGuide.getFormDataMap(); -// List inputParts = map.get("file"); -// -// for (InputPart inputPart : inputParts) { -// -// try { -// -// multivaluedMap = inputPart.getHeaders(); -// fileName = getFileName(multivaluedMap); -// -// //convert the uploaded file to inputstream -// InputStream inputStream = inputPart.getBody(InputStream.class,null); -// -// byte [] bytes = IOUtils.toByteArray(inputStream); -// -// //constructs upload file path -// fileName = UPLOADED_FILE_PATH + fileName; -// -// writeFile(bytes,fileName); -// -// System.out.println("Done"); -// -// } catch (IOException e) { -// e.printStackTrace(); -// } -// -// } - - // Recipient's email ID needs to be mentioned. + public void sendEnrolmentGuide(String enrolmentGuide) { -// String charset = enrolmentGuide.getFormDataMap().get("_charset_").get(0).getBodyAsString(); -// File file = (File) enrolmentGuide.getFormDataMap().get("file").get(0); String to = "oshani@entgra.io"; // Sender's email ID needs to be mentioned String from = "oshsilva1996@gmail.com"; - // Assuming you are sending email from through gmails smtp String host = "smtp.gmail.com"; // Get system properties @@ -1639,13 +1599,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.auth", "true"); - // Get the Session object.// and pass username and password + // Get the Session object Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("oshsilva1996@gmail.com", "layzvxhcxlwzkgwf"); - } }); @@ -1654,86 +1612,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv session.setDebug(true); try { - // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); - - // Set From: header field of the header. message.setFrom(new InternetAddress(from)); - - // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); - -// // Set Subject: header field message.setSubject("Enrollment Guide Triggered " + ++count); - -// message.setText(enrolmentGuide); - -// Multipart multipart = new MimeMultipart(); -// -// MimeBodyPart attachmentPart = new MimeBodyPart(); -// -// MimeBodyPart textPart = new MimeBodyPart(); -// -// try { -// -//// String charset = enrolmentGuide.getFormDataMap().get("_charset_").get(0).getBodyAsString(); -//// InputPart file = enrolmentGuide.getFormDataMap().get("file").get(0); -//// InputStream inputStream = file.getBody(InputStream.class, null); -//// BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset)); -//// String line; -//// StringBuilder content = new StringBuilder(); -//// while ((line = br.readLine()) != null) { -//// content.append(line); -//// } -//// -//// writeFile(content, "test"); -//// -//// File f =new File("H:\\pepipost_tutorials\\javaemail1.PNG"); -// -// attachmentPart.attachFile(file); -// textPart.setText("This is text"); -// multipart.addBodyPart(textPart); -// multipart.addBodyPart(attachmentPart); -// -// } catch (IOException e) { -// -// e.printStackTrace(); -// -// } -// -// message.setContent(multipart); - - System.out.println("sending..."); - // Send message + message.setText(enrolmentGuide); Transport.send(message); - System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } -// -// try { -// // Create a default MimeMessage object. -// MimeMessage message = new MimeMessage(session); -// -// // Set From: header field of the header. -// message.setFrom(new InternetAddress(from)); -// -// // Set To: header field of the header. -// message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); -// -// // Set Subject: header field -// message.setSubject("Enrollment Guide Triggered " + ++count); -// -// // Now set the actual message -// message.setText("enrolmentGuide"); -// -// System.out.println("sending..."); -// // Send message -// Transport.send(message); -// System.out.println("Sent message successfully...."); -// } catch (MessagingException mex) { -// mex.printStackTrace(); -// } } @Override From 0bbcac4a93f284f8c5e18b6fbb64d9fc9e5e3c68 Mon Sep 17 00:00:00 2001 From: osh Date: Fri, 10 Mar 2023 09:11:40 +0530 Subject: [PATCH 3/9] Add enrollment guide permissions fixes https://roadmap.entgra.net/issues/9939 --- .../mgt/jaxrs/service/api/DeviceManagementService.java | 9 ++++++++- .../src/main/resources/conf/mdm-ui-config.xml | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 2b9a2254f2..874b46db3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -189,6 +189,13 @@ import java.util.List; roles = {"Internal/devicemgt-user"}, permissions = {"/device-mgt/devices/owning-device/add"} ), + @Scope( + name = "Viewing Enrollment Guide", + description = "Show enrollment guide to users", + key = "perm:devices:enrollment-guide:view", + roles = {"Internal/devicemgt-user"}, + permissions = {"/device-mgt/devices/enrollment-guide/view"} + ), } ) @Path("/devices") @@ -816,7 +823,7 @@ public interface DeviceManagementService { tags = "Device Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:enrollment-guide:view") }) } ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index d36e26a3cc..64010515b0 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -100,6 +100,7 @@ perm:devices:details perm:devices:update perm:devices:view + perm:devices:enrollment-guide:view perm:view-configuration perm:manage-configuration perm:policies:remove From 5dab9433a44a458a0d3e985346e14d4f8c18cca7 Mon Sep 17 00:00:00 2001 From: osh Date: Fri, 17 Mar 2023 03:34:18 +0530 Subject: [PATCH 4/9] Update email subject fixes https://roadmap.entgra.net/issues/9939 --- .../DeviceManagementProviderServiceImpl.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 8e38720f40..ded3d067fe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1581,16 +1581,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void sendEnrolmentGuide(String enrolmentGuide) { + public void sendEnrolmentGuide(String enrolmentGuide) throws DeviceManagementException { String to = "oshani@entgra.io"; - - // Sender's email ID needs to be mentioned String from = "oshsilva1996@gmail.com"; - String host = "smtp.gmail.com"; - - // Get system properties Properties properties = System.getProperties(); // Setup mail server @@ -1599,7 +1594,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.auth", "true"); - // Get the Session object Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { @@ -1615,11 +1609,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); - message.setSubject("Enrollment Guide Triggered " + ++count); + message.setSubject("[Enrollment Guide Triggered] (#" + ++count + ")"); message.setText(enrolmentGuide); Transport.send(message); - } catch (MessagingException mex) { - mex.printStackTrace(); + } catch (MessagingException e) { + String msg = "Error occurred while sending message to support dev group"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } From f89e71d4bf2a730f181a179c571239507b0c3299 Mon Sep 17 00:00:00 2001 From: osh Date: Mon, 27 Mar 2023 19:46:51 +0530 Subject: [PATCH 5/9] Reuse email implementation fixes http://roadmap.entgra.net/issues/9939 --- .../mgt/core/DeviceManagementConstants.java | 1 + .../DeviceManagementProviderServiceImpl.java | 39 +++++-------------- .../email/templates/enrollment-guide.vm | 34 ++++++++++++++++ 3 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 features/transport-mgt/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/enrollment-guide.vm diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 9ba76f99fb..0d45a2efc1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -136,6 +136,7 @@ public final class DeviceManagementConstants { public static final String POLICY_VIOLATE_TEMPLATE = "policy-violating-notifier"; 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 class OperationAttributes { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index ded3d067fe..2b5c0dffae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1583,38 +1583,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendEnrolmentGuide(String enrolmentGuide) throws DeviceManagementException { - String to = "oshani@entgra.io"; - String from = "oshsilva1996@gmail.com"; - String host = "smtp.gmail.com"; - Properties properties = System.getProperties(); - - // Setup mail server - properties.put("mail.smtp.host", host); - properties.put("mail.smtp.port", "465"); - properties.put("mail.smtp.ssl.enable", "true"); - properties.put("mail.smtp.auth", "true"); - - Session session = Session.getInstance(properties, new javax.mail.Authenticator() { - - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("oshsilva1996@gmail.com", "layzvxhcxlwzkgwf"); - } - - }); - - // Used to debug SMTP issues - session.setDebug(true); + Properties props = new Properties(); + props.setProperty("mail-subject", "[Enrollment Guide Triggered] (#" + ++count + ")"); + props.setProperty("enrollment-guide", enrolmentGuide); try { - MimeMessage message = new MimeMessage(session); - message.setFrom(new InternetAddress(from)); - message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); - message.setSubject("[Enrollment Guide Triggered] (#" + ++count + ")"); - message.setText(enrolmentGuide); - Transport.send(message); - } catch (MessagingException e) { - String msg = "Error occurred while sending message to support dev group"; - log.error(msg, e); + EmailMetaInfo metaInfo = new EmailMetaInfo("support-dev-group@entgra.io", props); + sendEnrolmentInvitation(DeviceManagementConstants.EmailAttributes.ENROLLMENT_GUIDE_TEMPLATE, metaInfo); + + } catch (ConfigurationManagementException e) { + String msg = "Error occurred while sending the mail."; + log.error(msg); throw new DeviceManagementException(msg, e); } } diff --git a/features/transport-mgt/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/enrollment-guide.vm b/features/transport-mgt/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/enrollment-guide.vm new file mode 100644 index 0000000000..f4f16cc8d7 --- /dev/null +++ b/features/transport-mgt/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/enrollment-guide.vm @@ -0,0 +1,34 @@ +#* + Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + + WSO2 Inc. 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. +*# + + $mail-subject + + + + Enrollment Questions and Answers + + +
+ $enrollment-guide +
+ + + ]]> + +
From 0f28d318ed972cbcbc806706faaf436d9057b56b Mon Sep 17 00:00:00 2001 From: osh Date: Mon, 27 Mar 2023 20:09:04 +0530 Subject: [PATCH 6/9] Remove unwanted code fixes http://roadmap.entgra.net/issues/9939 --- .../service/api/DeviceManagementService.java | 1 - .../impl/DeviceManagementServiceImpl.java | 1 - .../DeviceManagementProviderService.java | 2 -- .../DeviceManagementProviderServiceImpl.java | 28 ++----------------- 4 files changed, 2 insertions(+), 30 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 66867545b2..df9cbfaae1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -54,7 +54,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; -import org.wso2.carbon.device.mgt.common.geo.service.Alert; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 813222a1b1..83081b5229 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -61,7 +61,6 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; -import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 4ea3cd30de..3dedb8d551 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -67,8 +67,6 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster; -import javax.mail.MessagingException; -import java.io.IOException; import java.sql.SQLException; import java.sql.Timestamp; import java.util.Collection; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 2b5c0dffae..f6b9274ac7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -38,7 +38,6 @@ package org.wso2.carbon.device.mgt.core.service; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import org.apache.commons.collections.map.SingletonMap; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -162,11 +161,11 @@ import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import javax.mail.*; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import java.io.*; +import java.io.IOException; +import java.io.StringWriter; import java.lang.reflect.Type; import java.sql.SQLException; import java.sql.Timestamp; @@ -186,12 +185,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMultipart; -import javax.ws.rs.core.MultivaluedMap; - public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -1563,23 +1556,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - // save to somewhere - private void writeFile(byte[] content, String filename) throws IOException { - - File file = new File(filename); - - if (!file.exists()) { - file.createNewFile(); - } - - FileOutputStream fop = new FileOutputStream(file); - - fop.write(content); - fop.flush(); - fop.close(); - - } - @Override public void sendEnrolmentGuide(String enrolmentGuide) throws DeviceManagementException { From d2bc4632842871a58eaf5f689b4018fd24614e97 Mon Sep 17 00:00:00 2001 From: osh Date: Tue, 28 Mar 2023 12:34:11 +0530 Subject: [PATCH 7/9] Resolve comments fixes http://roadmap.entgra.net/issues/9939 --- .../device/mgt/core/config/DeviceManagementConfig.java | 10 ++++++++++ .../service/DeviceManagementProviderServiceImpl.java | 7 ++++--- .../src/main/resources/conf/cdm-config.xml | 1 + .../templates/repository/conf/cdm-config.xml.j2 | 9 +++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index f2ad40abb7..052bb87886 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -70,6 +70,7 @@ public final class DeviceManagementConfig { private EnrollmentNotificationConfiguration enrollmentNotificationConfiguration; private DefaultRoles defaultRoles; private OperationTimeoutConfiguration operationTimeoutConfiguration; + private String enrollmentGuideMail; @XmlElement(name = "ManagementRepository", required = true) public DeviceManagementConfigRepository getDeviceManagementConfigRepository() { @@ -265,5 +266,14 @@ public final class DeviceManagementConfig { public void setOperationTimeoutConfiguration(OperationTimeoutConfiguration operationTimeoutConfiguration) { this.operationTimeoutConfiguration = operationTimeoutConfiguration; } + + @XmlElement(name = "EnrollmentGuideMail", required = true) + public String getEnrollmentGuideMail() { + return enrollmentGuideMail; + } + + public void setEnrollmentGuideMail(String enrollmentGuideMail) { + this.enrollmentGuideMail = enrollmentGuideMail; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index f6b9274ac7..2ae6e1f7c5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1559,17 +1559,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendEnrolmentGuide(String enrolmentGuide) throws DeviceManagementException { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + String recipientMail = config.getEnrollmentGuideMail(); Properties props = new Properties(); props.setProperty("mail-subject", "[Enrollment Guide Triggered] (#" + ++count + ")"); props.setProperty("enrollment-guide", enrolmentGuide); try { - EmailMetaInfo metaInfo = new EmailMetaInfo("support-dev-group@entgra.io", props); + EmailMetaInfo metaInfo = new EmailMetaInfo(recipientMail, props); sendEnrolmentInvitation(DeviceManagementConstants.EmailAttributes.ENROLLMENT_GUIDE_TEMPLATE, metaInfo); - } catch (ConfigurationManagementException e) { String msg = "Error occurred while sending the mail."; - log.error(msg); + log.error(msg, e); throw new DeviceManagementException(msg, e); } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml index 340b22802b..9598f16000 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml @@ -187,5 +187,6 @@ + support-dev-group@entgra.io diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 index f567bb9dd2..42077b0afd 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 @@ -322,7 +322,7 @@ - + @@ -350,6 +350,11 @@ {% endfor %} {% endif%} - + + {% if device_mgt_conf.enrollment_guide_mail is defined %} + {{device_mgt_conf.enrollment_guide_mail}} + {% else %} + support-dev-group@entgra.io + {% endif %} From 7747d7b42585634943a0aae138e2dc10e21484db Mon Sep 17 00:00:00 2001 From: osh Date: Tue, 28 Mar 2023 16:40:10 +0530 Subject: [PATCH 8/9] Update status history path --- .../device/mgt/jaxrs/service/api/DeviceManagementService.java | 4 ++-- .../mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index df9cbfaae1..a080208de2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -2011,7 +2011,7 @@ public interface DeviceManagementService { @GET @Produces(MediaType.APPLICATION_JSON) - @Path("/{type}/{id}/getstatushistory") + @Path("/{type}/{id}/status-history") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -2079,7 +2079,7 @@ public interface DeviceManagementService { @GET @Produces(MediaType.APPLICATION_JSON) - @Path("/{type}/{id}/getenrolmentstatushistory") + @Path("/{type}/{id}/enrolment-status-history") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 83081b5229..a938a71860 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -1344,7 +1344,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { * @return {@link Response} object */ @GET - @Path("/{type}/{id}/getstatushistory") + @Path("/{type}/{id}/status-history") public Response getDeviceStatusHistory(@PathParam("type") @Size(max = 45) String type, @PathParam("id") @Size(max = 45) String id) { //TODO check authorization for this @@ -1376,7 +1376,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { * @return {@link Response} object */ @GET - @Path("/{type}/{id}/getenrolmentstatushistory") + @Path("/{type}/{id}/enrolment-status-history") public Response getCurrentEnrolmentDeviceStatusHistory(@PathParam("type") @Size(max = 45) String type, @PathParam("id") @Size(max = 45) String id) { //TODO check authorization for this or current enrolment should be based on for the enrolment associated with the user From 5275bb180eb68c9209dcca9c4a572189cdfc48ae Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 29 Mar 2023 07:59:25 +0530 Subject: [PATCH 9/9] Change config implementation fixes https://roadmap.entgra.net/issues/9939 --- .../impl/DeviceManagementServiceImpl.java | 8 +++++ .../core/config/DeviceManagementConfig.java | 13 ++++---- .../guide/EnrollmentGuideConfiguration.java | 30 +++++++++++++++++++ .../DeviceManagementProviderServiceImpl.java | 2 +- .../src/main/resources/conf/cdm-config.xml | 5 +++- .../repository/conf/cdm-config.xml.j2 | 14 +++++---- 6 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/enrollment/guide/EnrollmentGuideConfiguration.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index a938a71860..206ed55be9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -80,6 +80,8 @@ import org.wso2.carbon.device.mgt.common.search.PropertyMap; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceStatus; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; @@ -791,6 +793,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (log.isDebugEnabled()) { log.debug("Sending enrollment invitation mail to existing user."); } + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + if (!config.getEnrollmentGuideConfiguration().isEnabled()) { + String msg = "Sending enrollment guide config is not enabled."; + log.error(msg); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + } DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); try { dms.sendEnrolmentGuide(enrolmentGuide); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index 052bb87886..cc3a3dc5b4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.core.config.cache.BillingCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.CertificateCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.GeoFenceCacheConfiguration; +import org.wso2.carbon.device.mgt.core.config.enrollment.guide.EnrollmentGuideConfiguration; import org.wso2.carbon.device.mgt.core.config.operation.timeout.OperationTimeoutConfiguration; import org.wso2.carbon.device.mgt.core.event.config.EventOperationTaskConfiguration; import org.wso2.carbon.device.mgt.core.config.geo.location.GeoLocationConfiguration; @@ -70,7 +71,7 @@ public final class DeviceManagementConfig { private EnrollmentNotificationConfiguration enrollmentNotificationConfiguration; private DefaultRoles defaultRoles; private OperationTimeoutConfiguration operationTimeoutConfiguration; - private String enrollmentGuideMail; + private EnrollmentGuideConfiguration enrollmentGuideConfiguration; @XmlElement(name = "ManagementRepository", required = true) public DeviceManagementConfigRepository getDeviceManagementConfigRepository() { @@ -267,13 +268,13 @@ public final class DeviceManagementConfig { this.operationTimeoutConfiguration = operationTimeoutConfiguration; } - @XmlElement(name = "EnrollmentGuideMail", required = true) - public String getEnrollmentGuideMail() { - return enrollmentGuideMail; + @XmlElement(name = "EnrollmentGuideConfiguration", required = true) + public EnrollmentGuideConfiguration getEnrollmentGuideConfiguration() { + return enrollmentGuideConfiguration; } - public void setEnrollmentGuideMail(String enrollmentGuideMail) { - this.enrollmentGuideMail = enrollmentGuideMail; + public void setEnrollmentGuideConfiguration(EnrollmentGuideConfiguration enrollmentGuideConfiguration) { + this.enrollmentGuideConfiguration = enrollmentGuideConfiguration; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/enrollment/guide/EnrollmentGuideConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/enrollment/guide/EnrollmentGuideConfiguration.java new file mode 100644 index 0000000000..5f54cf79d4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/enrollment/guide/EnrollmentGuideConfiguration.java @@ -0,0 +1,30 @@ +package org.wso2.carbon.device.mgt.core.config.enrollment.guide; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "EnrollmentGuideConfiguration") +public class EnrollmentGuideConfiguration { + + private boolean isEnabled; + private String mail; + + @XmlElement(name = "Enable", required = true) + public boolean isEnabled() { + return isEnabled; + } + + public void setEnabled(boolean enabled) { + isEnabled = enabled; + } + + @XmlElement(name = "Mail", required = true) + public String getMail() { + return mail; + } + + public void setMail(String mail) { + this.mail = mail; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 2ae6e1f7c5..6257e0b34d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1560,7 +1560,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public void sendEnrolmentGuide(String enrolmentGuide) throws DeviceManagementException { DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); - String recipientMail = config.getEnrollmentGuideMail(); + String recipientMail = config.getEnrollmentGuideConfiguration().getMail(); Properties props = new Properties(); props.setProperty("mail-subject", "[Enrollment Guide Triggered] (#" + ++count + ")"); props.setProperty("enrollment-guide", enrolmentGuide); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml index 9598f16000..f0ebf0de84 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml @@ -187,6 +187,9 @@ - support-dev-group@entgra.io + + false + Replace with mail + diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 index 42077b0afd..09e3fad19b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 @@ -351,10 +351,14 @@ {% endif%} - {% if device_mgt_conf.enrollment_guide_mail is defined %} - {{device_mgt_conf.enrollment_guide_mail}} - {% else %} - support-dev-group@entgra.io - {% endif %} + + {% if device_mgt_conf.enrollment_guide_conf is defined %} + {{device_mgt_conf.enrollment_guide_conf.enable}} + {{device_mgt_conf.enrollment_guide_conf.mail}} + {% else %} + false + Replace with mail + {% endif %} +