1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-23 17:07:42 +01:00

some helper functions for users and orgs

This commit is contained in:
Kyle Spearrin 2018-03-22 17:33:22 -04:00
parent b011b4e970
commit 7075d8396d
9 changed files with 227 additions and 20 deletions

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Bit.Admin.Models;
using System.Collections.Generic;
using Bit.Core.Models.Table;
using Bit.Core;
namespace Bit.Admin.Controllers
{
@ -13,10 +14,14 @@ namespace Bit.Admin.Controllers
public class OrganizationsController : Controller
{
private readonly IOrganizationRepository _organizationRepository;
private readonly GlobalSettings _globalSettings;
public OrganizationsController(IOrganizationRepository organizationRepository)
public OrganizationsController(
IOrganizationRepository organizationRepository,
GlobalSettings globalSettings)
{
_organizationRepository = organizationRepository;
_globalSettings = globalSettings;
}
public async Task<IActionResult> Index(string name = null, string userEmail = null, bool? paid = null,
@ -53,7 +58,7 @@ namespace Bit.Admin.Controllers
return RedirectToAction("Index");
}
return View(new OrganizationEditModel(organization));
return View(new OrganizationEditModel(organization, _globalSettings));
}
[HttpPost]

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Bit.Admin.Models;
using System.Collections.Generic;
using Bit.Core.Models.Table;
using Bit.Core;
namespace Bit.Admin.Controllers
{
@ -13,10 +14,14 @@ namespace Bit.Admin.Controllers
public class UsersController : Controller
{
private readonly IUserRepository _userRepository;
private readonly GlobalSettings _globalSettings;
public UsersController(IUserRepository userRepository)
public UsersController(
IUserRepository userRepository,
GlobalSettings globalSettings)
{
_userRepository = userRepository;
_globalSettings = globalSettings;
}
public async Task<IActionResult> Index(string email, int page = 1, int count = 25)
@ -50,7 +55,7 @@ namespace Bit.Admin.Controllers
return RedirectToAction("Index");
}
return View(new UserEditModel(user));
return View(new UserEditModel(user, _globalSettings));
}
[HttpPost]

View File

@ -1,6 +1,8 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Core;
using Bit.Core.Models.Table;
using Bit.Core.Utilities;
namespace Bit.Admin.Models
{
@ -8,9 +10,11 @@ namespace Bit.Admin.Models
{
public OrganizationEditModel() { }
public OrganizationEditModel(Organization org)
public OrganizationEditModel(Organization org, GlobalSettings globalSettings)
{
Organization = org;
BraintreeMerchantId = globalSettings.Braintree.MerchantId;
Name = org.Name;
BusinessName = org.BusinessName;
BusinessAddress1 = org.BusinessAddress1;
@ -39,6 +43,9 @@ namespace Bit.Admin.Models
}
public Organization Organization { get; set; }
public string RandomLicenseKey => CoreHelpers.SecureRandomString(20);
public string FourteenDayExpirationDate => DateTime.Now.AddDays(14).ToString("yyyy-MM-ddTHH:mm");
public string BraintreeMerchantId { get; set; }
[Required]
[Display(Name = "Name")]

View File

@ -1,6 +1,8 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Core;
using Bit.Core.Models.Table;
using Bit.Core.Utilities;
namespace Bit.Admin.Models
{
@ -8,9 +10,11 @@ namespace Bit.Admin.Models
{
public UserEditModel() { }
public UserEditModel(User user)
public UserEditModel(User user, GlobalSettings globalSettings)
{
User = user;
BraintreeMerchantId = globalSettings.Braintree.MerchantId;
Name = user.Name;
Email = user.Email;
EmailVerified = user.EmailVerified;
@ -24,6 +28,9 @@ namespace Bit.Admin.Models
}
public User User { get; set; }
public string RandomLicenseKey => CoreHelpers.SecureRandomString(20);
public string OneYearExpirationDate => DateTime.Now.AddYears(1).ToString("yyyy-MM-ddTHH:mm");
public string BraintreeMerchantId { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }

View File

@ -3,6 +3,74 @@
ViewData["Title"] = "Organization Edit: " + Model.Organization.Name;
}
@section Scripts {
<script>
(function() {
document.getElementById('enterprise-trial').addEventListener('click', function () {
if (document.getElementById('@(nameof(Model.PlanType))').value !==
'@((byte)Bit.Core.Enums.PlanType.Free)') {
alert('Organization is not on a free plan.');
return;
}
// Plan
document.getElementById('@(nameof(Model.PlanType))').value =
'@((byte)Bit.Core.Enums.PlanType.EnterpriseAnnually)';
document.getElementById('@(nameof(Model.Plan))').value = 'Enterprise (Trial)';
document.getElementById('@(nameof(Model.Seats))').value = '10';
document.getElementById('@(nameof(Model.MaxCollections))').value = '';
document.getElementById('@(nameof(Model.MaxStorageGb))').value = '1';
// Features
document.getElementById('@(nameof(Model.UseGroups))').checked = true;
document.getElementById('@(nameof(Model.UseDirectory))').checked = true;
document.getElementById('@(nameof(Model.UseEvents))').checked = true;
document.getElementById('@(nameof(Model.UsersGetPremium))').checked = true;
document.getElementById('@(nameof(Model.UseTotp))').checked = true;
document.getElementById('@(nameof(Model.SelfHost))').checked = true;
// Licensing
document.getElementById('@(nameof(Model.LicenseKey))').value = '@Model.RandomLicenseKey';
document.getElementById('@(nameof(Model.ExpirationDate))').value = '@Model.FourteenDayExpirationDate';
});
document.getElementById('@(nameof(Model.PlanType))').addEventListener('change', function() {
var selectEl = document.getElementById('@(nameof(Model.PlanType))');
var selectText = selectEl.options[selectEl.selectedIndex].text;
document.getElementById('@(nameof(Model.Plan))').value = selectText;
});
document.getElementById('gateway-customer-link').addEventListener('click', function () {
var gateway = document.getElementById('@(nameof(Model.Gateway))');
var customerId = document.getElementById('@(nameof(Model.GatewayCustomerId))');
if (!gateway || gateway.value === '' || !customerId || customerId.value === '') {
return;
}
if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
window.open('https://dashboard.stripe.com/customers/' + customerId.value, '_blank');
} else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/'
+ customerId.value, '_blank');
}
});
document.getElementById('gateway-subscription-link').addEventListener('click', function () {
var gateway = document.getElementById('@(nameof(Model.Gateway))');
var subId = document.getElementById('@(nameof(Model.GatewaySubscriptionId))');
if (!gateway || gateway.value === '' || !subId || subId.value === '') {
return;
}
if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
window.open('https://dashboard.stripe.com/subscriptions/' + subId.value, '_blank');
} else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/' +
'subscriptions/' + subId.value, '_blank');
}
});
})();
</script>
}
<h1>Edit Organization <small>@Model.Organization.Name</small></h1>
<form method="post">
@ -414,21 +482,40 @@
<div class="col-sm">
<div class="form-group">
<label asp-for="GatewayCustomerId"></label>
<input type="text" class="form-control" asp-for="GatewayCustomerId">
<div class="input-group">
<input type="text" class="form-control" asp-for="GatewayCustomerId">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" id="gateway-customer-link">
<i class="fa fa-external-link"></i>
</button>
</div>
</div>
</div>
</div>
<div class="col-sm">
<div class="form-group">
<label asp-for="GatewaySubscriptionId"></label>
<input type="text" class="form-control" asp-for="GatewaySubscriptionId">
<div class="input-group">
<input type="text" class="form-control" asp-for="GatewaySubscriptionId">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" id="gateway-subscription-link">
<i class="fa fa-external-link"></i>
</button>
</div>
</div>
</div>
</div>
</div>
<div class="d-flex mt-4">
<button type="submit" class="btn btn-primary">Save</button>
<a class="btn btn-danger ml-auto" asp-action="Delete" asp-route-id="@Model.Organization.Id"
onclick="return confirm('Are you sure you want to delete this organization (@Model.Organization.Name)?')">
Delete
</a>
<div class="ml-auto d-flex">
<button class="btn btn-secondary mr-2" type="button" id="enterprise-trial">
Enterprise Trial
</button>
<a class="btn btn-danger" asp-action="Delete" asp-route-id="@Model.Organization.Id"
onclick="return confirm('Are you sure you want to delete this organization (@Model.Organization.Name)?')">
Delete
</a>
</div>
</div>
</form>

