2018-05-04 13:56:22 +02:00
|
|
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2023-03-19 17:57:55 +00:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-05-04 13:56:22 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-03-19 17:57:55 +00:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-05-04 13:56:22 +02:00
|
|
|
|
using Yavsc.Models;
|
|
|
|
|
|
using Yavsc.ViewModels.Auth;
|
2023-03-19 17:57:55 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2018-05-04 13:56:22 +02:00
|
|
|
|
using Yavsc.Models.Blog;
|
2018-11-20 15:50:04 +01:00
|
|
|
|
using Yavsc.Helpers;
|
2023-03-19 17:57:55 +00:00
|
|
|
|
using Microsoft.AspNetCore.Localization;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-11-28 13:53:26 +00:00
|
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
|
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yavsc.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
public class BlogspotController : Controller
|
|
|
|
|
|
{
|
2020-09-12 01:11:30 +01:00
|
|
|
|
readonly ILogger _logger;
|
|
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
|
readonly RequestLocalizationOptions _localisationOptions;
|
2019-09-05 21:02:13 +01:00
|
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
|
public BlogspotController(
|
|
|
|
|
|
ApplicationDbContext context,
|
|
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
|
|
IAuthorizationService authorizationService,
|
2019-09-05 21:02:13 +01:00
|
|
|
|
IOptions<RequestLocalizationOptions> localisationOptions)
|
2018-05-04 13:56:22 +02:00
|
|
|
|
{
|
|
|
|
|
|
_context = context;
|
|
|
|
|
|
_logger = loggerFactory.CreateLogger<AccountController>();
|
|
|
|
|
|
_authorizationService = authorizationService;
|
2019-09-05 21:02:13 +01:00
|
|
|
|
_localisationOptions = localisationOptions.Value;
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Blog
|
|
|
|
|
|
[AllowAnonymous]
|
2020-10-09 19:35:39 +01:00
|
|
|
|
public async Task<IActionResult> Index(string id)
|
2018-05-04 13:56:22 +02:00
|
|
|
|
{
|
2018-12-12 14:32:03 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(id)) {
|
2018-11-20 15:50:04 +01:00
|
|
|
|
return await UserPosts(id);
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
2018-12-12 14:32:03 +00:00
|
|
|
|
return View();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-25 18:51:58 +00:00
|
|
|
|
[Route("~/Title/{id?}")]
|
2018-05-04 13:56:22 +02:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public IActionResult Title(string id)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
2018-05-04 13:56:22 +02:00
|
|
|
|
ViewData["Title"] = id;
|
|
|
|
|
|
return View("Title", _context.Blogspot.Include(
|
|
|
|
|
|
b => b.Author
|
|
|
|
|
|
).Where(x => x.Title == id && (x.Visible || x.AuthorId == uid )).OrderByDescending(
|
|
|
|
|
|
x => x.DateCreated
|
|
|
|
|
|
).ToList());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-25 18:51:58 +00:00
|
|
|
|
[Route("~/Blog/{userName}/{pageLen?}/{pageNum?}")]
|
2018-05-04 13:56:22 +02:00
|
|
|
|
[AllowAnonymous]
|
2018-11-28 13:53:26 +00:00
|
|
|
|
public async Task<IActionResult> UserPosts(string userName, int pageLen=10, int pageNum=0)
|
2018-05-04 13:56:22 +02:00
|
|
|
|
{
|
2018-11-28 13:53:26 +00:00
|
|
|
|
string posterId = (await _context.Users.SingleOrDefaultAsync(u=>u.UserName == userName))?.Id ?? null ;
|
2018-11-20 15:50:04 +01:00
|
|
|
|
var result = _context.UserPosts(posterId, User.Identity.Name);
|
2023-04-01 15:03:48 +01:00
|
|
|
|
return View("Index", result.ToArray().Skip(pageLen*pageNum).Take(pageLen).OrderByDescending(p => p.DateCreated).ToList().GroupBy(p=> p.Title ));
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
// GET: Blog/Details/5
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task<IActionResult> Details(long? id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (id == null)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlogPost blog = _context.Blogspot
|
|
|
|
|
|
.Include(p => p.Author)
|
|
|
|
|
|
.Include(p => p.Tags)
|
|
|
|
|
|
.Include(p => p.Comments)
|
|
|
|
|
|
.Include(p => p.ACL)
|
|
|
|
|
|
.Single(m => m.Id == id);
|
|
|
|
|
|
if (blog == null)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
2023-03-19 17:57:55 +00:00
|
|
|
|
if ( _authorizationService.AuthorizeAsync(User, blog, new ViewRequirement()).IsFaulted)
|
2018-05-04 13:56:22 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new ChallengeResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var c in blog.Comments) {
|
|
|
|
|
|
c.Author = _context.Users.First(u=>u.Id==c.AuthorId);
|
|
|
|
|
|
}
|
|
|
|
|
|
ViewData["apicmtctlr"] = "/api/blogcomments";
|
|
|
|
|
|
ViewData["moderatoFlag"] = User.IsInRole(Constants.BlogModeratorGroupName);
|
|
|
|
|
|
return View(blog);
|
|
|
|
|
|
}
|
2019-09-05 21:02:13 +01:00
|
|
|
|
void SetLangItems()
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewBag.LangItems = _localisationOptions.SupportedUICultures.Select
|
|
|
|
|
|
(
|
|
|
|
|
|
sc => new SelectListItem { Value = sc.IetfLanguageTag, Text = sc.NativeName, Selected = System.Globalization.CultureInfo.CurrentUICulture == sc }
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2018-05-04 13:56:22 +02:00
|
|
|
|
|
|
|
|
|
|
// GET: Blog/Create
|
|
|
|
|
|
[Authorize()]
|
|
|
|
|
|
public IActionResult Create(string title)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = new BlogPost{Title=title};
|
|
|
|
|
|
ViewData["PostTarget"]="Create";
|
2019-09-05 21:02:13 +01:00
|
|
|
|
SetLangItems();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
return View("Edit",result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Create
|
|
|
|
|
|
[HttpPost, Authorize, ValidateAntiForgeryToken]
|
|
|
|
|
|
public IActionResult Create(Models.Blog.BlogPost blog)
|
|
|
|
|
|
{
|
|
|
|
|
|
blog.Rate = 0;
|
|
|
|
|
|
blog.AuthorId = User.GetUserId();
|
|
|
|
|
|
blog.Id=0;
|
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
_context.Blogspot.Add(blog);
|
|
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
ModelState.AddModelError("Unknown","Invalid Blog posted ...");
|
|
|
|
|
|
ViewData["PostTarget"]="Create";
|
|
|
|
|
|
return View("Edit",blog);
|
|
|
|
|
|
}
|
|
|
|
|
|
[Authorize()]
|
|
|
|
|
|
// GET: Blog/Edit/5
|
|
|
|
|
|
public async Task<IActionResult> Edit(long? id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (id == null)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ViewData["PostTarget"]="Edit";
|
|
|
|
|
|
BlogPost blog = _context.Blogspot.Include(x => x.Author).Include(x => x.ACL).Single(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
2023-03-19 17:57:55 +00:00
|
|
|
|
if (!_authorizationService.AuthorizeAsync(User, blog, new EditRequirement()).IsFaulted)
|
2018-05-04 13:56:22 +02:00
|
|
|
|
{
|
|
|
|
|
|
ViewBag.ACL = _context.Circle.Where(
|
|
|
|
|
|
c=>c.OwnerId == blog.AuthorId)
|
|
|
|
|
|
.Select(
|
|
|
|
|
|
c => new SelectListItem
|
|
|
|
|
|
{
|
|
|
|
|
|
Text = c.Name,
|
|
|
|
|
|
Value = c.Id.ToString(),
|
|
|
|
|
|
Selected = blog.AuthorizeCircle(c.Id)
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
2019-09-05 21:02:13 +01:00
|
|
|
|
SetLangItems();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
return View(blog);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ChallengeResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Edit/5
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
[ValidateAntiForgeryToken,Authorize()]
|
|
|
|
|
|
public IActionResult Edit(BlogPost blog)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var auth = _authorizationService.AuthorizeAsync(User, blog, new EditRequirement());
|
2023-03-19 17:57:55 +00:00
|
|
|
|
if (!auth.IsFaulted)
|
2018-05-04 13:56:22 +02:00
|
|
|
|
{
|
|
|
|
|
|
// saves the change
|
|
|
|
|
|
_context.Update(blog);
|
|
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
|
|
ViewData["StatusMessage"] = "Post modified";
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewData["StatusMessage"] = "Accès restreint";
|
|
|
|
|
|
return new ChallengeResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ViewData["PostTarget"]="Edit";
|
|
|
|
|
|
return View(blog);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Blog/Delete/5
|
|
|
|
|
|
[ActionName("Delete"),Authorize()]
|
|
|
|
|
|
public IActionResult Delete(long? id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (id == null)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlogPost blog = _context.Blogspot.Include(
|
|
|
|
|
|
b => b.Author
|
|
|
|
|
|
).Single(m => m.Id == id);
|
|
|
|
|
|
if (blog == null)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return View(blog);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Delete/5
|
|
|
|
|
|
[HttpPost, ActionName("Delete"), Authorize()]
|
|
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
|
|
public IActionResult DeleteConfirmed(long id)
|
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
|
BlogPost blog = _context.Blogspot.Single(m => m.Id == id && m.GetOwnerId()== User.GetUserId());
|
|
|
|
|
|
|
|
|
|
|
|
_context.Blogspot.Remove(blog);
|
|
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|