using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net.Mime; using System.Runtime.Serialization.Formatters.Binary; using System.Web; using System.Web.Configuration; using System.Web.Profile; using System.Web.Security; using Npgsql.Web.Blog; using Yavsc; using Yavsc.Model; using Yavsc.Model.Blogs; using Yavsc.ApiControllers; using Yavsc.Model.RolesAndMembers; using System.Net; using System.Web.Mvc; using Yavsc.Model.Circles; using Yavsc.Helpers; namespace Yavsc.Controllers { /// /// Blogs controller. /// public class BlogsController : Controller { private string sitename = WebConfigurationManager.AppSettings ["Name"]; /// /// Index the specified title, pageIndex and pageSize. /// /// Title. /// Page index. /// Page size. public ActionResult Index (int pageIndex = 0, int pageSize = 10) { return BlogList (pageIndex, pageSize); } /// /// Chooses the media. /// /// The media. /// Identifier. public ActionResult ChooseMedia(long id) { return View (); } /// /// Blogs the list. /// /// The list. /// Page index. /// Page size. public ActionResult BlogList (int pageIndex = 0, int pageSize = 10) { int totalRecords; var bs = BlogManager.LastPosts (pageIndex, pageSize, out totalRecords); ViewData ["ResultCount"] = totalRecords; ViewData ["PageSize"] = pageSize; ViewData ["PageIndex"] = pageIndex; var bec = new BlogEntryCollection (bs); return View ("Index", bec ); } /// /// Title the specified title, pageIndex and pageSize. /// /// Title. /// Page index. /// Page size. /// [HttpGet] public ActionResult Title (string id, int pageIndex = 0, int pageSize = 10) { int recordCount; MembershipUser u = Membership.GetUser (); string username = u == null ? null : u.UserName; FindBlogEntryFlags sf = FindBlogEntryFlags.MatchTitle; BlogEntryCollection c = BlogManager.FindPost (username, id, sf, pageIndex, pageSize, out recordCount); var utc = new UTBlogEntryCollection (id); utc.AddRange (c); ViewData ["RecordCount"] = recordCount; ViewData ["PageIndex"] = pageIndex; ViewData ["PageSize"] = pageSize; return View (utc); } /// /// Users the posts. /// /// The posts. /// User. /// Page index. /// Page size. [HttpGet] public ActionResult UserPosts (string id, int pageIndex = 0, int pageSize = 10) { int recordcount=0; MembershipUser u = Membership.GetUser (); FindBlogEntryFlags sf = FindBlogEntryFlags.MatchUserName; ViewData ["SiteName"] = sitename; ViewData ["BlogUser"] = id; string readersName = null; ViewData ["PageIndex"] = pageIndex; ViewData ["pageSize"] = pageSize; // displays invisible items when the logged user is also the author if (u != null) { if (u.UserName == id || Roles.IsUserInRole ("Admin")) sf |= FindBlogEntryFlags.MatchInvisible; readersName = u.UserName; } // find entries BlogEntryCollection c = BlogManager.FindPost (readersName, id, sf, pageIndex, pageSize, out recordcount); // Get author's meta data var pr = ProfileBase.Create (id); if (pr != null) { Profile bupr = new Profile (pr); ViewData ["BlogUserProfile"] = bupr; // Inform of listing meta data ViewData ["BlogTitle"] = bupr.BlogTitle; ViewData ["Avatar"] = bupr.avatar; } ViewData ["RecordCount"] = recordcount; UUBlogEntryCollection uuc = new UUBlogEntryCollection (id, c); return View ("UserPosts", uuc); } /// /// Removes the comment. /// /// The comment. /// Cmtid. [Authorize] public ActionResult RemoveComment (long cmtid) { long postid = BlogManager.RemoveComment (cmtid); return GetPost (postid); } /// /// Returns the post. /// /// The post. /// Identifier. public ActionResult GetPost (long id) { ViewData ["id"] = id; BlogEntry e = BlogManager.GetForReading (id); UUTBlogEntryCollection c = new UUTBlogEntryCollection (e.Author,e.Title); c.Add (e); ViewData ["user"] = c.Author; ViewData ["title"] = c.Title; Profile pr = new Profile (ProfileBase.Create (c.Author)); if (pr == null) // the owner's profile must exist // in order to publish its bills return View ("NotAuthorized"); ViewData ["BlogUserProfile"] = pr; ViewData ["Avatar"] = pr.avatar; ViewData ["BlogTitle"] = pr.BlogTitle; return View ("UserPost",c); } /// /// Users the post. /// Assume that : /// * bec.Count > O /// * bec.All(x=>x.Author == bec[0].Author) ; /// /// The post. /// Bec. private ActionResult UserPost (UUTBlogEntryCollection bec) { if (ModelState.IsValid) if (bec.Count > 0) { Profile pr = new Profile (ProfileBase.Create (bec.Author)); if (pr == null) // the owner's profile must exist // in order to publish its bills // This should'nt occur, as long as // a profile must exist for each one of // existing user record in data base // and each post is deleted with user deletion // a post => an author => a profile throw new Exception("Unexpected error retreiving author's profile"); ViewData ["BlogUserProfile"] = pr; ViewData ["Avatar"] = pr.avatar; ViewData ["BlogTitle"] = pr.BlogTitle; MembershipUser u = Membership.GetUser (); ViewData ["Author"] = bec.Author; if (!pr.BlogVisible) { // only deliver to admins or owner if (u == null) return View ("NotAuthorized"); else { if (u.UserName != bec.Author) if (!Roles.IsUserInRole (u.UserName, "Admin")) return View ("NotAuthorized"); } } if (u == null || (u.UserName != bec.Author) && !Roles.IsUserInRole (u.UserName, "Admin")) { // Filer on allowed posts BlogEntryCollection filtered = bec.FilterFor((u == null)?null : u.UserName); UUTBlogEntryCollection nbec = new UUTBlogEntryCollection (bec.Author, bec.Title); nbec.AddRange (filtered); View ("UserPost",nbec); } } return View ("UserPost",bec); } /// /// Users the post. /// /// The post. /// User. /// Title. /// Page index. /// Page size. public ActionResult UserPost (string user, string title, int pageIndex = 0, int pageSize = 10) { ViewData ["user"] = user; ViewData ["title"] = title; ViewData ["PageIndex"] = pageIndex; ViewData ["pageSize"] = pageSize; var pb = ProfileBase.Create (user); if (pb == null) // the owner's profile must exist // in order to publish its bills return View ("NotAuthorized"); Profile pr = new Profile (pb); ViewData ["BlogUserProfile"] = pr; ViewData ["Avatar"] = pr.avatar; ViewData ["BlogTitle"] = pr.BlogTitle; UUTBlogEntryCollection c = new UUTBlogEntryCollection (user, title); c.AddRange ( BlogManager.FilterOnReadAccess (BlogManager.GetPost (user, title))); return View ("UserPost",c); } /// /// Post the specified title. /// /// Title. [Authorize] public ActionResult Post (string title) { string un = Membership.GetUser ().UserName; if (String.IsNullOrEmpty (title)) title = ""; ViewData ["SiteName"] = sitename; ViewData ["Author"] = un; ViewData ["AllowedCircles"] = CircleManager.DefaultProvider.List (un) .Select (x => new SelectListItem { Value = x.Id.ToString(), Text = x.Title }); return View ("Edit", new BlogEntry { Title = title, Author = un }); } /// /// Validates the edit. /// /// The edit. /// Model. [Authorize] public ActionResult ValidateEdit (BlogEntry model) { ViewData ["SiteName"] = sitename; ViewData ["Author"] = Membership.GetUser ().UserName; if (ModelState.IsValid) { if (model.Id != 0) { // ensures rights to update BlogManager.GetForEditing (model.Id, true); BlogManager.UpdatePost (model.Id, model.Title, model.Content, model.Visible, model.AllowedCircles); } else model.Id = BlogManager.Post (model.Author, model.Title, model.Content, model.Visible, model.AllowedCircles); if (model.Photo != null) BlogManager.UpdatePostPhoto (model.Id, model.Photo); return RedirectToAction ("Title", new { id = model.Title }); } ViewData ["AllowedCircles"] = CircleManager.DefaultProvider.List ( Membership.GetUser ().UserName).Select (x => new SelectListItem { Value = x.Id.ToString(), Text = x.Title, Selected = model.AllowedCircles.Contains (x.Id) }); return View ("Edit", model); } /// /// Edit the specified bill /// /// Identifier. [Authorize] public ActionResult Edit (long id) { BlogEntry e = BlogManager.GetForEditing (id); string user = Membership.GetUser ().UserName; Profile pr = new Profile (ProfileBase.Create(e.Author)); ViewData ["BlogTitle"] = pr.BlogTitle; ViewData ["LOGIN"] = user; ViewData ["Id"] = id; // Populates the circles combo items if (e.AllowedCircles == null) e.AllowedCircles = new long[0]; ViewData ["AllowedCircles"] = CircleManager.DefaultProvider.List ( Membership.GetUser ().UserName).Select (x => new SelectListItem { Value = x.Id.ToString(), Text = x.Title, Selected = e.AllowedCircles.Contains (x.Id) }); return View (e); } /// /// Comment the specified model. /// /// Model. [Authorize] public ActionResult Comment (Comment model) { string username = Membership.GetUser ().UserName; ViewData ["SiteName"] = sitename; if (ModelState.IsValid) { BlogManager.Comment (username, model.PostId, model.CommentText, model.Visible); return GetPost (model.PostId); } return GetPost (model.PostId); } /// /// Remove the specified blog entry, by its author and title, /// using returnUrl as the URL to return to, /// and confirm as a proof you really know what you do. /// /// Title. /// User. /// Return URL. /// If set to true confirm. [Authorize] public ActionResult RemoveTitle (string id, string user, string returnUrl, bool confirm = false) { if (returnUrl == null) if (Request.UrlReferrer != null) returnUrl = Request.UrlReferrer.AbsoluteUri; ViewData ["returnUrl"] = returnUrl; ViewData ["Author"] = user; ViewData ["Title"] = id; if (Membership.GetUser ().UserName != user) if (!Roles.IsUserInRole("Admin")) throw new AuthorizationDenied (user); if (!confirm) return View ("RemoveTitle"); BlogManager.RemoveTitle (user, id); if (returnUrl == null) RedirectToAction ("Index", new { user = user }); return Redirect (returnUrl); } /// /// Removes the post. /// /// The post. /// Identifier. /// Return URL. /// If set to true confirm. [Authorize] public ActionResult RemovePost (long id, string returnUrl, bool confirm = false) { // ensures the access control BlogEntry e = BlogManager.GetForEditing (id); if (e == null) return new HttpNotFoundResult ("post id "+id.ToString()); ViewData ["id"] = id; ViewData ["returnUrl"] = string.IsNullOrWhiteSpace(returnUrl)? Request.UrlReferrer.AbsoluteUri.ToString(): returnUrl; // TODO: cleaner way to disallow deletion if (!confirm) return View ("RemovePost",e); BlogManager.RemovePost (id); if (string.IsNullOrWhiteSpace(returnUrl)) return RedirectToAction ("Index"); return Redirect (returnUrl); } } }