From 4fb1c297d9efdb74448be476150dcd8409e1b87c Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sat, 21 Jan 2017 22:36:43 +0100 Subject: [PATCH] Ajoute ou supprime des cercle aux posts --- Yavsc/ApiControllers/BlogAclApiController.cs | 31 +++++++++--- Yavsc/Controllers/BlogspotController.cs | 2 +- .../ApplicationDbContextModelSnapshot.cs | 2 - .../Access/CircleAuthorizationToBlogPost.cs | 6 ++- Yavsc/Models/Blog/Blog.cs | 21 ++++++++- Yavsc/Models/Blog/BlogAccess.cs | 15 ------ .../CirclesControlViewComponent.cs | 47 +++++++++++++++++++ Yavsc/ViewModels/Controls/AjaxCheckBoxInfo.cs | 10 ++++ .../Relationship/CirclesViewModel.cs | 15 ++++++ Yavsc/Views/Blogspot/Edit.cshtml | 12 +++-- Yavsc/Views/Home/Chat.cshtml | 19 ++++---- .../Components/CirclesControl/Default.cshtml | 7 +++ .../Shared/DisplayTemplates/Circle.cshtml | 15 +----- Yavsc/Views/Shared/_Layout.cshtml | 2 + Yavsc/Views/_ViewImports.cshtml | 2 + Yavsc/wwwroot/js/jquery.signalR-2.2.1.min.js | 9 ---- Yavsc/wwwroot/js/site.js | 28 +++++++++++ YavscLib/ICircleAuthorization.cs | 8 ++++ YavscLib/ICircleAuthorized.cs | 15 ++++++ 19 files changed, 199 insertions(+), 67 deletions(-) delete mode 100644 Yavsc/Models/Blog/BlogAccess.cs create mode 100644 Yavsc/ViewComponents/CirclesControlViewComponent.cs create mode 100644 Yavsc/ViewModels/Controls/AjaxCheckBoxInfo.cs create mode 100644 Yavsc/ViewModels/Relationship/CirclesViewModel.cs create mode 100644 Yavsc/Views/Shared/Components/CirclesControl/Default.cshtml delete mode 100644 Yavsc/wwwroot/js/jquery.signalR-2.2.1.min.js create mode 100644 YavscLib/ICircleAuthorization.cs create mode 100644 YavscLib/ICircleAuthorized.cs diff --git a/Yavsc/ApiControllers/BlogAclApiController.cs b/Yavsc/ApiControllers/BlogAclApiController.cs index cead34a9..92253043 100644 --- a/Yavsc/ApiControllers/BlogAclApiController.cs +++ b/Yavsc/ApiControllers/BlogAclApiController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; @@ -35,8 +36,9 @@ namespace Yavsc.Controllers { return HttpBadRequest(ModelState); } - - CircleAuthorizationToBlogPost circleAuthorizationToBlogPost = await _context.BlogACL.SingleAsync(m => m.CircleId == id); + var uid = User.GetUserId(); + CircleAuthorizationToBlogPost circleAuthorizationToBlogPost = await _context.BlogACL.SingleAsync( + m => m.CircleId == id && m.Allowed.OwnerId == uid ); if (circleAuthorizationToBlogPost == null) { @@ -60,6 +62,10 @@ namespace Yavsc.Controllers return HttpBadRequest(); } + if (!CheckOwner(circleAuthorizationToBlogPost.CircleId)) + { + return new ChallengeResult(); + } _context.Entry(circleAuthorizationToBlogPost).State = EntityState.Modified; try @@ -80,7 +86,14 @@ namespace Yavsc.Controllers return new HttpStatusCodeResult(StatusCodes.Status204NoContent); } - + private bool CheckOwner (long circleId) + { + + var uid = User.GetUserId(); + var circle = _context.Circle.First(c=>c.Id==circleId); + _context.Entry(circle).State = EntityState.Detached; + return (circle.OwnerId == uid); + } // POST: api/BlogAclApi [HttpPost] public async Task PostCircleAuthorizationToBlogPost([FromBody] CircleAuthorizationToBlogPost circleAuthorizationToBlogPost) @@ -89,7 +102,10 @@ namespace Yavsc.Controllers { return HttpBadRequest(ModelState); } - + if (!CheckOwner(circleAuthorizationToBlogPost.CircleId)) + { + return new ChallengeResult(); + } _context.BlogACL.Add(circleAuthorizationToBlogPost); try { @@ -118,13 +134,16 @@ namespace Yavsc.Controllers { return HttpBadRequest(ModelState); } + var uid = User.GetUserId(); - CircleAuthorizationToBlogPost circleAuthorizationToBlogPost = await _context.BlogACL.SingleAsync(m => m.CircleId == id); + CircleAuthorizationToBlogPost circleAuthorizationToBlogPost = await _context.BlogACL.Include( + a=>a.Allowed + ).SingleAsync(m => m.CircleId == id + && m.Allowed.OwnerId == uid); if (circleAuthorizationToBlogPost == null) { return HttpNotFound(); } - _context.BlogACL.Remove(circleAuthorizationToBlogPost); await _context.SaveChangesAsync(); diff --git a/Yavsc/Controllers/BlogspotController.cs b/Yavsc/Controllers/BlogspotController.cs index b12b9fb4..cb9df4ef 100644 --- a/Yavsc/Controllers/BlogspotController.cs +++ b/Yavsc/Controllers/BlogspotController.cs @@ -146,7 +146,7 @@ namespace Yavsc.Controllers { Text = c.Name, Value = c.Id.ToString(), - Selected = blog.ACL.Any(a=>a.CircleId==c.Id) + Selected = blog.AuthorizeCircle(c.Id) }  ); return View(blog); diff --git a/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs b/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs index d4f8d1b4..6f0ea091 100644 --- a/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs @@ -1,8 +1,6 @@ using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; -using Microsoft.Data.Entity.Metadata; -using Microsoft.Data.Entity.Migrations; using Yavsc.Models; namespace Yavsc.Migrations diff --git a/Yavsc/Models/Access/CircleAuthorizationToBlogPost.cs b/Yavsc/Models/Access/CircleAuthorizationToBlogPost.cs index 42f13043..5da29e12 100644 --- a/Yavsc/Models/Access/CircleAuthorizationToBlogPost.cs +++ b/Yavsc/Models/Access/CircleAuthorizationToBlogPost.cs @@ -3,18 +3,20 @@ namespace Yavsc.Models.Access using System.ComponentModel.DataAnnotations.Schema; using Models.Relationship; using Newtonsoft.Json; + using YavscLib; - public class CircleAuthorizationToBlogPost + public class CircleAuthorizationToBlogPost : ICircleAuthorization { public long CircleId { get; set; } public long BlogPostId { get; set; } [JsonIgnore] [ForeignKey("BlogPostId")] - public virtual Blog Post { get; set; } + public virtual Blog Target { get; set; } [JsonIgnore] [ForeignKey("CircleId")] public virtual Circle Allowed { get; set; } + } } \ No newline at end of file diff --git a/Yavsc/Models/Blog/Blog.cs b/Yavsc/Models/Blog/Blog.cs index f200325b..0e0347c0 100644 --- a/Yavsc/Models/Blog/Blog.cs +++ b/Yavsc/Models/Blog/Blog.cs @@ -2,12 +2,14 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; using Newtonsoft.Json; using Yavsc.Models.Access; +using YavscLib; namespace Yavsc.Models { - public partial class Blog : IBlog + public partial class Blog : IBlog, ICircleAuthorized { [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } @@ -42,7 +44,22 @@ namespace Yavsc.Models get; set; } - [InverseProperty("Post")] + [InverseProperty("Target")] public virtual List ACL { get; set; } + + public bool AuthorizeCircle(long circleId) + { + return ACL.Any( i=>i.CircleId == circleId); + } + + public string GetOwnerId() + { + return AuthorId; + } + + public ICircleAuthorization[] GetACL() + { + return ACL.ToArray(); + } } } diff --git a/Yavsc/Models/Blog/BlogAccess.cs b/Yavsc/Models/Blog/BlogAccess.cs deleted file mode 100644 index 121aa40c..00000000 --- a/Yavsc/Models/Blog/BlogAccess.cs +++ /dev/null @@ -1,15 +0,0 @@ - -using System.ComponentModel.DataAnnotations.Schema; - -namespace Yavsc.Models -{ - - public partial class BlogAccess - { - [ForeignKey("Blog.Id")] - public long PostId { get; set; } - - [ForeignKey("Circle.Id")] - public long CircleId { get; set; } - } -} diff --git a/Yavsc/ViewComponents/CirclesControlViewComponent.cs b/Yavsc/ViewComponents/CirclesControlViewComponent.cs new file mode 100644 index 00000000..72bdab7e --- /dev/null +++ b/Yavsc/ViewComponents/CirclesControlViewComponent.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Web.UI.WebControls; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.Rendering; +using Yavsc.Models; +using Yavsc.ViewModels.Controls; +using Yavsc.ViewModels.Relationship; +using YavscLib; + +namespace Yavsc.ViewComponents +{ + public class CirclesControlViewComponent : ViewComponent + { + ApplicationDbContext dbContext; + public CirclesControlViewComponent(ApplicationDbContext dbContext) + { + this.dbContext = dbContext; + } + public async Task InvokeAsync (ICircleAuthorized target) + { + var oid = target.GetOwnerId(); + ViewBag.ACL = dbContext.Circle.Where( + c=>c.OwnerId == oid) + .Select( + c => new SelectListItem + { + Text = c.Name, + Value = c.Id.ToString(), + Selected = target.AuthorizeCircle(c.Id) + }  + ); + + ViewBag.Access = dbContext.Circle.Where( + c=>c.OwnerId == oid) + .Select( c=> + new AjaxCheckBoxInfo + { + Text = c.Name, + Checked = target.AuthorizeCircle(c.Id), + Value = c.Id.ToString() + }); + return View(new CirclesViewModel(target)); + } + } +} \ No newline at end of file diff --git a/Yavsc/ViewModels/Controls/AjaxCheckBoxInfo.cs b/Yavsc/ViewModels/Controls/AjaxCheckBoxInfo.cs new file mode 100644 index 00000000..b6000bd5 --- /dev/null +++ b/Yavsc/ViewModels/Controls/AjaxCheckBoxInfo.cs @@ -0,0 +1,10 @@ +namespace Yavsc.ViewModels.Controls +{ + public class AjaxCheckBoxInfo + { + public string Text { get; set; } + public string Value { get; set; } + public bool Checked { get; set; } + + } +} \ No newline at end of file diff --git a/Yavsc/ViewModels/Relationship/CirclesViewModel.cs b/Yavsc/ViewModels/Relationship/CirclesViewModel.cs new file mode 100644 index 00000000..5ce45756 --- /dev/null +++ b/Yavsc/ViewModels/Relationship/CirclesViewModel.cs @@ -0,0 +1,15 @@ +using YavscLib; + +namespace Yavsc.ViewModels.Relationship +{ + public class CirclesViewModel + { + public CirclesViewModel(ICircleAuthorized resource) + { + Target = resource; + TargetTypeName = resource.GetType().Name; + } + public ICircleAuthorized Target { get; set; } + public string TargetTypeName { get; set; } + } +} \ No newline at end of file diff --git a/Yavsc/Views/Blogspot/Edit.cshtml b/Yavsc/Views/Blogspot/Edit.cshtml index f35be8b3..be47324f 100644 --- a/Yavsc/Views/Blogspot/Edit.cshtml +++ b/Yavsc/Views/Blogspot/Edit.cshtml @@ -150,14 +150,16 @@ editorcontenu.on('text-change',function(delta,source){
- + +
- + +
@@ -165,7 +167,8 @@ editorcontenu.on('text-change',function(delta,source){
- + +
@@ -177,8 +180,7 @@ editorcontenu.on('text-change',function(delta,source){
- + @await Component.InvokeAsync("CirclesControl",Model)
diff --git a/Yavsc/Views/Home/Chat.cshtml b/Yavsc/Views/Home/Chat.cshtml index e358f159..774a68d9 100644 --- a/Yavsc/Views/Home/Chat.cshtml +++ b/Yavsc/Views/Home/Chat.cshtml @@ -29,7 +29,7 @@

Salons

  • Public

Utilisateurs

-
    +
@@ -51,10 +51,6 @@ @section scripts { - - - - @@ -172,12 +168,6 @@ $('#discussion').append('
  • ' + htmlEncode(tag) } } }; - @if (!ViewBag.IsAuthenticated) { - // Get the user name and store it to prepend to messages. - - $('#displayname').val(prompt('Enter your name:', '')); - - } var sendMessage = function() { @@ -189,6 +179,13 @@ $('#discussion').append('
  • ' + htmlEncode(tag) // Set initial focus to message input box. $('#message').focus(); + @if (!ViewBag.IsAuthenticated) { + // Get the user name and store it to prepend to messages. + + $('#displayname').val(prompt('Enter your name:', '')); + + } + // Start the connection. $.connection.hub.start().done(function () { diff --git a/Yavsc/Views/Shared/Components/CirclesControl/Default.cshtml b/Yavsc/Views/Shared/Components/CirclesControl/Default.cshtml new file mode 100644 index 00000000..815cdbd6 --- /dev/null +++ b/Yavsc/Views/Shared/Components/CirclesControl/Default.cshtml @@ -0,0 +1,7 @@ +@model CirclesViewModel + +@foreach (var cb in ViewBag.Access) {  + +} diff --git a/Yavsc/Views/Shared/DisplayTemplates/Circle.cshtml b/Yavsc/Views/Shared/DisplayTemplates/Circle.cshtml index 00ed2148..618ef3e7 100644 --- a/Yavsc/Views/Shared/DisplayTemplates/Circle.cshtml +++ b/Yavsc/Views/Shared/DisplayTemplates/Circle.cshtml @@ -1,16 +1,3 @@ @model Circle -
    -
    - @Html.DisplayNameFor(model => model.Name) -
    -
    - @Html.DisplayFor(model => model.Name) -
    -
    - @Html.DisplayNameFor(model => model.Owner) -
    -
    - @Html.DisplayFor(model => model.Owner) -
    -
    \ No newline at end of file + @Model.Name diff --git a/Yavsc/Views/Shared/_Layout.cshtml b/Yavsc/Views/Shared/_Layout.cshtml index 930ab8c3..2c3332c2 100755 --- a/Yavsc/Views/Shared/_Layout.cshtml +++ b/Yavsc/Views/Shared/_Layout.cshtml @@ -15,8 +15,10 @@ + +