Search in sources :

Example 16 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project identity-outbound-auth-sms-otp by wso2-extensions.

the class SMSOTPUtils method getMobileNumberForUsername.

/**
 * Get the mobile number for Username.
 *
 * @param username the username
 * @return mobile number
 * @throws SMSOTPException
 */
public static String getMobileNumberForUsername(String username) throws SMSOTPException, AuthenticationFailedException {
    UserRealm userRealm;
    String mobile;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(username);
        String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username);
        userRealm = getUserRealm(tenantDomain);
        if (userRealm != null) {
            mobile = userRealm.getUserStoreManager().getUserClaimValue(tenantAwareUsername, SMSOTPConstants.MOBILE_CLAIM, null);
        } else {
            throw new SMSOTPException("Cannot find the user realm for the given tenant domain : " + tenantDomain);
        }
    } catch (UserStoreException e) {
        throw new SMSOTPException("Cannot find the user " + username + " to get the mobile number ", e);
    }
    return mobile;
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) UserStoreException(org.wso2.carbon.user.api.UserStoreException) SMSOTPException(org.wso2.carbon.identity.authenticator.smsotp.exception.SMSOTPException)

Example 17 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project identity-outbound-auth-sms-otp by wso2-extensions.

the class SMSOTPUtils method verifyUserExists.

/**
 * Verify whether user Exist in the user store or not.
 *
 * @param username the Username
 * @throws SMSOTPException
 */
