示例#1
0
        public ActionResult Edit(int id)
        {
            //Get User by ID
            EditViewModel editVM;

            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Get User from DB
                User userDTO = context.Users.Find(id);
                if (userDTO == null)
                {
                    return(Content("Invalid Id"));
                }

                //Create EditVM
                editVM = new EditViewModel
                {
                    Username  = userDTO.Username,
                    FirstName = userDTO.FirstName,
                    LastName  = userDTO.LastName,
                    Email     = userDTO.Email,
                    Id        = userDTO.Id
                };
            }



            //Send VM to the View
            return(View(editVM));
        }
        public ActionResult PropertyProfile(int Id)
        {
            PropertyProfileViewModel propertyVM;

            //Retrieve the property from the DB
            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Populate the PropertyProfileViewModel
                Property propertyDTO = context.Properties.FirstOrDefault(x => x.Id == Id);
                if (propertyDTO == null)
                {
                    ModelState.AddModelError("", "Invalid Property Id");
                }
                propertyVM = new PropertyProfileViewModel()
                {
                    Id                = propertyDTO.Id,
                    Name              = propertyDTO.Name,
                    Unit              = propertyDTO.Unit,
                    Building          = propertyDTO.Building,
                    Street            = propertyDTO.Street,
                    City              = propertyDTO.City,
                    Region            = propertyDTO.Region,
                    Country           = propertyDTO.Country,
                    ZipCode           = propertyDTO.ZipCode,
                    Rating            = propertyDTO.Rating,
                    RatingDescription = propertyDTO.RatingDescription,
                    Description       = propertyDTO.Description
                };
            }

            //Return the View with the ViewModel
            return(View(propertyVM));
        }
示例#3
0
        public ActionResult UserProfile()
        {
            //Capture logged in user
            string username = User.Identity.Name;

            //Retrieve the User from the DB
            UserProfileViewModel profileVM;

            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                User userDTO = context.Users.FirstOrDefault(row => row.Username == username);
                if (userDTO == null)
                {
                    return(Content("Invalid Username"));
                }

                //Populate the UserProfileViewModel
                profileVM = new UserProfileViewModel()
                {
                    Id         = userDTO.Id,
                    FirstName  = userDTO.FirstName,
                    LastName   = userDTO.LastName,
                    Email      = userDTO.Email,
                    Username   = userDTO.Username,
                    IsAdmin    = userDTO.IsAdmin,
                    IsLandlord = userDTO.IsAdmin
                };
            }

            //Retrun the View with the viewModel
            return(View(profileVM));
        }
示例#4
0
        public ActionResult UserNavPartial()
        {
            //Capture logged in user
            string username;

            username = this.User.Identity.Name;
            UserNavPartialViewModel userNavVM;

            //Get info from db
            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Search for User
                Models.Data.User userDTO = context.Users.FirstOrDefault(x => x.Username == username);

                if (userDTO == null)
                {
                    return(Content(""));
                }

                //Build Partial view
                userNavVM = new UserNavPartialViewModel
                {
                    Username = userDTO.Username,
                    Id       = userDTO.Id
                };
            }

            //Send the View model
            return(PartialView(userNavVM));
        }
示例#5
0
        public ActionResult Create(CreateUserViewModel newUser)
        {
            //Validate the new User


            //Check That the required fields are set
            if (!ModelState.IsValid)
            {
                return(View(newUser));
            }

            //Check password matches confirmpassword
            if (!newUser.Password.Equals(newUser.PasswordConfirm))
            {
                ModelState.AddModelError("", "Password does not match Password Confirm.");
                return(View(newUser));
            }
            string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(newUser.Password, "MD5");

            //Create an instance of DbContext
            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Make sure username is unique
                if (context.Users.Any(row => row.Username.Equals(newUser.Username)))
                {
                    ModelState.AddModelError("", "Username '" + newUser.Username + "'already exists. Try again.");
                    newUser.Username = "";
                    return(View(newUser));
                }



                //Create our userDTO
                User newUserDTO = new Models.Data.User()
                {
                    FirstName    = newUser.FirstName,
                    LastName     = newUser.LastName,
                    Username     = newUser.Username,
                    Email        = newUser.Email,
                    Password     = hashedPassword,
                    AccountType  = newUser.AccountType,
                    IsActive     = true,
                    IsAdmin      = false,
                    IsLandlord   = newUser.IsLandlord,
                    landlordID   = newUser.LandlordId,
                    DateCreated  = DateTime.Now,
                    DateModified = DateTime.Now
                };

                //Add to DbContext
                newUserDTO = context.Users.Add(newUserDTO);

                //Save Changes
                context.SaveChanges();
            }

            //Redirect to the Login Page
            return(RedirectToAction("login"));
        }
        // GET: Property
        public ActionResult Index()
        {
            List <PropertyViewModel> propertyVM;

            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                propertyVM = context.Properties.ToArray().Select(x => new PropertyViewModel(x)).ToList();
            }
            return(View(propertyVM));
        }
示例#7
0
        public ActionResult Login(LoginUserViewModel loginUser)
        {
            //Validate username ans password is passed
            if (loginUser == null)
            {
                ModelState.AddModelError("", "Login is required");
                return(View());
            }
            if (string.IsNullOrWhiteSpace(loginUser.Username))
            {
                ModelState.AddModelError("", "Username is Required");
                return(View());
            }
            if (string.IsNullOrWhiteSpace(loginUser.Password))
            {
                ModelState.AddModelError("", "Password is required");
                return(View());
            }
            //open DB connection
            bool isValid = false;

            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Hash password
                string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(loginUser.Password, "MD5");

                //query for user based on username and password
                if (context.Users.Any(
                        row => row.Username.Equals(loginUser.Username) &&
                        row.Password.Equals(hashedPassword)
                        ))
                {
                    isValid = true;
                }
            }
            //if invalid send error
            if (!isValid)
            {
                ModelState.AddModelError("", "Invalid Username or Password");
                return(View());
            }
            else
            {
                //valid, redirect to user profile
                System.Web.Security.FormsAuthentication.SetAuthCookie(loginUser.Username, loginUser.RememberMe);

                return(Redirect(FormsAuthentication.GetRedirectUrl(loginUser.Username, loginUser.RememberMe)));
            }
        }
        public ActionResult Create(CreatePropertyViewModel newProperty)
        {
            //Validate that required Fields are filled in
            if (!ModelState.IsValid)
            {
                return(View(newProperty));
            }

            //Create an instance of DBContext
            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Make sure the new Property is Unique by comparing addresses.
                if (context.Properties.Any(row => row.Unit.Equals(newProperty.Unit)) &&
                    context.Properties.Any(row => row.Building.Equals(newProperty.Building)) &&
                    context.Properties.Any(row => row.Street.Equals(newProperty.Street)) &&
                    context.Properties.Any(row => row.City.Equals(newProperty.City)) &&
                    context.Properties.Any(row => row.Region.Equals(newProperty.Region)) &&
                    context.Properties.Any(row => row.Country.Equals(newProperty.Country)) &&
                    context.Properties.Any(row => row.ZipCode.Equals(newProperty.ZipCode))
                    )
                {
                    ModelState.AddModelError("", "This Property already exists.");
                    return(View());
                }
                //Create UserDTO
                Property newPropertyDTO = new Models.Data.Property()
                {
                    Name              = newProperty.Name,
                    Unit              = newProperty.Unit,
                    Building          = newProperty.Building,
                    Street            = newProperty.Street,
                    City              = newProperty.City,
                    Region            = newProperty.Region,
                    Country           = newProperty.Country,
                    ZipCode           = newProperty.ZipCode,
                    Rating            = newProperty.Rating,
                    RatingDescription = newProperty.RatingDescription,
                    Description       = newProperty.Description
                };
                //Add to context
                newPropertyDTO = context.Properties.Add(newPropertyDTO);
                // Save Changes
                context.SaveChanges();
            }

            //Redirect to Properties page.
            return(RedirectToAction("Index"));
        }
        public ActionResult Search(string query)
        {
            List <SearchResultViewModel> resultVMCollection = new List <SearchResultViewModel>();

            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                IQueryable <Property> propertyResults = context.Properties
                                                        .Where(p =>
                                                               p.Name.Contains(query) ||
                                                               p.Unit.Contains(query) ||
                                                               p.Building.Contains(query) ||
                                                               p.Street.Contains(query) ||
                                                               p.City.Contains(query) ||
                                                               p.Region.Contains(query)
                                                               );

                foreach (var item in propertyResults)
                {
                    resultVMCollection.Add(new SearchResultViewModel(item));
                }

                IQueryable <User> userResults = context.Users
                                                .Where(u =>
                                                       u.FirstName.Contains(query) ||
                                                       u.LastName.Contains(query) ||
                                                       u.Username.Contains(query) ||
                                                       u.Email.Contains(query)
                                                       );

                foreach (var item in userResults)
                {
                    resultVMCollection.Add(new SearchResultViewModel(item));
                }
            }

            return(View(resultVMCollection));
        }
示例#10
0
        public ActionResult Edit(EditViewModel editVM)
        {
            //Variables
            bool needsPasswordReset = false;
            bool usernameHasChanged = false;

            //Validate the model
            if (!ModelState.IsValid)
            {
                return(View(editVM));
            }

            //Check for password change
            if (!string.IsNullOrWhiteSpace(editVM.Password))
            {
                //Compare password with password confirm
                if (editVM.Password != editVM.PasswordConfrim)
                {
                    ModelState.AddModelError("", "Password and Confrim Password Must Match.");
                    return(View(editVM));
                }
                else
                {
                    needsPasswordReset = true;
                }
            }


            //Get user from DB
            User userDTO;

            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                userDTO = context.Users.Find(editVM.Id);
                if (userDTO == null)
                {
                    return(Content("Invalid User Id."));
                }

                //Check for Username Change
                if (userDTO.Username != editVM.Username)
                {
                    userDTO.Username   = editVM.Username;
                    usernameHasChanged = true;
                }

                //Set/ Update values from the view model
                userDTO.FirstName = editVM.FirstName;
                userDTO.LastName  = editVM.LastName;



                if (needsPasswordReset)
                {
                    userDTO.Password = editVM.Password;
                }

                //Save Changes
                context.SaveChanges();
            }
            if (usernameHasChanged || needsPasswordReset)
            {
                TempData["LogoutMessage"] = "After a username or password change, please log in with the new credentials.";
                return(RedirectToAction("Logout"));
            }
            else
            {
                return(RedirectToAction("UserProfile"));
            }
        }