diff --git a/Teknik/Areas/Admin/AdminAreaRegistration.cs b/Teknik/Areas/Admin/AdminAreaRegistration.cs index 7139aae..439a97a 100644 --- a/Teknik/Areas/Admin/AdminAreaRegistration.cs +++ b/Teknik/Areas/Admin/AdminAreaRegistration.cs @@ -68,6 +68,9 @@ namespace Teknik.Areas.Admin BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/UploadSearch", config.CdnHost).Include( "~/Scripts/bootbox/bootbox.min.js", "~/Areas/Admin/Scripts/UploadSearch.js")); + + BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/UserInfo", config.CdnHost).Include( + "~/Areas/Admin/Scripts/UserInfo.js")); } } } \ No newline at end of file diff --git a/Teknik/Areas/Admin/Controllers/AdminController.cs b/Teknik/Areas/Admin/Controllers/AdminController.cs index bea680e..9bc6641 100644 --- a/Teknik/Areas/Admin/Controllers/AdminController.cs +++ b/Teknik/Areas/Admin/Controllers/AdminController.cs @@ -4,10 +4,12 @@ using System.Linq; using System.Web; using System.Web.Mvc; using Teknik.Areas.Admin.ViewModels; +using Teknik.Areas.Users.Models; using Teknik.Areas.Users.Utility; using Teknik.Attributes; using Teknik.Controllers; using Teknik.Models; +using Teknik.Utilities; using Teknik.ViewModels; namespace Teknik.Areas.Admin.Controllers @@ -34,9 +36,15 @@ namespace Teknik.Areas.Admin.Controllers [HttpGet] public ActionResult UserInfo(string username) { - UserInfoViewModel model = new UserInfoViewModel(); - model.Username = username; - return View(model); + if (UserHelper.UserExists(db, username)) + { + User user = UserHelper.GetUser(db, username); + UserInfoViewModel model = new UserInfoViewModel(); + model.Username = user.Username; + model.AccountType = user.AccountType; + return View(model); + } + return Redirect(Url.SubRouteUrl("error", "Error.Http404")); } [HttpGet] @@ -90,5 +98,18 @@ namespace Teknik.Areas.Admin.Controllers } return Json(new { error = new { message = "Upload does not exist" } }); } + + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult EditUserAccountType(string username, AccountType accountType) + { + if (UserHelper.UserExists(db, username)) + { + // Edit the user's account type + UserHelper.EditAccountType(db, Config, username, accountType); + return Json(new { result = new { success = true } }); + } + return Redirect(Url.SubRouteUrl("error", "Error.Http404")); + } } } \ No newline at end of file diff --git a/Teknik/Areas/Admin/Scripts/UserInfo.js b/Teknik/Areas/Admin/Scripts/UserInfo.js new file mode 100644 index 0000000..05cd552 --- /dev/null +++ b/Teknik/Areas/Admin/Scripts/UserInfo.js @@ -0,0 +1,26 @@ +$(function () { + + $('.userAccountType').on('change', function () { + var selected = $(this).find("option:selected").val(); + + $.ajax({ + type: "POST", + url: editAccountType, + data: AddAntiForgeryToken({ username: username, accountType: selected }), + success: function (html) { + if (html) { + if (html.error) { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + html.error.message + '
'); + } + else { + $("#top_msg").css('display', 'none'); + $("#top_msg").html(''); + alert('Successfully changed the account type for \'' + username + '\' to type: ' + selected); + } + } + } + }); + }); + +}); \ No newline at end of file diff --git a/Teknik/Areas/Admin/ViewModels/UserInfoViewModel.cs b/Teknik/Areas/Admin/ViewModels/UserInfoViewModel.cs index 6aa8bbc..109a11b 100644 --- a/Teknik/Areas/Admin/ViewModels/UserInfoViewModel.cs +++ b/Teknik/Areas/Admin/ViewModels/UserInfoViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Web; +using Teknik.Utilities; using Teknik.ViewModels; namespace Teknik.Areas.Admin.ViewModels @@ -9,5 +10,6 @@ namespace Teknik.Areas.Admin.ViewModels public class UserInfoViewModel : ViewModelBase { public string Username { get; set; } + public AccountType AccountType { get; set; } } } \ No newline at end of file diff --git a/Teknik/Areas/Admin/Views/Admin/UserInfo.cshtml b/Teknik/Areas/Admin/Views/Admin/UserInfo.cshtml index 5d3ae42..cb02e02 100644 --- a/Teknik/Areas/Admin/Views/Admin/UserInfo.cshtml +++ b/Teknik/Areas/Admin/Views/Admin/UserInfo.cshtml @@ -2,8 +2,30 @@ @using Teknik.Utilities -
-
- @Model.Username + + +@Scripts.Render("~/bundles/UserInfo") + +
+
+ +
+
+
Account Type: + +
-
+
\ No newline at end of file diff --git a/Teknik/Areas/User/Utility/UserHelper.cs b/Teknik/Areas/User/Utility/UserHelper.cs index 4556f8c..0d402dc 100644 --- a/Teknik/Areas/User/Utility/UserHelper.cs +++ b/Teknik/Areas/User/Utility/UserHelper.cs @@ -216,6 +216,47 @@ namespace Teknik.Areas.Users.Utility } } + public static void EditAccountType(TeknikEntities db, Config config, string username, AccountType type) + { + try + { + if (!UserExists(db, username)) + throw new Exception($"The user provided does not exist: {username}"); + + // Get the user to edit + User user = GetUser(db, username); + + string email = GetUserEmailAddress(config, username); + + // Edit the user type + user.AccountType = type; + EditUser(db, config, user); + + // Add/Remove account type features depending on the type + switch (type) + { + case AccountType.Basic: + // Set the email size to 1GB + EditUserEmailMaxSize(config, email, config.EmailConfig.MaxSize); + + // Set the email max/day to 100 + EditUserEmailMaxEmailsPerDay(config, email, 100); + break; + case AccountType.Premium: + // Set the email size to 5GB + EditUserEmailMaxSize(config, email, 5000); + + // Set the email max/day to infinite (-1) + EditUserEmailMaxEmailsPerDay(config, email, -1); + break; + } + } + catch (Exception ex) + { + throw new Exception($"Unable to edit the account type [{type}] for: {username}", ex); + } + } + public static void DeleteAccount(TeknikEntities db, Config config, User user) { try @@ -426,6 +467,11 @@ namespace Teknik.Areas.Users.Utility } } + public static void EditUser(TeknikEntities db, Config config, User user) + { + EditUser(db, config, user, false, string.Empty); + } + public static void EditUser(TeknikEntities db, Config config, User user, bool changePass, string password) { try @@ -827,6 +873,50 @@ If you recieved this email and you did not reset your password, you can ignore t } } + public static void EditUserEmailMaxSize(Config config, string email, int size) + { + try + { + // If Email Server is enabled + if (config.EmailConfig.Enabled) + { + var app = new hMailServer.Application(); + app.Connect(); + app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password); + var domain = app.Domains.ItemByName[config.EmailConfig.Domain]; + var account = domain.Accounts.ItemByAddress[email]; + account.MaxSize = size; + account.Save(); + } + } + catch (Exception ex) + { + throw new Exception("Unable to edit email account mailbox size.", ex); + } + } + + public static void EditUserEmailMaxEmailsPerDay(Config config, string email, int maxPerDay) + { + try + { + // If Email Server is enabled + if (config.EmailConfig.Enabled) + { + // Query the Email DB + + // We need to check the actual git database + MysqlDatabase mySQL = new MysqlDatabase(config.EmailConfig.CounterDatabase.Server, config.EmailConfig.CounterDatabase.Database, config.EmailConfig.CounterDatabase.Username, config.EmailConfig.CounterDatabase.Password, config.EmailConfig.CounterDatabase.Port); + string sql = @"INSERT INTO mailcounter.counts (qname, lastdate, qlimit, count) VALUES ({1}, NOW(), {0}, 0) + ON DUPLICATE KEY UPDATE qlimit = {0}"; + mySQL.Execute(sql, new object[] { maxPerDay, email }); + } + } + catch (Exception ex) + { + throw new Exception("Unable to edit email account mailbox size.", ex); + } + } + public static void DeleteUserEmail(Config config, string email) { try diff --git a/Teknik/Scripts/_references.js b/Teknik/Scripts/_references.js index d9fbe68..382c9b1 100644 Binary files a/Teknik/Scripts/_references.js and b/Teknik/Scripts/_references.js differ diff --git a/Teknik/Teknik.csproj b/Teknik/Teknik.csproj index aa12f71..d6ca611 100644 --- a/Teknik/Teknik.csproj +++ b/Teknik/Teknik.csproj @@ -1,5 +1,7 @@  + + Debug @@ -89,6 +91,10 @@ ..\packages\Microsoft.Azure.KeyVault.Core.2.0.4\lib\net45\Microsoft.Azure.KeyVault.Core.dll True + + ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + True + ..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll @@ -382,6 +388,7 @@ + @@ -855,6 +862,8 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + +