mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
Populate user in current context
This commit is contained in:
@ -6,6 +6,7 @@ using Bit.Core.Enums;
|
|||||||
using Bit.Core.Exceptions;
|
using Bit.Core.Exceptions;
|
||||||
using Bit.Core.Models.Api;
|
using Bit.Core.Models.Api;
|
||||||
using Bit.Core.Models.Api.Request;
|
using Bit.Core.Models.Api.Request;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
using Bit.Core.Repositories;
|
using Bit.Core.Repositories;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
@ -23,16 +24,20 @@ namespace Bit.Api.Controllers
|
|||||||
private readonly IOrganizationRepository _organizationRepository;
|
private readonly IOrganizationRepository _organizationRepository;
|
||||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||||
private readonly ICurrentContext _currentContext;
|
private readonly ICurrentContext _currentContext;
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
|
||||||
public OrganizationSponsorshipsController(IOrganizationSponsorshipService organizationSponsorshipService,
|
public OrganizationSponsorshipsController(IOrganizationSponsorshipService organizationSponsorshipService,
|
||||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||||
IOrganizationRepository organizationRepository,
|
IOrganizationRepository organizationRepository,
|
||||||
IOrganizationUserRepository organizationUserRepository,
|
IOrganizationUserRepository organizationUserRepository,
|
||||||
|
IUserService userService,
|
||||||
ICurrentContext currentContext)
|
ICurrentContext currentContext)
|
||||||
{
|
{
|
||||||
_organizationsSponsorshipService = organizationSponsorshipService;
|
_organizationsSponsorshipService = organizationSponsorshipService;
|
||||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||||
_organizationRepository = organizationRepository;
|
_organizationRepository = organizationRepository;
|
||||||
_organizationUserRepository = organizationUserRepository;
|
_organizationUserRepository = organizationUserRepository;
|
||||||
|
_userService = userService;
|
||||||
_currentContext = currentContext;
|
_currentContext = currentContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +72,7 @@ namespace Bit.Api.Controllers
|
|||||||
model.PlanSponsorshipType, model.SponsoredEmail, model.FriendlyName);
|
model.PlanSponsorshipType, model.SponsoredEmail, model.FriendlyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("sponsored/redeem")]
|
[HttpPost("redeem")]
|
||||||
[SelfHosted(NotSelfHostedOnly = true)]
|
[SelfHosted(NotSelfHostedOnly = true)]
|
||||||
public async Task RedeemSponsorship([FromQuery] string sponsorshipToken, [FromBody] OrganizationSponsorshipRedeemRequestModel model)
|
public async Task RedeemSponsorship([FromQuery] string sponsorshipToken, [FromBody] OrganizationSponsorshipRedeemRequestModel model)
|
||||||
{
|
{
|
||||||
@ -81,12 +86,12 @@ namespace Bit.Api.Controllers
|
|||||||
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
|
var existingSponsorshipOffer = await _organizationSponsorshipRepository
|
||||||
.GetByOfferedToEmailAsync(_currentContext.User.Email);
|
.GetByOfferedToEmailAsync((await CurrentUser).Email);
|
||||||
if (existingSponsorshipOffer == null)
|
if (existingSponsorshipOffer == null)
|
||||||
{
|
{
|
||||||
throw new BadRequestException("No unredeemed sponsorship offer exists for you.");
|
throw new BadRequestException("No unredeemed sponsorship offer exists for you.");
|
||||||
}
|
}
|
||||||
if (_currentContext.User.Email != existingSponsorshipOffer.OfferedToEmail)
|
if ((await CurrentUser).Email != existingSponsorshipOffer.OfferedToEmail)
|
||||||
{
|
{
|
||||||
throw new BadRequestException("This sponsorship offer was issued to a different user email address.");
|
throw new BadRequestException("This sponsorship offer was issued to a different user email address.");
|
||||||
}
|
}
|
||||||
@ -170,5 +175,7 @@ namespace Bit.Api.Controllers
|
|||||||
|
|
||||||
await _organizationsSponsorshipService.RemoveSponsorshipAsync(sponsoredOrganization, existingOrgSponsorship);
|
await _organizationsSponsorshipService.RemoveSponsorshipAsync(sponsoredOrganization, existingOrgSponsorship);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Task<User> CurrentUser => _userService.GetUserByIdAsync(_currentContext.UserId.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user