Search in sources :

Example 11 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanService method acceptPlayerInvitationToken.

@SneakyThrows
void acceptPlayerInvitationToken(String stringToken, Authentication authentication) {
    Jwt token = jwtService.decodeAndVerify(stringToken);
    InvitationResult invitation = objectMapper.readValue(token.getClaims(), InvitationResult.class);
    if (invitation.isExpired()) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
    }
    final Integer clanId = invitation.getClan().getId();
    Player player = playerService.getPlayer(authentication);
    Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
    Player newMember = playerRepository.findById(invitation.getNewMember().getId()).orElseThrow(() -> new ProgrammingError("ClanMember does not exist: " + invitation.getNewMember().getId()));
    if (player.getId() != newMember.getId()) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
    }
    if (newMember.getClan() != null) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
    }
    ClanMembership membership = new ClanMembership();
    membership.setClan(clan);
    membership.setPlayer(newMember);
    clanMembershipRepository.save(membership);
}
Also used : Player(com.faforever.api.data.domain.Player) Jwt(org.springframework.security.jwt.Jwt) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 12 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanService method generatePlayerInvitationToken.

@SneakyThrows
String generatePlayerInvitationToken(Player requester, int newMemberId, int clanId) {
    Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
    if (requester.getId() != clan.getLeader().getId()) {
        throw new ApiException(new Error(ErrorCode.CLAN_NOT_LEADER, clanId));
    }
    Player newMember = playerRepository.findById(newMemberId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_GENERATE_LINK_PLAYER_NOT_FOUND, newMemberId)));
    long expire = Instant.now().plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES).toEpochMilli();
    InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
    return jwtService.sign(result);
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 13 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClansController method createClan.

// This request cannot be handled by JSON API because we must simultaneously create two resources (a,b)
// a: the new clan with the leader membership, b: the leader membership with the new clan
@ApiOperation("Create a clan with correct leader, founder and clan membership")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success with JSON { id: ?, type: 'clan'}"), @ApiResponse(code = 400, message = "Bad Request") })
@RequestMapping(path = "/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ROLE_USER')")
@Transactional
public Map<String, Serializable> createClan(@RequestParam(value = "name") String name, @RequestParam(value = "tag") String tag, @RequestParam(value = "description", required = false) String description, Authentication authentication) throws IOException {
    Player player = playerService.getPlayer(authentication);
    Clan clan = clanService.create(name, tag, description, player);
    return ImmutableMap.of("id", clan.getId(), "type", "clan");
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanControllerTest method meDataWithClan.

@Test
@WithUserDetails(AUTH_CLAN_MEMBER)
public void meDataWithClan() throws Exception {
    Player player = getPlayer();
    Clan clan = clanRepository.getOne(1);
    mockMvc.perform(get("/clans/me/")).andExpect(status().isOk()).andExpect(jsonPath("$.player.id", is(player.getId()))).andExpect(jsonPath("$.player.login", is(player.getLogin()))).andExpect(jsonPath("$.clan.id", is(clan.getId()))).andExpect(jsonPath("$.clan.tag", is(clan.getTag()))).andExpect(jsonPath("$.clan.name", is(clan.getName())));
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) AbstractIntegrationTest(com.faforever.api.AbstractIntegrationTest) Test(org.junit.Test) WithUserDetails(org.springframework.security.test.context.support.WithUserDetails)

Example 15 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanService method create.

@SneakyThrows
Clan create(String name, String tag, String description, Player creator) {
    if (!creator.getClanMemberships().isEmpty()) {
        throw new ApiException(new Error(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN));
    }
    if (clanRepository.findOneByName(name).isPresent()) {
        throw new ApiException(new Error(ErrorCode.CLAN_NAME_EXISTS, name));
    }
    if (clanRepository.findOneByTag(tag).isPresent()) {
        throw new ApiException(new Error(ErrorCode.CLAN_TAG_EXISTS, name));
    }
    Clan clan = new Clan();
    clan.setName(name);
    clan.setTag(tag);
    clan.setDescription(description);
    clan.setFounder(creator);
    clan.setLeader(creator);
    ClanMembership membership = new ClanMembership();
    membership.setClan(clan);
    membership.setPlayer(creator);
    clan.setMemberships(Collections.singletonList(membership));
    // clan membership is saved over cascading, otherwise validation will fail
    clanRepository.save(clan);
    return clan;
}
Also used : Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Aggregations

Clan (com.faforever.api.data.domain.Clan)20 Player (com.faforever.api.data.domain.Player)17 Test (org.junit.Test)15 ApiException (com.faforever.api.error.ApiException)10 ClanMembership (com.faforever.api.data.domain.ClanMembership)7 Jwt (org.springframework.security.jwt.Jwt)5 ProgrammingError (com.faforever.api.error.ProgrammingError)4 InvitationResult (com.faforever.api.clan.result.InvitationResult)3 Error (com.faforever.api.error.Error)3 SneakyThrows (lombok.SneakyThrows)3 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ClanResult (com.faforever.api.clan.result.ClanResult)1 MeResult (com.faforever.api.clan.result.MeResult)1 FafApiProperties (com.faforever.api.config.FafApiProperties)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1