Skip to content

Commit

Permalink
Merge pull request #93 from bardia-p/issue65_milestone3
Browse files Browse the repository at this point in the history
Resolve Issue 65 Login Aspect
  • Loading branch information
maxcurkovic authored Dec 6, 2023
2 parents 34997ce + c4ec8eb commit 449e710
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 43 deletions.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
* The starting point for the application.
*/
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OpinionOwlApplication {
public static void main(String[] args) {
SpringApplication.run(OpinionOwlApplication.class, args);
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/opinionowl/opinionowl/aspect/LoginAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.opinionowl.opinionowl.aspect;

import com.opinionowl.opinionowl.controllers.CookieController;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
* Class LoginAspect for defining an aspect for all login checks within APIController and PageController.
*/
@Aspect
@Component
public class LoginAspect {

/**
* Method for defining the annotation needsLogin.
* @param needsLogin NeedsLogin object
*/
@Pointcut("@annotation(needsLogin)")
public void callAt(NeedsLogin needsLogin) {
}

/**
* Method for checking the type of the NeedsLogin annotation and proceeding with the actual login check through the aspect.
* @param pjp A ProceedingJoinPoint pjp.
* @param needsLogin A NeedsLogin needsLogin.
* @return The type of the aspect annotation
* @throws Throwable An error
*/
@Around("callAt(needsLogin)")
public Object around(ProceedingJoinPoint pjp, NeedsLogin needsLogin) throws Throwable {
Object[] args = pjp.getArgs();
HttpServletRequest request = null;
for (Object arg : args) {
if (arg instanceof HttpServletRequest) {
request = (HttpServletRequest) arg;
}
}
if (request == null) {
return "redirect:/";
}
String res = CookieController.getUsernameFromCookie(request);
if (res == null) {
return getReturnType(needsLogin);
}
return pjp.proceed();
}

/**
* Method for getting the return type for the around method.
* @param needsLogin A needsLogin object
* @return Html, string or int type
*/
public Object getReturnType(NeedsLogin needsLogin) {
if (needsLogin.type().equals("html")) {
return "redirect:/";
} else if (needsLogin.type().equals("string")) {
return "";
} else if (needsLogin.type().equals("int")) {
return 400;
} else {
return null;
}
}
}

21 changes: 21 additions & 0 deletions src/main/java/com/opinionowl/opinionowl/aspect/NeedsLogin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.opinionowl.opinionowl.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Interface for the NeedsLogin aspect.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NeedsLogin {
/*
"html"
"string"
"int"
*/
String type() default "html";

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.opinionowl.opinionowl.controllers;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.opinionowl.opinionowl.aspect.NeedsLogin;
import com.opinionowl.opinionowl.models.*;
import com.opinionowl.opinionowl.repos.QuestionRepository;
import com.opinionowl.opinionowl.repos.ResponseRepository;
Expand Down Expand Up @@ -141,15 +142,10 @@ public int postSurveyResponses(@PathVariable("surveyId") Long surveyId, HttpServ
* @throws IOException
*/
@PostMapping("/createSurvey")
@NeedsLogin(type="int")
public int createSurvey(HttpServletRequest request) throws IOException {
System.out.println("createSurvey() API");

String username = CookieController.getUsernameFromCookie(request);
if (username == null){
System.out.println("You must be logged in first");
return 400;
}

String jsonData = this.JSONBuilder(request);
ObjectMapper objectMapper = new ObjectMapper();
HashMap<String, Object> surveyData = objectMapper.readValue(jsonData, new TypeReference<HashMap<String, Object>>() {});
Expand Down Expand Up @@ -229,15 +225,10 @@ public int createSurvey(HttpServletRequest request) throws IOException {
* @throws JSONException
*/
@GetMapping("/getSurveyResults/{id}")
@NeedsLogin(type="string")
public String getSurveyResults(@PathVariable("id") String id, HttpServletRequest request) throws JSONException {
System.out.println("getSurveyResults() API");

String username = CookieController.getUsernameFromCookie(request);
if (username == null){
System.out.println("You must be logged in first");
return "";
}

Long surveyId = Long.valueOf(id);
Optional<Survey> s = surveyRepo.findById(surveyId);
JSONObject resObject = new JSONObject();
Expand Down Expand Up @@ -308,15 +299,11 @@ public int createUser(HttpServletRequest request) throws IOException {
* @throws IOException
*/
@PostMapping("/closeSurvey/{id}")
@NeedsLogin(type="int")
public int closeSurvey(@PathVariable("id") Long id, HttpServletRequest request) throws IOException {
System.out.println("closeSurvey() API");

String username = CookieController.getUsernameFromCookie(request);
if (username == null){
System.out.println("You must be logged in first");
return 400;
}

Survey survey = surveyRepo.findById(id).orElse(null);
if (survey == null) {
return 400;
Expand Down Expand Up @@ -359,7 +346,11 @@ public int closeSurvey(@PathVariable("id") Long id, HttpServletRequest request)
* @return resObject, the results of the survey in JSON format.
* @throws IOException
*/



@GetMapping("/savedResponses/{username}")
@NeedsLogin(type="string")
public String getSavedResponses(@PathVariable("username") String username, HttpServletRequest request) throws IOException, JSONException {
System.out.println("getSavedResponses() API");

Expand Down Expand Up @@ -523,15 +514,10 @@ public String getSurveyQuestions(@PathVariable("id") String id, HttpServletReque
* @throws IOException
*/
@PostMapping("/updateSurvey/{id}")
@NeedsLogin(type="int")
public int updateSurvey(@PathVariable("id") String id, HttpServletRequest request) throws IOException {
System.out.println("Updating survey API()");

String username = CookieController.getUsernameFromCookie(request);
if (username == null){
System.out.println("You must be logged in first");
return 400;
}

String jsonData = this.JSONBuilder(request);
ObjectMapper objectMapper = new ObjectMapper();
HashMap<String, Object> surveyData = objectMapper.readValue(jsonData, new TypeReference<HashMap<String, Object>>() {});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.opinionowl.opinionowl.controllers;

import com.opinionowl.opinionowl.aspect.NeedsLogin;
import com.opinionowl.opinionowl.models.*;
import com.opinionowl.opinionowl.repos.SurveyRepository;
import com.opinionowl.opinionowl.repos.UserRepository;
Expand Down Expand Up @@ -51,7 +52,9 @@ public String getHomePage(Model model, HttpServletRequest request) {
* @return String ,the html template
*/
@GetMapping("/createSurvey")
@NeedsLogin
public String getCreateSurveyPage(Model model, HttpServletRequest request) {

String cookieUsername = CookieController.getUsernameFromCookie(request);
if (cookieUsername == null){
System.out.println("You must be logged in first");
Expand Down Expand Up @@ -123,12 +126,10 @@ public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyI
}

@GetMapping("/editSurvey")
@NeedsLogin
public String editSurveyPage(@RequestParam(value = "surveyId") Long surveyId, Model model, HttpServletRequest request) {

String cookieUsername = CookieController.getUsernameFromCookie(request);
if (cookieUsername == null){
System.out.println("You must be logged in first");
return "redirect:/";
}
CookieController.setUsernameCookie(model, request);
Optional<Survey> surveyO = surveyRepo.findById(surveyId);
if (surveyO.isPresent()) {
Expand All @@ -153,13 +154,10 @@ public String editSurveyPage(@RequestParam(value = "surveyId") Long surveyId, Mo
* @return String, the html template
*/
@GetMapping("/viewResponse")
@NeedsLogin
public String getViewResponsePage(@RequestParam(value = "surveyId") Long surveyId, Model model, HttpServletRequest request) {
String cookieUsername = CookieController.getUsernameFromCookie(request);
if (cookieUsername == null){
System.out.println("You must be logged in first");
return "redirect:/";
}

String cookieUsername = CookieController.getUsernameFromCookie(request);
CookieController.setUsernameCookie(model, request);

// find the survey by id
Expand Down Expand Up @@ -216,13 +214,9 @@ public String addUser(){
* @return, String HTML template for manageSurvey
*/
@GetMapping("/manageSurvey")
@NeedsLogin
public String getManageSurvey(@RequestParam(value = "username") String username, Model model, HttpServletRequest request) {
String cookieUsername = CookieController.getUsernameFromCookie(request);
if (cookieUsername == null){
System.out.println("You must be logged in first");
return "redirect:/";
}

CookieController.setUsernameCookie(model, request);

if (!cookieUsername.equals(username)){
Expand All @@ -247,13 +241,9 @@ public String getManageSurvey(@RequestParam(value = "username") String username,
* @return, String HTML template for manageSurvey
*/
@GetMapping("/savedResponses")
@NeedsLogin
public String getSavedResponses(@RequestParam(value = "username") String username, Model model, HttpServletRequest request) {
String cookieUsername = CookieController.getUsernameFromCookie(request);
if (cookieUsername == null){
System.out.println("You must be logged in first");
return "redirect:/";
}

CookieController.setUsernameCookie(model, request);

if (!cookieUsername.equals(username)){
Expand Down

0 comments on commit 449e710

Please sign in to comment.