mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
update premium license and self host attr checks
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class UpdateLicenseRequestModel
|
||||
{
|
||||
[Required]
|
||||
public IFormFile License { get; set; }
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ namespace Bit.Core.Models.Api
|
||||
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
License = new UserLicense(user, billing, licenseService);
|
||||
Expiration = License.Expires;
|
||||
}
|
||||
|
||||
public BillingResponseModel(User user)
|
||||
@ -29,6 +30,7 @@ namespace Bit.Core.Models.Api
|
||||
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
|
||||
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
Expiration = user.PremiumExpirationDate;
|
||||
}
|
||||
|
||||
public string StorageName { get; set; }
|
||||
@ -39,6 +41,7 @@ namespace Bit.Core.Models.Api
|
||||
public BillingInvoice UpcomingInvoice { get; set; }
|
||||
public IEnumerable<BillingCharge> Charges { get; set; }
|
||||
public UserLicense License { get; set; }
|
||||
public DateTime? Expiration { get; set; }
|
||||
}
|
||||
|
||||
public class BillingSource
|
||||
|
@ -41,6 +41,7 @@ namespace Bit.Core.Services
|
||||
Task<IdentityResult> DeleteAsync(User user, string token);
|
||||
Task SendDeleteConfirmationAsync(string email);
|
||||
Task SignUpPremiumAsync(User user, string paymentToken, short additionalStorageGb, UserLicense license);
|
||||
Task UpdateLicenseAsync(User user, UserLicense license);
|
||||
Task AdjustStorageAsync(User user, short storageAdjustmentGb);
|
||||
Task ReplacePaymentMethodAsync(User user, string paymentToken);
|
||||
Task CancelPremiumAsync(User user, bool endOfPeriod = false);
|
||||
|
@ -598,6 +598,30 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateLicenseAsync(User user, UserLicense license)
|
||||
{
|
||||
if(!_globalSettings.SelfHosted)
|
||||
{
|
||||
throw new InvalidOperationException("Licenses require self hosting.");
|
||||
}
|
||||
|
||||
if(license == null || !_licenseService.VerifyLicense(license))
|
||||
{
|
||||
throw new BadRequestException("Invalid license.");
|
||||
}
|
||||
|
||||
var dir = $"{_globalSettings.LicenseDirectory}/user";
|
||||
Directory.CreateDirectory(dir);
|
||||
File.WriteAllText($"{dir}/{user.Id}.json", JsonConvert.SerializeObject(license, Formatting.Indented));
|
||||
|
||||
user.Premium = true;
|
||||
user.RevisionDate = DateTime.UtcNow;
|
||||
user.MaxStorageGb = 10240; // 10 TB
|
||||
user.LicenseKey = license.LicenseKey;
|
||||
user.PremiumExpirationDate = license.Expires;
|
||||
await SaveUserAsync(user);
|
||||
}
|
||||
|
||||
public async Task AdjustStorageAsync(User user, short storageAdjustmentGb)
|
||||
{
|
||||
if(user == null)
|
||||
|
Reference in New Issue
Block a user