Merge branch 'develop' of https://bitbucket.org/ncarminati/cms_step into develop

This commit is contained in:
Alessandro Francia
2018-06-22 12:10:16 +02:00
9 changed files with 90 additions and 72 deletions
@@ -4,6 +4,7 @@ using Step.Model.DTOModels.MaintenanceModels;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using static Step.Config.ServerConfig;
using static Step.Model.Constants;
@@ -301,15 +302,16 @@ namespace Step.Database.Controllers
.FirstOrDefault();
}
public MaintenanceFileModel AddAttachment(string fileName, string localFileName ,int maintenanceId)
public MaintenanceFileModel AddAttachment(string fileName, string localFileName ,int maintenanceId, int userId)
{
MaintenanceFileModel file = new MaintenanceFileModel()
{
FileName = fileName,
LocalFileName = localFileName,
MaintenanceId = maintenanceId
MaintenanceId = maintenanceId,
UserId = userId
};
// Add to db
dbCtx.MaintenanceFiles.Add(file);
dbCtx.SaveChanges();
@@ -333,6 +335,9 @@ namespace Step.Database.Controllers
dbCtx.MaintenanceFiles.Remove(attachment);
dbCtx.SaveChanges();
if (File.Exists(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName))
File.Delete(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName);
}
#endregion
}
@@ -13,7 +13,7 @@ namespace Step.Database.Migrations
string IMigrationMetadata.Id
{
get { return "201806191402402_InitMigration"; }
get { return "201806220920193_InitMigration"; }
}
string IMigrationMetadata.Source
@@ -79,10 +79,13 @@ namespace Step.Database.Migrations
file_name = c.String(unicode: false),
local_file_name = c.String(unicode: false),
maintenance_id = c.Int(nullable: false),
user_id = c.Int(),
})
.PrimaryKey(t => t.id)
.ForeignKey("dbo.maintenance", t => t.maintenance_id, cascadeDelete: true)
.Index(t => t.maintenance_id);
.ForeignKey("dbo.user", t => t.user_id)
.Index(t => t.maintenance_id)
.Index(t => t.user_id);
CreateTable(
"dbo.maintenance",
@@ -153,6 +156,7 @@ namespace Step.Database.Migrations
DropForeignKey("dbo.performed_maintenance", "maintenance", "dbo.maintenance");
DropForeignKey("dbo.maintenance_note", "user_id", "dbo.user");
DropForeignKey("dbo.maintenance_note", "maintenance_id", "dbo.maintenance");
DropForeignKey("dbo.maintenance_file", "user_id", "dbo.user");
DropForeignKey("dbo.maintenance_file", "maintenance_id", "dbo.maintenance");
DropForeignKey("dbo.maintenance", "user_id", "dbo.user");
DropForeignKey("dbo.machine_user", "user_id", "dbo.user");
@@ -163,6 +167,7 @@ namespace Step.Database.Migrations
DropIndex("dbo.maintenance_note", new[] { "maintenance_id" });
DropIndex("dbo.maintenance_note", new[] { "user_id" });
DropIndex("dbo.maintenance", new[] { "user_id" });
DropIndex("dbo.maintenance_file", new[] { "user_id" });
DropIndex("dbo.maintenance_file", new[] { "maintenance_id" });
DropIndex("dbo.machine_user", new[] { "role_id" });
DropIndex("dbo.machine_user", "unique_machine_user");
+4 -1
View File
@@ -37,7 +37,10 @@ namespace Step.Database.Migrations
new FunctionAccessModel() { Name = NC_SOFTKEY, Area = UNDER_HOOD, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 4 },
new FunctionAccessModel() { Name = USER_SOFTKEY, Area = UNDER_HOOD, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 5 },
new FunctionAccessModel() { Name = HEADS_CMD, Area = UNDER_HOOD, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 6 },
new FunctionAccessModel() { Name = AXES_CMD, Area = UNDER_HOOD, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 7 }
new FunctionAccessModel() { Name = AXES_CMD, Area = UNDER_HOOD, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 7 },
new FunctionAccessModel() { Name = TOOL_MANAGER, Area = TOOLING_KEY, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 8 },
new FunctionAccessModel() { Name = MAINTENANCE, Area = MAINTENANCE_KEY, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1, PlcId = 9 }
);
context.SaveChanges();
+5 -5
View File
@@ -77,9 +77,9 @@
<Compile Include="Controllers\UsersController.cs" />
<Compile Include="Controllers\MachinesUsersController.cs" />
<Compile Include="DatabaseContext.cs" />
<Compile Include="Migrations\201806191402402_InitMigration.cs" />
<Compile Include="Migrations\201806191402402_InitMigration.Designer.cs">
<DependentUpon>201806191402402_InitMigration.cs</DependentUpon>
<Compile Include="Migrations\201806220920193_InitMigration.cs" />
<Compile Include="Migrations\201806220920193_InitMigration.Designer.cs">
<DependentUpon>201806220920193_InitMigration.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -115,8 +115,8 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Migrations\201806191402402_InitMigration.resx">
<DependentUpon>201806191402402_InitMigration.cs</DependentUpon>
<EmbeddedResource Include="Migrations\201806220920193_InitMigration.resx">
<DependentUpon>201806220920193_InitMigration.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+2
View File
@@ -170,6 +170,8 @@ namespace Step.Model
public const string USER_SOFTKEY = "userSoftkeys";
public const string HEADS_CMD = "headsCmd";
public const string AXES_CMD = "axesCmd";
public const string TOOL_MANAGER = "toolManager";
public const string MAINTENANCE = "maintenance";
}
}
}
@@ -26,5 +26,11 @@ namespace Step.Model.DatabaseModels
[ForeignKey("MaintenanceId")]
public MaintenanceModel Maintenance { get; set; }
[Column("user_id")]
public int? UserId { get; set; }
[ForeignKey("UserId")]
public UserModel User { get; set; }
}
}
@@ -26,14 +26,12 @@ namespace Step.Controllers.WebApi
public class MaintenanceController : ApiController
{
[Route("maintenances"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.READ)]
public IHttpActionResult GetMaintenances()
{
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (NcHandler ncHandler = new NcHandler())
{
@@ -48,7 +46,7 @@ namespace Step.Controllers.WebApi
}
[Route("maintenance"), HttpPost]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult AddMaintenance([Required]DTONewMaintenanceModel newMaint)
{
if (!ModelState.IsValid)
@@ -57,8 +55,6 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
@@ -77,7 +73,7 @@ namespace Step.Controllers.WebApi
}
[Route("maintenance/{maintenanceId:int}"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult EditMaintenance(int maintenanceId, [Required]DTOUpdateMaintenanceModel newMaint)
{
if (!ModelState.IsValid)
@@ -86,8 +82,6 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
@@ -123,7 +117,7 @@ namespace Step.Controllers.WebApi
}
[Route("maintenance/{maintenanceId:int}"), HttpDelete]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult DeleteMaintenance(int maintenanceId)
{
if (!ModelState.IsValid)
@@ -132,8 +126,6 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
@@ -160,7 +152,7 @@ namespace Step.Controllers.WebApi
}
[Route("maintenance/{maintenanceId:int}/note"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.READ)]
public IHttpActionResult GetMaintenanceNotes(int maintenanceId)
{
if (!ModelState.IsValid)
@@ -190,8 +182,6 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
@@ -208,7 +198,7 @@ namespace Step.Controllers.WebApi
}
[Route("maintenance/{maintenanceId:int}/note/{noteId:int}"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult EditMaintenanceNote(int noteId, DTONewMaintenanceNoteModel newNote)
{
if (!ModelState.IsValid)
@@ -217,8 +207,6 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
@@ -239,7 +227,7 @@ namespace Step.Controllers.WebApi
}
[Route("maintenance/{maintenanceId:int}/note/{noteId:int}"), HttpDelete]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult DeleteMaintenanceNote(int noteId)
{
if (!ModelState.IsValid)
@@ -248,8 +236,6 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
@@ -272,7 +258,7 @@ namespace Step.Controllers.WebApi
[Route("maintenance/{maintenanceId:int}/performe"), HttpPost]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult PerformeMaintenance(int maintenanceId)
{
if (!ModelState.IsValid)
@@ -281,9 +267,7 @@ namespace Step.Controllers.WebApi
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
// Update data
using (NcHandler ncHandler = new NcHandler())
{
@@ -307,39 +291,9 @@ namespace Step.Controllers.WebApi
}
}
[Route("maintenance/{maintenanceId:int}/attachment"), HttpPost]
public async Task<IHttpActionResult> AddAttachment(int maintenanceId)
{
// Check whether the POST operation is MultiPart?
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// Create CustomMultipartFormDataStreamProvider
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
// MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
List<string> files = new List<string>();
// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
var result = await Request.Content.ReadAsMultipartAsync(provider);
MaintenanceFileModel attachment = null;
using (MaintenancesController maintenancesController = new MaintenancesController())
{
// Remove foreach
foreach (MultipartFileData file in provider.FileData)
{
var fileName = Path.GetFileName(file.LocalFileName);
files.Add(fileName);
attachment = maintenancesController.AddAttachment(file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty), fileName, maintenanceId);
}
}
// Send OK Response along with saved file names to the client.
return Ok(attachment);
}
[Route("maintenance/{maintenanceId:int}/attachments"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.READ)]
public IHttpActionResult GetAttachments(int maintenanceId)
{
using (MaintenancesController maintenancesController = new MaintenancesController())
@@ -365,9 +319,52 @@ namespace Step.Controllers.WebApi
}
}
[Route("maintenance/{maintenanceId:int}/attachment"), HttpPost]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public async Task<IHttpActionResult> AddAttachment(int maintenanceId)
{
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
// Check whether the POST operation is MultiPart?
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// Create CustomMultipartFormDataStreamProvider
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
// MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
List<string> files = new List<string>();
// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
var result = await Request.Content.ReadAsMultipartAsync(provider);
MaintenanceFileModel attachment = null;
using (MaintenancesController maintenancesController = new MaintenancesController())
{
// Remove foreach
foreach (MultipartFileData file in provider.FileData)
{
var fileName = Path.GetFileName(file.LocalFileName);
files.Add(fileName);
attachment = maintenancesController.AddAttachment(file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty), fileName, maintenanceId, Convert.ToInt32(userId.Value));
}
}
// Send OK Response along with saved file names to the client.
return Ok(attachment);
}
[Route("attachment/{attachmentId:int}"), HttpDelete]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult DeleteAttachment(int attachmentId)
{
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
// Get single file
@@ -375,12 +372,12 @@ namespace Step.Controllers.WebApi
// Check if exist in db or physically
if (attachment == null)
return NotFound();
// Check user
if (attachment.UserId != Convert.ToInt32(userId.Value))
return Unauthorized();
maintenancesController.DeleteAttachment(attachment);
if (File.Exists(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName))
File.Delete(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName);
return Ok();
}
}