Changeset 5306751 for src


Ignore:
Timestamp:
09/04/21 11:14:25 (3 years ago)
Author:
NikolaCenevski <cenevskinikola@…>
Branches:
master
Children:
ab49338
Parents:
194776a
Message:

Dodadeno posts to be approved

Location:
src/main
Files:
3 added
8 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/charitable/CharitableApplication.java

    r194776a r5306751  
    44import org.springframework.boot.SpringApplication;
    55import org.springframework.boot.autoconfigure.SpringBootApplication;
     6import org.springframework.scheduling.annotation.EnableScheduling;
    67
    78@SpringBootApplication
     9@EnableScheduling
    810public class CharitableApplication {
    911
  • src/main/java/it/finki/charitable/controller/DonationPostController.java

    r194776a r5306751  
    7373        post.setDateDue(dateDue);
    7474        post.setBankAccount(bankAccount);
     75        post.setApproved(false);
    7576
    7677        List<String> phoneNumbers = Arrays.asList(telekom, a1);
     
    123124    @RequestMapping("/album")
    124125    public String album(Model model) {
    125         List<DonationPost> postList = donationPostService.findAll();
     126        List<DonationPost> postList = donationPostService.findAllByApproved(true);
    126127        if (postList.size() == 0) {
    127128            model.addAttribute("noPosts", true);
     
    148149    }
    149150
    150     @RequestMapping("/deletePost")
    151     public String deletePost(@RequestParam Long postid) {
    152         DonationPost post = donationPostService.getById(postid);
    153         if (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
    154             List<String> fileForDeletion = post.getPhotosForDeletion();
    155             for (String f : fileForDeletion) {
    156                 File file = new File(f);
    157                 file.delete();
    158             }
    159             donationPostService.delete(post);
    160         }
    161 
    162         return "redirect:/myPosts";
    163     }
     151//    @RequestMapping("/deletePost")
     152//    public String deletePost(@RequestParam Long postid) {
     153//        DonationPost post = donationPostService.getById(postid);
     154//        if (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
     155//            List<String> fileForDeletion = post.getPhotosForDeletion();
     156//            for (String f : fileForDeletion) {
     157//                File file = new File(f);
     158//                file.delete();
     159//            }
     160//            donationPostService.delete(post);
     161//        }
     162//
     163//        return "redirect:/myPosts";
     164//    }
    164165
    165166    @RequestMapping("/donate")
  • src/main/java/it/finki/charitable/entities/DonationPost.java

    r194776a r5306751  
    3737    private LocalDate dateDue;
    3838    private String bankAccount;
     39    private Boolean approved;
    3940
    4041    @ElementCollection
     
    7475
    7576        List<String> photoPaths = new ArrayList<>();
     77
    7678        for(String path: images) {
     79            photoPaths.add("../../../../post-photos/" + id + "/" + path);
     80        }
     81
     82        for(String path: moderatorImages) {
    7783            photoPaths.add("../../../../moderator-photos/" + id + "/" + path);
    7884        }
     
    158164    }
    159165
     166    public Boolean getApproved() {
     167        return approved;
     168    }
     169
     170    public void setApproved(Boolean approved) {
     171        this.approved = approved;
     172    }
     173
    160174    public List<String> getPhoneNumbers() {
    161175        return phoneNumbers;
  • src/main/java/it/finki/charitable/repository/DonationPostRepository.java

    r194776a r5306751  
    1111public interface DonationPostRepository extends JpaRepository<DonationPost, Long> {
    1212    List<DonationPost> findAllByUser(AppUser user);
     13    List<DonationPost> findAllByApproved(Boolean approved);
    1314}
  • src/main/java/it/finki/charitable/security/SecurityConfig.java

    r194776a r5306751  
    77import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    88import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
     9import org.springframework.security.core.Authentication;
    910import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
     11import org.springframework.security.web.DefaultRedirectStrategy;
     12import org.springframework.security.web.RedirectStrategy;
     13import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
    1014import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
     15
     16import javax.servlet.ServletException;
     17import javax.servlet.http.HttpServletRequest;
     18import javax.servlet.http.HttpServletResponse;
     19import java.io.IOException;
    1120
    1221@Configuration
     
    4049                .authorizeRequests()
    4150                .antMatchers(publicMatchers).permitAll()
    42                 .antMatchers("/moderator-photos/**").hasAuthority(UserRole.MODERATOR.name())
    43                 .anyRequest().authenticated();
     51                .antMatchers("/moderator-photos/**", "/moderator/**").hasAuthority(UserRole.MODERATOR.name())
     52                .anyRequest().hasAuthority(UserRole.USER.name());
    4453
    4554        http
     
    4756                .cors().disable()
    4857                .formLogin().loginPage("/login")
    49                 .defaultSuccessUrl("/", true)
     58                .successHandler(authenticationSuccessHandler)
    5059                .and()
    5160                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
     
    5564    }
    5665
     66    AuthenticationSuccessHandler authenticationSuccessHandler = (httpServletRequest, httpServletResponse, authentication) -> {
     67        RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
     68        if(authentication.getAuthorities().toString().contains("MODERATOR")) {
     69            redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/moderator/approval");
     70        } else {
     71            redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/");
     72        }
     73    };
     74
    5775    @Override
    5876    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  • src/main/java/it/finki/charitable/services/DonationPostService.java

    r194776a r5306751  
    4141    }
    4242
     43    public List<DonationPost> findAllByApproved(Boolean approved) {
     44        return donationPostRepository.findAllByApproved(approved);
     45    }
     46
    4347    public void delete(DonationPost donationPost) {
    4448        donationPostRepository.delete(donationPost);
  • src/main/java/it/finki/charitable/services/EmailService.java

    r194776a r5306751  
    2424        javaMailSender.send(message);
    2525    }
     26
     27    public void sendApprovalEmail(String to, String subject, Long postId) {
     28        SimpleMailMessage message = new SimpleMailMessage();
     29        message.setTo(to);
     30        message.setSubject(subject);
     31
     32        String text = "Your post has been approved\n" + "http://localhost:8080/post?postid=" + postId;
     33        message.setText(text);
     34        javaMailSender.send(message);
     35    }
     36
     37    public void sendNoApprovalEmail(String to, String subject, String description) {
     38        SimpleMailMessage message = new SimpleMailMessage();
     39        message.setTo(to);
     40        message.setSubject(subject);
     41
     42        String text = "Sorry, your post hasn't been approved" + "\n" +
     43                "Moderator:\n" + description;
     44        message.setText(text);
     45        javaMailSender.send(message);
     46    }
    2647}
  • src/main/resources/templates/common/navbar.html

    r194776a r5306751  
    99    <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
    1010        <div class="container-fluid">
    11             <a class="navbar-brand" href="#">
    12                 <img th:src="@{image/charity.png}" class="bi me-2" width="40" height="32" />
     11            <a class="navbar-brand" th:href="@{/}">
     12                <img th:src="@{/image/charity.png}" class="bi me-2" width="40" height="32" />
    1313                <span class="fs-4">Charitable</span>
    1414            </a>
     
    2020                <ul class="navbar-nav me-auto mb-2 mb-md-0">
    2121                    <li class="nav-item">
    22                         <a th:href="@{/}" class="nav-link px-2 text-white">Home</a>
     22                        <a sec:authorize="isAnonymous() or hasAuthority('USER')" th:href="@{/}" class="nav-link px-2 text-white">Home</a>
    2323                    </li>
    2424                    <li class="nav-item">
    25                         <a th:href="@{/album?page=1}" class="nav-link px-2 text-white">Posts</a>
     25                        <a sec:authorize="isAnonymous() or hasAuthority('USER')" th:href="@{/album?page=1}" class="nav-link px-2 text-white">Posts</a>
     26                        <a sec:authorize="hasAuthority('MODERATOR')" th:href="@{/moderator/approval}" class="nav-link px-2 text-white">Posts for approval</a>
    2627                    </li>
    2728                    <li class="nav-item">
    28                         <a th:href="@{/upload}" class="nav-link px-2 text-white">Upload</a>
     29                        <a sec:authorize="hasAuthority('USER')" th:href="@{/upload}" class="nav-link px-2 text-white">Upload</a>
     30                        <a sec:authorize="hasAuthority('MODERATOR')" th:href="@{/moderator/report}" class="nav-link px-2 text-white">Reported posts</a>
    2931                    </li>
    3032                </ul>
Note: See TracChangeset for help on using the changeset viewer.