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

ROMANOFAFARD_SERVICE_HOUR2 #32

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@

@RestController
public class AuthorizationController {

const String admiral = "ADMIRAL";
const String vice_admiral = "VICE_ADMIRAL";
const String captain = "CAPTAIN";
const String commander = "COMMANDER";
const String lieutenant ="LIEUTENANT";
const String ensign = "ENSIGN";
const String crewman = "CREWMAN";
const String[] ranks = {admiral, vice_admiral, captain, commander, lieutenant, ensign, crewman};

@Autowired
private UsersService usersService;

Expand All @@ -38,12 +48,36 @@ public void checkAccess(Authentication authentication, @RequestBody ActionDto ac
String recipientRole = usersService.loadUserRole(actionDto.getRecipient());
System.out.println("Role: " + recipientRole);


for (GrantedAuthority grantedAuthority : userDetails.getAuthorities()){

if (RankDifference(grantedAuthority.getAuthority(), recipientRole) + 1 < 0) {
throw new ForbiddenAccessException();
}
if (grantedAuthority.getAuthority().equals(ROLE_CREWMAN)) {
if (recipientRole.contains(ROLE_ADMIRAL)) {
throw new ForbiddenAccessException();
}
}
}
}


private int RankDifference(String rank1, String rank2)
{
int rank1Level = 0;
int rank2Level = 0;
for (int i = 0; i < ranks.length; ++i)
{
if (ranks[i] == rank1)
{
rank1Level = i;
}
if (ranks[i] == rank2)
{
rank2Level = i;
}
}
return rank1Level - rank2Level;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

@RestController
public class MessageController {

@Autowired
private DiscoveryClient discoveryClient;

Expand Down Expand Up @@ -54,13 +53,14 @@ private void checkAccess(ActionDto actionDto, String session) {
ServiceInstance service = discoveryClient.getInstances("authorization").get(0);
String url = "http://" + service.getHost() + ":" + service.getPort() + "/" + "checkAccess";

String sender = actionDto.getSender();
String requestJson = "{\"recipient\":\"" + actionDto.getRecipient() + "\"}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Cookie", "SESSION=" + session);

HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
restTemplate.postForObject(url, entity, String.class);
// TODO envoyer notif au recipient
}

}