public static async Task<string> SignIn(StartupServer server, AccountingUser user, string password = null) { if (password == null) { password = testPassword; } var signinResult = await Signin(server, user.UserName, password); return signinResult.GenerateCookieHeader(); }
public static Transaction BuildTransaction(AccountingUser user, decimal total = 100, Category fromCat = null, Category toCat = null, TransactionType transType = null) { return new Transaction { Total = total, AccountingUser = user, From = fromCat == null ? new Category { Name = "TestCategoryType" } : fromCat, To = toCat == null ? new Category { Name = "OtherTestCategory" } : toCat, DateTime = DateTime.Now, TransactionType = transType == null ? new TransactionType { Name = "TestTransactionType" } : transType }; }
private async Task SeedDevelopmentData() { if (await this.userManager.FindByEmailAsync("*****@*****.**") == null) { var newUser = new AccountingUser { UserName = "******", Email = "*****@*****.**" }; var result = await this.userManager.CreateAsync(newUser, "password"); } var base64Token = "OMuo2TLAB/iOX4IQs98+ag=="; SignupToken token = null; if (!this.context.SignupTokens.Any(t => t.Token == base64Token)) { this.context.SignupTokens.Add(new SignupToken { Token = base64Token }); } else { token = this.context.SignupTokens.First(t => t.Token == base64Token); token.Used = false; token.AccountingUser = null; token.AccountingUserId = null; this.context.SignupTokens.Update(token); } var user = await this.userManager.FindByNameAsync("testuser"); var expenseType = new CategoryType { Name = "Expenses", DisplayOrder = 2, AddTo = true }; var assetType = new CategoryType { Name = "Assets", DisplayOrder = 1, AddTo = true, AddFrom = false }; var incomeType = new CategoryType { Name = "Income", DisplayOrder = 3, AddFrom = true }; if (!this.context.CategoryTypes.Any(c => c.Name == expenseType.Name)) { this.context.CategoryTypes.Add(expenseType); } else { expenseType = this.context.CategoryTypes.First(c => c.Name == expenseType.Name); } if (!this.context.CategoryTypes.Any(c => c.Name == assetType.Name)) { this.context.CategoryTypes.Add(assetType); } else { assetType = this.context.CategoryTypes.First(c => c.Name == assetType.Name); } if (!this.context.CategoryTypes.Any(c => c.Name == incomeType.Name)) { this.context.CategoryTypes.Add(incomeType); } else { incomeType = this.context.CategoryTypes.First(c => c.Name == incomeType.Name); } var savings = new Category { Name = "Savings", NormalizedName = Category.NormalizeName("Savings"), CategoryType = assetType, AccountingUser = user }; var grocceries = new Category { Name = "Grocceries", NormalizedName = Category.NormalizeName("Grocceries"), CategoryType = expenseType, AccountingUser = user }; var salary = new Category { Name = "Salary", NormalizedName = Category.NormalizeName("Salary"), CategoryType = incomeType, AccountingUser = user }; var work = new Category { Name = "Work", NormalizedName = Category.NormalizeName("Work"), CategoryType = incomeType, AccountingUser = user, ParentCategory = salary }; var subwork = new Category { Name = "Subwork", NormalizedName = Category.NormalizeName("Subwork"), CategoryType = incomeType, AccountingUser = user, ParentCategory = work }; var incomeTransType = new TransactionType { Name = "Income", From = incomeType, To = assetType }; var expenseTransType = new TransactionType { Name = "Expense", From = assetType, To = expenseType }; var transferTransType = new TransactionType { Name = "Transfer", From = assetType, To = assetType }; if (!this.context.Categories.Any(c => c.Name == savings.Name)) { this.context.Categories.Add(savings); this.context.Categories.Add(grocceries); this.context.Categories.Add(salary); this.context.Categories.Add(work); this.context.Categories.Add(subwork); } else { savings = this.context.Categories.SingleOrDefault(c => c.Name == savings.Name); grocceries = this.context.Categories.SingleOrDefault(c => c.Name == grocceries.Name); salary = this.context.Categories.SingleOrDefault(c => c.Name == salary.Name); work = this.context.Categories.SingleOrDefault(c => c.Name == work.Name); subwork = this.context.Categories.SingleOrDefault(c => c.Name == subwork.Name); } if (!this.context.TransactionTypes.Any(t => t.Name == incomeTransType.Name)) { this.context.TransactionTypes.Add(incomeTransType); } else { incomeTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == incomeTransType.Name); } if (!this.context.TransactionTypes.Any(t => t.Name == expenseTransType.Name)) { this.context.TransactionTypes.Add(expenseTransType); } else { expenseTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == expenseTransType.Name); } if (!this.context.TransactionTypes.Any(t => t.Name == transferTransType.Name)) { this.context.TransactionTypes.Add(transferTransType); } else { transferTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == transferTransType.Name); } this.context.SaveChanges(); var trans = new Transaction { From = salary, To = savings, TransactionType = incomeTransType, Total = 100, DateTime = DateTime.UtcNow, AccountingUser = user }; if (!this.context.Transactions.Any()) { this.context.Transactions.Add(trans); trans.To.AddTransaction(trans, from: false); trans.From.AddTransaction(trans, from: true); this.context.SaveChanges(); } }
public async Task<ActionResult> Signup(RegisterViewModel vm) { // If the user is already logged in, ignore their request if (this.User != null && this.User.Identity.IsAuthenticated) { return this.RedirectToAction("Index", "Index"); } this.ViewBag.RequireToken = config.RequireSignupToken; if (this.ModelState.IsValid) { SignupToken token = null; if (config.RequireSignupToken) { token = tokenRepo.FindAllWhere(t => string.Equals(t.Token, vm.Token)).FirstOrDefault(); if (token == null || token.Used) { this.Alert("Invalid sign up token.", "danger"); return this.View(); } } var userExists = this.userRepository.DoesEmailExist(vm.Email) || this.userRepository.DoesUsernameExist(vm.Username); if (userExists) { this.Alert("Username or email already used.", "danger"); return this.View(); } var user = new AccountingUser { UserName = vm.Username, Email = vm.Email }; var createResult = await this.userRepository.CreateAsync(user, vm.Password); if (createResult.Succeeded) { token.Used = true; token.AccountingUser = user; tokenRepo.Update(token); tokenRepo.SaveChanges(); this.CreateCategories(user); await this.signInManager.PasswordSignInAsync(user, vm.Password, true, false); this.Alert("Successfully signed up.", "success"); return this.RedirectToAction("Index", "Index"); } } this.Alert("An error occurred whilst signing up. Please try again.", "danger"); return this.View(); }
private void CreateCategories(AccountingUser user) { var sysCategories = this.sysCategoryRepo.FindAll(c => c.CategoryType).ToList(); foreach (var sysCat in sysCategories) { var userCat = new Category { Name = sysCat.Name, AccountingUser = user, CategoryType = sysCat.CategoryType }; this.categoryRepo.Add(userCat); } this.categoryRepo.SaveChanges(); }
public static async Task CreateUser(HttpContext context, AccountingUser user, string password = null) { AccountUserRepository userRepo = context.RequestServices.GetRequiredService<AccountUserRepository>(); await userRepo.CreateAsync(user, password == null ? testPassword : password); }