Пример #1
0
        public ActionResult LogIn(LoginDTO loginDTO)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    User user = SessionManager.AuthenticateUser(loginDTO.Email, loginDTO.Password);

                    using (var db = new Models.HacForoContainer())
                    {
                        string userJson  = new JavaScriptSerializer().Serialize(TableUserMap.MapTo(user));
                        string encTicket = SessionManager.GetAuthTicket(user.UserName, userJson);

                        HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Login details are wrong.");
            }

            return(View());
        }
Пример #2
0
 public User MapTo(TableUserDTO dto)
 {
     using (var db = new Models.HacForoContainer())
     {
         return(db.UserSet.Find(dto.Id));
     }
 }
Пример #3
0
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";

            using (var db = new Models.HacForoContainer())
            {
                var threads = db.ForumThreadSet.ToList();

                ViewBag.Threads = threads.ToList()
                                  .Select(ft => Mapper.MapTo(ft))
                                  .OrderByDescending(t => t.CreationDate)
                                  .ToList();
            }

            return(View());
        }
Пример #4
0
 // GET: User/Details/5
 public ActionResult Details(int?id)
 {
     using (var db = new Models.HacForoContainer())
     {
         if (id == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         UserDTO userDTO = Mapper.MapTo(db.UserSet.Find(id));
         if (userDTO == null)
         {
             return(HttpNotFound());
         }
         return(View(userDTO));
     }
 }
Пример #5
0
        public bool CheckUserCanPoint(int userId)
        {
            using (var db = new Models.HacForoContainer())
            {
                if (Id == 0 || userId == 0)
                {
                    UserCanPoint = false;
                }

                UserCanPoint = userId != User.Id &&
                               db.UserThreadPointsSet.Where(utp => utp.UserId == userId &&
                                                            utp.ThreadId == Id).Count() == 0;
            }

            return(UserCanPoint);
        }
Пример #6
0
        public User MapTo(RegistrationDTO dto)
        {
            using (var db = new Models.HacForoContainer())
            {
                var dbModel = db.UserSet.Create();
                dbModel.SetPassword(dto.Password);
                dbModel.Email              = dto.Email;
                dbModel.FirstName          = dto.FirstName;
                dbModel.LastName           = dto.LastName;
                dbModel.UserName           = dto.UserName;
                dbModel.CreationDate       = DateTime.Now;
                dbModel.ProfilePictureLink = dto.ProfilePictureLink;

                return(dbModel);
            }
        }
Пример #7
0
        public ActionResult Edit(RegistrationDTO registrationDTO)
        {
            try
            {
                if (!ValidateUser(registrationDTO.Id))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                using (var db = new Models.HacForoContainer())
                {
                    if (registrationDTO.IsValid(db, ModelState))
                    {
                        var userDb = db.UserSet.Find(registrationDTO.Id);

                        if (userDb == null)
                        {
                            return(HttpNotFound());
                        }

                        registrationDTO.UpdateModel(userDb);
                        db.SaveChanges();

                        return(View("Details", Mapper.MapTo(userDb)));
                    }
                    else
                    {
                        ModelState.AddModelError("", "The data is not valid.");
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            return(View());
        }
Пример #8
0
        internal static User AuthenticateUser(string email, string password)
        {
            var crypto = new SimpleCrypto.PBKDF2();

            using (var db = new Models.HacForoContainer())
            {
                User user = db.UserSet.FirstOrDefault(u => u.Email == email);
                if (user != null)
                {
                    if (user.Password == crypto.Compute(password, user.PasswordSalt))
                    {
                        return(user);
                    }
                }
            }

            throw new UnauthorizedAccessException("The login data is invalid");
        }
Пример #9
0
        public ActionResult ChangePassword(ChangePasswordDTO newChangePassword)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new Models.HacForoContainer())
                    {
                        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                        UserDTO    cookieUser = SessionManager.GetLoggedUser(authCookie);

                        var userDb = db.UserSet.Find(cookieUser.Id);

                        if (userDb == null)
                        {
                            return(HttpNotFound());
                        }
                        userDb.SetPassword(newChangePassword.Password);
                        db.SaveChanges();

                        return(View("Details", Mapper.MapTo(userDb)));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The data is not correct");
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            return(View());
        }
Пример #10
0
        public ForumThread MapTo(TableThreadDTO dto)
        {
            ForumThread modelDb;

            if (dto.Id != 0)
            {
                using (var db = new Models.HacForoContainer())
                {
                    modelDb = db.ForumThreadSet.Find(dto.Id);
                }
            }
            else
            {
                modelDb              = new ForumThread();
                modelDb.Title        = dto.Title;
                modelDb.UserId       = dto.User.Id;
                modelDb.CreationDate = DateTime.Now;
                modelDb.ImageLink    = dto.ImageLink;
            }

            return(modelDb);
        }
Пример #11
0
 public ActionResult Register(RegistrationDTO registrationDTO)
 {
     try
     {
         if (ModelState.IsValid)
         {
             using (var db = new Models.HacForoContainer())
             {
                 if (registrationDTO.IsValid(db, ModelState))
                 {
                     db.UserSet.Add(RegistrationMap.MapTo(registrationDTO));
                     db.SaveChanges();
                     return(RedirectToAction("Index", "Home"));
                 }
             }
         }
         else
         {
             ModelState.AddModelError("", "The data is not correct");
         }
     }
     catch (DbEntityValidationException e)
     {
         foreach (var eve in e.EntityValidationErrors)
         {
             Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                               eve.Entry.Entity.GetType().Name, eve.Entry.State);
             foreach (var ve in eve.ValidationErrors)
             {
                 Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                   ve.PropertyName, ve.ErrorMessage);
             }
         }
         throw;
     }
     return(View());
 }