Ejemplo n.º 1
0
        public ActionResult Edit(Felbook.Models.User model)
        {
            TryUpdateModel(CurrentUser);

            //upload změna profilového obrázku
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase imageToUpload = Request.Files["profileimage"];
            int userId = CurrentUser.Id;
            string fileDir = "../Web_Data/profile_images/";
            //název souboru je vždy stejný
            string fileName = "profileimage.png";
            string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
            string fileDirPath = Path.GetDirectoryName(fileFullPath);
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else
            {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (model.OldPassword != null && String.Equals(model.Password, model.ConfirmPassword))
            {
                if (CurrentUser.CheckPassword(model.OldPassword))
                {
                    CurrentUser.ChangePassword(model.Password);
                }
                else
                {
                    ModelState.AddModelError("", "The password provided is incorrect.");
                }
            }

            if (ModelState.IsValid)
            {
                if (uploadImage == true)
                {
                    try
                    {
                        System.IO.File.Delete(fileFullPath);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Unexpected file error.");
                        return View("Index", "Profile", new { username = model.Username });
                    }
                    imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                }

                Model.UserService.Edit(CurrentUser);
                return View("Index", "Profile", new { username = model.Username });
            }
            //v případě nějaké chyby se vrátí tohle
            return View("Edit", "../Profile", new { username = model.Username });
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Felbook.Models.User model) //FormCollection collection
        {
            TryUpdateModel(CurrentUser);

            //upload změna profilového obrázku
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase          imageToUpload = Request.Files["profileimage"];
            int    userId  = CurrentUser.Id;
            string fileDir = "../Web_Data/profile_images/";
            //název souboru je vždy stejný
            string fileName     = "profileimage.png";
            string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
            string fileDirPath  = Path.GetDirectoryName(fileFullPath);
            bool   uploadImage  = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else
            {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (model.OldPassword != null && String.Equals(model.Password, model.ConfirmPassword))
            {
                if (CurrentUser.CheckPassword(model.OldPassword))
                {
                    CurrentUser.ChangePassword(model.Password);
                }
                else
                {
                    ModelState.AddModelError("", "The password provided is incorrect.");
                }
            }

            if (ModelState.IsValid)
            {
                //zpráva pro uživatele že editoval profil bez problému
                ViewData["EditResult"] = "The edit is successful.";

                if (uploadImage == true)
                {
                    try
                    {
                        System.IO.File.Delete(fileFullPath);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Unexpected file error.");
                        return(View(CurrentUser));
                    }
                    imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                }

                Model.UserService.Edit(CurrentUser);
                return(View(CurrentUser));
            }
            //v případě nějaké chyby se vrátí tohle
            return(View(CurrentUser));
        }
Ejemplo n.º 3
0
        public ActionResult Register(RegisterModel model)
        {
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase imageToUpload = Request.Files["profileimage"];
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                    //upload user profile image
                    User actualUser = Model.UserService.FindByUsername(model.UserName);
                    int userId = actualUser.Id;
                    string fileDir = "../Web_Data/profile_images/";
                    //název souboru je vždy stejný
                    string fileName = "profileimage.png";
                    string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
                    string fileDirPath = Path.GetDirectoryName(fileFullPath);

                    try
                    {
                        //pokusíme se vytvořit adresář
                        Directory.CreateDirectory(fileDirPath);
                    }
                    //jednotlivě odchytávám chyby
                    catch (UnauthorizedAccessException)
                    {
                        ModelState.AddModelError("file", "Upload wasn´t successful");
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Some uknown error");
                    }

                    if (uploadImage == true)
                    {
                        imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                    }
                    else
                    {
                        //zjistím si cesty k souboru
                        string sourceFile = Path.Combine(HttpContext.Server.MapPath(fileDir + "/default/"), fileName);
                        string destFile = System.IO.Path.Combine(fileDirPath, fileName);

                        //kopíruje to soubor
                        System.IO.File.Copy(sourceFile, destFile, true);
                    }
                    return RedirectToAction("Index", "Profile", new { username = actualUser.Username });
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }
Ejemplo n.º 4
0
        public ActionResult Register(RegisterModel model)
        {
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase          imageToUpload = Request.Files["profileimage"];
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else
            {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                    //upload user profile image
                    User   actualUser = Model.UserService.FindByUsername(model.UserName);
                    int    userId     = actualUser.Id;
                    string fileDir    = "../Web_Data/profile_images/";
                    //název souboru je vždy stejný
                    string fileName     = "profileimage.png";
                    string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
                    string fileDirPath  = Path.GetDirectoryName(fileFullPath);

                    try
                    {
                        //pokusíme se vytvořit adresář
                        Directory.CreateDirectory(fileDirPath);
                    }
                    //jednotlivě odchytávám chyby
                    catch (UnauthorizedAccessException)
                    {
                        ModelState.AddModelError("file", "Upload wasn´t successful");
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Some uknown error");
                    }

                    if (uploadImage == true)
                    {
                        imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                    }
                    else
                    {
                        //zjistím si cesty k souboru
                        string sourceFile = Path.Combine(HttpContext.Server.MapPath(fileDir + "/default/"), fileName);
                        string destFile   = System.IO.Path.Combine(fileDirPath, fileName);

                        //kopíruje to soubor
                        System.IO.File.Copy(sourceFile, destFile, true);
                    }
                    return(RedirectToAction("Index", "Profile", new { username = actualUser.Username }));
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return(View(model));
        }