public ActionResult Create([Bind(Include = "TicketId")] TicketAttachment ticketAttachment, HttpPostedFileBase FilePath, string attachmentDescription)
        {
            ticketAttachment.Description = attachmentDescription;
            ticketAttachment.Created     = DateTime.Now;
            ticketAttachment.UserId      = User.Identity.GetUserId();


            if (ModelState.IsValid)
            {
                //Use Attachment Upload Validator to verify file
                if (ImageUploader.IsValidAttachment(FilePath))
                {
                    var fileName      = Path.GetFileName(FilePath.FileName);
                    var ext           = Path.GetExtension(FilePath.FileName);
                    var unique        = $"{fileName}-{DateTime.Now}";
                    var slug          = SlugHelper.CreateSlug(unique);
                    var formattedFile = $"{slug}{ext}";
                    FilePath.SaveAs(Path.Combine(Server.MapPath("~/Attachments/"), formattedFile));
                    ticketAttachment.FilePath = "/Attachments/" + formattedFile;
                }

                db.TicketAttachments.Add(ticketAttachment);
                db.SaveChanges();
                return(RedirectToAction("Dashboard", "Home"));
            }

            ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketAttachment.TicketId);
            ViewBag.UserId   = new SelectList(db.Users, "Id", "FirstName", ticketAttachment.UserId);
            return(View(ticketAttachment));
        }
Ejemplo n.º 2
0
        public ActionResult ChangeUserProfile([Bind(Include = "Id,FirstName,LastName,AvatarPath,Email")] UserProfileViewModel profile, HttpPostedFileBase AvatarPath)
        {
            if (ModelState.IsValid)
            {
                var currentUser = db.Users.Find(profile.Id);
                //Avatar Validator
                //profile picture setting
                if (ImageUploader.IsWebFriendlyImage(AvatarPath))
                {
                    var fileName = Path.GetFileName(AvatarPath.FileName);
                    var ext      = Path.GetExtension(AvatarPath.FileName);
                    //extended this to format the image file with an always unique title
                    var unique        = $"{fileName}-{DateTime.Now}";
                    var slug          = SlugHelper.CreateSlug(unique);
                    var formattedFile = $"{slug}{ext}";
                    AvatarPath.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), formattedFile));
                    //save formatted version to the register viewmodel
                    currentUser.AvatarPath = "/Avatars/" + formattedFile;
                }
                else
                {
                    currentUser.AvatarPath = profile.AvatarPath;
                }

                currentUser.FirstName = profile.FirstName;
                currentUser.LastName  = profile.LastName;
                currentUser.Email     = profile.Email;
                db.SaveChanges();
                return(RedirectToAction("Dashboard", "Home"));
            }
            else
            {
                return(RedirectToAction("ChangesNotSaved", "Home"));
            }
        }
Ejemplo n.º 3
0
        public ActionResult Submit(SubmitPageViewModel currentPage)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Admin"));
            }

            if (ModelState.IsValid)
            {
                var now  = DateTime.Now;
                var slug = SlugHelper.CreateSlug(now.Year, currentPage.Slug);

                //validate the slug, null means slug exists
                if (string.IsNullOrWhiteSpace(slug))
                {
                    ModelState.AddModelError("Slug", "The slug is already in use.");
                    currentPage.Slug = string.Empty;
                    return(View("Submit", currentPage));
                }

                StorageHelper.SavePost(new Post
                {
                    BodyHtml      = CommonMarkConverter.Convert(currentPage.BodyText),
                    BodyMarkdown  = currentPage.BodyText,
                    DontIndexPost = currentPage.DontIndexNewPost,
                    MetaDescPost  = currentPage.MetaDescNewPost,
                    PostedTime    = now,
                    Slug          = slug,
                    Title         = currentPage.Title,
                    ReadTime      = PostHelper.GetReadTime(currentPage.BodyText),
                    Categories    = currentPage.Categories
                });

                return(RedirectToAction("Index", "Post", new { year = now.Year, slug }));
            }

            //not valid
            return(View("Submit", currentPage));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase AvatarPath)
        {
            if (ModelState.IsValid)
            {
                //default avatar picture
                model.AvatarPath = "/Avatars/avatar-placeholder.png";
                //profile picture setting
                if (ImageUploader.IsWebFriendlyImage(AvatarPath))
                {
                    var fileName = Path.GetFileName(AvatarPath.FileName);
                    var ext      = Path.GetExtension(AvatarPath.FileName);
                    //extended this to format the image file with an always unique title
                    var unique        = $"{fileName}-{DateTime.Now}";
                    var slug          = SlugHelper.CreateSlug(unique);
                    var formattedFile = $"{slug}{ext}";
                    AvatarPath.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), formattedFile));
                    //save formatted version to the register viewmodel
                    model.AvatarPath = "/Avatars/" + formattedFile;
                }

                var user = new ApplicationUser {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    AvatarPath  = model.AvatarPath
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                    var emailFrom = WebConfigurationManager.AppSettings["emailto"];
                    var email     = new MailMessage(emailFrom, model.Email)
                    {
                        Subject    = "Confirm your account",
                        Body       = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>",
                        IsBodyHtml = true
                    };

                    var emailServe = new PersonalEmail();

                    await emailServe.SendAsync(email);


                    return(RedirectToAction("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }