예제 #1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Listing = await _context.Listing.FindAsync(id);

            if (Listing != null)
            {
                if (!(Listing.PhotoPath == "default-box.png"))
                {
                    System.IO.File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Uploads/Listings", Listing.PhotoPath));
                }
                _context.Listing.Remove(Listing);
                //await _context.SaveChangesAsync();

                // Once a record is deleted, create an audit record
                if (await _context.SaveChangesAsync() > 0)
                {
                    var auditrecord = new AuditRecord();
                    auditrecord.AuditActionType = "Delete Listing";
                    auditrecord.DateTimeStamp   = DateTime.Now;
                    auditrecord.ListingID       = Listing.ID;
                    var userID = User.Identity.Name.ToString();
                    auditrecord.Username = userID;
                    _context.AuditRecords.Add(auditrecord);
                    await _context.SaveChangesAsync();
                }
            }

            return(RedirectToPage("./Index"));
        }
예제 #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (!(Listing.Photo == null))
            {
                await UploadPhoto();
            }

            //File Upload path
            var DirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Uploads/Listings");
            var FinalPath     = Path.Combine(DirectoryPath, Listing.PhotoPath);

            //Validate uploaded photo
            if (GetImageType(FinalPath) == "")
            {
                TempData["notice"] = "Please upload a valid file";
                return(Page());
            }

            _context.Attach(Listing).State = EntityState.Modified;


            //try
            //{
            //    await _context.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    if (!ListingExists(Listing.ID))
            //    {
            //        return NotFound();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //if listing is edited, create record
            if (await _context.SaveChangesAsync() > 0)
            {
                var auditrecord = new AuditRecord();
                auditrecord.AuditActionType = "Edit Listing";
                auditrecord.DateTimeStamp   = DateTime.Now;
                auditrecord.ListingID       = Listing.ID;
                var userID = User.Identity.Name.ToString();
                auditrecord.Username = userID;
                _context.AuditRecords.Add(auditrecord);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (Listing.Photo == null)
            {
                Listing.PhotoPath = "default-box.png";
            }
            else
            {
                await UploadPhoto();
            }

            //File Upload path
            var DirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Uploads/Listings");
            var FinalPath     = Path.Combine(DirectoryPath, Listing.PhotoPath);

            //Validate uploaded photo
            if (GetImageType(FinalPath) == "")
            {
                TempData["notice"] = "Please upload a valid file";
                return(Page());
            }

            //Record User who created listing
            //Listing.UserID = Int32.Parse((await _userManager.GetUserAsync(HttpContext.User))?.Id);
            Listing.UserName = User.Identity.Name.ToString();

            //Recorde date listing is created
            Listing.PostedDateTime = DateTime.Now;

            _context.Listing.Add(Listing);
            //await _context.SaveChangesAsync();

            // Once a record is added, create an audit record
            if (await _context.SaveChangesAsync() > 0)
            {
                // Create an auditrecord object
                var auditrecord = new AuditRecord();
                auditrecord.AuditActionType = "Add Listing";
                auditrecord.DateTimeStamp   = DateTime.Now;
                auditrecord.ListingID       = Listing.ID;
                // Get current logged-in user
                var userID = User.Identity.Name.ToString();
                auditrecord.Username = userID;
                _context.AuditRecords.Add(auditrecord);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            var changePasswordResult = await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword);

            if (!changePasswordResult.Succeeded)
            {
                foreach (var error in changePasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }

                // Password change failed attempt - create an audit record
                var auditrecordfail = new AuditRecord();
                auditrecordfail.AuditActionType = "Change Password FAIL";
                auditrecordfail.DateTimeStamp   = DateTime.Now;
                auditrecordfail.Username        = user.UserName;

                // save the email used for the failed change
                _context.AuditRecords.Add(auditrecordfail);
                await _context.SaveChangesAsync();

                return(Page());
            }

            // Password change success attempt - create an audit record
            var auditrecord = new AuditRecord();

            auditrecord.AuditActionType = "Change Password SUCCESS";
            auditrecord.DateTimeStamp   = DateTime.Now;
            auditrecord.Username        = user.UserName;

            // save the email used for the successsful change
            _context.AuditRecords.Add(auditrecord);
            await _context.SaveChangesAsync();

            await _signInManager.SignInAsync(user, isPersistent : false);

            _logger.LogInformation("User changed their password successfully.");
            StatusMessage = "Your password has been changed.";

            return(RedirectToPage());
        }
예제 #5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(AuditRecord).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuditRecordExists(AuditRecord.Audit_ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
예제 #6
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.AuditRecords.Add(AuditRecord);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
예제 #7
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            ReturnUrl = returnUrl;

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    return(LocalRedirect(Url.GetLocalUrl(returnUrl)));
                }
                else
                {   // Login failed attempt - create an audit record
                    var auditrecord = new AuditRecord();
                    auditrecord.AuditActionType = "Failed Login";
                    auditrecord.DateTimeStamp   = DateTime.Now;

                    auditrecord.Username = Input.Email;
                    // save the email used for the failed login

                    _context.AuditRecords.Add(auditrecord);
                    await _context.SaveChangesAsync();
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }


            // If we got this far, something failed, redisplay form
            return(Page());
        }
예제 #8
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            AuditRecord = await _context.AuditRecords.FindAsync(id);

            if (AuditRecord != null)
            {
                _context.AuditRecords.Remove(AuditRecord);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #9
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Listing = await _context.Listing.SingleOrDefaultAsync(m => m.ID == id);

            Promotion.ID        = 0;
            Promotion.ListingID = Listing.ID;

            _context.Promotion.Add(Promotion);
            Listing.Title    = "(Promoted) " + Listing.Title;
            Listing.Promoted = true;

            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
예제 #10
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync(User);

            if (Input.ProfilePic != null)
            {
                var fileExtension = Input.ProfilePic.FileName.Substring(Input.ProfilePic.FileName.IndexOf("."));
                if (fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".jpeg")
                {
                    var fileName             = Guid.NewGuid().ToString() + Input.ProfilePic.FileName;
                    var uploadsDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Uploads/ProfilePics");
                    var uploadedfilePath     = Path.Combine(uploadsDirectoryPath, fileName);

                    using (var fileStream = new FileStream(uploadedfilePath, FileMode.Create))
                    {
                        user.ProfilePic = fileName;
                        await Input.ProfilePic.CopyToAsync(fileStream);
                    }
                    FileName = fileName;
                }
                else
                {
                    StatusMessage = "Please upload a valid profile picture.";
                    return(RedirectToPage("./Index"));
                }
            }

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (Input.Email != user.Email)
            {
                var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email);

                if (!setEmailResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
                }
            }

            if (Input.PhoneNumber != user.PhoneNumber)
            {
                var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);

                if (!setPhoneResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
                }
            }

            await _context.SaveChangesAsync();

            StatusMessage = "Your profile has been updated";
            return(RedirectToPage("./Index"));
        }