public BusinessAccountInformation CreateNewBusinessAccount(BusinessAccountInformation command) { try { // Add transactions // 1. Save Business var businessAccount = new Qup.Database.Business() { Name = command.BusinessName, Address = command.BusinessAddress, Capacity = command.Capacity, IsActive = true, DateCreated = DateTime.Now }; _context.Businesses.Add(businessAccount); // 2. Save Business Admin // 2.1 generate password Hash var encryptedCredentials = GenericUtilityService.EncryptPassword(command.AdminPassword); // Next item var businessAdmin = new Qup.Database.User() { FirstName = command.AdminFirstName, LastName = command.AdminLastName, Email = command.AdminEmail, PhoneNumber = command.AdminPhone, DateCreated = DateTime.Now, Salt = encryptedCredentials.Salt, UserPassword = encryptedCredentials.EncryptedPassword }; _context.Users.Add(businessAdmin); _context.SaveChanges(); command.BusinessId = businessAccount.Id; // Link User To UserGroup var mapUserToGroup = new UsersToUserGroup() { UserId = businessAdmin.Id, UserGroupId = 2 // Bar Admin user group }; _context.UsersToUserGroups.Add(mapUserToGroup); _context.SaveChanges(); // Generate QR code - just for now - move this to Business Logic GenerateQuickResponseCodeForNewBusiness(businessAccount.Id); command.BusinessAccountCreated = true; } catch (Exception e) { command.BusinessAccountCreated = false; } return(command); }
public void RegisterCustomerInQueue(UserRegistration userInstruction) { var user = new Qup.Database.UserLedger() { Name = userInstruction.Name, Email = userInstruction.Email, PhoneNumber = userInstruction.Phone, DateCreated = DateTime.Now, BusinessId = 1 // Change this later }; dbContext.UserLedgers.Add(user); dbContext.SaveChanges(); // Save Queue Ledger var queueHandler = new QueueLedger(); queueHandler.AddCustomerToQueue(new QueueInstruction { BusinessId = 1, QueueJoinTime = DateTime.Now, ExpectedEntryTime = DateTime.Now, ActualEntryTime = DateTime.Now, UserLedgerId = user.Id }); }
public void AddCustomerToQueue(QueueInstruction instruction) { var queueInstruction = new Database.QueueTransaction() { BusinessId = 1, QueueJoinDateTime = instruction.QueueJoinTime, ExpectedEntryDateTime = instruction.ExpectedEntryTime, ActualEntryDateTime = instruction.ActualEntryTime, ExitTime = instruction.ExitTime, PatronId = instruction.PatronId, UserLedgerId = instruction.UserLedgerId }; dbContext.QueueTransactions.Add(queueInstruction); dbContext.SaveChanges(); }
public UserSession AuthenticUserCredentials(UserSession userCredentials) { // Get user from DB if webname matches var query = from c in _dbContext.Users where c.Email.Equals(userCredentials.UserName) select c; // Match password if (query.Count() == 1) { var foundMatch = query.ToList().First(); var passwordHash = foundMatch.UserPassword; byte[] hashBytes = Convert.FromBase64String(passwordHash); var salt = foundMatch.Salt; var saltBytes = Guid.Parse(salt).ToByteArray(); var pbkdf2 = new Rfc2898DeriveBytes(userCredentials.Password, saltBytes); byte[] hash = pbkdf2.GetBytes(20); if (String.Equals(Convert.ToBase64String(hash), passwordHash)) { // If success - log the session // Create New Session Token // 1. Get a random number between 1 and 100000, hash it to get the sessionId, save string to DB and in cookie, return var randomSessionId = new Random().Next(1, 100000).ToString(); var sessionHash = new Rfc2898DeriveBytes(randomSessionId, 10).GetBytes(7); userCredentials.UserId = foundMatch.Id; _dbContext.SessionLogs.Add(new SessionLog { UserId = userCredentials.UserId, Browser = userCredentials.Browser, IpAddress = userCredentials.IpAddress, XForwardedFor = userCredentials.XForwardedFor, ServerName = userCredentials.ServerName, SessionKey = Convert.ToBase64String(sessionHash), DateCreated = userCredentials.DateCreated }); _dbContext.SaveChanges(); //Save the String of Session Hash in DB for the authenticated user var userResult = _dbContext.Users.Find(foundMatch.Id); userResult.SessionKey = Convert.ToBase64String(sessionHash); _dbContext.SaveChanges(); userCredentials.SessionValidated = true; userCredentials.SessionKey = userResult.SessionKey; // Get User Group int userId = foundMatch.Id; var userGroupQuery = (from c in _dbContext.UsersToUserGroups where c.UserId == userId select c.UserGroupId).Single(); if (userGroupQuery != null) { userCredentials.UserGroup = Convert.ToInt16(userGroupQuery); } } } else { userCredentials.SessionValidated = false; } return(userCredentials); }