Search in sources :

Example 6 with User

use of fr.codechill.spring.model.User in project code-chill by CodeChillAlluna.

the class UserController method setNewPassword.

@PostMapping(value = "/reset")
public ResponseEntity<?> setNewPassword(@RequestBody Map<String, String> requestParams) {
    User user = urepo.findByTokenPassword(requestParams.get("token"));
    HttpHeaders responseHeaders = new HttpHeaders();
    if (user != null) {
        user.setPassword(bCryptPasswordEncoder.encode(requestParams.get("password")));
        user.setTokenPassword(null);
        urepo.save(user);
        return ResponseEntity.ok().headers(responseHeaders).body(user);
    }
    return ResponseEntity.badRequest().headers(responseHeaders).body(null);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) User(fr.codechill.spring.model.User) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 7 with User

use of fr.codechill.spring.model.User in project code-chill by CodeChillAlluna.

the class UserController method updateEmailError.

// Method sending an error message for the former email adress
public boolean updateEmailError(String email) {
    User user = urepo.findByEmail(email);
    if (user != null) {
        SimpleMailMessage infoUpdateFail = new SimpleMailMessage();
        infoUpdateFail.setFrom(SENDFROM);
        infoUpdateFail.setTo(user.getEmail());
        infoUpdateFail.setSubject("Suspicious access to your account");
        infoUpdateFail.setText("We have registered a suspicious activity on your acccount");
        mailSender.send(infoUpdateFail);
        return true;
    }
    return false;
}
Also used : User(fr.codechill.spring.model.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage)

Example 8 with User

use of fr.codechill.spring.model.User in project code-chill by CodeChillAlluna.

the class AuthenticationRestControllerTest method successfulRefreshTokenWithUserRole.

@Test
@WithMockUser(roles = "USER")
public void successfulRefreshTokenWithUserRole() throws Exception {
    Authority authority = new Authority();
    authority.setId(0L);
    authority.setName(AuthorityName.ROLE_USER);
    List<Authority> authorities = Arrays.asList(authority);
    User user = new User();
    user.setUsername("username");
    user.setAuthorities(authorities);
    user.setEnabled(Boolean.TRUE);
    user.setLastPasswordResetDate(new Date(System.currentTimeMillis() + 1000 * 1000));
    JwtUser jwtUser = JwtUserFactory.create(user);
    when(this.jwtTokenUtil.getUsernameFromToken(any())).thenReturn(user.getUsername());
    when(this.userDetailsService.loadUserByUsername(eq(user.getUsername()))).thenReturn(jwtUser);
    when(this.jwtTokenUtil.canTokenBeRefreshed(any(), any())).thenReturn(true);
    this.mvc.perform(get("/refresh").header("Authorization", "Bearer 5d1103e-b3e1-4ae9-b606-46c9c1bc915a")).andExpect(status().is2xxSuccessful());
}
Also used : User(fr.codechill.spring.model.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) WithAnonymousUser(org.springframework.security.test.context.support.WithAnonymousUser) JwtUser(fr.codechill.spring.security.JwtUser) Authority(fr.codechill.spring.model.security.Authority) JwtUser(fr.codechill.spring.security.JwtUser) Date(java.util.Date) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with User

use of fr.codechill.spring.model.User in project code-chill by CodeChillAlluna.

the class AuthenticationRestControllerTest method successfulRefreshTokenWithAdminRole.

@Test
@WithMockUser(roles = "ADMIN")
public void successfulRefreshTokenWithAdminRole() throws Exception {
    Authority authority = new Authority();
    authority.setId(1L);
    authority.setName(AuthorityName.ROLE_ADMIN);
    List<Authority> authorities = Arrays.asList(authority);
    User user = new User();
    user.setUsername("admin");
    user.setAuthorities(authorities);
    user.setEnabled(Boolean.TRUE);
    user.setLastPasswordResetDate(new Date(System.currentTimeMillis() + 1000 * 1000));
    JwtUser jwtUser = JwtUserFactory.create(user);
    when(this.jwtTokenUtil.getUsernameFromToken(any())).thenReturn(user.getUsername());
    when(this.userDetailsService.loadUserByUsername(eq(user.getUsername()))).thenReturn(jwtUser);
    when(this.jwtTokenUtil.canTokenBeRefreshed(any(), any())).thenReturn(true);
    this.mvc.perform(get("/refresh").header("Authorization", "Bearer 5d1103e-b3e1-4ae9-b606-46c9c1bc915a")).andExpect(status().is2xxSuccessful());
}
Also used : User(fr.codechill.spring.model.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) WithAnonymousUser(org.springframework.security.test.context.support.WithAnonymousUser) JwtUser(fr.codechill.spring.security.JwtUser) Authority(fr.codechill.spring.model.security.Authority) JwtUser(fr.codechill.spring.security.JwtUser) Date(java.util.Date) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 10 with User

use of fr.codechill.spring.model.User in project code-chill by CodeChillAlluna.

the class UserRestControllerTest method getPersonsSuccessfullyWithUserRole.

@Test
@WithMockUser(roles = "USER")
public void getPersonsSuccessfullyWithUserRole() throws Exception {
    Authority authority = new Authority();
    authority.setId(1L);
    authority.setName(AuthorityName.ROLE_ADMIN);
    List<Authority> authorities = Arrays.asList(authority);
    User user = new User();
    user.setUsername("username");
    user.setAuthorities(authorities);
    user.setEnabled(Boolean.TRUE);
    user.setLastPasswordResetDate(new Date(System.currentTimeMillis() + 1000 * 1000));
    JwtUser jwtUser = JwtUserFactory.create(user);
    when(this.jwtTokenUtil.getUsernameFromToken(any())).thenReturn(user.getUsername());
    when(this.userDetailsService.loadUserByUsername(eq(user.getUsername()))).thenReturn(jwtUser);
    this.mvc.perform(get("/user").header("Authorization", "Bearer nsodunsodiuv")).andExpect(status().is2xxSuccessful());
}
Also used : User(fr.codechill.spring.model.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) JwtUser(fr.codechill.spring.security.JwtUser) Authority(fr.codechill.spring.model.security.Authority) JwtUser(fr.codechill.spring.security.JwtUser) Date(java.util.Date) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (fr.codechill.spring.model.User)10 Date (java.util.Date)5 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Authority (fr.codechill.spring.model.security.Authority)3 JwtUser (fr.codechill.spring.security.JwtUser)3 HttpHeaders (org.springframework.http.HttpHeaders)3 WithMockUser (org.springframework.security.test.context.support.WithMockUser)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)2 WithAnonymousUser (org.springframework.security.test.context.support.WithAnonymousUser)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 Docker (fr.codechill.spring.model.Docker)1 Calendar (java.util.Calendar)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1