Skip to content
Snippets Groups Projects

Resolve "Move functionality from frontend to backend"

Files

package server.controller;
import nl.tudelft.labracore.lib.security.user.AuthenticatedPerson;
import nl.tudelft.labracore.lib.security.user.Person;
import org.gitlab4j.api.GitLabApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import server.service.AccountService;
import server.service.InstanceMappingService;
@Controller
@RequestMapping("account")
public class AccountController {
@Autowired
private AccountService accountService;
@Autowired
private InstanceMappingService instanceMappingService;
@GetMapping()
public String getAccount(Model model, @AuthenticatedPerson Person person) {
String netId = person.getUsername();
GitLabApi gitLabApi = instanceMappingService.getInstance(netId);
if (gitLabApi == null) {
model.addAttribute("message", "You do not have any key.");
} else {
model.addAttribute("message", "You already have a key.");
}
return "account";
}
/**
* POST endpoint to add a GitLab API key to an existing user.
*
* @param apiKey String containing the key to be added.
*/
@PostMapping("/addKey")
public String addKey(@AuthenticatedPerson Person person, Model model,
@RequestParam(value = "apiKey") String apiKey) {
//String currentUser = gitBullSecurity.getCurrentUsername(apiKeyDTO.getSecret());
String netId = person.getUsername();
if (apiKey == null) {
model.addAttribute("message", "You need to enter a key first.");
return "redirect:/account";
}
accountService.addAPIKey(netId, apiKey);
instanceMappingService.addInstance(netId, apiKey);
model.addAttribute("message", "The key was added. You may go back.");
return "redirect:/account";
}
// TODO : look into override a get method with a delete
/**
* GET endpoint to remove the GitLab API key of an existing user.
*
*/
@GetMapping("/removeKey")
public String removeKey(@AuthenticatedPerson Person person, Model model) {
//String currentUser = gitBullSecurity.getCurrentUsername(apiKeyDTO.getSecret());
String netId = person.getUsername();
GitLabApi gitLabApi = instanceMappingService.getInstance(netId);
if (gitLabApi == null)
return "redirect:/account";
accountService.removeAPIKey(netId);
instanceMappingService.removeInstance(netId);
model.addAttribute("message", "Key successfully deleted.");
return "redirect:/account";
}
}
Loading