Search in sources :

Example 51 with User

use of com.laurentiuspilca.springsecurityc2.entities.User in project tapestry-5 by apache.

the class PersistAll method onCreateUser.

@CommitAfter
void onCreateUser() {
    final User user = new User();
    user.setFirstName("Foo User");
    entityManager.persist(user);
    this.user = user;
}
Also used : User(org.example.app1.entities.User) CommitAfter(org.apache.tapestry5.jpa.annotations.CommitAfter)

Example 52 with User

use of com.laurentiuspilca.springsecurityc2.entities.User in project youtubechannel by lspil.

the class JpaUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) {
    var o = userRepository.findUserByUsername(username);
    User u = o.orElseThrow(() -> new UsernameNotFoundException(":("));
    return new SecurityUser(u);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SecurityUser(com.laurentiuspilca.ssc6.security.model.SecurityUser) User(com.laurentiuspilca.ssc6.entities.User) SecurityUser(com.laurentiuspilca.ssc6.security.model.SecurityUser)

Example 53 with User

use of com.laurentiuspilca.springsecurityc2.entities.User in project youtubechannel by lspil.

the class JPAUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) {
    Optional<User> user = userRepository.findUserByUsername(username);
    User u = user.orElseThrow(() -> new UsernameNotFoundException("Error!"));
    return new SecurityUser(u);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.laurentiuspilca.springsecurityc2.entities.User)

Example 54 with User

use of com.laurentiuspilca.springsecurityc2.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class UserDetailsServiceImpl method loadUserByUsername.

// TODO: add courier repo for finding user by phoneNumber
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
    User user = userRepo.findByEmail(login);
    // if user not found by Email try find client/courier by phoneNumber
    if (user == null) {
        Client client = clientRepo.findByPhoneNumber(login);
        if (client == null) {
            throw new NotFoundEx(login);
        }
        user = client.getUser();
    }
    return user;
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) Client(com.ncedu.fooddelivery.api.v1.entities.Client)

Example 55 with User

use of com.laurentiuspilca.springsecurityc2.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method deleteImgNotAdminNotOwner.

@Test
public void deleteImgNotAdminNotOwner() throws IOException {
    // prepare
    Long ownerId = 1L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    Long notOwnerId = 2L;
    User leonardNotOwner = UserUtils.moderatorLeonardHofstadter(notOwnerId);
    String imgName = "test.jpeg";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "55420882-1e23-4559-b84e-03c3f4d597af";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File fileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, imgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    doNothing().when(fileRepoMock).delete(fileEntity);
    Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
        fileService.delete(fileEntity, leonardNotOwner);
    });
    String perfectMessage = new CustomAccessDeniedException().getMessage();
    String resultMessage = exception.getMessage();
    assertEquals(perfectMessage, resultMessage);
    verify(fileRepoMock, never()).delete(fileEntity);
}
Also used : Path(java.nio.file.Path) User(com.ncedu.fooddelivery.api.v1.entities.User) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)58 Test (org.junit.jupiter.api.Test)49 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)49 File (com.ncedu.fooddelivery.api.v1.entities.File)18 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)18 MultipartFile (org.springframework.web.multipart.MultipartFile)18 Path (java.nio.file.Path)16 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)12 UserInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO)9 BufferedImage (java.awt.image.BufferedImage)8 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)7 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)6 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)6 PersistenceContext (javax.persistence.PersistenceContext)6 User (org.example.app0.entities.User)6 User (org.example.app1.entities.User)6 User (org.example.app6.entities.User)5 EmailChangeDTO (com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO)4 PasswordChangeDTO (com.ncedu.fooddelivery.api.v1.dto.user.PasswordChangeDTO)4 IOException (java.io.IOException)4