Esempio n. 1
0
        public async Task <IActionResult> Feedback([Bind("Id,Title,Tags,Content")] Ticket ticket)
        {
            if (ModelState.IsValid)
            {
                ContraUser user = _userManager.GetUserAsync(User).Result;
                if (user.IsBanned)
                {
                    return(Redirect("/Identity/Account/Login"));
                }

                if (string.IsNullOrEmpty(ticket.Title) && string.IsNullOrEmpty(ticket.Content))
                {
                    return(View(ticket));
                }

                ticket.OwnerID    = user.Id;
                ticket.AuthorName = user.Name;
                ticket.Approved   = HandledStatus.Submitted;
                ticket.Date       = DateTime.Now;
                ticket.AssignedTo = "None";

                _context.Add(ticket);
                await _context.SaveChangesAsync();

                return(Redirect("~/success"));
            }

            return(View(ticket));
        }
Esempio n. 2
0
        public async Task <IActionResult> Comment([Bind("Id,Content")] Comment comment, int PostId)
        {
            if (ModelState.IsValid)
            {
                ContraUser user = _userManager.GetUserAsync(User).Result;
                if (user.IsBanned)
                {
                    return(Redirect("/Identity/Account/Login"));
                }

                comment.OwnerID    = _userManager.GetUserId(User);
                comment.AuthorName = user.Name;
                comment.PostId     = PostId;
                comment.Date       = DateTime.Now;

                if (User.IsInRole("Staff"))
                {
                    comment.Approved = ApprovalStatus.Approved;
                }
                else
                {
                    comment.Approved = ApprovalStatus.Submitted;
                }

                _context.Add(comment);
                await _context.SaveChangesAsync();

                return(Redirect($"~/article/{PostId}"));
            }
            return(View(comment));
        }
Esempio n. 3
0
        public async Task <IActionResult> Profile(string userID)
        {
            ContraUser user = await _userManager.FindByIdAsync(userID);

            if (user == null)
            {
                return(Redirect("/404"));
            }

            return(View(user));
        }
Esempio n. 4
0
        private async Task LoadAsync(ContraUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            Username = userName;

            Input = new InputModel
            {
                Name = user.Name,
                Bio  = user.Bio
            };
        }
Esempio n. 5
0
        private async Task LoadAsync(ContraUser user)
        {
            var email = await _userManager.GetEmailAsync(user);

            Email = email;

            Input = new InputModel
            {
                NewEmail = email,
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
        }
Esempio n. 6
0
        public async Task <string> GetProfilePicture(string id)
        {
            ContraUser user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                return(user.ProfilePictureURL);
            }
            else
            {
                return("Not found!");
            }
        }
Esempio n. 7
0
        private static async Task <string> EnsureUser(IServiceProvider serviceProvider, string testUserPw, string UserName)
        {
            var userManager = serviceProvider.GetService <UserManager <ContraUser> >();

            var user = await userManager.FindByNameAsync(UserName);

            if (user == null)
            {
                // Create MD5 Hash for Gravatar
                StringBuilder sb = new StringBuilder();
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    byte[] inputBytes = Encoding.ASCII.GetBytes(UserName);
                    byte[] hashBytes  = md5.ComputeHash(inputBytes);

                    for (int i = 0; i < hashBytes.Length; i++)
                    {
                        sb.Append(hashBytes[i].ToString("X2"));
                    }
                }

                user = new ContraUser
                {
                    Name = "Qi",

                    Articles       = new List <Article>(),
                    ArticlesLiked  = new List <Article>(),
                    ArticlesViewed = new List <Article>(),
                    CommentsLiked  = new List <Comment>(),

                    ProfilePictureURL = "https://gravatar.com/avatar/" + sb.ToString() + "?d=identicon",
                    UserName          = UserName,
                    Email             = UserName,
                    EmailConfirmed    = true
                };
                await userManager.CreateAsync(user, testUserPw);
            }
            if (user == null)
            {
                throw new Exception("The password is probably not strong enough!");
            }

            return(user.Id);
        }
