Search in sources :

Example 11 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthController method updateUser.

// Update a user
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/user", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateUser(@RequestBody WormholeUser user, HttpServletRequest request) {
    WormholeUserDetails userDetail = accessTokenService.getCurrentUser(request);
    Optional<WormholeUser> currentUserOptional = userRepository.findById(userDetail.getUserId());
    WormholeUser currentUser = currentUserOptional.get();
    WormholeUser old = null;
    if (currentUser.getUserName().equals(user.getUserName())) {
        old = currentUser;
    } else if (currentUser.getRoleNames().contains(FlowgateConstant.Role_admin)) {
        Optional<WormholeUser> oldUserOptional = userRepository.findById(user.getId());
        old = oldUserOptional.get();
    } else {
        throw new WormholeRequestException(HttpStatus.FORBIDDEN, "Forbidden", null);
    }
    if (old == null) {
        throw new WormholeRequestException(HttpStatus.NOT_FOUND, "User not found", null);
    }
    if (user.getPassword() != null && !"".equals(user.getPassword().trim())) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        old.setPassword(encoder.encode(user.getPassword()));
        old.setLastPasswordResetDate(new Date().getTime());
    }
    if (user.getEmailAddress() != null && !"".equals(user.getEmailAddress().trim())) {
        old.setEmailAddress(user.getEmailAddress());
    }
    if (user.getRoleNames() != null) {
        old.setRoleNames(user.getRoleNames());
    }
    userRepository.save(old);
}
Also used : WormholeRequestException(com.vmware.flowgate.exception.WormholeRequestException) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) Optional(java.util.Optional) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) Date(java.util.Date) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthController method readUserByName.

// Read a user by user name
@RequestMapping(value = "/user/username/{name}", method = RequestMethod.GET)
public WormholeUser readUserByName(@PathVariable(required = false) String name, HttpServletRequest request) {
    WormholeUserDetails userDetail = accessTokenService.getCurrentUser(request);
    Optional<WormholeUser> currentUserOptional = userRepository.findById(userDetail.getUserId());
    WormholeUser user = null;
    WormholeUser currentUser = currentUserOptional.get();
    if (currentUser.getRoleNames().contains(FlowgateConstant.Role_admin)) {
        user = userRepository.findOneByUserName(name);
    } else if (currentUser.getUserName().equals(name)) {
        user = currentUser;
    } else {
        throw new WormholeRequestException(HttpStatus.FORBIDDEN, "Forbidden", null);
    }
    if (user != null) {
        return DesensitizationUserData.desensitizationUser(user);
    }
    return user;
}
Also used : WormholeRequestException(com.vmware.flowgate.exception.WormholeRequestException) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method testCreateTokenUseAdapterServiceKey.

@Test
public void testCreateTokenUseAdapterServiceKey() throws Exception {
    WormholeUser user = null;
    expectedEx.expect(WormholeRequestException.class);
    expectedEx.expectMessage("Invalid username or password");
    MvcResult result = this.mockMvc.perform(post("/v1/auth/token").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(user))).andReturn();
    if (result.getResolvedException() != null) {
        throw result.getResolvedException();
    }
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 14 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method testCreateToken1.

@Test
public void testCreateToken1() throws Exception {
    WormholeUser user = null;
    expectedEx.expect(WormholeRequestException.class);
    expectedEx.expectMessage("Invalid username or password");
    MvcResult result = this.mockMvc.perform(post("/v1/auth/token").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(user))).andReturn();
    if (result.getResolvedException() != null) {
        throw result.getResolvedException();
    }
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 15 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method updateUserExample1.

@Test
public void updateUserExample1() throws Exception {
    WormholeUserDetails userdetail = new WormholeUserDetails();
    String userId = UUID.randomUUID().toString();
    userdetail.setUserId(userId);
    List<String> rolenames = new ArrayList<String>();
    rolenames.add("admin");
    WormholeUser adminUser = new WormholeUser();
    adminUser.setUserName("admin");
    adminUser.setRoleNames(rolenames);
    adminUser.setId(userId);
    userRepository.save(adminUser);
    Mockito.doReturn(userdetail).when(tokenService).getCurrentUser(any());
    WormholeUser user = createUser();
    userRepository.save(user);
    this.mockMvc.perform(put("/v1/auth/user").contentType(MediaType.APPLICATION_JSON_VALUE).content(objectMapper.writeValueAsString(user))).andExpect(status().isOk()).andDo(document("AuthController-updateUser-example", requestFields(fieldWithPath("id").description("ID of User, created by flowgate"), fieldWithPath("userName").description("userName.").type(JsonFieldType.STRING), fieldWithPath("gender").description("gender").type(JsonFieldType.NUMBER), fieldWithPath("password").description("password").type(JsonFieldType.STRING), fieldWithPath("mobile").description("mobile").type(JsonFieldType.STRING), fieldWithPath("status").description("status").type(JsonFieldType.NUMBER), fieldWithPath("createTime").description("createTime").type(Date.class), fieldWithPath("emailAddress").description("emailAddress").type(JsonFieldType.STRING), fieldWithPath("roleNames").description("roleNames").type(List.class), fieldWithPath("userGroupIDs").description("userGroupIDs").type(List.class), fieldWithPath("lastPasswordResetDate").description("lastPasswordResetDate").type(JsonFieldType.NUMBER)))).andReturn();
    userRepository.deleteById(user.getId());
    userRepository.deleteById(adminUser.getId());
}
Also used : WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) ArrayList(java.util.ArrayList) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

WormholeUser (com.vmware.flowgate.common.model.WormholeUser)27 WormholeUserDetails (com.vmware.flowgate.util.WormholeUserDetails)18 Test (org.junit.Test)18 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 ArrayList (java.util.ArrayList)14 MvcResult (org.springframework.test.web.servlet.MvcResult)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)4 WormholeRequestException (com.vmware.flowgate.exception.WormholeRequestException)3 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Date (java.util.Date)2 Optional (java.util.Optional)2 PageRequest (org.springframework.data.domain.PageRequest)2 AuthToken (com.vmware.flowgate.common.model.AuthToken)1 SoftwareType (com.vmware.flowgate.common.model.FacilitySoftwareConfig.SoftwareType)1 AuthorityUtil (com.vmware.flowgate.util.AuthorityUtil)1 IOException (java.io.IOException)1 Cookie (javax.servlet.http.Cookie)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1