Search in sources :

Example 1 with TranslatableExceptionI

use of com.infiniteautomation.mango.util.exception.TranslatableExceptionI in project ma-modules-public by infiniteautomation.

the class UserRestController method approveUsers.

@ApiOperation(value = "Approve publicly registered User(s)", notes = "Superadmin permission required", response = UserModel.class)
@RequestMapping(method = RequestMethod.POST, value = "/approve-users")
@PreAuthorize("isAdmin()")
public ApprovedUsersModel approveUsers(@RequestBody() ApproveUsersModel model) {
    ApprovedUsersModel result = new ApprovedUsersModel();
    for (String username : model.getUsernames()) {
        try {
            User approved = service.approveUser(username, model.isSendEmail());
            result.addApproved(approved.getUsername());
        } catch (Exception e) {
            if (e instanceof TranslatableExceptionI) {
                result.addFailedApproval(username, ((TranslatableExceptionI) e).getTranslatableMessage());
            } else {
                result.addFailedApproval(username, new TranslatableMessage("common.default", e.getMessage()));
            }
        }
    }
    return result;
}
Also used : ApprovedUsersModel(com.infiniteautomation.mango.rest.latest.model.user.ApprovedUsersModel) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) TranslatableExceptionI(com.infiniteautomation.mango.util.exception.TranslatableExceptionI) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)1 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)1 ApprovedUsersModel (com.infiniteautomation.mango.rest.latest.model.user.ApprovedUsersModel)1 TranslatableExceptionI (com.infiniteautomation.mango.util.exception.TranslatableExceptionI)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 User (com.serotonin.m2m2.vo.User)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1