View File

@ -50,6 +50,16 @@
&copy; 2015-@DateTime.Now.Year 8bit Solutions LLC
</footer>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<environment include="Development">
<script src="~/js/site.js" asp-append-version="true"></script>
</environment>

View File

@ -3,6 +3,57 @@
ViewData["Title"] = "User Edit: " + Model.User.Email;
}
@section Scripts {
<script>
(function() {
document.getElementById('upgrade-premium').addEventListener('click', function () {
if (document.getElementById('@(nameof(Model.Premium))').checked) {
alert('User is already premium.');
return;
}
// Premium
document.getElementById('@(nameof(Model.MaxStorageGb))').value = '1';
document.getElementById('@(nameof(Model.Premium))').checked = true;
// Licensing
document.getElementById('@(nameof(Model.LicenseKey))').value = '@Model.RandomLicenseKey';
document.getElementById('@(nameof(Model.PremiumExpirationDate))').value =
'@Model.OneYearExpirationDate';
});
document.getElementById('gateway-customer-link').addEventListener('click', function () {
var gateway = document.getElementById('@(nameof(Model.Gateway))');
var customerId = document.getElementById('@(nameof(Model.GatewayCustomerId))');
if (!gateway || gateway.value === '' || !customerId || customerId.value === '') {
return;
}
if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
window.open('https://dashboard.stripe.com/customers/' + customerId.value, '_blank');
} else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/'
+ customerId.value, '_blank');
}
});
document.getElementById('gateway-subscription-link').addEventListener('click', function () {
var gateway = document.getElementById('@(nameof(Model.Gateway))');
var subId = document.getElementById('@(nameof(Model.GatewaySubscriptionId))');
if (!gateway || gateway.value === '' || !subId || subId.value === '') {
return;
}
if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
window.open('https://dashboard.stripe.com/subscriptions/' + subId.value, '_blank');
} else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/' +
'subscriptions/' + subId.value, '_blank');
}
});
})();
</script>
}
<h1>Edit User <small>@Model.User.Email</small></h1>
<form method="post">
@ -69,21 +120,40 @@
<div class="col-sm">
<div class="form-group">
<label asp-for="GatewayCustomerId"></label>
<input type="text" class="form-control" asp-for="GatewayCustomerId">
<div class="input-group">
<input type="text" class="form-control" asp-for="GatewayCustomerId">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" id="gateway-customer-link">
<i class="fa fa-external-link"></i>
</button>
</div>
</div>
</div>
</div>
<div class="col-sm">
<div class="form-group">
<label asp-for="GatewaySubscriptionId"></label>
<input type="text" class="form-control" asp-for="GatewaySubscriptionId">
<div class="input-group">
<input type="text" class="form-control" asp-for="GatewaySubscriptionId">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" id="gateway-subscription-link">
<i class="fa fa-external-link"></i>
</button>
</div>
</div>
</div>
</div>
</div>
<div class="d-flex mt-4">
<button type="submit" class="btn btn-primary">Save</button>
<a class="btn btn-danger ml-auto" asp-action="Delete" asp-route-id="@Model.User.Id"
onclick="return confirm('Are you sure you want to delete this user (@Model.User.Email)?')">
Delete
</a>
<div class="ml-auto d-flex">
<button class="btn btn-secondary mr-2" type="button" id="upgrade-premium">
Upgrade Premium
</button>
<a class="btn btn-danger ml-auto" asp-action="Delete" asp-route-id="@Model.User.Id"
onclick="return confirm('Are you sure you want to delete this user (@Model.User.Email)?')">
Delete
</a>
</div>
</div>
</form>

View File

@ -1,11 +1,18 @@
namespace Bit.Core.Enums
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Enums
{
public enum GatewayType : byte
{
[Display(Name = "Stripe")]
Stripe = 0,
[Display(Name = "Braintree")]
Braintree = 1,
[Display(Name = "Apple App Store")]
AppStore = 2,
[Display(Name = "Google Play Store")]
PlayStore = 3,
[Display(Name = "Coinbase")]
Coinbase = 4
}
}

View File

@ -1,13 +1,22 @@
namespace Bit.Core.Enums
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Enums
{
public enum PlanType : byte
{
[Display(Name = "Free")]
Free = 0,
[Display(Name = "Families")]
FamiliesAnnually = 1,
[Display(Name = "Teams (Monthly)")]
TeamsMonthly = 2,
[Display(Name = "Teams (Annually)")]
TeamsAnnually = 3,
[Display(Name = "Enterprise (Monthly)")]
EnterpriseMonthly = 4,
[Display(Name = "Enterprise (Annually)")]
EnterpriseAnnually = 5,
[Display(Name = "Custom")]
Custom = 6
}
}