using System; using Yavsc.Model.Blogs; using Yavsc.Model.RolesAndMembers; using System.Web; using System.Web.Security; using Yavsc.Model.Circles; using System.Web.Mvc; using System.IO; using System.Linq; using System.Collections.Generic; namespace Yavsc.Model.Blogs { /// /// Blog manager. /// public static class BlogManager { /// /// Removes the comment. /// /// The comment. /// Cmtid. public static long RemoveComment (long cmtid) { return Provider.RemoveComment (cmtid); } /// /// Comment the specified from, postid, content and visible. /// /// From. /// Postid. /// Content. /// If set to true visible. public static void Comment (string from, long postid, string content, bool visible) { Provider.Comment (from, postid, content); } static BlogProvider provider; /// /// Gets the provider. /// /// The provider. public static BlogProvider Provider { get { if (provider == null) provider = BlogHelper.GetProvider (); return provider; } } /// /// Gets the post. /// /// The post. /// Username. /// Title. public static UUTBlogEntryCollection GetPost (string username, string title) { return Provider.GetPost (username, title); } /// /// Gets the post. /// /// The post. /// Postid. public static BlogEntry GetPost (long postid) { return Provider.GetPost (postid); } /// /// Post the specified username, title, content and visible. /// /// Username. /// Title. /// Content. /// If set to true visible. /// sets the circles. public static long Post (string username, string title, string content, bool visible, long[] cids) { return Provider.Post (username, title, content, visible, cids); } /// /// Updates the post. /// /// Postid. /// Title. /// Content. /// If set to true visible. /// sets the circles. public static void UpdatePost (long postid, string title, string content, bool visible, long[] cids) { Provider.UpdatePost (postid, title, content, visible, cids); } /// /// Updates the post photo. /// /// Postid. /// Photo. public static void UpdatePostPhoto (long postid, string photo) { Provider.UpdatePostPhoto (postid, photo); } /// /// Finds the post. /// /// The post. /// Readers name. /// Pattern. /// Searchflags. /// Page index. /// Page size. /// Total records. public static BlogEntryCollection FindPost (string readersName, string pattern, FindBlogEntryFlags searchflags, int pageIndex, int pageSize, out int totalRecords) { return Provider.FindPost (readersName, pattern, searchflags, pageIndex, pageSize, out totalRecords); } /// /// Removes the post. /// /// Post identifier. public static void RemovePost (long post_id) { Provider.RemovePost (post_id); } /// /// Removes the post. /// /// Username. /// Title. public static void RemoveTitle (string username, string title) { if (!Roles.IsUserInRole ("Admin")) { string rguser = Membership.GetUser ().UserName; if (rguser != username) { throw new AccessViolationException ( string.Format ( "{1}, Vous n'avez pas le droit de suprimer les Blogs de {0}", username, rguser)); } } Provider.RemoveTitle (username, title); } public static TagInfo GetTagInfo(string tagname) { return Provider.GetTagInfo (tagname); } /// /// Lasts the posts. /// /// The posts. /// Page index. /// Page size. /// Total records. public static IEnumerable LastPosts (int pageIndex, int pageSize, out int totalRecords) { var c = Provider.LastPosts (pageIndex, pageSize, out totalRecords); return FilterOnReadAccess (c); } /// /// Gets the comments. /// /// The comments. /// Postid. /// If set to true get hidden. public static Comment[] GetComments (long postid, bool getHidden = true) { return Provider.GetComments (postid, getHidden); } /// /// Tag the specified post by postid. /// /// Postid. /// Tag. /// The tag identifier public static void Tag (long postid, string tag) { Provider.Tag (postid, tag); } public static void Note (long postid, int note) { Provider.Note (postid, note); } /// /// Checks the auth can edit. /// /// true, if auth can edit was checked, false otherwise. /// Postid. /// If set to true throw ex. public static BlogEntry GetForEditing (long postid, bool throwEx = true) { BlogEntry e = BlogManager.GetPost (postid); if (e == null) throw new PostNotFoundException (); if (!Roles.IsUserInRole ("Admin")) { string rguser = Membership.GetUser ().UserName; if (rguser != e.Author) { if (throwEx) throw new AccessViolationException ( string.Format ( "Vous n'avez pas le droit d'editer ce billet (id:{0})", e.Id)); else return null; } } return e; } /// /// Untag the specified postid and tagname. /// /// Postid. /// Tagname. public static void Untag (long postid, string tagname) { Provider.Untag (postid, tagname); } private static bool CanView (BlogEntry e, MembershipUser u = null) { if (e.AllowedCircles != null && e.AllowedCircles.Length > 0) { // only deliver to admins, owner, or specified circle memebers if (u == null) return false; if (u.UserName != e.Author) if (!Roles.IsUserInRole (u.UserName, "Admin")) { if (!e.Visible) return false; if (!CircleManager.DefaultProvider.Matches (e.AllowedCircles, u.UserName)) return false; } } return true; } /// /// Checks the auth can read. /// /// true, if auth can read was checked, false otherwise. /// Postid. /// If set to true throw ex. public static BlogEntry GetForReading (long postid, bool throwEx = true) { BlogEntry e = BlogManager.GetPost (postid); if (e == null) if (throwEx) throw new FileNotFoundException (); if ( CanView (e, Membership.GetUser ())) return e; if (throwEx) throw new AccessViolationException (string.Format ( "Vous n'avez pas le droit de lire ce billet (id:{0})", postid.ToString ())); return null; } /// /// Checks the auth can read. /// /// true, if auth can read was checked, false otherwise. /// Bec. /// If set to true throw ex. private static bool HasReadAccess (BlogEntryCollection bec, bool throwEx = true) { if (bec == null) throw new FileNotFoundException (); if (Roles.IsUserInRole ("Admin")) return true; var u = Membership.GetUser (); BlogEntry e = bec.First (x=>!CanView(x,u)); if (e == null) return true; if (throwEx) throw new AccessViolationException ( string.Format ( "Vous n'avez pas le droit de lire cette collection de billet (titles:{0})", bec.ToString())); else return false; } /// /// Filters the on read access. /// /// The on read access. /// Bec. /// The 1st type parameter. public static IEnumerable FilterOnReadAccess ( IEnumerable bec) { if (bec == null) return null; if (Roles.IsUserInRole ("Admin")) return bec; var u = Membership.GetUser (); var r = bec.Where (x => CanView (x as BlogEntry, u)); return r; } } }