yavsc/yavscModel/Blogs/BlogManager.cs
Paul Schneider 7bf9b6a771 a note on billls,
+refactoring

* instdbws.sql:
* BlogsController.cs:
* NpgsqlBlogProvider.cs: Implements the note

* robots.txt:
* Web.config:
* Web.csproj:
* Catalog.xml:
* Global.asax.cs:
* pgsql.xcf:
* p8-av4.png:
* pgsql.jpeg:
* logoDev.png:
* logoDev.xcf:
* debian-pb.gif:
* apache_pb.gif:
* theme.css:
* style.css:
* apache_pby.gif:
* apache_pbw.gif:
* Book.aspx:
* jquery-ui.css:
* Login.aspx:
* debian-powered.png:
* BlogManager.cs:
* FhHRx.gif:
* pgsql.png:
* TagControl.ascx:
* jquery-ui.min.css:
* BlogProvider.cs:
* theme.css:
* p8-av4.s.jpg:
* test-domain-TestAPI.config:
* noavatar.png:
* p8-av4.xxs.jpg:
* apache_pbw.png:
* debian-logo.png:
* TestCatalogInit.cs:
* Mono-powered.png:
* helix-nebula-1400x1400.l.jpg:
* star-939235_1280.jpg:
* animated-overlay.gif:
* star-939235_1280.s.jpg:
* sign-in-with-google.png:
* star-939235_1280.xxs.jpg:
* sign-in-with-google-s.png:
* helix-nebula-1400x1400.jpg:
* helix-nebula-1400x1400.s.jpg:
* helix-nebula-1400x1400.xxs.jpg: removes the /images folder from the app root folder,
  all images are moved to /App_Themes/images
2015-11-04 11:04:22 +01:00

