From 161d47ad1359f01f8d174268044292837777b2af Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 13 Dec 2016 10:57:25 +0100 Subject: [PATCH] blacklisting + refactoring --- .../ApiControllers/BlackListApiController.cs | 165 ++++++++++++++++++ Yavsc/Models/Access/BlackList.cs | 5 +- Yavsc/Models/ApplicationDbContext.cs | 3 + Yavsc/Models/Bank/AccountBalance.cs | 1 - Yavsc/Models/Blog/Blog.cs | 1 - Yavsc/project.json | 3 +- Yavsc/project.lock.json | 19 +- Yavsc/tasks.todo | 6 + YavscLib/IAccountBalance.cs | 1 - YavscLib/IBlackListed.cs | 9 + YavscLib/IBlog.cs | 15 +- YavscLib/Properties/AssemblyInfo.cs | 2 - YavscLib/YavscLib.csproj | 1 + YavscLib/YavscLib.sln | 2 +- YavscLib/project.json | 9 +- YavscLib/project.lock.json | 17 +- 16 files changed, 235 insertions(+), 24 deletions(-) create mode 100644 Yavsc/ApiControllers/BlackListApiController.cs create mode 100644 YavscLib/IBlackListed.cs diff --git a/Yavsc/ApiControllers/BlackListApiController.cs b/Yavsc/ApiControllers/BlackListApiController.cs new file mode 100644 index 00000000..379eea9a --- /dev/null +++ b/Yavsc/ApiControllers/BlackListApiController.cs @@ -0,0 +1,165 @@ +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using Microsoft.AspNet.Authorization; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Mvc; +using Microsoft.Data.Entity; +using Yavsc.Models; +using Yavsc.Models.Access; + +namespace Yavsc.Controllers +{ + [Produces("application/json")] + [Route("api/blacklist"), Authorize] + public class BlackListApiController : Controller + { + private ApplicationDbContext _context; + + public BlackListApiController(ApplicationDbContext context) + { + _context = context; + } + + // GET: api/BlackListApi + [HttpGet] + public IEnumerable GetBlackListed() + { + return _context.BlackListed; + } + + // GET: api/BlackListApi/5 + [HttpGet("{id}", Name = "GetBlackListed")] + public IActionResult GetBlackListed([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + BlackListed blackListed = _context.BlackListed.Single(m => m.Id == id); + if (blackListed == null) + { + return HttpNotFound(); + } + if (!CheckPermission(blackListed)) + return HttpBadRequest(); + + return Ok(blackListed); + } + + private bool CheckPermission(BlackListed blackListed) + { + var uid = User.GetUserId(); + if (uid != blackListed.OwnerId) + if (!User.IsInRole(Constants.AdminGroupName)) + if (!User.IsInRole(Constants.FrontOfficeGroupName)) + return false; + return true; + } + // PUT: api/BlackListApi/5 + [HttpPut("{id}")] + public IActionResult PutBlackListed(long id, [FromBody] BlackListed blackListed) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + if (id != blackListed.Id) + { + return HttpBadRequest(); + } + if (!CheckPermission(blackListed)) + return HttpBadRequest(); + _context.Entry(blackListed).State = EntityState.Modified; + + try + { + _context.SaveChanges(); + } + catch (DbUpdateConcurrencyException) + { + if (!BlackListedExists(id)) + { + return HttpNotFound(); + } + else + { + throw; + } + } + + return new HttpStatusCodeResult(StatusCodes.Status204NoContent); + } + + // POST: api/BlackListApi + [HttpPost] + public IActionResult PostBlackListed([FromBody] BlackListed blackListed) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + if (!CheckPermission(blackListed)) + return HttpBadRequest(); + + _context.BlackListed.Add(blackListed); + try + { + _context.SaveChanges(); + } + catch (DbUpdateException) + { + if (BlackListedExists(blackListed.Id)) + { + return new HttpStatusCodeResult(StatusCodes.Status409Conflict); + } + else + { + throw; + } + } + + return CreatedAtRoute("GetBlackListed", new { id = blackListed.Id }, blackListed); + } + + // DELETE: api/BlackListApi/5 + [HttpDelete("{id}")] + public IActionResult DeleteBlackListed(long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + BlackListed blackListed = _context.BlackListed.Single(m => m.Id == id); + if (blackListed == null) + { + return HttpNotFound(); + } + + if (!CheckPermission(blackListed)) + return HttpBadRequest(); + + _context.BlackListed.Remove(blackListed); + _context.SaveChanges(); + + return Ok(blackListed); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _context.Dispose(); + } + base.Dispose(disposing); + } + + private bool BlackListedExists(long id) + { + return _context.BlackListed.Count(e => e.Id == id) > 0; + } + } +} \ No newline at end of file diff --git a/Yavsc/Models/Access/BlackList.cs b/Yavsc/Models/Access/BlackList.cs index 5bbb3b42..1d3fc354 100644 --- a/Yavsc/Models/Access/BlackList.cs +++ b/Yavsc/Models/Access/BlackList.cs @@ -1,14 +1,15 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using YavscLib; namespace Yavsc.Models.Access { - public class BlackListed + public class BlackListed: IBlackListed { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } public string UserId { get; set; } - public long OwnerId { get; set; } + public string OwnerId { get; set; } [ForeignKey("OwnerId")] public virtual ApplicationUser Owner { get; set; } diff --git a/Yavsc/Models/ApplicationDbContext.cs b/Yavsc/Models/ApplicationDbContext.cs index e02c08a0..ba6eb571 100644 --- a/Yavsc/Models/ApplicationDbContext.cs +++ b/Yavsc/Models/ApplicationDbContext.cs @@ -14,6 +14,7 @@ using Yavsc.Models.Identity; using Yavsc.Models.Market; using Yavsc.Model.Chat; using Yavsc.Models.Messaging; +using Yavsc.Models.Access; namespace Yavsc.Models { @@ -189,6 +190,8 @@ namespace Yavsc.Models public DbSet ClientProviderInfo { get; set; } public DbSet Connections { get; set; } + + public DbSet BlackListed { get; set; } } } diff --git a/Yavsc/Models/Bank/AccountBalance.cs b/Yavsc/Models/Bank/AccountBalance.cs index 29f19a02..8dc95a6e 100644 --- a/Yavsc/Models/Bank/AccountBalance.cs +++ b/Yavsc/Models/Bank/AccountBalance.cs @@ -3,7 +3,6 @@ using System.ComponentModel.DataAnnotations.Schema; namespace Yavsc.Models { - using Interfaces; public partial class AccountBalance: IAccountBalance { [Key] diff --git a/Yavsc/Models/Blog/Blog.cs b/Yavsc/Models/Blog/Blog.cs index 4982819e..136ad6db 100644 --- a/Yavsc/Models/Blog/Blog.cs +++ b/Yavsc/Models/Blog/Blog.cs @@ -2,7 +2,6 @@ using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Newtonsoft.Json; -using Yavsc.Interfaces; namespace Yavsc.Models { diff --git a/Yavsc/project.json b/Yavsc/project.json index d4aeaeb5..89a917ff 100755 --- a/Yavsc/project.json +++ b/Yavsc/project.json @@ -105,7 +105,8 @@ "Microsoft.AspNet.Authentication.OAuth": "1.0.0-rc1-final", "Microsoft.AspNet.Mvc.Formatters.Json": "6.0.0-rc1-final", "Microsoft.AspNet.OWin": "1.0.0-rc1-final", - "System.Json": "4.0.20126.16343" + "System.Json": "4.0.20126.16343", + "YavscLib": "1.0.0-*" }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://*:5000", diff --git a/Yavsc/project.lock.json b/Yavsc/project.lock.json index 2924b4e0..05501016 100644 --- a/Yavsc/project.lock.json +++ b/Yavsc/project.lock.json @@ -2809,6 +2809,10 @@ "lib/WebGrease.dll": {} } }, + "YavscLib/1.0.0": { + "type": "project", + "framework": ".NETFramework,Version=v4.5.1" + }, "Zlib.Portable.Signed/1.11.0": { "type": "package", "compile": { @@ -5626,6 +5630,10 @@ "lib/WebGrease.dll": {} } }, + "YavscLib/1.0.0": { + "type": "project", + "framework": ".NETFramework,Version=v4.5.1" + }, "Zlib.Portable.Signed/1.11.0": { "type": "package", "compile": { @@ -8443,6 +8451,10 @@ "lib/WebGrease.dll": {} } }, + "YavscLib/1.0.0": { + "type": "project", + "framework": ".NETFramework,Version=v4.5.1" + }, "Zlib.Portable.Signed/1.11.0": { "type": "package", "compile": { @@ -8455,6 +8467,10 @@ } }, "libraries": { + "YavscLib/1.0.0": { + "type": "project", + "path": "../YavscLib/project.json" + }, "Antlr/3.4.1.9004": { "type": "package", "sha512": "c1S+HBE+KYA5EBxtn25LEK02hHPH/tDQ6RviUTTCJpZIPoputtn8ArsQJy9lVJWZOnw37ufByO2Fmf1M8wpr8Q==", @@ -11421,7 +11437,8 @@ "Microsoft.AspNet.Authentication.OAuth >= 1.0.0-rc1-final", "Microsoft.AspNet.Mvc.Formatters.Json >= 6.0.0-rc1-final", "Microsoft.AspNet.OWin >= 1.0.0-rc1-final", - "System.Json >= 4.0.20126.16343" + "System.Json >= 4.0.20126.16343", + "YavscLib >= 1.0.0-*" ], "DNX,Version=v4.5.1": [ "fx/System.Drawing >= 4.0.0" diff --git a/Yavsc/tasks.todo b/Yavsc/tasks.todo index b221794f..7cc32ba8 100644 --- a/Yavsc/tasks.todo +++ b/Yavsc/tasks.todo @@ -77,6 +77,12 @@ Ceci est une grosse liste de fonctionnalités, existantes, ou à implémenter, o ☐ Podcasts. ☐ Personalisation des blogs. ☐ Monétarisations. + ☐ Distribution de gold card (illimité), de carte vertes (à durée fixe), + de la part d'un prestataire, donnant accès au destinataire au chat privé. + ☐ La carte blanche: en la confiant, le prestataire délègue la gestion de son agenda à un autre utilisateur, + elle implique le droit au chat privé + ☐ de badges temporaires, donnant accès à un téléchargement unique et limité dans le temps, d'un seul fichier, + sans autre forme d'autorisation. ### Réécritures prévues : diff --git a/YavscLib/IAccountBalance.cs b/YavscLib/IAccountBalance.cs index 35a0c1fe..437e6020 100644 --- a/YavscLib/IAccountBalance.cs +++ b/YavscLib/IAccountBalance.cs @@ -4,7 +4,6 @@ { long ContactCredits { get; set; } decimal Credits { get; set; } - IApplicationUser Owner { get; set; } string UserId { get; set; } } } \ No newline at end of file diff --git a/YavscLib/IBlackListed.cs b/YavscLib/IBlackListed.cs new file mode 100644 index 00000000..3d898a4b --- /dev/null +++ b/YavscLib/IBlackListed.cs @@ -0,0 +1,9 @@ +namespace YavscLib +{ + public interface IBlackListed + { + long Id { get; set; } + string UserId { get; set; } + string OwnerId { get; set; } + } +} \ No newline at end of file diff --git a/YavscLib/IBlog.cs b/YavscLib/IBlog.cs index 6fcfab1b..d6500168 100644 --- a/YavscLib/IBlog.cs +++ b/YavscLib/IBlog.cs @@ -4,15 +4,14 @@ namespace Yavsc.Models { public interface IBlog { - IApplicationUser Author { get; set; } string AuthorId { get; set; } - string bcontent { get; set; } + string Content { get; set; } long Id { get; set; } - DateTime modified { get; set; } - string photo { get; set; } - DateTime posted { get; set; } - int rate { get; set; } - string title { get; set; } - bool visible { get; set; } + DateTime Modified { get; set; } + string Photo { get; set; } + DateTime Posted { get; set; } + int Rate { get; set; } + string Title { get; set; } + bool Visible { get; set; } } } \ No newline at end of file diff --git a/YavscLib/Properties/AssemblyInfo.cs b/YavscLib/Properties/AssemblyInfo.cs index fb9baf41..fadca10f 100644 --- a/YavscLib/Properties/AssemblyInfo.cs +++ b/YavscLib/Properties/AssemblyInfo.cs @@ -1,7 +1,5 @@ using System.Resources; using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information diff --git a/YavscLib/YavscLib.csproj b/YavscLib/YavscLib.csproj index 85b13f6e..0305d7aa 100644 --- a/YavscLib/YavscLib.csproj +++ b/YavscLib/YavscLib.csproj @@ -57,6 +57,7 @@ + diff --git a/YavscLib/YavscLib.sln b/YavscLib/YavscLib.sln index 88788b1f..1f586d05 100644 --- a/YavscLib/YavscLib.sln +++ b/YavscLib/YavscLib.sln @@ -1,7 +1,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yavsc.Client", "Yavsc.Client.csproj", "{67F9D3A8-F71E-4428-913F-C37AE82CDB24}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YavscLib", "YavscLib.csproj", "{67F9D3A8-F71E-4428-913F-C37AE82CDB24}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/YavscLib/project.json b/YavscLib/project.json index 6204fd2a..7700f259 100644 --- a/YavscLib/project.json +++ b/YavscLib/project.json @@ -15,6 +15,13 @@ "dependencies": {}, "frameworks": { "net451": {}, - ".NETPortable,Version=v4.5,Profile=Profile111": {} + ".NETPortable,Version=v4.5,Profile=Profile111": { + "frameworkAssemblies": { + "System.Runtime": "4.0.0", + "System.Globalization": "4.0.0", + "System.Resources.ResourceManager": "4.0.0", + "System.Resources.Reader": "4.0.0" + } + } } } \ No newline at end of file diff --git a/YavscLib/project.lock.json b/YavscLib/project.lock.json index a7ebab43..29807b00 100644 --- a/YavscLib/project.lock.json +++ b/YavscLib/project.lock.json @@ -3,14 +3,21 @@ "version": 2, "targets": { ".NETFramework,Version=v4.5.1": {}, - ".NETPortable,Version=v4.5,Profile=Profile111": {} + ".NETPortable,Version=v4.5,Profile=Profile111": {}, + ".NETFramework,Version=v4.5.1/debian.8-x86": {}, + ".NETFramework,Version=v4.5.1/debian.8-x64": {}, + ".NETPortable,Version=v4.5,Profile=Profile111/debian.8-x86": {}, + ".NETPortable,Version=v4.5,Profile=Profile111/debian.8-x64": {} }, "libraries": {}, "projectFileDependencyGroups": { "": [], ".NETFramework,Version=v4.5.1": [], - ".NETPortable,Version=v4.5,Profile=Profile111": [] - }, - "tools": {}, - "projectFileToolGroups": {} + ".NETPortable,Version=v4.5,Profile=Profile111": [ + "fx/System.Runtime >= 4.0.0", + "fx/System.Globalization >= 4.0.0", + "fx/System.Resources.ResourceManager >= 4.0.0", + "fx/System.Resources.Reader >= 4.0.0" + ] + } } \ No newline at end of file