1
0
mirror of https://github.com/bitwarden/server synced 2025-12-26 13:13:24 +00:00

WIP: scaffolding for families for enterprise sponsorship flow

This commit is contained in:
Matt Gibson
2021-10-30 13:34:03 -04:00
committed by Justin Baur
parent f9fd83d809
commit 0d9c0bdaea
7 changed files with 258 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Bit.Core.Context;
using Bit.Core.Enums;
@@ -35,7 +36,7 @@ namespace Bit.Api.Controllers
}
[HttpPost("{sponsoringOrgId}/families-for-enterprise")]
public async Task<IActionResult> CreateSponsorship(string sponsoringOrgId, [FromBody] OrganizationSponsorshipRequestModel model)
public async Task CreateSponsorship(string sponsoringOrgId, [FromBody] OrganizationSponsorshipRequestModel model)
{
// TODO: validate has right to sponsor, send sponsorship email
var sponsoringOrgIdGuid = new Guid(sponsoringOrgId);
@@ -50,32 +51,45 @@ namespace Bit.Api.Controllers
{
throw new BadRequestException("Only confirm users can sponsor other organizations.");
}
if (sponsoringOrgUser.UserId != _currentContext.UserId)
{
throw new BadRequestException("Can only create organization sponsorships for yourself.");
}
var existingOrgSponsorship = await _organizationSponsorshipRepository.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id);
if (existingOrgSponsorship != null)
{
throw new BadRequestException("Can only sponsor one organization per Organization User");
throw new BadRequestException("Can only sponsor one organization per Organization User.");
}
// TODO: send sponsorship email
throw new NotImplementedException();
await _organizationsSponsorshipService.OfferSponsorshipAsync(sponsoringOrg, sponsoringOrgUser, model.sponsoredEmail);
}
[HttpPost("sponsored/redeem/families-for-enterprise")]
public async Task<IActionResult> RedeemSponsorship([FromQuery] string sponsorshipInfo, [FromBody] OrganizationSponsorshipRedeemRequestModel model)
public async Task RedeemSponsorship([FromQuery] string sponsorshipInfo, [FromBody] OrganizationSponsorshipRedeemRequestModel model)
{
// TODO: parse out sponsorshipInfo
if (!await _currentContext.OrganizationOwner(model.SponsoredOrganizationId))
{
throw new BadRequestException("Can only redeem sponsorship for and organization you own");
throw new BadRequestException("Can only redeem sponsorship for an organization you own");
}
var existingSponsorshipOffer = await _organizationSponsorshipRepository
.GetByOfferedToEmailAsync(_currentContext.User.Email);
if (existingSponsorshipOffer == null)
{
throw new BadRequestException("No unredeemed sponsorship offer exists for you.");
}
var existingOrgSponsorship = await _organizationSponsorshipRepository.GetBySponsoredOrganizationIdAsync(model.SponsoredOrganizationId);
var existingOrgSponsorship = await _organizationSponsorshipRepository
.GetBySponsoredOrganizationIdAsync(model.SponsoredOrganizationId);
if (existingOrgSponsorship != null)
{
throw new BadRequestException("Cannot redeem a sponsorship offer for and organization that is already sponsored. Revoke existing sponsorship first.");
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);
@@ -85,36 +99,34 @@ namespace Bit.Api.Controllers
throw new BadRequestException("Can only redeem sponsorship offer on families organizations");
}
// TODO: check user is owner of proposed org, it isn't currently sponsored, and set up sponsorship
throw new NotImplementedException();
await _organizationsSponsorshipService.SetUpSponsorshipAsync(existingSponsorshipOffer, organizationToSponsor);
}
[HttpDelete("{sponsoringOrgId}/{sponsoringOrgUserId}")]
[HttpPost("{sponsoringOrgId}/{sponsoringOrgUserId}/delete")]
public async Task<IActionResult> RevokeSponsorship(string sponsoringOrgId, string sponsoringOrgUserId)
[HttpDelete("{sponsoringOrgUserId}")]
[HttpPost("{sponsoringOrgUserId}/delete")]
public async Task RevokeSponsorship(string sponsoringOrgUserId)
{
var sponsoringOrgIdGuid = new Guid(sponsoringOrgId);
var sponsoringOrgUserIdGuid = new Guid(sponsoringOrgUserId);
var orgUser = await _organizationUserRepository.GetByIdAsync(sponsoringOrgUserIdGuid);
if (_currentContext.UserId != orgUser?.UserId)
{
throw new BadRequestException("Can only revoke a sponsorship you own.");
throw new BadRequestException("Can only revoke a sponsorship you granted.");
}
var existingOrgSponsorship = await _organizationSponsorshipRepository.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUserIdGuid);
var existingOrgSponsorship = await _organizationSponsorshipRepository
.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUserIdGuid);
if (existingOrgSponsorship == null)
{
throw new BadRequestException("You are not currently sponsoring and organization.");
}
// TODO: remove sponsorship
throw new NotImplementedException();
await _organizationsSponsorshipService.RemoveSponsorshipAsync(existingOrgSponsorship);
}
[HttpDelete("sponsored/{sponsoredOrgId}")]
[HttpPost("sponsored/{sponsoredOrgId}/remove")]
public async Task<IActionResult> RemoveSponsorship(string sponsoredOrgId)
public async Task RemoveSponsorship(string sponsoredOrgId)
{
var sponsoredOrgIdGuid = new Guid(sponsoredOrgId);
@@ -123,14 +135,14 @@ namespace Bit.Api.Controllers
throw new BadRequestException("Only the owner of an organization can remove sponsorship.");
}
var existingOrgSponsorship = await _organizationSponsorshipRepository.GetBySponsoringOrganizationUserIdAsync(sponsoredOrgIdGuid);
var existingOrgSponsorship = await _organizationSponsorshipRepository
.GetBySponsoredOrganizationIdAsync(sponsoredOrgIdGuid);
if (existingOrgSponsorship == null)
{
throw new BadRequestException("The requested organization is not currently being sponsored");
throw new BadRequestException("The requested organization is not currently being sponsored.");
}
// TODO: remove sponsorship
throw new NotImplementedException();
await _organizationsSponsorshipService.RemoveSponsorshipAsync(existingOrgSponsorship);
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Table
@@ -7,10 +8,15 @@ namespace Bit.Core.Models.Table
{
public Guid Id { get; set; }
public Guid InstallationId { get; set; }
[Required]
public Guid SponsoringOrganizationId { get; set; }
[Required]
public Guid SponsoringOrganizationUserId { get; set; }
public Guid SponsoringUserId { get; set; }
[MaxLength(256)]
public string OfferedToEmail { get; set; }
public Guid? SponsoredOrganizationId { get; set; }
[Required]
public bool CloudSponsor { get; set; }
public DateTime? LastSyncDate { get; set; }
public byte TimesRenewedWithoutValidation { get; set; }

View File

@@ -10,5 +10,6 @@ namespace Bit.Core.Repositories
{
Task<OrganizationSponsorship> GetBySponsoringOrganizationUserIdAsync(Guid sponsoringOrganizationUserId);
Task<OrganizationSponsorship> GetBySponsoredOrganizationIdAsync(Guid sponsoredOrganizationId);
Task<OrganizationSponsorship> GetByOfferedToEmailAsync(string email);
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
@@ -26,7 +25,10 @@ namespace Bit.Core.Repositories.SqlServer
{
var results = await connection.QueryAsync<OrganizationSponsorship>(
"[dbo].[OrganizationSponsorship_ReadBySponsoringOrganizationUserId]",
new { SponsoringOrganizationUserId = sponsoringOrganizationUserId },
new
{
SponsoringOrganizationUserId = sponsoringOrganizationUserId
},
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
@@ -45,5 +47,21 @@ namespace Bit.Core.Repositories.SqlServer
return results.SingleOrDefault();
}
}
public async Task<OrganizationSponsorship> GetByOfferedToEmailAsync(string offeredToEmail)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<OrganizationSponsorship>(
"[dbo].[OrganizationSponsorship_ReadByOfferedToEmail]",
new
{
OfferedToEmail = offeredToEmail
},
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
}
}
}
}

View File

@@ -1,7 +1,12 @@
using System.Threading.Tasks;
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
public interface IOrganizationSponsorshipService
{
Task OfferSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser, string sponsoredEmail);
Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship, Organization sponsoredOrganization);
Task RemoveSponsorshipAsync(OrganizationSponsorship sponsorship);
}
}

View File

@@ -1,3 +1,6 @@
using System;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using Bit.Core.Repositories;
namespace Bit.Core.Services
@@ -10,5 +13,24 @@ namespace Bit.Core.Services
{
_organizationSponsorshipRepository = organizationSponsorshipRepository;
}
public async Task OfferSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser, string sponsoredEmail)
{
// TODO: send sponsorship email, update sponsorship with offered email
throw new NotImplementedException();
}
public async Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship, Organization sponsoredOrganization)
{
// TODO: set up sponsorship
throw new NotImplementedException();
}
public async Task RemoveSponsorshipAsync(OrganizationSponsorship sponsorship)
{
// TODO: remove sponsorship
throw new NotImplementedException();
}
}
}