From b011b4e9709c6d34aad81146c266dd45adf06407 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 22 Mar 2018 15:50:56 -0400 Subject: [PATCH] user edit --- .../Controllers/OrganizationsController.cs | 11 +++ src/Admin/Controllers/UsersController.cs | 36 ++++++++ src/Admin/Models/UserEditModel.cs | 65 ++++++++++++++ src/Admin/Views/Organizations/Edit.cshtml | 68 +++++++------- src/Admin/Views/Users/Edit.cshtml | 89 +++++++++++++++++++ src/Admin/Views/Users/Index.cshtml | 2 +- .../SqlServer/OrganizationRepository.cs | 6 +- 7 files changed, 244 insertions(+), 33 deletions(-) create mode 100644 src/Admin/Models/UserEditModel.cs create mode 100644 src/Admin/Views/Users/Edit.cshtml diff --git a/src/Admin/Controllers/OrganizationsController.cs b/src/Admin/Controllers/OrganizationsController.cs index ac5ae9ac59..eca13470fd 100644 --- a/src/Admin/Controllers/OrganizationsController.cs +++ b/src/Admin/Controllers/OrganizationsController.cs @@ -69,5 +69,16 @@ namespace Bit.Admin.Controllers await _organizationRepository.ReplaceAsync(organization); return RedirectToAction("Edit", new { id }); } + + public async Task Delete(Guid id) + { + var organization = await _organizationRepository.GetByIdAsync(id); + if(organization != null) + { + await _organizationRepository.DeleteAsync(organization); + } + + return RedirectToAction("Index"); + } } } diff --git a/src/Admin/Controllers/UsersController.cs b/src/Admin/Controllers/UsersController.cs index 7ee29ee2fd..694d732498 100644 --- a/src/Admin/Controllers/UsersController.cs +++ b/src/Admin/Controllers/UsersController.cs @@ -41,5 +41,41 @@ namespace Bit.Admin.Controllers Count = count }); } + + public async Task Edit(Guid id) + { + var user = await _userRepository.GetByIdAsync(id); + if(user == null) + { + return RedirectToAction("Index"); + } + + return View(new UserEditModel(user)); + } + + [HttpPost] + public async Task Edit(Guid id, UserEditModel model) + { + var user = await _userRepository.GetByIdAsync(id); + if(user == null) + { + return RedirectToAction("Index"); + } + + model.ToUser(user); + await _userRepository.ReplaceAsync(user); + return RedirectToAction("Edit", new { id }); + } + + public async Task Delete(Guid id) + { + var user = await _userRepository.GetByIdAsync(id); + if(user != null) + { + await _userRepository.DeleteAsync(user); + } + + return RedirectToAction("Index"); + } } } diff --git a/src/Admin/Models/UserEditModel.cs b/src/Admin/Models/UserEditModel.cs new file mode 100644 index 0000000000..3d688fb678 --- /dev/null +++ b/src/Admin/Models/UserEditModel.cs @@ -0,0 +1,65 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Bit.Core.Models.Table; + +namespace Bit.Admin.Models +{ + public class UserEditModel + { + public UserEditModel() { } + + public UserEditModel(User user) + { + User = user; + Name = user.Name; + Email = user.Email; + EmailVerified = user.EmailVerified; + Premium = user.Premium; + MaxStorageGb = user.MaxStorageGb; + Gateway = user.Gateway; + GatewayCustomerId = user.GatewayCustomerId; + GatewaySubscriptionId = user.GatewaySubscriptionId; + LicenseKey = user.LicenseKey; + PremiumExpirationDate = user.PremiumExpirationDate; + } + + public User User { get; set; } + + [Display(Name = "Name")] + public string Name { get; set; } + [Required] + [Display(Name = "Email")] + public string Email { get; set; } + [Display(Name = "Email Verified")] + public bool EmailVerified { get; set; } + [Display(Name = "Premium")] + public bool Premium { get; set; } + [Display(Name = "Max. Storage GB")] + public short? MaxStorageGb { get; set; } + [Display(Name = "Gateway")] + public Core.Enums.GatewayType? Gateway { get; set; } + [Display(Name = "Gateway Customer Id")] + public string GatewayCustomerId { get; set; } + [Display(Name = "Gateway Subscription Id")] + public string GatewaySubscriptionId { get; set; } + [Display(Name = "License Key")] + public string LicenseKey { get; set; } + [Display(Name = "Premium Expiration Date")] + public DateTime? PremiumExpirationDate { get; set; } + + public User ToUser(User existingUser) + { + existingUser.Name = Name; + existingUser.Email = Email; + existingUser.EmailVerified = EmailVerified; + existingUser.Premium = Premium; + existingUser.MaxStorageGb = MaxStorageGb; + existingUser.Gateway = Gateway; + existingUser.GatewayCustomerId = GatewayCustomerId; + existingUser.GatewaySubscriptionId = GatewaySubscriptionId; + existingUser.LicenseKey = LicenseKey; + existingUser.PremiumExpirationDate = PremiumExpirationDate; + return existingUser; + } + } +} diff --git a/src/Admin/Views/Organizations/Edit.cshtml b/src/Admin/Views/Organizations/Edit.cshtml index d8ca67a31c..97430033d5 100644 --- a/src/Admin/Views/Organizations/Edit.cshtml +++ b/src/Admin/Views/Organizations/Edit.cshtml @@ -8,7 +8,7 @@

