Exemplo n.º 1
0
        public async Task <IActionResult> CreateMany(BatchOwnerCreateVM vm)
        {
            var validUsers = new List <MinOwnerInfo>();

            // Make sure that at least the first and last name for the primary owner is set
            if (String.IsNullOrEmpty(vm.PrimaryOwner.FirstName) || String.IsNullOrEmpty(vm.PrimaryOwner.LastName))
            {
                ModelState.AddModelError("PrimaryOwner", "Please enter at least one user as a primary owner");
            }
            else
            {
                validUsers.Add(vm.PrimaryOwner);
            }

            foreach (var owner in vm.OwnerList)
            {
                var firstEmpty = String.IsNullOrEmpty(owner.FirstName);
                var lastEmpty  = String.IsNullOrEmpty(owner.LastName);
                // Check that all first names have a last name and vice versa
                if ((firstEmpty && !lastEmpty) || (!firstEmpty && lastEmpty))
                {
                    ModelState.AddModelError("OwnerList", "Make sure all first names have a matching last name");
                }
                else
                {
                    // Add the information to the list so we don't have to check the entire list again
                    validUsers.Add(owner);
                }
            }

            if (ModelState.IsValid)
            {
                var identityUser = await _userManager.GetUserAsync(HttpContext.User);

                var loggedInUser = _context.Owner.Find(identityUser.OwnerId);

                vm.Address.LastModifiedBy   = loggedInUser.FullName;
                vm.Address.LastModifiedDate = DateTime.Now;
                _context.Add(vm.Address);

                foreach (var ownerInfo in validUsers)
                {
                    var owner = new SunridgeHOA.Models.Owner
                    {
                        Address          = vm.Address,
                        FirstName        = ownerInfo.FirstName,
                        LastName         = ownerInfo.LastName,
                        Email            = ownerInfo.Email,
                        LastModifiedBy   = loggedInUser.FullName,
                        LastModifiedDate = DateTime.Now
                    };

                    _context.Add(owner);
                    await _context.SaveChangesAsync();

                    //// Find a default username - adds a number to the end if there is a duplicate
                    //var username = $"{owner.FirstName}{owner.LastName}";
                    //int count = 0;
                    //while (await _userManager.FindByNameAsync(username) != null)
                    //{
                    //    count++;
                    //    username = $"{username}{count}";
                    //}
                    var username = await OwnerUtility.GenerateUsername(_userManager, owner);

                    var defaultPassword = OwnerUtility.GenerateDefaultPassword(owner);

                    // Create user with default credentials
                    //  - Username: FirstnameLastname (e.g. JessBrunker)
                    //  - Password: 1234 (change in Areas/Admin/Data/OwnerUtility.cs)
                    var newOwner = new ApplicationUser
                    {
                        UserName = username,
                        Email    = owner.Email,
                        OwnerId  = owner.OwnerId
                    };

                    //var defaultPassword = $"Sunridge{username}123$";
                    var result = await _userManager.CreateAsync(newOwner, defaultPassword);

                    if (result.Succeeded)
                    {
                        var roles = new List <string> {
                            "Owner"
                        };
                        if (ownerInfo.IsAdmin)
                        {
                            roles.Add("Admin");
                        }
                        ;
                        await _userManager.AddToRolesAsync(newOwner, roles);

                        // Link Owner to the Application User
                        owner.ApplicationUserId = newOwner.Id;
                        //_context.Add(vm.Owner);
                        await _context.SaveChangesAsync();

                        // Add the Owner to a Lot
                        if (vm.LotId != -1)
                        {
                            _context.OwnerLot.Add(new OwnerLot
                            {
                                LotId     = vm.LotId,
                                OwnerId   = owner.OwnerId,
                                StartDate = DateTime.Now,
                                IsPrimary = ownerInfo == vm.PrimaryOwner
                            });

                            await _context.SaveChangesAsync();
                        }
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["LotsSelect"] = new SelectList(_context.Lot.OrderBy(u => u.LotNumber).ToList(), "LotId", "LotNumber", vm.LotId);
            return(View());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(OwnerVM vm)
        {
            if (vm.Owner.Email != null)
            {
                var existingUser = await _userManager.FindByEmailAsync(vm.Owner.Email);

                if (existingUser != null)
                {
                    ModelState.AddModelError("Email", "There is an existing user with that email address");
                }
            }

            if (ModelState.IsValid)
            {
                var identityUser = await _userManager.GetUserAsync(HttpContext.User);

                var loggedInUser = _context.Owner.Find(identityUser.OwnerId);

                vm.Address.LastModifiedBy   = loggedInUser.FullName;
                vm.Address.LastModifiedDate = DateTime.Now;
                _context.Add(vm.Address);

                vm.Owner.Address          = vm.Address;
                vm.Owner.LastModifiedBy   = loggedInUser.FullName;
                vm.Owner.LastModifiedDate = DateTime.Now;

                // Don't save yet - need to link the Owner to the ApplicationUser
                _context.Add(vm.Owner);
                await _context.SaveChangesAsync();

                // Find a default username - adds a number to the end if there is a duplicate
                //var username = $"{vm.Owner.FirstName}{vm.Owner.LastName}";
                //int count = 0;
                //while (await _userManager.FindByNameAsync(username) != null)
                //{
                //    count++;
                //    username = $"{username}{count}";
                //}

                var username = await OwnerUtility.GenerateUsername(_userManager, vm.Owner);

                var defaultPassword = OwnerUtility.GenerateDefaultPassword(vm.Owner);

                // Create user with default credentials
                //  - Username: FirstnameLastname (e.g. JessBrunker)
                //  - Password: 1234 (change in Areas/Admin/Data/OwnerUtility.cs)
                var newOwner = new ApplicationUser
                {
                    UserName = username,
                    Email    = vm.Owner.Email,
                    OwnerId  = vm.Owner.OwnerId
                };

                var result = await _userManager.CreateAsync(newOwner, defaultPassword);

                if (result.Succeeded)
                {
                    var roles = new List <string> {
                        "Owner"
                    };
                    if (vm.IsAdmin)
                    {
                        roles.Add("Admin");
                    }
                    ;
                    await _userManager.AddToRolesAsync(newOwner, roles);

                    // Link Owner to the Application User
                    vm.Owner.ApplicationUserId = newOwner.Id;
                    //_context.Add(vm.Owner);
                    await _context.SaveChangesAsync();

                    // Add the Owner to a Lot
                    if (vm.LotId != 0)
                    {
                        _context.OwnerLot.Add(new OwnerLot
                        {
                            LotId     = vm.LotId,
                            OwnerId   = vm.Owner.OwnerId,
                            StartDate = DateTime.Now
                        });

                        await _context.SaveChangesAsync();
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["LotsSelect"] = new SelectList(_context.Lot.OrderBy(u => u.LotNumber).ToList(), "LotId", "LotNumber", vm.LotId);
            return(View(vm));
        }