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);
}
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;
}
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();
}
}
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();
}
}
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());
}
Aggregations