|
|
|
@ -43,12 +43,44 @@ namespace Yavsc.Controllers
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Blog
|
|
|
|
// GET: Blog
|
|
|
|
[AllowAnonymous]
|
|
|
|
[AllowAnonymous]
|
|
|
|
public async Task<IActionResult> Index(string id)
|
|
|
|
public async Task<IActionResult> Index(string id, int skip=0, int take=25)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(id)) {
|
|
|
|
if (!string.IsNullOrEmpty(id)) {
|
|
|
|
return View("UserPosts", await UserPosts(id));
|
|
|
|
return View("UserPosts", await UserPosts(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return View();
|
|
|
|
IEnumerable<BlogPost> posts;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (User.Identity.IsAuthenticated)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string viewerId = User.GetUserId();
|
|
|
|
|
|
|
|
long[] usercircles = await _context.Circle.Include(c=>c.Members).
|
|
|
|
|
|
|
|
Where(c=>c.Members.Any(m=>m.MemberId == viewerId))
|
|
|
|
|
|
|
|
.Select(c=>c.Id).ToArrayAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
posts = _context.BlogSpot
|
|
|
|
|
|
|
|
.Include(b => b.Author)
|
|
|
|
|
|
|
|
.Include(p=>p.ACL)
|
|
|
|
|
|
|
|
.Include(p=>p.Tags)
|
|
|
|
|
|
|
|
.Include(p=>p.Comments)
|
|
|
|
|
|
|
|
.Where(p =>(p.ACL.Count == 0)
|
|
|
|
|
|
|
|
|| (p.AuthorId == viewerId)
|
|
|
|
|
|
|
|
|| (usercircles != null && p.ACL.Any(a => usercircles.Contains(a.CircleId)))
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
posts = _context.BlogSpot
|
|
|
|
|
|
|
|
.Include(b => b.Author)
|
|
|
|
|
|
|
|
.Include(p=>p.ACL)
|
|
|
|
|
|
|
|
.Include(p=>p.Tags)
|
|
|
|
|
|
|
|
.Include(p=>p.Comments)
|
|
|
|
|
|
|
|
.Where(p => p.ACL.Count == 0 ).ToArray();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var data = posts.OrderByDescending( p=> p.DateCreated);
|
|
|
|
|
|
|
|
var grouped = data.GroupBy(p=> p.Title).Skip(skip).Take(take);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return View(grouped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Route("~/Title/{id?}")]
|
|
|
|
[Route("~/Title/{id?}")]
|
|
|
|
@ -59,7 +91,7 @@ namespace Yavsc.Controllers
|
|
|
|
ViewData["Title"] = id;
|
|
|
|
ViewData["Title"] = id;
|
|
|
|
return View("Title", _context.BlogSpot.Include(
|
|
|
|
return View("Title", _context.BlogSpot.Include(
|
|
|
|
b => b.Author
|
|
|
|
b => b.Author
|
|
|
|
).Where(x => x.Title == id && (x.Visible || x.AuthorId == uid )).OrderByDescending(
|
|
|
|
).Where(x => x.Title == id && (x.AuthorId == uid )).OrderByDescending(
|
|
|
|
x => x.DateCreated
|
|
|
|
x => x.DateCreated
|
|
|
|
).ToList());
|
|
|
|
).ToList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -88,7 +120,7 @@ namespace Yavsc.Controllers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( _authorizationService.AuthorizeAsync(User, blog, new ViewRequirement()).IsFaulted)
|
|
|
|
if ( _authorizationService.AuthorizeAsync(User, blog, new ReadPermission()).IsFaulted)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return new ChallengeResult();
|
|
|
|
return new ChallengeResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -111,7 +143,8 @@ namespace Yavsc.Controllers
|
|
|
|
[Authorize()]
|
|
|
|
[Authorize()]
|
|
|
|
public IActionResult Create(string title)
|
|
|
|
public IActionResult Create(string title)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var result = new BlogPostInputViewModel{Title=title,Content=""};
|
|
|
|
var result = new BlogPostInputViewModel{Title=title
|
|
|
|
|
|
|
|
};
|
|
|
|
ViewData["PostTarget"]="Create";
|
|
|
|
ViewData["PostTarget"]="Create";
|
|
|
|
SetLangItems();
|
|
|
|
SetLangItems();
|
|
|
|
return View(result);
|
|
|
|
return View(result);
|
|
|
|
@ -168,7 +201,14 @@ namespace Yavsc.Controllers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
SetLangItems();
|
|
|
|
SetLangItems();
|
|
|
|
return View(blog);
|
|
|
|
return View(new BlogPostEditViewModel
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Id = blog.Id,
|
|
|
|
|
|
|
|
Title = blog.Title,
|
|
|
|
|
|
|
|
Content = blog.Content,
|
|
|
|
|
|
|
|
ACL = blog.ACL,
|
|
|
|
|
|
|
|
Photo = blog.Photo
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -179,27 +219,31 @@ namespace Yavsc.Controllers
|
|
|
|
// POST: Blog/Edit/5
|
|
|
|
// POST: Blog/Edit/5
|
|
|
|
[HttpPost]
|
|
|
|
[HttpPost]
|
|
|
|
[ValidateAntiForgeryToken,Authorize()]
|
|
|
|
[ValidateAntiForgeryToken,Authorize()]
|
|
|
|
public IActionResult Edit(BlogPost blog)
|
|
|
|
public async Task<IActionResult> Edit(BlogPostEditViewModel blogEdit)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var auth = _authorizationService.AuthorizeAsync(User, blog, new EditPermission());
|
|
|
|
var blog = _context.BlogSpot.SingleOrDefault(b=>b.Id == blogEdit.Id);
|
|
|
|
if (!auth.IsFaulted)
|
|
|
|
if (blog == null) {
|
|
|
|
{
|
|
|
|
ModelState.AddModelError("Id", "not found");
|
|
|
|
|
|
|
|
return View();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(await _authorizationService.AuthorizeAsync(User, blog, new EditPermission())).Succeeded) {
|
|
|
|
|
|
|
|
ViewData["StatusMessage"] = "Accès restreint";
|
|
|
|
|
|
|
|
return new ChallengeResult();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
blog.Content=blogEdit.Content;
|
|
|
|
|
|
|
|
blog.Title = blogEdit.Title;
|
|
|
|
|
|
|
|
blog.Photo = blogEdit.Photo;
|
|
|
|
|
|
|
|
blog.ACL = blogEdit.ACL;
|
|
|
|
// saves the change
|
|
|
|
// saves the change
|
|
|
|
_context.Update(blog);
|
|
|
|
_context.Update(blog);
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
ViewData["StatusMessage"] = "Post modified";
|
|
|
|
ViewData["StatusMessage"] = "Post modified";
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ViewData["StatusMessage"] = "Accès restreint";
|
|
|
|
|
|
|
|
return new ChallengeResult();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewData["PostTarget"]="Edit";
|
|
|
|
ViewData["PostTarget"]="Edit";
|
|
|
|
return View(blog);
|
|
|
|
return View(blogEdit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Blog/Delete/5
|
|
|
|
// GET: Blog/Delete/5
|
|
|
|
@ -223,12 +267,12 @@ namespace Yavsc.Controllers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Delete/5
|
|
|
|
// POST: Blog/Delete/5
|
|
|
|
[HttpPost, ActionName("Delete"), Authorize()]
|
|
|
|
[HttpPost, ActionName("Delete"), Authorize("IsTheAuthor")]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
public IActionResult DeleteConfirmed(long id)
|
|
|
|
public IActionResult DeleteConfirmed(long id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var uid = User.GetUserId();
|
|
|
|
var uid = User.GetUserId();
|
|
|
|
BlogPost blog = _context.BlogSpot.Single(m => m.Id == id && m.AuthorId == uid );
|
|
|
|
BlogPost blog = _context.BlogSpot.Single(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
|
|
_context.BlogSpot.Remove(blog);
|
|
|
|
_context.BlogSpot.Remove(blog);
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
_context.SaveChanges(User.GetUserId());
|
|
|
|
|