Search in sources :

Example 1 with AuthenticationException

use of org.openlmis.stockmanagement.exception.AuthenticationException in project openlmis-stockmanagement by OpenLMIS.

the class AuthenticationHelper method getCurrentUser.

/**
 * Method returns current user based on Spring context
 * and fetches his data from reference-data service.
 *
 * @return UserDto entity of current user.
 * @throws AuthenticationException if user cannot be found.
 */
public UserDto getCurrentUser() {
    UUID userId = (UUID) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserDto user = userReferenceDataService.findOne(userId);
    if (user == null) {
        throw new AuthenticationException(new Message(ERROR_USER_NOT_FOUND, userId));
    }
    return user;
}
Also used : AuthenticationException(org.openlmis.stockmanagement.exception.AuthenticationException) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) UUID(java.util.UUID)

Example 2 with AuthenticationException

use of org.openlmis.stockmanagement.exception.AuthenticationException in project openlmis-stockmanagement by OpenLMIS.

the class StockCardTemplatesControllerIntegrationTest method shouldReturn401WhenUserUnauthorized.

@Test
public void shouldReturn401WhenUserUnauthorized() throws Exception {
    // given
    Mockito.doThrow(new AuthenticationException("MANAGE_STOCK_CARD_TEMPLATES")).when(permissionService).canCreateStockCardTemplate();
    // when
    ResultActions resultActions = mvc.perform(post(STOCK_CARD_TEMPLATE_API).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(new StockCardTemplate())));
    // then
    resultActions.andExpect(status().isUnauthorized());
}
Also used : StockCardTemplate(org.openlmis.stockmanagement.domain.template.StockCardTemplate) AuthenticationException(org.openlmis.stockmanagement.exception.AuthenticationException) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test)

Aggregations

AuthenticationException (org.openlmis.stockmanagement.exception.AuthenticationException)2 UUID (java.util.UUID)1 Test (org.junit.Test)1 StockCardTemplate (org.openlmis.stockmanagement.domain.template.StockCardTemplate)1 UserDto (org.openlmis.stockmanagement.dto.referencedata.UserDto)1 ResultActions (org.springframework.test.web.servlet.ResultActions)1