diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index 086eed5d..fac6eede 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Database/Controllers/MachinesUsersController.cs b/Step.Database/Controllers/MachinesUsersController.cs index dae8cbe3..7b627d9c 100644 --- a/Step.Database/Controllers/MachinesUsersController.cs +++ b/Step.Database/Controllers/MachinesUsersController.cs @@ -2,6 +2,7 @@ using Step.Model.DTOModels; using System; using System.Linq; +using static Step.Model.Constants; namespace Step.Database.Controllers { @@ -75,6 +76,17 @@ namespace Step.Database.Controllers }; } + + public bool UserIsCmsAdmin(int machineId, int userId) + { + MachineUserModel user = FindByUserId(machineId, userId); + if (user.Role.RoleId != (int)ROLE_IDS.CMS_SERVICE_ONLY) + return true; + + return false; + } + + public int CompareUsersRole(int firstUserId, int secondUserId, int machineId) { MachineUserModel firstUser = FindByUserId(machineId, firstUserId); @@ -87,5 +99,7 @@ namespace Step.Database.Controllers else return -1; } + + } } \ No newline at end of file diff --git a/Step.Database/Controllers/MaintenancesController.cs b/Step.Database/Controllers/MaintenancesController.cs index bc409de8..ac6a7fb3 100644 --- a/Step.Database/Controllers/MaintenancesController.cs +++ b/Step.Database/Controllers/MaintenancesController.cs @@ -3,6 +3,7 @@ using Step.Model.DTOModels; using Step.Model.DTOModels.MaintenanceModels; using System; using System.Collections.Generic; +using System.Data.Entity; using System.Linq; using static Step.Config.ServerConfig; using static Step.Model.Constants; @@ -196,7 +197,9 @@ namespace Step.Database.Controllers { return dbCtx .MaintenancesNotes - .Find(noteId); + .Where(x => x.Id == noteId) + .Include("User") + .FirstOrDefault(); } public List FindNotes() @@ -242,6 +245,17 @@ namespace Step.Database.Controllers dbCtx.SaveChanges(); + dbCtx.Entry(note).Reference(x => x.User).Load(); + dbCtx.Users.Attach(note.User); + + return (DTOMaintenanceNoteModel)note; + } + + public DTOMaintenanceNoteModel UpdateNote(MaintenanceNoteModel note, DTONewMaintenanceNoteModel newNote) + { + note.Message = newNote.Message; + dbCtx.SaveChanges(); + return (DTOMaintenanceNoteModel)note; } diff --git a/Step.Database/Controllers/UsersController.cs b/Step.Database/Controllers/UsersController.cs index 4e95b95a..da653e85 100644 --- a/Step.Database/Controllers/UsersController.cs +++ b/Step.Database/Controllers/UsersController.cs @@ -113,7 +113,7 @@ namespace Step.Database.Controllers { // If not exist add new user user = dbCtx.Users.Add( - CreateUserModel("cms", "cms", "cms", "cms", new CultureInfo("en")) + CreateUserModel("cms", "cms", "cms", "cms", new CultureInfo("it")) ); // Commit changes diff --git a/Step.Database/Migrations/201806141227365_InitMigration.Designer.cs b/Step.Database/Migrations/201806141227365_InitMigration.Designer.cs new file mode 100644 index 00000000..093e50f1 --- /dev/null +++ b/Step.Database/Migrations/201806141227365_InitMigration.Designer.cs @@ -0,0 +1,29 @@ +// +namespace Step.Database.Migrations +{ + using System.CodeDom.Compiler; + using System.Data.Entity.Migrations; + using System.Data.Entity.Migrations.Infrastructure; + using System.Resources; + + [GeneratedCode("EntityFramework.Migrations", "6.2.0-61023")] + public sealed partial class InitMigration : IMigrationMetadata + { + private readonly ResourceManager Resources = new ResourceManager(typeof(InitMigration)); + + string IMigrationMetadata.Id + { + get { return "201806141227365_InitMigration"; } + } + + string IMigrationMetadata.Source + { + get { return null; } + } + + string IMigrationMetadata.Target + { + get { return Resources.GetString("Target"); } + } + } +} diff --git a/Step.Database/Migrations/201806141227365_InitMigration.cs b/Step.Database/Migrations/201806141227365_InitMigration.cs new file mode 100644 index 00000000..d6954295 --- /dev/null +++ b/Step.Database/Migrations/201806141227365_InitMigration.cs @@ -0,0 +1,165 @@ +namespace Step.Database.Migrations +{ + using System; + using System.Data.Entity.Migrations; + + public partial class InitMigration : DbMigration + { + public override void Up() + { + CreateTable( + "dbo.function_access", + c => new + { + id = c.Int(nullable: false, identity: true), + name = c.String(unicode: false), + write_level_min = c.Int(nullable: false), + read_level_min = c.Int(nullable: false), + area = c.String(unicode: false), + enabled = c.Boolean(nullable: false), + plc_id = c.Int(nullable: false), + }) + .PrimaryKey(t => t.id); + + CreateTable( + "dbo.machine", + c => new + { + id = c.Int(nullable: false, identity: true), + name = c.String(unicode: false), + unique_id = c.String(unicode: false), + }) + .PrimaryKey(t => t.id); + + CreateTable( + "dbo.machine_user", + c => new + { + id = c.Int(nullable: false, identity: true), + machine_id = c.Int(nullable: false), + user_id = c.Int(nullable: false), + role_id = c.Int(nullable: false), + }) + .PrimaryKey(t => t.id) + .ForeignKey("dbo.machine", t => t.machine_id, cascadeDelete: true) + .ForeignKey("dbo.role", t => t.role_id, cascadeDelete: true) + .ForeignKey("dbo.user", t => t.user_id, cascadeDelete: true) + .Index(t => new { t.machine_id, t.user_id }, unique: true, clustered: true, name: "unique_machine_user") + .Index(t => t.role_id); + + CreateTable( + "dbo.role", + c => new + { + id = c.Int(nullable: false, identity: true), + name = c.String(unicode: false), + level = c.Int(nullable: false), + }) + .PrimaryKey(t => t.id); + + CreateTable( + "dbo.user", + c => new + { + id = c.Int(nullable: false, identity: true), + username = c.String(nullable: false, unicode: false), + first_name = c.String(unicode: false), + last_name = c.String(unicode: false), + password = c.String(unicode: false), + security_stamp = c.String(unicode: false), + language = c.String(unicode: false), + }) + .PrimaryKey(t => t.id); + + CreateTable( + "dbo.maintenance", + c => new + { + id = c.Int(nullable: false, identity: true), + intervall = c.Double(), + deadline = c.DateTime(nullable: false, precision: 0), + type = c.Int(nullable: false), + counter_id = c.Int(nullable: false), + title = c.String(unicode: false), + description = c.String(unicode: false), + unit_of_measure = c.Int(), + creation_date = c.DateTime(nullable: false, precision: 0), + last_expiration_date = c.DateTime(precision: 0), + user_id = c.Int(), + }) + .PrimaryKey(t => t.id) + .ForeignKey("dbo.user", t => t.user_id) + .Index(t => t.user_id); + + CreateTable( + "dbo.maintenance_note", + c => new + { + id = c.Int(nullable: false, identity: true), + message = c.String(unicode: false), + user_id = c.Int(nullable: false), + maintenance_id = c.Int(), + timestamp = c.DateTime(nullable: false, precision: 0), + }) + .PrimaryKey(t => t.id) + .ForeignKey("dbo.maintenance", t => t.maintenance_id) + .ForeignKey("dbo.user", t => t.user_id, cascadeDelete: true) + .Index(t => t.user_id) + .Index(t => t.maintenance_id); + + CreateTable( + "dbo.performed_maintenance", + c => new + { + id = c.Int(nullable: false, identity: true), + date = c.DateTime(nullable: false, precision: 0), + counter_value = c.Int(nullable: false), + maintenance = c.Int(nullable: false), + }) + .PrimaryKey(t => t.id) + .ForeignKey("dbo.maintenance", t => t.maintenance, cascadeDelete: true) + .Index(t => t.maintenance); + + CreateTable( + "dbo.session", + c => new + { + id = c.Int(nullable: false, identity: true), + token = c.String(unicode: false), + machine_user_id = c.Int(nullable: false), + }) + .PrimaryKey(t => t.id) + .ForeignKey("dbo.machine_user", t => t.machine_user_id, cascadeDelete: true) + .Index(t => t.machine_user_id); + + } + + public override void Down() + { + DropForeignKey("dbo.session", "machine_user_id", "dbo.machine_user"); + 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", "user_id", "dbo.user"); + DropForeignKey("dbo.machine_user", "user_id", "dbo.user"); + DropForeignKey("dbo.machine_user", "role_id", "dbo.role"); + DropForeignKey("dbo.machine_user", "machine_id", "dbo.machine"); + DropIndex("dbo.session", new[] { "machine_user_id" }); + DropIndex("dbo.performed_maintenance", new[] { "maintenance" }); + DropIndex("dbo.maintenance_note", new[] { "maintenance_id" }); + DropIndex("dbo.maintenance_note", new[] { "user_id" }); + DropIndex("dbo.maintenance", new[] { "user_id" }); + DropIndex("dbo.machine_user", new[] { "role_id" }); + DropIndex("dbo.machine_user", "unique_machine_user"); + DropTable("dbo.session"); + DropTable("dbo.performed_maintenance"); + DropTable("dbo.maintenance_note"); + DropTable("dbo.maintenance"); + DropTable("dbo.user"); + DropTable("dbo.role"); + DropTable("dbo.machine_user"); + DropTable("dbo.machine"); + DropTable("dbo.function_access"); + } + } +} diff --git a/Step.Database/Migrations/201806141227365_InitMigration.resx b/Step.Database/Migrations/201806141227365_InitMigration.resx new file mode 100644 index 00000000..6e0f7d04 --- /dev/null +++ b/Step.Database/Migrations/201806141227365_InitMigration.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + H4sIAAAAAAAEAO1dW2/juBV+L9D/IOixnY2TGSzQBvYuvI7TNTpOgrFnun0yGInxCNXFK1HZBEV/WR/6k/oXSupKiheRutjOoFhgMaHIj4eHHw/JQ/L4v//+z/THl8C3nmGceFE4s68uLm0Lhk7keuF+Zqfo6bs/2T/+8PvfTZdu8GJ9KfN9IPlwyTCZ2V8ROlxPJonzFQYguQg8J46S6AldOFEwAW40eX95+efJ1dUEYggbY1nW9FMaIi+A2R/4z0UUOvCAUuCvIxf6SZGOv2wyVOsOBDA5AAfO7A2Ch4sbgMAjSKBtzX0PYCE20H+yLRCGEQIIi3j9OYEbFEfhfnPACcDfvh4gzvcEfFIqE/26zq7bisv3pBWTumAJ5aQJigJDwKsPhVomzeKdlGtXasOKW2IFo1fS6kx5M/s2DR0CPXccmCSZnm2rWe/1wo9JmULNWa5K2XnfXAiA3ll19ncVSzCZyH/vrEXqozSGsxCmKAY4x0P66HvOX+HrNvoHDGdh6vu08Fh8/I1JwEkPcXSAMXr9BJ+ETVq5tjVhUSZNmApEipC3fhWiD+9t6w4LBh59WPGG0tQGRTH8CwxhDBB0HwBCMMbdvnJhpnlOlkbN5P9lbZioeLjZ1hq8fIThHn2d2fiftnXrvUC3TCkk+Bx6eHTiQihOYVslf4s9BD/C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o + + \ No newline at end of file diff --git a/Step.Database/Migrations/Configuration.cs b/Step.Database/Migrations/Configuration.cs index 5f4ee6ea..15cf4205 100644 --- a/Step.Database/Migrations/Configuration.cs +++ b/Step.Database/Migrations/Configuration.cs @@ -4,6 +4,7 @@ namespace Step.Database.Migrations using System.Data.Entity.Migrations; using static Step.Model.Constants.FUNCTIONALITY_NAMES; using static Step.Model.Constants.AREAS; + using static Step.Model.Constants; public sealed class Configuration : DbMigrationsConfiguration { @@ -18,8 +19,9 @@ namespace Step.Database.Migrations { // This method will be called after migrating to the latest version. context.Roles.AddOrUpdate( - new RoleModel() { RoleId = 1, Level = 10, Name = "Admin" }, - new RoleModel() { RoleId = 2, Level = 0, Name = "Guest" } + new RoleModel() { RoleId = (int)ROLE_IDS.CMS_SERVICE_ONLY, Level = 100, Name = "cmsServiceOnly" }, + new RoleModel() { RoleId = (int)ROLE_IDS.ADMIN, Level = 10, Name = "Admin" }, + new RoleModel() { RoleId = (int)ROLE_IDS.GUEST, Level = 0, Name = "Guest" } ); context.FunctionsAccess.AddOrUpdate( diff --git a/Step.Database/Step.Database.csproj b/Step.Database/Step.Database.csproj index 452d795f..72bcd6fd 100644 --- a/Step.Database/Step.Database.csproj +++ b/Step.Database/Step.Database.csproj @@ -77,9 +77,9 @@ - - - 201806130949510_InitMigration.cs + + + 201806141227365_InitMigration.cs @@ -115,8 +115,8 @@ - - 201806130949510_InitMigration.cs + + 201806141227365_InitMigration.cs diff --git a/Step.Model/Constants.cs b/Step.Model/Constants.cs index 259ac72d..5de97765 100644 --- a/Step.Model/Constants.cs +++ b/Step.Model/Constants.cs @@ -6,6 +6,13 @@ namespace Step.Model { public static class Constants { + public enum ROLE_IDS + { + CMS_SERVICE_ONLY = 1, + ADMIN = 2, + GUEST = 3 + } + public enum ACTIONS { READ, diff --git a/Step.Model/DTOModels/DTOUserModel.cs b/Step.Model/DTOModels/DTOUserModel.cs index 054c1eb1..492c6ef9 100644 --- a/Step.Model/DTOModels/DTOUserModel.cs +++ b/Step.Model/DTOModels/DTOUserModel.cs @@ -17,12 +17,11 @@ namespace Step.Model.DTOModels public static explicit operator DTOMessageUserModel(UserModel user) { - return new DTOUserModel() + return new DTOMessageUserModel() { Id = user.UserId, FirstName = user.FirstName, LastName = user.LastName, - Language = user.Language, Username = user.Username }; } diff --git a/Step.Model/DTOModels/MaintenanceModels/DTOMaintenanceNoteModel.cs b/Step.Model/DTOModels/MaintenanceModels/DTOMaintenanceNoteModel.cs index b3d04154..48d81172 100644 --- a/Step.Model/DTOModels/MaintenanceModels/DTOMaintenanceNoteModel.cs +++ b/Step.Model/DTOModels/MaintenanceModels/DTOMaintenanceNoteModel.cs @@ -1,6 +1,7 @@ using Step.Model.DatabaseModels; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,6 +10,7 @@ namespace Step.Model.DTOModels.MaintenanceModels { public class DTONewMaintenanceNoteModel { + [Required] public string Message { get; set; } } diff --git a/Step.Model/DatabaseModels/MaintenanceNoteModel.cs b/Step.Model/DatabaseModels/MaintenanceNoteModel.cs index f1ddf8af..55c035bf 100644 --- a/Step.Model/DatabaseModels/MaintenanceNoteModel.cs +++ b/Step.Model/DatabaseModels/MaintenanceNoteModel.cs @@ -18,6 +18,12 @@ namespace Step.Model.DatabaseModels [Column("message")] public string Message { get; set; } + + [Column("user_id")] + public int UserId { get; set; } + + [ForeignKey("UserId")] + public UserModel User { get; set; } [Column("maintenance_id")] public int? MaintenanceId { get; set; } @@ -25,12 +31,6 @@ 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; } - [Column("timestamp")] public DateTime DateTime { get; set; } } diff --git a/Step.NC/NcHandler.cs b/Step.NC/NcHandler.cs index a3cb716a..cdf90b33 100644 --- a/Step.NC/NcHandler.cs +++ b/Step.NC/NcHandler.cs @@ -446,10 +446,14 @@ namespace Step.NC if(currMaintenance.UserId != null) { // Check if user can edit the maintenance -> caller id - maintenance user id - int comparision = machineUsersController.CompareUsersRole(Convert.ToInt32(userId), currMaintenance.UserId.Value, MachineConfig.MachineId); + int comparision = machineUsersController.CompareUsersRole(userId, currMaintenance.UserId.Value, MachineConfig.MachineId); if (comparision >= 0) canEdit = true; } + else + { + canEdit = machineUsersController.UserIsCmsAdmin(MachineConfig.MachineId, userId); + } } dtoMaintenancesModel.Add(new DTOMaintenanceModel() diff --git a/Step/Controllers/WebApi/MaintenanceController.cs b/Step/Controllers/WebApi/MaintenanceController.cs index df753dfe..dc76f940 100644 --- a/Step/Controllers/WebApi/MaintenanceController.cs +++ b/Step/Controllers/WebApi/MaintenanceController.cs @@ -204,5 +204,69 @@ namespace Step.Controllers.WebApi return Ok(notes); } } + + [Route("maintenance/{maintenanceId:int}/note/{noteId:int}"), HttpPut] + + [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)] + public IHttpActionResult EditMaintenanceNote(int noteId, DTONewMaintenanceNoteModel newNote) + { + if (!ModelState.IsValid) + return BadRequest(ModelState); + + 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()) + { + // Check if maintenance id + MaintenanceNoteModel dbNote = maintenancesController.FindNoteById(noteId); + if (dbNote == null) + return NotFound(); + + // Check if user is different + if (dbNote.UserId != Convert.ToInt32(userId.Value)) + return Unauthorized(); + + // Update data + DTOMaintenanceNoteModel notes = maintenancesController.UpdateNote(dbNote, newNote); + + return Ok(notes); + } + } + + [Route("maintenance/{maintenanceId:int}/note/{noteId:int}"), HttpDelete] + + [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)] + public IHttpActionResult DeleteMaintenanceNote(int noteId) + { + if (!ModelState.IsValid) + return BadRequest(ModelState); + + 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()) + { + // Check if maintenance id + MaintenanceNoteModel dbNote = maintenancesController.FindNoteById(noteId); + if (dbNote == null) + return NotFound(); + + // Check if user is different + if (dbNote.UserId != Convert.ToInt32(userId.Value)) + return Unauthorized(); + + // Update data + maintenancesController.DeleteMessage(dbNote.Id); + + return Ok(); + } + } } } \ No newline at end of file