yavsc/Yavsc/Controllers/BlogspotController.cs

254 lines
8.9 KiB
C#
Raw Normal View History

2016-07-29 13:16:08 +02:00
2016-05-17 18:22:18 +02:00
using System.Linq;
2016-07-29 13:16:08 +02:00
using System.Security.Claims;
using System.Threading.Tasks;
2016-05-17 18:22:18 +02:00
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Authorization;
using Microsoft.Data.Entity;
using Microsoft.Extensions.OptionsModel;
2016-07-29 13:16:08 +02:00
using Yavsc.Models;
using Yavsc.ViewModels.Auth;
2017-01-20 14:20:44 +01:00
using Microsoft.AspNet.Mvc.Rendering;
2017-04-12 02:10:02 +02:00
using Yavsc.ViewModels.Blogspot;
2017-10-04 23:53:47 +02:00
using Yavsc.Models.Blog;
2016-05-17 18:22:18 +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
{
ILogger _logger;
private ApplicationDbContext _context;
private SiteSettings _siteSettings;
private IAuthorizationService _authorizationService;
public BlogspotController(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
ILoggerFactory loggerFactory,
IAuthorizationService authorizationService,
IOptions<SiteSettings> siteSettings)
{
_context = context;
_logger = loggerFactory.CreateLogger<AccountController>();
_authorizationService = authorizationService;
_siteSettings = siteSettings.Value;
}
// GET: Blog
[AllowAnonymous]
public IActionResult Index(string id, int skip=0, int maxLen=25)
2016-05-17 18:22:18 +02:00
{
if (!string.IsNullOrEmpty(id))
return UserPosts(id);
2017-02-06 17:30:16 +01:00
string uid = User.GetUserId();
long[] usercircles = _context.Circle.Include(c=>c.Members).Where(c=>c.Members.Any(m=>m.MemberId == uid))
.Select(c=>c.Id).ToArray();
2017-10-04 23:53:47 +02:00
IQueryable<BlogPost> posts ;
2017-02-06 17:30:16 +01:00
if (usercircles != null) {
2017-04-12 02:10:02 +02:00
posts = _context.Blogspot.Include(b => b.Author)
2017-10-04 23:53:47 +02:00
.Include(p=>p.Tags)
.Include(p=>p.Comments)
2017-04-12 02:10:02 +02:00
.Include(p=>p.ACL)
.Where(p=> p.AuthorId == uid || p.Visible &&
(p.ACL.Count == 0 || p.ACL.Any(a=> usercircles.Contains(a.CircleId))))
;
2017-02-06 17:30:16 +01:00
}
else {
2017-04-12 02:10:02 +02:00
posts = _context.Blogspot.Include(b => b.Author)
.Include(p=>p.ACL).Where(p=>p.AuthorId == uid || p.Visible && p.ACL.Count == 0);
2017-02-06 17:30:16 +01:00
}
return View(posts.OrderByDescending( p=> p.DateCreated)
.GroupBy(p=> p.Title).Skip(skip).Take(maxLen));
2016-05-17 18:22:18 +02:00
}
[Route("/Title/{id?}")]
[AllowAnonymous]
2016-05-17 18:22:18 +02:00
public IActionResult Title(string id)
{
var uid = User.GetUserId();
2017-03-11 01:06:42 +01: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());
2016-05-17 18:22:18 +02:00
}
[Route("/Blog/{id?}")]
[AllowAnonymous]
2016-05-17 18:22:18 +02:00
public IActionResult UserPosts(string id)
{
2017-03-11 01:06:42 +01:00
if (string.IsNullOrEmpty(id)) return Index(null);
var uid = User.GetUserId();
long[] usercircles = _context.Circle.Include(c=>c.Members).Where(c=>c.Members.Any(m=>m.MemberId == uid))
.Select(c=>c.Id).ToArray();
var result = (User.IsSignedIn())?
_context.Blogspot.Include(
b => b.Author
2017-03-11 01:06:42 +01:00
).Include(p=>p.ACL).Where(x => x.Author.UserName == id && (x.Visible && (x.ACL.Count==0 || x.ACL.Any(a=> usercircles.Contains(a.CircleId))))):
_context.Blogspot.Include(
b => b.Author
2017-03-11 01:06:42 +01:00
).Where(x => x.Author.UserName == id && x.Visible);
// BlogIndexKey
return View("Index", result.OrderByDescending(p => p.DateCreated).ToList().GroupBy(p=>new BlogIndexKey { Title = p.Title, AuthorId = p.AuthorId } ));
2016-05-17 18:22:18 +02:00
}
// GET: Blog/Details/5
[AllowAnonymous]
2017-02-06 17:51:49 +01:00
public async Task<IActionResult> Details(long? id)
2016-05-17 18:22:18 +02:00
{
if (id == null)
{
return HttpNotFound();
}
2017-10-04 23:53:47 +02:00
BlogPost blog = _context.Blogspot.Include(
b => b.Author
2017-10-02 13:15:01 +02:00
)
.Include(p=>p.Tags)
2017-10-04 23:53:47 +02:00
.Include(p=>p.Comments)
2017-10-02 13:15:01 +02:00
.Include(p => p.ACL).Single(m => m.Id == id);
2016-05-17 18:22:18 +02:00
if (blog == null)
{
return HttpNotFound();
}
2017-02-06 17:51:49 +01:00
if (!await _authorizationService.AuthorizeAsync(User, blog, new ViewRequirement()))
{
2017-03-29 01:55:25 +02:00
return new ChallengeResult();
2017-02-06 17:51:49 +01:00
}
2017-10-04 23:53:47 +02:00
ViewData["apicmtctlr"] = "/api/blogcomments";
2017-10-10 20:50:05 +02:00
ViewData["moderatoFlag"] = User.IsInRole(Constants.BlogModeratorGroupName);
2016-05-17 18:22:18 +02:00
return View(blog);
}
// GET: Blog/Create
2016-06-01 23:47:06 +02:00
[Authorize()]
2017-03-11 01:06:42 +01:00
public IActionResult Create(string title)
2016-05-17 18:22:18 +02:00
{
2017-10-04 23:53:47 +02:00
var result = new BlogPost{Title=title};
2017-05-27 18:29:45 +02:00
ViewData["PostTarget"]="Create";
return View("Edit",result);
2016-05-17 18:22:18 +02:00
}
// POST: Blog/Create
2017-05-27 18:29:45 +02:00
[HttpPost, Authorize, ValidateAntiForgeryToken]
2017-10-04 23:53:47 +02:00
public IActionResult Create(Models.Blog.BlogPost blog)
2016-05-17 18:22:18 +02:00
{
2016-08-02 17:02:38 +02:00
blog.Rate = 0;
blog.AuthorId = User.GetUserId();
2017-03-11 01:06:42 +01:00
blog.Id=0;
2016-05-17 18:22:18 +02:00
if (ModelState.IsValid)
{
2017-05-27 18:29:45 +02:00
2016-05-17 18:22:18 +02:00
_context.Blogspot.Add(blog);
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-05-17 18:22:18 +02:00
return RedirectToAction("Index");
}
ModelState.AddModelError("Unknown","Invalid Blog posted ...");
2017-05-27 18:29:45 +02:00
ViewData["PostTarget"]="Create";
return View("Edit",blog);
2016-05-17 18:22:18 +02:00
}
2016-06-01 23:47:06 +02:00
[Authorize()]
2016-05-17 18:22:18 +02:00
// GET: Blog/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if (id == null)
{
return HttpNotFound();
}
2017-05-27 18:29:45 +02:00
ViewData["PostTarget"]="Edit";
2017-10-04 23:53:47 +02:00
BlogPost blog = _context.Blogspot.Include(x => x.Author).Include(x => x.ACL).Single(m => m.Id == id);
2017-01-17 14:56:53 +01:00
2016-05-17 18:22:18 +02:00
if (blog == null)
{
return HttpNotFound();
}
if (await _authorizationService.AuthorizeAsync(User, blog, new EditRequirement()))
{
2017-01-20 14:20:44 +01:00
ViewBag.ACL = _context.Circle.Where(
c=>c.OwnerId == blog.AuthorId)
.Select(
2017-03-29 01:55:25 +02:00
c => new SelectListItem
{
Text = c.Name,
Value = c.Id.ToString(),
Selected = blog.AuthorizeCircle(c.Id)
2017-01-20 14:20:44 +01:00
} 
);
2016-05-17 18:22:18 +02:00
return View(blog);
}
else
{
return new ChallengeResult();
}
}
// POST: Blog/Edit/5
[HttpPost]
2016-06-01 23:47:06 +02:00
[ValidateAntiForgeryToken,Authorize()]
2017-10-04 23:53:47 +02:00
public IActionResult Edit(BlogPost blog)
2016-05-17 18:22:18 +02:00
{
if (ModelState.IsValid)
{
var auth = _authorizationService.AuthorizeAsync(User, blog, new EditRequirement());
if (auth.Result)
{
2017-01-20 14:20:44 +01:00
// saves the change
2016-05-17 18:22:18 +02:00
_context.Update(blog);
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
ViewData["StatusMessage"] = "Post modified";
2016-05-17 18:22:18 +02:00
return RedirectToAction("Index");
2017-03-29 01:55:25 +02:00
}
else
{
2017-02-07 00:13:10 +01:00
ViewData["StatusMessage"] = "Accès restreint";
return new ChallengeResult();
2016-05-17 18:22:18 +02:00
}
}
2017-05-27 18:29:45 +02:00
ViewData["PostTarget"]="Edit";
2016-05-17 18:22:18 +02:00
return View(blog);
}
// GET: Blog/Delete/5
2016-06-01 23:47:06 +02:00
[ActionName("Delete"),Authorize()]
2016-05-17 18:22:18 +02:00
public IActionResult Delete(long? id)
{
if (id == null)
{
return HttpNotFound();
}
2017-10-04 23:53:47 +02:00
BlogPost blog = _context.Blogspot.Include(
b => b.Author
2016-05-17 18:22:18 +02:00
).Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
return View(blog);
}
// POST: Blog/Delete/5
2016-06-01 23:47:06 +02:00
[HttpPost, ActionName("Delete"), Authorize()]
2016-05-17 18:22:18 +02:00
[ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(long id)
{
2017-10-04 23:53:47 +02:00
BlogPost blog = _context.Blogspot.Single(m => m.Id == id);
2016-05-17 18:22:18 +02:00
var auth = _authorizationService.AuthorizeAsync(User, blog, new EditRequirement());
if (auth.Result)
{
2016-05-17 18:22:18 +02:00
_context.Blogspot.Remove(blog);
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-05-17 18:22:18 +02:00
}
return RedirectToAction("Index");
}
}
}