316 lines
9.2 KiB
C#

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
{
/// <summary>
/// Blog manager.
/// </summary>
public static class BlogManager
{
/// <summary>
/// Removes the comment.
/// </summary>
/// <returns>The comment.</returns>
/// <param name="cmtid">Cmtid.</param>
public static long RemoveComment (long cmtid)
{
return Provider.RemoveComment (cmtid);
}
/// <summary>
/// Comment the specified from, postid, content and visible.
/// </summary>
/// <param name="from">From.</param>
/// <param name="postid">Postid.</param>
/// <param name="content">Content.</param>
/// <param name="visible">If set to <c>true</c> visible.</param>
public static void Comment (string from, long postid, string content, bool visible)
{
Provider.Comment (from, postid, content);
}
static BlogProvider provider;
/// <summary>
/// Gets the provider.
/// </summary>
/// <value>The provider.</value>
public static BlogProvider Provider {
get {
if (provider == null)
provider = BlogHelper.GetProvider ();
return provider;
}
}
/// <summary>
/// Gets the post.
/// </summary>
/// <returns>The post.</returns>
/// <param name="username">Username.</param>
/// <param name="title">Title.</param>
public static UUTBlogEntryCollection GetPost (string username, string title)
{
return Provider.GetPost (username, title);
}
/// <summary>
/// Gets the post.
/// </summary>
/// <returns>The post.</returns>
/// <param name="postid">Postid.</param>
public static BlogEntry GetPost (long postid)
{
return Provider.GetPost (postid);
}
/// <summary>
/// Post the specified username, title, content and visible.
/// </summary>
/// <param name="username">Username.</param>
/// <param name="title">Title.</param>
/// <param name="content">Content.</param>
/// <param name="visible">If set to <c>true</c> visible.</param>
/// <param name="cids">sets the circles.</param>
public static long Post (string username, string title, string content, bool visible, long[] cids)
{
return Provider.Post (username, title, content, visible, cids);
}
/// <summary>
/// Updates the post.
/// </summary>
/// <param name="postid">Postid.</param>
/// <param name="title">Title.</param>
/// <param name="content">Content.</param>
/// <param name="visible">If set to <c>true</c> visible.</param>
/// <param name="cids">sets the circles.</param>
public static void UpdatePost (long postid, string title, string content, bool visible, long[] cids)
{
Provider.UpdatePost (postid, title, content, visible, cids);
}
/// <summary>
/// Updates the post photo.
/// </summary>
/// <param name="postid">Postid.</param>
/// <param name="photo">Photo.</param>
public static void UpdatePostPhoto (long postid, string photo)
{
Provider.UpdatePostPhoto (postid, photo);
}
/// <summary>
/// Finds the post.
/// </summary>
/// <returns>The post.</returns>
/// <param name="readersName">Readers name.</param>
/// <param name="pattern">Pattern.</param>
/// <param name="searchflags">Searchflags.</param>
/// <param name="pageIndex">Page index.</param>
/// <param name="pageSize">Page size.</param>
/// <param name="totalRecords">Total records.</param>
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);
}
/// <summary>
/// Removes the post.
/// </summary>
/// <param name="post_id">Post identifier.</param>
public static void RemovePost (long post_id)
{
Provider.RemovePost (post_id);
}
/// <summary>
/// Removes the post.
/// </summary>
/// <param name="username">Username.</param>
/// <param name="title">Title.</param>
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);
}
/// <summary>
/// Lasts the posts.
/// </summary>
/// <returns>The posts.</returns>
/// <param name="pageIndex">Page index.</param>
/// <param name="pageSize">Page size.</param>
/// <param name="totalRecords">Total records.</param>
public static IEnumerable<BlogEntry> LastPosts (int pageIndex, int pageSize, out int totalRecords)
{
var c = Provider.LastPosts (pageIndex, pageSize, out totalRecords);
return FilterOnReadAccess (c);
}
/// <summary>
/// Gets the comments.
/// </summary>
/// <returns>The comments.</returns>
/// <param name="postid">Postid.</param>
/// <param name="getHidden">If set to <c>true</c> get hidden.</param>
public static Comment[] GetComments (long postid, bool getHidden = true)
{
return Provider.GetComments (postid, getHidden);
}
/// <summary>
/// Tag the specified post by postid.
/// </summary>
/// <param name="postid">Postid.</param>
/// <param name="tag">Tag.</param>
/// <returns>The tag identifier</returns>
public static void Tag (long postid, string tag)
{
Provider.Tag (postid, tag);
}
public static void Note (long postid, int note)
{
Provider.Note (postid, note);
}
/// <summary>
/// Checks the auth can edit.
/// </summary>
/// <returns><c>true</c>, if auth can edit was checked, <c>false</c> otherwise.</returns>
/// <param name="postid">Postid.</param>
/// <param name="throwEx">If set to <c>true</c> throw ex.</param>
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;
}
/// <summary>
/// Untag the specified postid and tagname.
/// </summary>
/// <param name="postid">Postid.</param>
/// <param name="tagname">Tagname.</param>
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;
}
/// <summary>
/// Checks the auth can read.
/// </summary>
/// <returns><c>true</c>, if auth can read was checked, <c>false</c> otherwise.</returns>
/// <param name="postid">Postid.</param>
/// <param name="throwEx">If set to <c>true</c> throw ex.</param>
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;
}
/// <summary>
/// Checks the auth can read.
/// </summary>
/// <returns><c>true</c>, if auth can read was checked, <c>false</c> otherwise.</returns>
/// <param name="bec">Bec.</param>
/// <param name="throwEx">If set to <c>true</c> throw ex.</param>
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;
}
/// <summary>
/// Filters the on read access.
/// </summary>
/// <returns>The on read access.</returns>
/// <param name="bec">Bec.</param>
/// <typeparam name="TEntry">The 1st type parameter.</typeparam>
public static IEnumerable<TEntry> FilterOnReadAccess<TEntry> ( IEnumerable<TEntry> 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;
}
}
}