General

-
+
@@ -21,13 +21,13 @@

Business Information

-
+
-
+
@@ -35,13 +35,13 @@
-
+
-
+
@@ -49,13 +49,13 @@
-
+
-
+
-
+
@@ -331,19 +331,19 @@
-
+
-
+
-
+
@@ -375,15 +375,30 @@
+

Licensing

+
+
+
+ + +
+
+
+
+ + +
+
+

Billing

-
+
-
+
@@ -396,33 +411,24 @@
-
+
-
+
-

Licensing

-
-
-
- - -
-
-
-
- - -
-
+
+ + + Delete +
- diff --git a/src/Admin/Views/Users/Edit.cshtml b/src/Admin/Views/Users/Edit.cshtml new file mode 100644 index 0000000000..d7ff5c0d93 --- /dev/null +++ b/src/Admin/Views/Users/Edit.cshtml @@ -0,0 +1,89 @@ +@model UserEditModel +@{ + ViewData["Title"] = "User Edit: " + Model.User.Email; +} + +

Edit User @Model.User.Email

+ +
+

General

+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+

Premium

+
+
+
+ + +
+
+
+
+ + +
+

Licensing

+
+
+
+ + +
+
+
+
+ + +
+
+
+

Billing

+
+
+
+
+ + +
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + + Delete + +
+
diff --git a/src/Admin/Views/Users/Index.cshtml b/src/Admin/Views/Users/Index.cshtml index 5fc662b9d2..c997770690 100644 --- a/src/Admin/Views/Users/Index.cshtml +++ b/src/Admin/Views/Users/Index.cshtml @@ -33,7 +33,7 @@ { - @user.Email + @user.Email diff --git a/src/Core/Repositories/SqlServer/OrganizationRepository.cs b/src/Core/Repositories/SqlServer/OrganizationRepository.cs index a00efdce43..7610f9c3f2 100644 --- a/src/Core/Repositories/SqlServer/OrganizationRepository.cs +++ b/src/Core/Repositories/SqlServer/OrganizationRepository.cs @@ -55,7 +55,11 @@ namespace Bit.Core.Repositories.SqlServer new { Name = name, UserEmail = userEmail, Paid = paid, Skip = skip, Take = take }, commandType: CommandType.StoredProcedure); - return results.ToList(); + // Select distinct results by Id + return results + .GroupBy(c => c.Id) + .Select(g => g.First()) + .ToList(); } }