1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-03 00:52:49 -05:00

Create sponsorship offer (#1688)

This commit is contained in:
Matt Gibson
2021-11-04 08:25:40 -05:00
committed by Justin Baur
parent d7642d692b
commit 1b6d1b52a3
6 changed files with 351 additions and 13 deletions

View File

@ -8,6 +8,7 @@ using Bit.Core.Models.Api;
using Bit.Core.Models.Api.Request;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -36,6 +37,7 @@ namespace Bit.Api.Controllers
}
[HttpPost("{sponsoringOrgId}/families-for-enterprise")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task CreateSponsorship(string sponsoringOrgId, [FromBody] OrganizationSponsorshipRequestModel model)
{
// TODO: validate has right to sponsor, send sponsorship email
@ -66,13 +68,18 @@ namespace Bit.Api.Controllers
}
[HttpPost("sponsored/redeem/families-for-enterprise")]
public async Task RedeemSponsorship([FromQuery] string sponsorshipInfo, [FromBody] OrganizationSponsorshipRedeemRequestModel model)
[SelfHosted(NotSelfHostedOnly = true)]
public async Task RedeemSponsorship([FromQuery] string sponsorshipToken, [FromBody] OrganizationSponsorshipRedeemRequestModel model)
{
// TODO: parse out sponsorshipInfo
if (!await _organizationsSponsorshipService.ValidateRedemptionTokenAsync(sponsorshipToken))
{
throw new BadRequestException("Failed to parse sponsorship token.");
}
if (!await _currentContext.OrganizationOwner(model.SponsoredOrganizationId))
{
throw new BadRequestException("Can only redeem sponsorship for an organization you own");
throw new BadRequestException("Can only redeem sponsorship for an organization you own.");
}
var existingSponsorshipOffer = await _organizationSponsorshipRepository
.GetByOfferedToEmailAsync(_currentContext.User.Email);
@ -80,6 +87,10 @@ namespace Bit.Api.Controllers
{
throw new BadRequestException("No unredeemed sponsorship offer exists for you.");
}
if (_currentContext.User.Email != existingSponsorshipOffer.OfferedToEmail)
{
throw new BadRequestException("This sponsorship offer was issued to a different user email address.");
}
var existingOrgSponsorship = await _organizationSponsorshipRepository
.GetBySponsoredOrganizationIdAsync(model.SponsoredOrganizationId);
@ -87,16 +98,12 @@ namespace Bit.Api.Controllers
{
throw new BadRequestException("Cannot redeem a sponsorship offer for an organization that is already sponsored. Revoke existing sponsorship first.");
}
if (_currentContext.User.Email != existingOrgSponsorship.OfferedToEmail)
{
throw new BadRequestException("This sponsorship offer was issued to a different user email address.");
}
var organizationToSponsor = await _organizationRepository.GetByIdAsync(model.SponsoredOrganizationId);
// TODO: only current families plan?
if (organizationToSponsor == null || !PlanTypeHelper.HasFamiliesPlan(organizationToSponsor))
{
throw new BadRequestException("Can only redeem sponsorship offer on families organizations");
throw new BadRequestException("Can only redeem sponsorship offer on families organizations.");
}
await _organizationsSponsorshipService.SetUpSponsorshipAsync(existingSponsorshipOffer, organizationToSponsor);
@ -104,6 +111,7 @@ namespace Bit.Api.Controllers
[HttpDelete("{sponsoringOrgUserId}")]
[HttpPost("{sponsoringOrgUserId}/delete")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task RevokeSponsorship(string sponsoringOrgUserId)
{
var sponsoringOrgUserIdGuid = new Guid(sponsoringOrgUserId);
@ -126,6 +134,7 @@ namespace Bit.Api.Controllers
[HttpDelete("sponsored/{sponsoredOrgId}")]
[HttpPost("sponsored/{sponsoredOrgId}/remove")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task RemoveSponsorship(string sponsoredOrgId)
{
var sponsoredOrgIdGuid = new Guid(sponsoredOrgId);