Ajoute ou supprime des cercle aux posts

This commit is contained in:
2017-01-21 22:36:43 +01:00
parent 1268411e29
commit 4fb1c297d9
19 changed files with 199 additions and 67 deletions

View File

@ -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<IActionResult> 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();

View File

@ -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);

View File

@ -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

View File

@ -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; }
}
}

View File

@ -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<CircleAuthorizationToBlogPost> 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();
}
}
}

View File

@ -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; }
}
}

View File

@ -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<IViewComponentResult> 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));
}
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -150,14 +150,16 @@ editorcontenu.on('text-change',function(delta,source){
<label asp-for="Title" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger" />
<span asp-validation-for="Title" class="text-danger" >
</span>
</div>
</div>
<div class="form-group">
<label asp-for="Photo" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Photo" class="form-control" />
<span asp-validation-for="Photo" class="text-danger" />
<span asp-validation-for="Photo" class="text-danger" >
</span>
</div>
</div>
<div class="form-group">
@ -165,7 +167,8 @@ editorcontenu.on('text-change',function(delta,source){
<div class="col-md-10">
<textarea asp-for="Content" class="form-control" >
</textarea>
<span asp-validation-for="Content" class="text-danger" />
<span asp-validation-for="Content" class="text-danger" >
</span>
</div>
</div>
<div class="form-group">
@ -177,8 +180,7 @@ editorcontenu.on('text-change',function(delta,source){
<div class="form-group">
<label asp-for="ACL" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="ACL" asp-items=@ViewBag.ACL multiple>
</select>
@await Component.InvokeAsync("CirclesControl",Model)
</div>
</div>

View File

@ -29,7 +29,7 @@
<h3>Salons</h3>
<ul><li id="pubChan">Public</li></ul>
<h3>Utilisateurs</h3>
<ul id="userlist" style="list-style:none; padding: 1em; margin:1em;sqc">
<ul id="userlist" style="list-style:none; padding: 1em; margin:1em;">
</ul>
</div>
@ -51,10 +51,6 @@
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/js/jquery.signalR-2.2.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/api/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
@ -172,12 +168,6 @@ $('#discussion').append('<li class="notif"><i>' + htmlEncode(tag)
}
}
};
@if (!ViewBag.IsAuthenticated) {
// Get the user name and store it to prepend to messages.
<text>
$('#displayname').val(prompt('Enter your name:', ''));
</text>
}
var sendMessage = function() {
@ -189,6 +179,13 @@ $('#discussion').append('<li class="notif"><i>' + 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.
<text>
$('#displayname').val(prompt('Enter your name:', ''));
</text>
}
// Start the connection.
$.connection.hub.start().done(function () {

View File

@ -0,0 +1,7 @@
@model CirclesViewModel
@foreach (var cb in ViewBag.Access) { 
<label><input type="checkbox" class="@(Model.TargetTypeName)cirle" checked="@cb.Checked" value="@cb.Text"
data-target-id="@Model.Target.Id" data-circle-id="@cb.Value" data-targe-type="">
@cb.Text </label>
}

View File

@ -1,16 +1,3 @@
@model Circle
<dl class="circle dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Owner)
</dt>
<dd>
@Html.DisplayFor(model => model.Owner)
</dd>
</dl>
<span class="circle"> @Model.Name </span>

View File

@ -15,8 +15,10 @@
</environment>
<environment names="Development">
<script src="~/js/jquery.js"></script>
<script src="~/js/jquery.ui.js"></script>
<script src="~/js/bootstrap.js"></script>
<script src="~/js/site.js"></script>
<script src="~/js/jquery.signalR-2.2.1.js"></script>
</environment>
<environment names="Staging,Production,yavsc,yavscpre,booking,lua">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"

View File

@ -9,6 +9,7 @@
@using Yavsc;
@using Yavsc.Helpers;
@using Yavsc.Models;
@using Yavsc.Models.Access;
@using Yavsc.Models.Google;
@using Yavsc.Models.Booking;
@using Yavsc.Models.Market;
@ -19,6 +20,7 @@
@using Yavsc.ViewModels.Calendar;
@using Yavsc.ViewModels.Auth;
@using Yavsc.ViewModels.Administration;
@using Yavsc.ViewModels.Relationship;
@inject IViewLocalizer LocString
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

File diff suppressed because one or more lines are too long

View File

@ -1 +1,29 @@
// Write your Javascript code.
var allowCircleToBlog = function (e) {
var allow = $(this).prop('checked');
var circleid = $(this).data('circle-id');
var targetid = $(this).data('target-id');
var auth = { CircleId: circleid, BlogPostId: targetid };
var url = '/api/blogacl';
if (!allow) url+='/'+circleid;
console.log(auth);
$.ajax({
url: url,
type: allow?'POST':'DELETE',
data: JSON.stringify(auth),
contentType: "application/json;charset=utf-8",
success: function (data) {
console.log('auth '+allow?'POSTed':'DELETEd'+' Successfully');
},
error: function () {
console.log('auth not '+allow?'POSTed':'DELETEd');
}
});
e.preventDefault();
};
$(document).ready(function(){
$('input.Blogcirle[type=checkbox]').on('change',allowCircleToBlog);
}
);

View File

@ -0,0 +1,8 @@
namespace YavscLib
{
public interface ICircleAuthorization
{
long CircleId { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace YavscLib
{
public interface ICircleAuthorized
{
long Id { get; set; }
string GetOwnerId ();
bool AuthorizeCircle(long circleId);
ICircleAuthorization [] GetACL();
}
}