Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Feb 19, 2024
1 parent 7468470 commit e48b6dc
Show file tree
Hide file tree
Showing 127 changed files with 2,295 additions and 2,198 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
yarn-error.log
frontend/node_modules/
.idea/*
.DS-Store
uploads/
.run
2 changes: 1 addition & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/build/
!gradle/wrapper/gradle-wrapper.jar
/uploads/

*.run.xml
/src/main/resources/secrets.properties

### STS ###
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/it/chalmers/gamma/GammaApplication.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package it.chalmers.gamma;

import it.chalmers.gamma.util.controller.ErrorHandlingControllerAdvice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@ConfigurationPropertiesScan
@Import(ErrorHandlingControllerAdvice.class)
public class GammaApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.chalmers.gamma.adapter.primary.api.allowlist;

import it.chalmers.gamma.app.user.allowlist.AllowListFacade;
import it.chalmers.gamma.util.response.SuccessResponse;
import it.chalmers.gamma.adapter.primary.api.utils.SuccessResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import it.chalmers.gamma.app.supergroup.SuperGroupFacade;
import it.chalmers.gamma.app.user.MeFacade;
import it.chalmers.gamma.app.user.UserFacade;
import it.chalmers.gamma.util.response.NotFoundResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
* If you need changes, then create a new version of the API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import it.chalmers.gamma.app.group.GroupFacade;
import it.chalmers.gamma.app.user.UserFacade;
import it.chalmers.gamma.util.response.NotFoundResponse;
import it.chalmers.gamma.adapter.primary.api.utils.NotFoundResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.chalmers.gamma.util.response;
package it.chalmers.gamma.adapter.primary.api.utils;

import org.springframework.http.HttpStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.chalmers.gamma.util.response;
package it.chalmers.gamma.adapter.primary.api.utils;

import org.springframework.http.HttpStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.chalmers.gamma.util.response;
package it.chalmers.gamma.adapter.primary.api.utils;

import it.chalmers.gamma.util.ClassNameGeneratorUtils;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.chalmers.gamma.util.response;
package it.chalmers.gamma.adapter.primary.api.utils;

import org.springframework.http.HttpStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.chalmers.gamma.util.response;
package it.chalmers.gamma.adapter.primary.api.utils;

import it.chalmers.gamma.util.ClassNameGeneratorUtils;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import it.chalmers.gamma.app.apikey.ApiKeyFacade;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
Expand Down Expand Up @@ -96,8 +93,20 @@ public ModelAndView createApiKey(@RequestHeader(value = "HX-Request", required =
mv.setViewName("pages/api-key-credentials");
mv.addObject("apiKeyId", apiKeyCredentials.apiKeyId());
mv.addObject("apiKeyToken", apiKeyCredentials.token());
mv.addObject("name", form.prettyName);

return mv;
}

@DeleteMapping("/api-keys/{id}")
public ModelAndView deleteApiKey(@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest, @PathVariable("id") UUID id) {
try {
this.apiKeyFacade.delete(id);
} catch (ApiKeyFacade.ApiKeyNotFoundException e) {
throw new RuntimeException(e);
}

return new ModelAndView("redirect:/api-keys");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
Expand Down Expand Up @@ -48,12 +45,15 @@ public ModelAndView getClients(@RequestHeader(value = "HX-Request", required = f

ModelAndView mv = new ModelAndView();
if(htmxRequest) {
mv.setViewName("pages/clients");
mv.setViewName("pages/official-clients");
} else {
mv.setViewName("index");
mv.addObject("page", "pages/clients");
mv.addObject("page", "pages/official-clients");
}
mv.addObject("clients", clients);
mv.addObject("clients", clients
.stream()
.filter(client -> client.owner() instanceof ClientFacade.ClientDTO.OfficialOwner)
.toList());

return mv;
}
Expand All @@ -76,6 +76,7 @@ public ModelAndView getClients(@RequestHeader(value = "HX-Request", required = f
mv.setViewName("index");
mv.addObject("page", "pages/client-details");
}
mv.addObject("clientUid", client.get().clientUid());
mv.addObject("client", client.get());
mv.addObject("clientAuthorities", clientAuthorities);
mv.addObject("userApprovals", userApprovals);
Expand Down Expand Up @@ -217,7 +218,7 @@ public ModelAndView createClient(@RequestHeader(value = "HX-Request", required =
BindingResult bindingResult) {
ModelAndView mv = new ModelAndView();

ClientFacade.ClientAndApiKeySecrets secrets = this.clientFacade.create(new ClientFacade.NewClient(
ClientFacade.ClientAndApiKeySecrets secrets = this.clientFacade.createOfficialClient(new ClientFacade.NewClient(
form.redirectUrl,
form.prettyName,
form.svDescription,
Expand All @@ -233,6 +234,7 @@ public ModelAndView createClient(@RequestHeader(value = "HX-Request", required =
mv.addObject("clientUid", secrets.clientUid());
mv.addObject("clientSecret", secrets.clientSecret());
mv.addObject("apiKeyToken", secrets.apiKeyToken());
mv.addObject("name", form.prettyName);

return mv;
}
Expand Down Expand Up @@ -344,8 +346,7 @@ public void setAuthority(String authority) {
@PostMapping("/clients/{id}/authority")
public ModelAndView createAuthority(@RequestHeader(value = "HX-Request", required = true) boolean htmxRequest,
@PathVariable("id") UUID clientUid,
CreateAuthority form,
HttpServletResponse response) {
CreateAuthority form) {

// TODO: Move this to one call in the facade.
try {
Expand All @@ -354,27 +355,59 @@ public ModelAndView createAuthority(@RequestHeader(value = "HX-Request", require
throw new RuntimeException(e);
}

form.superGroups.forEach(superGroup -> {
try {
this.clientAuthorityFacade.addSuperGroupToClientAuthority(clientUid, form.authority, superGroup);
} catch (ClientAuthorityFacade.ClientAuthorityNotFoundException |
ClientAuthorityFacade.SuperGroupNotFoundException e) {
throw new RuntimeException(e);
}
});
List<SuperGroupFacade.SuperGroupDTO> superGroups = new ArrayList<>();
List<UserFacade.UserDTO> users = new ArrayList<>();

if(form.superGroups != null) {
form.superGroups.forEach(superGroup -> {
try {
this.clientAuthorityFacade.addSuperGroupToClientAuthority(clientUid, form.authority, superGroup);
} catch (ClientAuthorityFacade.ClientAuthorityNotFoundException |
ClientAuthorityFacade.SuperGroupNotFoundException e) {
throw new RuntimeException(e);
}

superGroups.add(this.superGroupFacade.get(superGroup).orElseThrow());
});
}

if(form.users != null) {
form.users.forEach(user -> {
try {
this.clientAuthorityFacade.addUserToClientAuthority(clientUid, form.authority, user);
} catch (ClientAuthorityFacade.ClientAuthorityNotFoundException |
ClientAuthorityFacade.UserNotFoundException e) {
throw new RuntimeException(e);
}

users.add(this.userFacade.get(user).orElseThrow().user());
});
}

ModelAndView mv = new ModelAndView("partial/created-client-authority");

form.users.forEach(user -> {
try {
this.clientAuthorityFacade.addUserToClientAuthority(clientUid, form.authority, user);
} catch (ClientAuthorityFacade.ClientAuthorityNotFoundException |
ClientAuthorityFacade.UserNotFoundException e) {
throw new RuntimeException(e);
}
});
mv.addObject("clientUid", clientUid);
mv.addObject("clientAuthority", new ClientAuthorityFacade.ClientAuthorityDTO(
clientUid,
form.authority,
superGroups,
users
));

return mv;
}

response.addHeader("HX-Trigger", "authorities-updated");
@DeleteMapping("/clients/{id}/authority/{name}")
public ModelAndView deleteClientAuthority(@RequestHeader(value = "HX-Request", required = true) boolean htmxRequest,
@PathVariable("id") UUID clientUid,
@PathVariable("name") String authorityName) {
try {
this.clientAuthorityFacade.delete(clientUid, authorityName);
} catch (ClientAuthorityFacade.ClientAuthorityNotFoundException e) {
throw new RuntimeException(e);
}

return new ModelAndView("common/empty");
return new ModelAndView("partial/deleted-client-authority");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package it.chalmers.gamma.adapter.primary.web;

import it.chalmers.gamma.app.client.ClientFacade;
import it.chalmers.gamma.app.user.UserFacade;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
Expand All @@ -9,21 +11,30 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import static it.chalmers.gamma.app.oauth2.GammaRegisteredClientRepository.IS_OFFICIAL;

@Controller
public class ConsentController {

private final RegisteredClientRepository registeredClientRepository;
private final ClientFacade clientFacade;

public ConsentController(RegisteredClientRepository registeredClientRepository) {
public ConsentController(RegisteredClientRepository registeredClientRepository,
ClientFacade clientFacade) {
this.registeredClientRepository = registeredClientRepository;
this.clientFacade = clientFacade;
}

public record UserOwner(String name) {

}

@GetMapping("/oauth2/consent")
public ModelAndView getOAuth2Consent(@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
@RequestParam(OAuth2ParameterNames.CLIENT_ID) String clientId,
@RequestParam(OAuth2ParameterNames.SCOPE) String scope,
@RequestParam(OAuth2ParameterNames.STATE) String state) {

RegisteredClient client = this.registeredClientRepository.findByClientId(clientId);

//TODO: Do something better than this.
Expand All @@ -32,7 +43,7 @@ public ModelAndView getOAuth2Consent(@RequestHeader(value = "HX-Request", requir
}

ModelAndView mv = new ModelAndView();

if(htmxRequest) {
mv.setViewName("pages/consent");
} else {
Expand All @@ -46,6 +57,15 @@ public ModelAndView getOAuth2Consent(@RequestHeader(value = "HX-Request", requir
mv.addObject("hasEmailScope", scope.contains("email"));
mv.addObject("scopes", scope.split(" "));

boolean isOfficial = client.getClientSettings().getSetting(IS_OFFICIAL);

if (!isOfficial) {
UserFacade.UserDTO owner = this.clientFacade.getClientOwner(client.getClientId()).orElseThrow();

mv.addObject("userOwner", new UserOwner(
"%s '%s' %s".formatted(owner.firstName(), owner.nick(), owner.lastName())
));
}

return mv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,27 @@ public ModelAndView getGroup(@RequestHeader(value = "HX-Request", required = fal
return mv;
}

//TODO: Investigate if this can be a record...
@GetMapping("/groups/{id}/cancel-edit")
public ModelAndView getCancelEditGroup(@RequestHeader(value = "HX-Request", required = true) boolean htmxRequest, @PathVariable("id") UUID id) {
Optional<GroupFacade.GroupWithMembersDTO> group = this.groupFacade.getWithMembers(id);

if (group.isEmpty()) {
throw new RuntimeException();
}

ModelAndView mv = new ModelAndView();
mv.setViewName("pages/group-details :: group-details-article");

mv.addObject("group", group.get());
mv.addObject("members", group.get().groupMembers()
.stream()
.map(groupMember -> groupMember.user().nick() + " - " + groupMember.post().enName() + " - " + Objects.requireNonNullElse(groupMember.unofficialPostName(), ""))
.toList()
);

return mv;
}

public static final class GroupForm {
private int version;
private String name;
Expand Down
Loading

0 comments on commit e48b6dc

Please sign in to comment.