diff --git a/IoTSharp.Hub/Controllers/CustomersController.cs b/IoTSharp.Hub/Controllers/CustomersController.cs index 727e6191d095f0fc6f46557cea9de9567464ef14..9391079987016da634bf1995885c4f4b73ecd94c 100644 --- a/IoTSharp.Hub/Controllers/CustomersController.cs +++ b/IoTSharp.Hub/Controllers/CustomersController.cs @@ -6,10 +6,12 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using IoTSharp.Hub.Data; +using Microsoft.AspNetCore.Authorization; namespace IoTSharp.Hub.Controllers { [Route("api/[controller]")] + [Authorize] [ApiController] public class CustomersController : ControllerBase { @@ -22,12 +24,22 @@ namespace IoTSharp.Hub.Controllers // GET: api/Tenants [HttpGet("Tenant/{tenantId}")] + [Authorize(Roles = nameof(UserRole.NormalUser))] public async Task>> GetCustomers(Guid tenantId) { - return (await _context.Tenant.FindAsync(tenantId)).Customers?.ToList(); + var f = from c in _context.Customer where c.Tenant.Id == tenantId select c; + if (!f.Any()) + { + return NotFound(); + } + else + { + return await f.ToArrayAsync(); + } } // GET: api/Customers/5 + [Authorize(Roles = nameof(UserRole.NormalUser))] [HttpGet("{id}")] public async Task> GetCustomer(Guid id) { @@ -42,6 +54,7 @@ namespace IoTSharp.Hub.Controllers } // PUT: api/Customers/5 + [Authorize(Roles = nameof(UserRole.CustomerAdmin))] [HttpPut("{id}")] public async Task PutCustomer(Guid id, Customer customer) { @@ -49,12 +62,7 @@ namespace IoTSharp.Hub.Controllers { return BadRequest(); } - var tent=await _context.Tenant.FindAsync(customer.Id); - if (tent == null) - { - return NotFound(); - } - customer.Tenant = tent; + customer.Tenant = _context.Tenant.Find(customer.Tenant.Id); _context.Entry(customer).State = EntityState.Modified; try { @@ -76,16 +84,18 @@ namespace IoTSharp.Hub.Controllers } // POST: api/Customers + [Authorize(Roles = nameof(UserRole.CustomerAdmin))] [HttpPost] public async Task> PostCustomer(Customer customer) { - customer.Tenant= _context.Tenant.Find(customer.Tenant.Id); + customer.Tenant = _context.Tenant.Find(customer.Tenant.Id); _context.Customer.Add(customer); await _context.SaveChangesAsync(); - return CreatedAtAction("GetCustomer",customer.Id); + return await GetCustomer(customer.Id); } // DELETE: api/Customers/5 + [Authorize(Roles = nameof(UserRole.TenantAdmin))] [HttpDelete("{id}")] public async Task> DeleteCustomer(Guid id) { @@ -108,7 +118,7 @@ namespace IoTSharp.Hub.Controllers public class TenantDto { - public Guid Id { get; set; } + public Guid Id { get; set; } } } -} +} \ No newline at end of file diff --git a/IoTSharp.Hub/Controllers/DevicesController.cs b/IoTSharp.Hub/Controllers/DevicesController.cs index 92400a9c1e23fbdb54b368216ff4abd5104360ab..4f75a07fce6e4ea43a139fc67b638822319f85df 100644 --- a/IoTSharp.Hub/Controllers/DevicesController.cs +++ b/IoTSharp.Hub/Controllers/DevicesController.cs @@ -6,10 +6,13 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using IoTSharp.Hub.Data; +using Microsoft.AspNetCore.Authorization; +using IoTSharp.Hub.Dtos; namespace IoTSharp.Hub.Controllers { [Route("api/[controller]")] + [Authorize] [ApiController] public class DevicesController : ControllerBase { @@ -21,13 +24,24 @@ namespace IoTSharp.Hub.Controllers } // GET: api/Devices + [HttpGet("Customers/{customerId}")] + [Authorize(Roles = nameof(UserRole.NormalUser))] [HttpGet] - public async Task>> GetDevice() + public async Task>> GetDevices(Guid customerId) { - return await _context.Device.ToListAsync(); + var f = from c in _context.Device where c.Customer.Id == customerId select c; + if (!f.Any()) + { + return NotFound(new ApiResult(ApiCode.NotFoundCustomer, $"Customer {customerId} not found ", customerId)); + } + else + { + return await f.ToArrayAsync(); + } } // GET: api/Devices/5 + [Authorize(Roles = nameof(UserRole.NormalUser))] [HttpGet("{id}")] public async Task> GetDevice(Guid id) { @@ -35,13 +49,14 @@ namespace IoTSharp.Hub.Controllers if (device == null) { - return NotFound(); + return NotFound(new ApiResult(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); } return device; } // PUT: api/Devices/5 + [Authorize(Roles = nameof(UserRole.CustomerAdmin))] [HttpPut("{id}")] public async Task PutDevice(Guid id, Device device) { @@ -60,7 +75,7 @@ namespace IoTSharp.Hub.Controllers { if (!DeviceExists(id)) { - return NotFound(); + return NotFound(new ApiResult(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); } else { @@ -72,23 +87,30 @@ namespace IoTSharp.Hub.Controllers } // POST: api/Devices + [Authorize(Roles = nameof(UserRole.CustomerAdmin))] [HttpPost] public async Task> PostDevice(Device device) { + device.Tenant = _context.Tenant.Find(device.Tenant.Id); + device.Customer = _context.Customer.Find(device.Customer.Id); + if (device.Tenant == null || device.Customer == null) + { + return NotFound(new ApiResult(ApiCode.NotFoundTenantOrCustomer, $"Not found Tenant or Customer ", device)); + } _context.Device.Add(device); await _context.SaveChangesAsync(); - - return CreatedAtAction("GetDevice", new { id = device.Id }, device); + return await GetDevice(device.Id); } // DELETE: api/Devices/5 + [Authorize(Roles = nameof(UserRole.CustomerAdmin))] [HttpDelete("{id}")] public async Task> DeleteDevice(Guid id) { var device = await _context.Device.FindAsync(id); if (device == null) { - return NotFound(); + return NotFound(new ApiResult(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); } _context.Device.Remove(device); @@ -102,4 +124,4 @@ namespace IoTSharp.Hub.Controllers return _context.Device.Any(e => e.Id == id); } } -} +} \ No newline at end of file diff --git a/IoTSharp.Hub/Controllers/TenantsController.cs b/IoTSharp.Hub/Controllers/TenantsController.cs index a4f1bdc424821ad0987f578cddb98ff73b92e9d1..637a2c0642de1cff5b12ecf7c3f8bb951e5c1c28 100644 --- a/IoTSharp.Hub/Controllers/TenantsController.cs +++ b/IoTSharp.Hub/Controllers/TenantsController.cs @@ -22,7 +22,6 @@ namespace IoTSharp.Hub.Controllers private ApplicationDbContext _context; private ILogger _logger; private readonly UserManager _userManager; - private readonly IConfiguration _configuration; private readonly SignInManager _signInManager; public TenantsController( diff --git a/IoTSharp.Hub/Data/ApplicationDbContext.cs b/IoTSharp.Hub/Data/ApplicationDbContext.cs index 0081b947ac6a5e29a97b6738214e4b21970067b0..a6874d00fd510b0f2895417dab110b3c51794057 100644 --- a/IoTSharp.Hub/Data/ApplicationDbContext.cs +++ b/IoTSharp.Hub/Data/ApplicationDbContext.cs @@ -24,7 +24,6 @@ namespace IoTSharp.Hub.Data { Database.Migrate(); } - } public DatabaseType DatabaseType { get; private set; } @@ -32,7 +31,7 @@ namespace IoTSharp.Hub.Data protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - + modelBuilder.Entity() .HasDiscriminator(nameof(Data.DataStorage.Catalog)) .HasValue(DataCatalog.None) @@ -45,9 +44,6 @@ namespace IoTSharp.Hub.Data modelBuilder.Entity().HasDiscriminator(nameof(Data.DataStorage.Catalog)); modelBuilder.Entity().HasDiscriminator(nameof(Data.DataStorage.Catalog)); modelBuilder.Entity().HasDiscriminator(nameof(Data.DataStorage.Catalog)); - - - switch (DatabaseType) { @@ -67,7 +63,6 @@ namespace IoTSharp.Hub.Data } } - private void ForNpgsql(ModelBuilder modelBuilder) { modelBuilder.Entity() @@ -131,5 +126,6 @@ namespace IoTSharp.Hub.Data public DbSet DataStorage { get; set; } public DbSet AttributeData { get; set; } public DbSet TelemetryLatest { get; set; } + public DbSet DeviceIdentities { get; set; } } } \ No newline at end of file diff --git a/IoTSharp.Hub/Data/Devices.cs b/IoTSharp.Hub/Data/Device.cs similarity index 84% rename from IoTSharp.Hub/Data/Devices.cs rename to IoTSharp.Hub/Data/Device.cs index 8febfcedef09a45060776adbd68391a7854debc3..c3dd96d1f69b473b42c52a0ddc9dff129fc78093 100644 --- a/IoTSharp.Hub/Data/Devices.cs +++ b/IoTSharp.Hub/Data/Device.cs @@ -7,20 +7,19 @@ namespace IoTSharp.Hub.Data { public class Device { - public Guid Id { get; set; } = Guid.NewGuid(); public string Name { get; set; } - + public string Type { get; set; } - public Tenant Tenant { get; set; } + public Tenant Tenant { get; set; } - public Customer Customer { get; set; } + public Customer Customer { get; set; } public virtual List AttributeData { get; set; } public virtual List AttributeLatest { get; set; } - public virtual List TelemetryData { get; set; } + public virtual List TelemetryData { get; set; } public virtual List TelemetryLatest { get; set; } } -} +} \ No newline at end of file diff --git a/IoTSharp.Hub/Data/DeviceIdentity.cs b/IoTSharp.Hub/Data/DeviceIdentity.cs new file mode 100644 index 0000000000000000000000000000000000000000..22fc2029b9b1e6a657507c67d51e26227b0ae6d0 --- /dev/null +++ b/IoTSharp.Hub/Data/DeviceIdentity.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; + +namespace IoTSharp.Hub.Data +{ + public class DeviceIdentity + { + [Key] + public Guid Id { get; set; } + + [Required] + [EnumDataType(typeof(IdentityType))] + public IdentityType IdentityType { get; set; } + + /// + /// When Is ,this is a Token. + /// When Is ,this is a device name. + /// When Is ,this is X509 Certificate' Fingerprint. + /// + [Required] + public string IdentityId { get; set; } + + /// + /// When Is ,this is null. + /// When Is ,this is a password. + /// When Is ,this is X509 Certificate' PEM. + /// + public string IdentityValue { get; set; } + + [Required] + public Device Device { get; set; } + } +} \ No newline at end of file diff --git a/IoTSharp.Hub/Data/Enums.cs b/IoTSharp.Hub/Data/Enums.cs index ea9b56a9f47fd7d2174c01ea7289639ad060b7a2..281f0c9c84477750488722b033ea0f4bcc477a7b 100644 --- a/IoTSharp.Hub/Data/Enums.cs +++ b/IoTSharp.Hub/Data/Enums.cs @@ -9,17 +9,21 @@ namespace IoTSharp.Hub.Data { public static class IoTSharpClaimTypes { - public const string Customer = "http://schemas.iotsharp.net/ws/2019/01/identity/claims/customer"; public const string Tenant = "http://schemas.iotsharp.net/ws/2019/01/identity/claims/tenant"; } - public enum ApiCode:int + + public enum ApiCode : int { - Success= 10000, - LoginError=10001, + Success = 10000, + LoginError = 10001, Exception = 10002, AlreadyExists = 10003, + NotFoundTenantOrCustomer = 10004, + NotFoundDevice = 10005, + NotFoundCustomer = 10006, } + public enum DataCatalog { None, @@ -27,8 +31,8 @@ namespace IoTSharp.Hub.Data AttributeLatest, TelemetryData, TelemetryLatest, - } + public enum UserRole { Anonymous, @@ -36,7 +40,6 @@ namespace IoTSharp.Hub.Data CustomerAdmin, TenantAdmin, SystemAdmin, - } public enum DataType @@ -49,10 +52,18 @@ namespace IoTSharp.Hub.Data XML, Binary } + public enum DatabaseType { mssql, npgsql, sqlite } -} + + public enum IdentityType + { + AccessToken, + DevicePassword, + X509Certificate + } +} \ No newline at end of file diff --git a/IoTSharp.Hub/Data/Migrations/20190118115805_AddDeviceIdentity.Designer.cs b/IoTSharp.Hub/Data/Migrations/20190118115805_AddDeviceIdentity.Designer.cs new file mode 100644 index 0000000000000000000000000000000000000000..b4e464e9054e19ee7d51ab5f0a0cdac2cebf44c0 --- /dev/null +++ b/IoTSharp.Hub/Data/Migrations/20190118115805_AddDeviceIdentity.Designer.cs @@ -0,0 +1,510 @@ +// +using System; +using IoTSharp.Hub.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace IoTSharp.Hub.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20190118115805_AddDeviceIdentity")] + partial class AddDeviceIdentity + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn) + .HasAnnotation("ProductVersion", "2.2.0-rtm-35687") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("IoTSharp.Hub.Data.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Address"); + + b.Property("City"); + + b.Property("Country"); + + b.Property("Email"); + + b.Property("Name"); + + b.Property("Phone"); + + b.Property("Province"); + + b.Property("Street"); + + b.Property("TenantId"); + + b.Property("ZipCode"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.ToTable("Customer"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.DataStorage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Catalog"); + + b.Property("DateTime"); + + b.Property("KeyName") + .IsRequired(); + + b.Property("Type"); + + b.Property("Value_Binary"); + + b.Property("Value_Boolean"); + + b.Property("Value_Double"); + + b.Property("Value_Json") + .HasColumnType("jsonb"); + + b.Property("Value_Long"); + + b.Property("Value_String"); + + b.Property("Value_XML") + .HasColumnType("xml"); + + b.HasKey("Id"); + + b.ToTable("DataStorage"); + + b.HasDiscriminator("Catalog").HasValue(0); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.Device", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("CustomerId"); + + b.Property("Name"); + + b.Property("TenantId"); + + b.Property("Type"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("TenantId"); + + b.ToTable("Device"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.DeviceIdentity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("DeviceId"); + + b.Property("IdentityId") + .IsRequired(); + + b.Property("IdentityType"); + + b.Property("IdentityValue"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.ToTable("DeviceIdentities"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.Relationship", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("CustomerId"); + + b.Property("IdentityUserId"); + + b.Property("TenantId"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("IdentityUserId"); + + b.HasIndex("TenantId"); + + b.ToTable("Relationship"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.Tenant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Address"); + + b.Property("City"); + + b.Property("Country"); + + b.Property("EMail"); + + b.Property("Name"); + + b.Property("Phone"); + + b.Property("Province"); + + b.Property("Street"); + + b.Property("ZipCode"); + + b.HasKey("Id"); + + b.ToTable("Tenant"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken(); + + b.Property("Name") + .HasMaxLength(256); + + b.Property("NormalizedName") + .HasMaxLength(256); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("ClaimType"); + + b.Property("ClaimValue"); + + b.Property("RoleId") + .IsRequired(); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("AccessFailedCount"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken(); + + b.Property("Email") + .HasMaxLength(256); + + b.Property("EmailConfirmed"); + + b.Property("LockoutEnabled"); + + b.Property("LockoutEnd"); + + b.Property("NormalizedEmail") + .HasMaxLength(256); + + b.Property("NormalizedUserName") + .HasMaxLength(256); + + b.Property("PasswordHash"); + + b.Property("PhoneNumber"); + + b.Property("PhoneNumberConfirmed"); + + b.Property("SecurityStamp"); + + b.Property("TwoFactorEnabled"); + + b.Property("UserName") + .HasMaxLength(256); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasName("UserNameIndex"); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("ClaimType"); + + b.Property("ClaimValue"); + + b.Property("UserId") + .IsRequired(); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider"); + + b.Property("ProviderKey"); + + b.Property("ProviderDisplayName"); + + b.Property("UserId") + .IsRequired(); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId"); + + b.Property("RoleId"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId"); + + b.Property("LoginProvider"); + + b.Property("Name"); + + b.Property("Value"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.AttributeData", b => + { + b.HasBaseType("IoTSharp.Hub.Data.DataStorage"); + + b.Property("DeviceId"); + + b.HasIndex("DeviceId"); + + b.HasDiscriminator().HasValue(1); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.AttributeLatest", b => + { + b.HasBaseType("IoTSharp.Hub.Data.DataStorage"); + + b.Property("DeviceId") + .HasColumnName("AttributeLatest_DeviceId"); + + b.HasIndex("DeviceId"); + + b.HasDiscriminator().HasValue(2); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryData", b => + { + b.HasBaseType("IoTSharp.Hub.Data.DataStorage"); + + b.Property("DeviceId") + .HasColumnName("TelemetryData_DeviceId"); + + b.HasIndex("DeviceId"); + + b.HasDiscriminator().HasValue(3); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryLatest", b => + { + b.HasBaseType("IoTSharp.Hub.Data.DataStorage"); + + b.Property("DeviceId") + .HasColumnName("TelemetryLatest_DeviceId"); + + b.HasIndex("DeviceId"); + + b.HasDiscriminator().HasValue(4); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.Customer", b => + { + b.HasOne("IoTSharp.Hub.Data.Tenant", "Tenant") + .WithMany("Customers") + .HasForeignKey("TenantId"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.Device", b => + { + b.HasOne("IoTSharp.Hub.Data.Customer", "Customer") + .WithMany("Devices") + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Hub.Data.Tenant", "Tenant") + .WithMany("Devices") + .HasForeignKey("TenantId"); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.DeviceIdentity", b => + { + b.HasOne("IoTSharp.Hub.Data.Device", "Device") + .WithMany() + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.Relationship", b => + { + b.HasOne("IoTSharp.Hub.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser") + .WithMany() + .HasForeignKey("IdentityUserId"); + + b.HasOne("IoTSharp.Hub.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole") + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole") + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.AttributeData", b => + { + b.HasOne("IoTSharp.Hub.Data.Device", "Device") + .WithMany("AttributeData") + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.AttributeLatest", b => + { + b.HasOne("IoTSharp.Hub.Data.Device", "Device") + .WithMany("AttributeLatest") + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryData", b => + { + b.HasOne("IoTSharp.Hub.Data.Device", "Device") + .WithMany("TelemetryData") + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryLatest", b => + { + b.HasOne("IoTSharp.Hub.Data.Device", "Device") + .WithMany("TelemetryLatest") + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IoTSharp.Hub/Data/Migrations/20190118115805_AddDeviceIdentity.cs b/IoTSharp.Hub/Data/Migrations/20190118115805_AddDeviceIdentity.cs new file mode 100644 index 0000000000000000000000000000000000000000..c8ba86b2e34beb9c5dbc31a03c6fb6decb667909 --- /dev/null +++ b/IoTSharp.Hub/Data/Migrations/20190118115805_AddDeviceIdentity.cs @@ -0,0 +1,43 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace IoTSharp.Hub.Migrations +{ + public partial class AddDeviceIdentity : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "DeviceIdentities", + columns: table => new + { + Id = table.Column(nullable: false), + IdentityType = table.Column(nullable: false), + IdentityId = table.Column(nullable: false), + IdentityValue = table.Column(nullable: true), + DeviceId = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DeviceIdentities", x => x.Id); + table.ForeignKey( + name: "FK_DeviceIdentities_Device_DeviceId", + column: x => x.DeviceId, + principalTable: "Device", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DeviceIdentities_DeviceId", + table: "DeviceIdentities", + column: "DeviceId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DeviceIdentities"); + } + } +} diff --git a/IoTSharp.Hub/Data/Migrations/ApplicationDbContextModelSnapshot.cs b/IoTSharp.Hub/Data/Migrations/ApplicationDbContextModelSnapshot.cs index c95aff8703670768a2390156be26a83d120a23fd..19c6ecd75fcb962bfbeee365e2fdc5fd7b2aa5da 100644 --- a/IoTSharp.Hub/Data/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/IoTSharp.Hub/Data/Migrations/ApplicationDbContextModelSnapshot.cs @@ -110,6 +110,27 @@ namespace IoTSharp.Hub.Migrations b.ToTable("Device"); }); + modelBuilder.Entity("IoTSharp.Hub.Data.DeviceIdentity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("DeviceId"); + + b.Property("IdentityId") + .IsRequired(); + + b.Property("IdentityType"); + + b.Property("IdentityValue"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.ToTable("DeviceIdentities"); + }); + modelBuilder.Entity("IoTSharp.Hub.Data.Relationship", b => { b.Property("Id") @@ -382,6 +403,14 @@ namespace IoTSharp.Hub.Migrations .HasForeignKey("TenantId"); }); + modelBuilder.Entity("IoTSharp.Hub.Data.DeviceIdentity", b => + { + b.HasOne("IoTSharp.Hub.Data.Device", "Device") + .WithMany() + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade); + }); + modelBuilder.Entity("IoTSharp.Hub.Data.Relationship", b => { b.HasOne("IoTSharp.Hub.Data.Customer", "Customer") diff --git a/IoTSharp.Hub/Dtos/ApiResult.cs b/IoTSharp.Hub/Dtos/ApiResult.cs new file mode 100644 index 0000000000000000000000000000000000000000..0124bb454270eb51c91942d09b045fc859ae220a --- /dev/null +++ b/IoTSharp.Hub/Dtos/ApiResult.cs @@ -0,0 +1,34 @@ +using IoTSharp.Hub.Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace IoTSharp.Hub.Dtos +{ + public class ApiResult + { + public ApiResult() + { + } + + public ApiResult(ApiCode _code, string _msg) + { + Code = (int)_code; + Msg = _msg; + } + + public int Code { get; set; } = (int)ApiCode.Success; + public string Msg { get; set; } = "OK"; + } + + public class ApiResult : ApiResult + { + public ApiResult(ApiCode _code, string _msg, T data) : base(_code, _msg) + { + Data = data; + } + + public T Data { set; get; } + } +} \ No newline at end of file