Esempio n. 8
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                StringBuilder sb = new StringBuilder();
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    byte[] inputBytes = Encoding.ASCII.GetBytes(Input.Email.Trim().ToLower());
                    byte[] hashBytes  = md5.ComputeHash(inputBytes);

                    for (int i = 0; i < hashBytes.Length; i++)
                    {
                        sb.Append(hashBytes[i].ToString("X2"));
                    }
                }

                var user = new ContraUser
                {
                    Name = info.Principal.FindFirstValue(ClaimTypes.Name),

                    Articles       = new List <Article>(),
                    ArticlesLiked  = new List <Article>(),
                    ArticlesViewed = new List <Article>(),
                    CommentsLiked  = new List <Comment>(),

                    ProfilePictureURL = "https://gravatar.com/avatar/" + sb.ToString() + "?d=identicon",
                    UserName          = Input.Email,
                    Email             = Input.Email,
                    DateJoined        = DateTime.Now
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

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

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

                        var userId = await _userManager.GetUserIdAsync(user);

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { area = "Identity", userId, code },
                            protocol: Request.Scheme);

                        await _emailSender.SendConfirmEmailAsync(Input.Email, info.Principal.FindFirstValue(ClaimTypes.Name), callbackUrl);

                        if (_userManager.Options.SignIn.RequireConfirmedAccount)
                        {
                            return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                        }
                        else
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            return(LocalRedirect(returnUrl));
                        }
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Esempio n. 9
0
        public async Task <IActionResult> SubmitResponse([Bind("Id,ResponseId,AuthorName,Title,Content,Anonymous,Sensitive,Spoiler")] Article article, IFormFile thumbnail)
        {
            if (ModelState.IsValid)
            {
                ContraUser user = await _userManager.GetUserAsync(User);

                if (user.IsBanned)
                {
                    return(Redirect("/Identity/Account/Login"));
                }

                if (ValidateImage(thumbnail))
                {
                    article.ThumbnailURL = await UploadImage(user.Id, thumbnail);
                }
                else
                {
                    return(View(article));
                }

                article.ArticleType = ArticleType.Response;
                if (!_context.Article.Any(a => a.Id == article.ResponseId))
                {
                    return(View(article));
                }

                article.OwnerID = user.Id;

                if (article.Anonymous)
                {
                    article.AuthorName = "Anonymous";
                }
                else if (!string.IsNullOrWhiteSpace(article.AuthorName))
                {
                    article.AuthorName = user.Name + ", " + article.AuthorName;
                }
                else
                {
                    article.AuthorName = user.Name;
                }

                article.Date  = DateTime.Now;
                article.Views = 0;
                article.Likes = 0;

                if (User.IsInRole("Staff"))
                {
                    article.IsEditorial = true;
                }

                article.Approved = ApprovalStatus.Approved;

                HtmlSanitizer sanitizer = new HtmlSanitizer();
                article.Content = sanitizer.Sanitize(article.Content);

                article.SummaryLong = Regex.Replace(article.Content, @"<[^>]*>", string.Empty).Trim().Substring(0, 60) + "...";

                _context.Article.Add(article);
                await _context.SaveChangesAsync();

                return(Redirect("~/success"));
            }

            return(View(article));
        }
Esempio n. 10
0
        public async Task <IActionResult> SubmitQuick(string type, [Bind("Id,AuthorName,Title,Content,Anonymous,Sensitive,Spoiler")] Article article, IFormFile thumbnail)
        {
            if (ModelState.IsValid)
            {
                ContraUser user = await _userManager.GetUserAsync(User);

                if (user.IsBanned)
                {
                    return(Redirect("/Identity/Account/Login"));
                }

                if (ValidateImage(thumbnail))
                {
                    article.ThumbnailURL = await UploadImage(user.Id, thumbnail);
                }
                else
                {
                    return(View(article));
                }

                switch (type.ToLower())
                {
                case "creative":
                    article.ArticleType = ArticleType.Creative;
                    break;

                case "meta":
                    article.ArticleType = ArticleType.Meta;
                    break;

                case "blog":
                    article.ArticleType = ArticleType.Blog;
                    break;

                default:
                    return(Redirect("/submit"));
                }

                article.OwnerID = user.Id;

                if (article.Anonymous)
                {
                    article.AuthorName = "Anonymous";
                }
                else if (!string.IsNullOrWhiteSpace(article.AuthorName))
                {
                    article.AuthorName = user.Name + ", " + article.AuthorName;
                }
                else
                {
                    article.AuthorName = user.Name;
                }

                article.Date  = DateTime.Now;
                article.Views = 0;
                article.Likes = 0;

                if (User.IsInRole("Staff"))
                {
                    article.IsEditorial = true;
                }

                article.Approved = ApprovalStatus.Approved;

                HtmlSanitizer sanitizer = new HtmlSanitizer();
                article.Content = sanitizer.Sanitize(article.Content);

                string summary = Regex.Replace(article.Content, @"<[^>]*>", string.Empty).Trim();
                if (summary.Length > 60)
                {
                    article.SummaryLong = summary.Substring(0, 60) + "...";
                }
                else
                {
                    article.SummaryLong = summary;
                }

                if (user.Articles == null)
                {
                    user.Articles = new List <Article>();
                }
                user.Articles.Add(article);

                _context.Article.Add(article);
                await _context.SaveChangesAsync();

                return(Redirect("~/success"));
            }

            return(View(article));
        }
Esempio n. 11
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            if (!Input.Email.EndsWith("@mvla.net"))
            {
                ModelState.AddModelError(string.Empty, "Not a valid MVLA email address!");
                return(Page());
            }

            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                StringBuilder sb = new StringBuilder();
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    byte[] inputBytes = Encoding.ASCII.GetBytes(Input.Email.Trim().ToLower());
                    byte[] hashBytes  = md5.ComputeHash(inputBytes);

                    for (int i = 0; i < hashBytes.Length; i++)
                    {
                        sb.Append(hashBytes[i].ToString("X2"));
                    }
                }

                var user = new ContraUser {
                    Name = Input.Name,

                    Articles       = new List <Article>(),
                    ArticlesLiked  = new List <Article>(),
                    ArticlesViewed = new List <Article>(),
                    CommentsLiked  = new List <Comment>(),

                    ProfilePictureURL = "https://gravatar.com/avatar/" + sb.ToString() + "?d=identicon",
                    UserName          = Input.Email,
                    Email             = Input.Email,
                    DateJoined        = DateTime.Now
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code },
                        protocol: Request.Scheme);

                    await _emailSender.SendConfirmEmailAsync(Input.Email, Input.Name, callbackUrl);

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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