mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Modify send-invitation API to send enrollment steps and OTP token
This commit is contained in:
parent
58217ac77a
commit
d97de23ece
@ -49,6 +49,7 @@ import io.swagger.annotations.ResponseHeader;
|
|||||||
import org.apache.axis2.transport.http.HTTPConstants;
|
import org.apache.axis2.transport.http.HTTPConstants;
|
||||||
import org.wso2.carbon.apimgt.annotations.api.Scopes;
|
import org.wso2.carbon.apimgt.annotations.api.Scopes;
|
||||||
import org.wso2.carbon.apimgt.annotations.api.Scope;
|
import org.wso2.carbon.apimgt.annotations.api.Scope;
|
||||||
|
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation;
|
||||||
import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList;
|
import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList;
|
||||||
import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfo;
|
import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfo;
|
||||||
import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfoList;
|
import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfoList;
|
||||||
@ -74,7 +75,6 @@ import javax.ws.rs.Produces;
|
|||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@SwaggerDefinition(
|
@SwaggerDefinition(
|
||||||
info = @Info(
|
info = @Info(
|
||||||
@ -893,7 +893,8 @@ public interface UserManagementService {
|
|||||||
@ApiParam(
|
@ApiParam(
|
||||||
name = "users",
|
name = "users",
|
||||||
value = "List of users",
|
value = "List of users",
|
||||||
required = true) List<String> usernames);
|
required = true)
|
||||||
|
@Valid DeviceEnrollmentInvitation deviceEnrollmentInvitation);
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/enrollment-invite")
|
@Path("/enrollment-invite")
|
||||||
|
|||||||
@ -45,8 +45,11 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|||||||
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
||||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.exceptions.OTPManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation;
|
||||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
||||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.OTPManagementService;
|
||||||
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
|
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
|
||||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
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.EmailMetaInfo;
|
||||||
@ -716,48 +719,29 @@ public class UserManagementServiceImpl implements UserManagementService {
|
|||||||
return CredentialManagementResponseBuilder.buildChangePasswordResponse(credentials);
|
return CredentialManagementResponseBuilder.buildChangePasswordResponse(credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method used to send an invitation email to a existing user to enroll a device.
|
|
||||||
*
|
|
||||||
* @param usernames Username list of the users to be invited
|
|
||||||
*/
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/send-invitation")
|
@Path("/send-invitation")
|
||||||
@Produces({MediaType.APPLICATION_JSON})
|
@Produces({MediaType.APPLICATION_JSON})
|
||||||
public Response inviteExistingUsersToEnrollDevice(List<String> usernames) {
|
public Response inviteExistingUsersToEnrollDevice(DeviceEnrollmentInvitation deviceEnrollmentInvitation) {
|
||||||
if (log.isDebugEnabled()) {
|
if (deviceEnrollmentInvitation.getUsernames() == null || deviceEnrollmentInvitation.getUsernames().isEmpty()) {
|
||||||
log.debug("Sending enrollment invitation mail to existing user.");
|
String msg = "Error occurred while validating list of user-names. User-names cannot be empty.";
|
||||||
|
log.error(msg);
|
||||||
|
throw new BadRequestException(
|
||||||
|
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg)
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Sending device enrollment invitation mail to existing user/s.");
|
||||||
|
}
|
||||||
|
OTPManagementService oms = DeviceMgtAPIUtils.getOTPManagementService();
|
||||||
try {
|
try {
|
||||||
for (String username : usernames) {
|
oms.sendDeviceEnrollmentInvitationMail(deviceEnrollmentInvitation);
|
||||||
String recipient = getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS);
|
} catch (OTPManagementException e) {
|
||||||
|
String msg = "Error occurred while generating OTP and inviting user/s to enroll their device/s.";
|
||||||
Properties props = new Properties();
|
|
||||||
props.setProperty("first-name", getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME));
|
|
||||||
props.setProperty("username", username);
|
|
||||||
|
|
||||||
EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props);
|
|
||||||
dms.sendEnrolmentInvitation(DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE,
|
|
||||||
metaInfo);
|
|
||||||
}
|
|
||||||
} catch (DeviceManagementException e) {
|
|
||||||
String msg = "Error occurred while inviting user to enrol their device";
|
|
||||||
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
|
|
||||||
msg = e.getMessage();
|
|
||||||
}
|
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
return Response.serverError().entity(
|
return Response.serverError().entity(
|
||||||
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||||
} catch (UserStoreException e) {
|
|
||||||
String msg = "Error occurred while getting claim values to invite user";
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build();
|
return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,23 @@
|
|||||||
* KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.wso2.carbon.device.mgt.jaxrs.util;
|
package org.wso2.carbon.device.mgt.jaxrs.util;
|
||||||
@ -142,6 +159,7 @@ public class DeviceMgtAPIUtils {
|
|||||||
|
|
||||||
private static IntegrationClientService integrationClientService;
|
private static IntegrationClientService integrationClientService;
|
||||||
private static MetadataManagementService metadataManagementService;
|
private static MetadataManagementService metadataManagementService;
|
||||||
|
private static OTPManagementService otpManagementService;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
|
String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
|
||||||
@ -338,6 +356,29 @@ public class DeviceMgtAPIUtils {
|
|||||||
return integrationClientService;
|
return integrationClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializing and accessing method for OTPManagementService.
|
||||||
|
*
|
||||||
|
* @return OTPManagementService instance
|
||||||
|
* @throws IllegalStateException if OTPManagementService cannot be initialized
|
||||||
|
*/
|
||||||
|
public static synchronized OTPManagementService getOTPManagementService() {
|
||||||
|
if (otpManagementService == null) {
|
||||||
|
synchronized (DeviceMgtAPIUtils.class) {
|
||||||
|
if (otpManagementService == null) {
|
||||||
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||||
|
otpManagementService = (OTPManagementService) ctx.getOSGiService(OTPManagementService.class, null);
|
||||||
|
if (otpManagementService == null) {
|
||||||
|
String msg = "OTP Management service has not initialized.";
|
||||||
|
log.error(msg);
|
||||||
|
throw new IllegalStateException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return otpManagementService;
|
||||||
|
}
|
||||||
|
|
||||||
public static RegistryService getRegistryService() {
|
public static RegistryService getRegistryService() {
|
||||||
RegistryService registryService;
|
RegistryService registryService;
|
||||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||||
|
|||||||
@ -14,6 +14,23 @@
|
|||||||
* KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. 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.
|
||||||
*/
|
*/
|
||||||
package org.wso2.carbon.device.mgt.core.util;
|
package org.wso2.carbon.device.mgt.core.util;
|
||||||
|
|
||||||
@ -79,6 +96,7 @@ import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerSer
|
|||||||
import org.wso2.carbon.user.api.TenantManager;
|
import org.wso2.carbon.user.api.TenantManager;
|
||||||
import org.wso2.carbon.user.api.UserRealm;
|
import org.wso2.carbon.user.api.UserRealm;
|
||||||
import org.wso2.carbon.user.api.UserStoreException;
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreManager;
|
||||||
import org.wso2.carbon.utils.CarbonUtils;
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
import org.wso2.carbon.utils.ConfigurationContextService;
|
import org.wso2.carbon.utils.ConfigurationContextService;
|
||||||
import org.wso2.carbon.utils.NetworkUtils;
|
import org.wso2.carbon.utils.NetworkUtils;
|
||||||
@ -1047,4 +1065,17 @@ public final class DeviceManagerUtil {
|
|||||||
}
|
}
|
||||||
return roleList;
|
return roleList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the value of the user property from the user profile
|
||||||
|
* @param username of the user
|
||||||
|
* @param claimUri name of the claim
|
||||||
|
* @return value for the claim uri of user
|
||||||
|
* @throws UserStoreException when there is error in retrieving the user store manager
|
||||||
|
*/
|
||||||
|
public static String getUserClaimValue(String username, String claimUri) throws UserStoreException {
|
||||||
|
UserStoreManager userStoreManager = CarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||||
|
.getUserStoreManager();
|
||||||
|
return userStoreManager.getUserClaimValue(username, claimUri, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user