1 | package edu.ucsb.cs156.gauchoride.services; | |
2 | ||
3 | import java.io.FileInputStream; | |
4 | import java.util.Collections; | |
5 | import java.util.Date; | |
6 | ||
7 | import org.springframework.stereotype.Controller; | |
8 | import org.springframework.web.bind.annotation.GetMapping; | |
9 | import org.springframework.web.bind.annotation.RequestMapping; | |
10 | import org.springframework.web.bind.annotation.RequestParam; | |
11 | import org.springframework.stereotype.Service; | |
12 | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | |
14 | import edu.ucsb.cs156.gauchoride.services.GoogleTokenService; | |
15 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | |
16 | import org.springframework.beans.factory.annotation.Value; | |
17 | import org.springframework.http.HttpStatus; | |
18 | import org.springframework.http.ResponseEntity; | |
19 | import org.springframework.security.access.prepost.PreAuthorize; | |
20 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | |
21 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; | |
22 | import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient; | |
23 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | |
24 | import org.springframework.security.oauth2.core.OAuth2AccessToken; | |
25 | import org.springframework.stereotype.Controller; | |
26 | import org.springframework.web.bind.annotation.GetMapping; | |
27 | import org.springframework.web.bind.annotation.RequestMapping; | |
28 | import org.springframework.web.bind.annotation.RequestParam; | |
29 | ||
30 | import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | |
31 | import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | |
32 | import com.google.api.client.json.JsonFactory; | |
33 | import com.google.api.client.json.jackson2.JacksonFactory; | |
34 | import com.google.api.client.util.DateTime; | |
35 | import com.google.api.services.calendar.Calendar.Events; | |
36 | import com.google.api.services.calendar.Calendar; | |
37 | import com.google.api.services.calendar.model.AclRule; | |
38 | import com.google.api.services.calendar.model.CalendarListEntry; | |
39 | ||
40 | import io.swagger.v3.oas.annotations.Operation; | |
41 | import io.swagger.v3.oas.annotations.Parameter; | |
42 | import io.swagger.v3.oas.annotations.tags.Tag; | |
43 | import lombok.extern.slf4j.Slf4j; | |
44 | ||
45 | //import edu.ucsb.cs156.example.services.GoogleTokenService; | |
46 | import com.google.api.client.http.javanet.NetHttpTransport; | |
47 | ||
48 | @Slf4j | |
49 | @Service("googlecalendarservice") | |
50 | public class GoogleCalendarService { | |
51 | ||
52 | @Autowired | |
53 | GoogleTokenService googleTokenService; | |
54 | ||
55 | @Value("${app.gcal.calendarId:primary}") | |
56 | private String calendarId; | |
57 | ||
58 | private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); | |
59 | private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart"; | |
60 | ||
61 | ||
62 | ||
63 | public boolean isCalendarConfigured(){ | |
64 |
1
1. isCalendarConfigured : negated conditional → NO_COVERAGE |
if(!calendarId.equals("none")){ |
65 |
1
1. isCalendarConfigured : replaced boolean return with false for edu/ucsb/cs156/gauchoride/services/GoogleCalendarService::isCalendarConfigured → NO_COVERAGE |
return true; |
66 | } | |
67 | ||
68 |
1
1. isCalendarConfigured : replaced boolean return with true for edu/ucsb/cs156/gauchoride/services/GoogleCalendarService::isCalendarConfigured → NO_COVERAGE |
return false; |
69 | } | |
70 | ||
71 | public void giveUserAdminAccess(String email) throws Exception{ | |
72 | // String principalName = authentication.getPrincipal().getName(); | |
73 | // log.info("principalName={}", principalName); | |
74 | // String token = googleTokenService.getAccessToken(principalName).getTokenValue(); | |
75 | // log.info("token={}", token); | |
76 | // GoogleCredential credential = new GoogleCredential().setAccessToken(token); | |
77 | // log.info("credential={}", credential); | |
78 | ||
79 | GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("credentials.json")) | |
80 | .createScoped(Collections.singleton("https://www.googleapis.com/auth/calendar")); | |
81 | ||
82 | NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); | |
83 | Calendar service = new Calendar.Builder(httpTransport, JSON_FACTORY, credential) | |
84 | .setApplicationName(APPLICATION_NAME).build(); | |
85 | ||
86 | // CalendarListEntry calendarListEntry = new CalendarListEntry(); | |
87 | // calendarListEntry.setId("calendarId"); | |
88 | // CalendarListEntry createdCalendarListEntry = service.calendarList().insert(calendarListEntry).execute(); | |
89 | ||
90 | | |
91 | AclRule rule = new AclRule(); | |
92 | AclRule.Scope scope = new AclRule.Scope(); | |
93 | scope.setType("user"); | |
94 | scope.setValue(email); | |
95 | rule.setScope(scope); | |
96 | rule.setRole("reader"); | |
97 | ||
98 | service.acl().insert(calendarId, rule).execute(); | |
99 | ||
100 | ||
101 | } | |
102 | ||
103 | ||
104 | } | |
Mutations | ||
64 |
1.1 |
|
65 |
1.1 |
|
68 |
1.1 |