Skip to content
This repository was archived by the owner on Mar 25, 2018. It is now read-only.

BATTLESTAR_CONCORDIA_SERVICES_HOUR2 #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Java micro-services

BATTLESTARCONCORDIA_SERVICES_HOUR1
- [x] Adds whitespace, rip

^


## Build
build all projects
```
Expand Down Expand Up @@ -179,3 +185,4 @@ Centralize configuration for all micro services.
- [Spring cloud bootstrap](https://github.com/eugenp/tutorials/tree/master/spring-cloud/spring-cloud-bootstrap)
- [Securing cloud services](http://www.baeldung.com/spring-cloud-securing-services)


Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions authorization/out/production/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spring.cloud.config:
name: authorization
username: configUser
password: configPassword

---
spring.profiles: native
spring.cloud.config.uri: http://localhost:8888

---
spring.profiles: docker
spring.cloud.config.uri: http://config-server:8888
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import static com.ship.authorization.service.UsersService.ROLE_ADMIRAL;
import static com.ship.authorization.service.UsersService.ROLE_CREWMAN;
import static com.ship.authorization.service.UsersService.*;

@RestController
public class AuthorizationController {
Expand All @@ -40,7 +39,8 @@ public void checkAccess(Authentication authentication, @RequestBody ActionDto ac

for (GrantedAuthority grantedAuthority : userDetails.getAuthorities()){
if (grantedAuthority.getAuthority().equals(ROLE_CREWMAN)) {
if (recipientRole.contains(ROLE_ADMIRAL)) {
// Crewman can only send to same rank or rank + 1
if (ROLE_VALUES.get(recipientRole) > ROLE_VALUES.get(grantedAuthority.getAuthority()) + 1) {
throw new ForbiddenAccessException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -16,6 +17,20 @@ public class UsersService {
public static final String ROLE_ENSIGN = "ROLE_ENSIGN";
public static final String ROLE_CREWMAN = "ROLE_CREWMAN";

public static final Map<String, Integer> ROLE_VALUES;

static {
Map<String, Integer> aMap = new HashMap<>();
aMap.put(ROLE_ADMIRAL, 6);
aMap.put(ROLE_VICE_ADMIRAL, 5);
aMap.put(ROLE_CAPTAIN, 4);
aMap.put(ROLE_COMMANDER, 3);
aMap.put(ROLE_LIEUTENANT, 2);
aMap.put(ROLE_ENSIGN, 1);
aMap.put(ROLE_CREWMAN, 0);
ROLE_VALUES = Collections.unmodifiableMap(aMap);
}

private Map<String, String> users = new HashMap<>();

public UsersService() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ subprojects {
maven { url 'https://repo.spring.io/libs-snapshot'
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.ship.communication.model.Message;
import com.ship.communication.model.resource.MessageResource;
import com.ship.communication.repository.MessageRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
Expand All @@ -16,13 +18,14 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@RestController
public class MessageController {

private Logger log = LoggerFactory.getLogger(this.getClass());

@Autowired
private DiscoveryClient discoveryClient;

Expand All @@ -32,9 +35,19 @@ public class MessageController {
@Autowired
private MessageRepository messageRepository;

public Message findByRecipientAndSender(String r, String s, MessageRepository mr){
// stub gg find da message
return null;
}

@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
public Message sendMessage(@RequestBody Message message, @CookieValue("SESSION") String cookie) {
checkAccess(new ActionDto(message.getRecipient()), cookie);
// log message
log.info(message.getTitle().toUpperCase() + ": " + message.getContent());
// allow the reply to a personal message
if(findByRecipientAndSender(message.getRecipient(), message.getSender()) === null) {
checkAccess(new ActionDto(message.getRecipient()), cookie);
}
return messageRepository.save(message);
}

Expand Down
2 changes: 1 addition & 1 deletion config-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ security.user:
---
spring:
profiles: native
cloud.config.server.native.search-locations: file:///${user.home}/Documents/wilau2/cs-games-2018-relay-cloud/config-server/config
cloud.config.server.native.search-locations: file:///${user.home}/Documents/relay/cs-games-2018-relay-cloud/config-server/config

eureka:
client:
Expand Down