public static void verifyUserExists(String username, String tenantDomain) throws SMSOTPException, AuthenticationFailedException {
    UserRealm userRealm;
    boolean isUserExist = false;
    try {
        userRealm = SMSOTPUtils.getUserRealm(tenantDomain);
        if (userRealm == null) {
            throw new SMSOTPException("Super tenant realm not loaded.");
        }
        UserStoreManager userStoreManager = userRealm.getUserStoreManager();
        if (userStoreManager.isExistingUser(username)) {
            isUserExist = true;
        }
    } catch (UserStoreException e) {
        throw new SMSOTPException("Error while validating the user.", e);
    }
    if (!isUserExist) {
        if (log.isDebugEnabled()) {
            log.debug("User does not exist in the User Store");
        }
        throw new SMSOTPException("User does not exist in the User Store.");
    }
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) UserStoreException(org.wso2.carbon.user.api.UserStoreException) SMSOTPException(org.wso2.carbon.identity.authenticator.smsotp.exception.SMSOTPException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Example 18 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project product-iots by wso2.

the class DeviceTypeServiceImpl method downloadSketch.

/**
 * To download device type agent source code as zip file.
 *
 * @param deviceName name for the device type instance
 * @param sketchType folder name where device type agent was installed into server
 * @return Agent source code as zip file
 */
@Path("/device/download")
@GET
@Produces("application/zip")
public Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) {
    try {
        ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
        Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
        response.status(Response.Status.OK);
        response.type("application/zip");
        response.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
        Response resp = response.build();
        zipFile.getZipFile().delete();
        return resp;
    } catch (IllegalArgumentException ex) {
        // bad request
        return Response.status(400).entity(ex.getMessage()).build();
    } catch (DeviceManagementException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (JWTClientException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (APIManagerException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (UserStoreException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) APIManagerException(org.wso2.carbon.apimgt.application.extension.exception.APIManagerException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ZipArchive(org.wso2.iot.sampledevice.api.util.ZipArchive) IOException(java.io.IOException) JWTClientException(org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project carbon-business-process by wso2.

the class UserSubstitutionService method getSubstitute.

/**
 * Return the substitute info for the given user in path parameter
 * @param user
 * @return SubstituteInfoResponse
 * @throws URISyntaxException
 */
@GET
@Path("/{user}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getSubstitute(@PathParam("user") String user) throws UserStoreException {
    if (!subsFeatureEnabled) {
        return Response.status(405).build();
    }
    user = getTenantAwareUser(user);
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    String loggedInUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
    if (!loggedInUser.equals(user) && !isUserAuthorizedForSubstitute(loggedInUser)) {
        throw new BPMNForbiddenException("Not allowed to view others substitution details. No sufficient permission");
    }
    SubstitutesDataModel model = UserSubstitutionUtils.getSubstituteOfUser(user, tenantId);
    if (model != null) {
        SubstituteInfoResponse response = new SubstituteInfoResponse();
        response.setSubstitute(model.getSubstitute());
        response.setAssignee(model.getUser());
        response.setEnabled(model.isEnabled());
        response.setStartTime(model.getSubstitutionStart());
        response.setEndTime(model.getSubstitutionEnd());
        return Response.ok(response).build();
    } else {
        return Response.status(404).build();
    }
}
Also used : SubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel) BPMNForbiddenException(org.wso2.carbon.bpmn.rest.common.exception.BPMNForbiddenException)

Example 20 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project carbon-business-process by wso2.

the class UserSubstitutionService method substitute.

/**
 * Add new addSubstituteInfo record.
 * Following request body parameters are required,
 *  assignee : optional, logged in user is used if not provided
 *  substitute : required
 *  startTime : optional, current timestamp if not provided, the timestamp the substitution should start in ISO format
 *  endTime : optional, considered as forever if not provided, the timestamp the substitution should end in ISO format
 * @param request
 * @return 201 created response with the resource location. 405 if substitution disabled
 */
@POST
@Path("/")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response substitute(SubstitutionRequest request) {
    try {
        if (!subsFeatureEnabled) {
            return Response.status(405).build();
        }
        String assignee = getRequestedAssignee(request.getAssignee());
        String substitute = validateAndGetSubstitute(request.getSubstitute(), assignee);
        Date endTime = null;
        Date startTime = new Date();
        DateTime requestStartTime = null;
        if (request.getStartTime() != null) {
            requestStartTime = new DateTime(request.getStartTime());
            startTime = new Date(requestStartTime.getMillis());
        }
        if (request.getEndTime() != null) {
            endTime = validateEndTime(request.getEndTime(), requestStartTime);
        }
        if (!UserSubstitutionUtils.validateTasksList(request.getTaskList(), assignee)) {
            throw new ActivitiIllegalArgumentException("Invalid task list provided, for substitution.");
        }
        int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        // at this point, substitution is enabled by default
        UserSubstitutionUtils.handleNewSubstituteAddition(assignee, substitute, startTime, endTime, true, request.getTaskList(), tenantId);
        return Response.created(new URI("substitutes/" + assignee)).build();
    } catch (UserStoreException e) {
        throw new ActivitiException("Error accessing User Store", e);
    } catch (URISyntaxException e) {
        throw new ActivitiException("Response location URI creation header", e);
    } catch (ActivitiIllegalArgumentException e) {
        throw new ActivitiIllegalArgumentException(e.getMessage());
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DateTime(org.joda.time.DateTime)

Aggregations

UserStoreException (org.wso2.carbon.user.api.UserStoreException)21 UserRealm (org.wso2.carbon.user.api.UserRealm)10 AuthenticationFailedException (org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException)6 RealmService (org.wso2.carbon.user.core.service.RealmService)6 ArrayList (java.util.ArrayList)5 SMSOTPException (org.wso2.carbon.identity.authenticator.smsotp.exception.SMSOTPException)5 ActivitiException (org.activiti.engine.ActivitiException)4 HumanTaskRuntimeException (org.wso2.carbon.humantask.core.engine.runtime.api.HumanTaskRuntimeException)4 UserRealm (org.wso2.carbon.user.core.UserRealm)4 UserStoreException (org.wso2.carbon.user.core.UserStoreException)4 IOException (java.io.IOException)3 List (java.util.List)3 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)3 BPMNAuthenticationException (org.wso2.carbon.bpmn.core.exception.BPMNAuthenticationException)3 BPMNForbiddenException (org.wso2.carbon.bpmn.rest.common.exception.BPMNForbiddenException)3 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2