瀏覽代碼

Now we share via code not via Id.

master
radivoje.milutinovic 3 年之前
父節點
當前提交
22299245ce

+ 2
- 1
SecureSharing.Business/Dtos/MessageDto.cs 查看文件

{ {
public string Text { get; set; } public string Text { get; set; }
public bool IsValid { get; set; } = true; public bool IsValid { get; set; } = true;

public Guid Code { get; set; } = Guid.NewGuid();
public string FileName { get; set; }
public DateTime? ExpiryDate { get; set; } public DateTime? ExpiryDate { get; set; }
} }

+ 2
- 1
SecureSharing.Business/Interfaces/IMessageService.cs 查看文件

public interface IMessageService public interface IMessageService
{ {
public Task<IEnumerable<MessageDto>> GetAll(); public Task<IEnumerable<MessageDto>> GetAll();
public Task<int> Create(MessageDto messageDto, PeriodOfValidity chosenPeriod);
public Task<Guid> Create(MessageDto messageDto, PeriodOfValidity chosenPeriod);
public Task DeleteExpiredMessages(); public Task DeleteExpiredMessages();
public Task InvalidateMessage(int id); public Task InvalidateMessage(int id);
public Task Update(MessageDto messageDto); public Task Update(MessageDto messageDto);


public Task<MessageDto> GetById(int messageDto); public Task<MessageDto> GetById(int messageDto);
public Task<MessageDto> GetByCode(Guid code);


public Task<bool> Delete(int messageDto); public Task<bool> Delete(int messageDto);
} }

+ 9
- 2
SecureSharing.Business/Services/MessageService.cs 查看文件

_mapper = mapper; _mapper = mapper;
} }


public async Task<int> Create(MessageDto messageDto, PeriodOfValidity chosenPeriod)
public async Task<Guid> Create(MessageDto messageDto, PeriodOfValidity chosenPeriod)
{ {
var message = _mapper.Map<Message>(messageDto); var message = _mapper.Map<Message>(messageDto);
message.ExpiryDate = chosenPeriod switch message.ExpiryDate = chosenPeriod switch


await _dbContext.Messages.AddAsync(message); await _dbContext.Messages.AddAsync(message);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
return message.Id;
return Guid.Parse(message.Code);
} }


public async Task DeleteExpiredMessages() public async Task DeleteExpiredMessages()
return mappedResult; return mappedResult;
} }


public async Task<MessageDto> GetByCode(Guid code)
{
var result = await _dbContext.Messages.AsNoTracking().FirstOrDefaultAsync(x => x.Code == code.ToString());
var mappedResult = _mapper.Map<MessageDto>(result);
return mappedResult;
}
public async Task Update(MessageDto messageDto) public async Task Update(MessageDto messageDto)
{ {
var a = _dbContext.Messages.Update(_mapper.Map<Message>(messageDto)); var a = _dbContext.Messages.Update(_mapper.Map<Message>(messageDto));

+ 2
- 0
SecureSharing.Data/Data/Message.cs 查看文件

public sealed class Message : BaseEntity public sealed class Message : BaseEntity
{ {
public string Text { get; set; } public string Text { get; set; }
public string Code { get; set; }
public string FileName { get; set; }
public bool IsValid { get; set; } public bool IsValid { get; set; }
public DateTime? ExpiryDate { get; set; } public DateTime? ExpiryDate { get; set; }
} }

+ 310
- 0
SecureSharing.Data/Migrations/20220927104345_AddingFileNameAndCode.Designer.cs 查看文件

// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SecureSharing.Data.DbContexts;

#nullable disable

namespace SecureSharing.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20220927104345_AddingFileNameAndCode")]
partial class AddingFileNameAndCode
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");

b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");

b.Property<string>("Name")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");

b.Property<string>("NormalizedName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");

b.HasKey("Id");

b.HasIndex("NormalizedName")
.IsUnique()
.HasDatabaseName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL");

b.ToTable("AspNetRoles", (string)null);
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<string>("ClaimType")
.HasColumnType("nvarchar(max)");

b.Property<string>("ClaimValue")
.HasColumnType("nvarchar(max)");

b.Property<string>("RoleId")
.IsRequired()
.HasColumnType("nvarchar(450)");

b.HasKey("Id");

b.HasIndex("RoleId");

b.ToTable("AspNetRoleClaims", (string)null);
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");

b.Property<int>("AccessFailedCount")
.HasColumnType("int");

b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");

b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");

b.Property<bool>("EmailConfirmed")
.HasColumnType("bit");

b.Property<bool>("LockoutEnabled")
.HasColumnType("bit");

b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("datetimeoffset");

b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");

b.Property<string>("NormalizedUserName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");

b.Property<string>("PasswordHash")
.HasColumnType("nvarchar(max)");

b.Property<string>("PhoneNumber")
.HasColumnType("nvarchar(max)");

b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("bit");

b.Property<string>("SecurityStamp")
.HasColumnType("nvarchar(max)");

b.Property<bool>("TwoFactorEnabled")
.HasColumnType("bit");

b.Property<string>("UserName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");

b.HasKey("Id");

b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");

b.HasIndex("NormalizedUserName")
.IsUnique()
.HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL");

b.ToTable("AspNetUsers", (string)null);
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<string>("ClaimType")
.HasColumnType("nvarchar(max)");

b.Property<string>("ClaimValue")
.HasColumnType("nvarchar(max)");

b.Property<string>("UserId")
.IsRequired()
.HasColumnType("nvarchar(450)");

b.HasKey("Id");

b.HasIndex("UserId");

b.ToTable("AspNetUserClaims", (string)null);
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");

b.Property<string>("ProviderKey")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");

b.Property<string>("ProviderDisplayName")
.HasColumnType("nvarchar(max)");

b.Property<string>("UserId")
.IsRequired()
.HasColumnType("nvarchar(450)");

b.HasKey("LoginProvider", "ProviderKey");

b.HasIndex("UserId");

b.ToTable("AspNetUserLogins", (string)null);
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("nvarchar(450)");

b.Property<string>("RoleId")
.HasColumnType("nvarchar(450)");

b.HasKey("UserId", "RoleId");

b.HasIndex("RoleId");

b.ToTable("AspNetUserRoles", (string)null);
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("nvarchar(450)");

b.Property<string>("LoginProvider")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");

b.Property<string>("Name")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");

b.Property<string>("Value")
.HasColumnType("nvarchar(max)");

b.HasKey("UserId", "LoginProvider", "Name");

b.ToTable("AspNetUserTokens", (string)null);
});

modelBuilder.Entity("SecureSharing.Data.Data.Message", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<string>("Code")
.HasColumnType("nvarchar(max)");

b.Property<DateTime?>("ExpiryDate")
.HasColumnType("datetime2");

b.Property<string>("FileName")
.HasColumnType("nvarchar(max)");

b.Property<bool>("IsValid")
.HasColumnType("bit");

b.Property<string>("Text")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Messages");
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}

+ 35
- 0
SecureSharing.Data/Migrations/20220927104345_AddingFileNameAndCode.cs 查看文件

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace SecureSharing.Data.Migrations
{
public partial class AddingFileNameAndCode : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Code",
table: "Messages",
type: "nvarchar(max)",
nullable: true);

migrationBuilder.AddColumn<string>(
name: "FileName",
table: "Messages",
type: "nvarchar(max)",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Code",
table: "Messages");

migrationBuilder.DropColumn(
name: "FileName",
table: "Messages");
}
}
}

+ 47
- 35
SecureSharing.Data/Migrations/AppDbContextModelSnapshot.cs 查看文件

// <auto-generated /> // <auto-generated />
using System; using System;
using SecureSharing.Data.DbContexts;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SecureSharing.Data.DbContexts;

#nullable disable


namespace SecureSharing.Data.Migrations namespace SecureSharing.Data.Migrations
{ {
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.7")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasAnnotation("ProductVersion", "6.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128);


modelBuilder.Entity("SecureSharing.Data.Data.Message", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<DateTime?>("ExpiryDate")
.HasColumnType("datetime2");

b.Property<bool>("IsValid")
.HasColumnType("bit");

b.Property<string>("Text")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Messages");
});
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{ {
.HasDatabaseName("RoleNameIndex") .HasDatabaseName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL"); .HasFilter("[NormalizedName] IS NOT NULL");


b.ToTable("AspNetRoles");
b.ToTable("AspNetRoles", (string)null);
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);


b.Property<string>("ClaimType") b.Property<string>("ClaimType")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");


b.HasIndex("RoleId"); b.HasIndex("RoleId");


b.ToTable("AspNetRoleClaims");
b.ToTable("AspNetRoleClaims", (string)null);
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
.HasDatabaseName("UserNameIndex") .HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL"); .HasFilter("[NormalizedUserName] IS NOT NULL");


b.ToTable("AspNetUsers");
b.ToTable("AspNetUsers", (string)null);
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);


b.Property<string>("ClaimType") b.Property<string>("ClaimType")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");


b.HasIndex("UserId"); b.HasIndex("UserId");


b.ToTable("AspNetUserClaims");
b.ToTable("AspNetUserClaims", (string)null);
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>


b.HasIndex("UserId"); b.HasIndex("UserId");


b.ToTable("AspNetUserLogins");
b.ToTable("AspNetUserLogins", (string)null);
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>


b.HasIndex("RoleId"); b.HasIndex("RoleId");


b.ToTable("AspNetUserRoles");
b.ToTable("AspNetUserRoles", (string)null);
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>


b.HasKey("UserId", "LoginProvider", "Name"); b.HasKey("UserId", "LoginProvider", "Name");


b.ToTable("AspNetUserTokens");
b.ToTable("AspNetUserTokens", (string)null);
});

modelBuilder.Entity("SecureSharing.Data.Data.Message", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<string>("Code")
.HasColumnType("nvarchar(max)");

b.Property<DateTime?>("ExpiryDate")
.HasColumnType("datetime2");

b.Property<string>("FileName")
.HasColumnType("nvarchar(max)");

b.Property<bool>("IsValid")
.HasColumnType("bit");

b.Property<string>("Text")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Messages");
}); });


modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>

+ 4
- 4
SecureSharing/Controllers/HomeController.cs 查看文件

public async Task<IActionResult> CreateMessage(MessageModel model) public async Task<IActionResult> CreateMessage(MessageModel model)
{ {
var message = new MessageDto { Text = model.Text }; var message = new MessageDto { Text = model.Text };
var id = await _messageService.Create(message, model.ChosenPeriod);
return RedirectToAction("Link", "Home", new { id, share = true });
var code = await _messageService.Create(message, model.ChosenPeriod);
return RedirectToAction("Link", "Home", new { code = code, share = true });
} }


[HttpGet] [HttpGet]
public async Task<IActionResult> Link(int id, bool? share)
public async Task<IActionResult> Link(Guid code, bool? share)
{ {
var model = await _modelFactory.PrepareLinkVM(id, share);
var model = await _modelFactory.PrepareLinkVM(code, share);
return View(model); return View(model);
} }



+ 1
- 1
SecureSharing/Infrastructure/IModelFactory.cs 查看文件

public interface IModelFactory public interface IModelFactory
{ {
//public MessageModel PrepareMessageVM(MessageDto message); //public MessageModel PrepareMessageVM(MessageDto message);
public Task<LinkModel> PrepareLinkVM(int id, bool? share);
public Task<LinkModel> PrepareLinkVM(Guid code, bool? share);
} }

+ 8
- 5
SecureSharing/Infrastructure/ModelFactory.cs 查看文件

_messageService = messageService; _messageService = messageService;
} }


public async Task<LinkModel> PrepareLinkVM(int id, bool? share)
public async Task<LinkModel> PrepareLinkVM(Guid code, bool? share)
{ {
//share is true when the link is created //share is true when the link is created


LinkModel model = null; LinkModel model = null;
try try
{ {
var message = await _messageService.GetById(id);
var message = await _messageService.GetByCode(code);


model = new LinkModel { MessageModel = new MessageModel { Id = id, Text = message.Text }, Share = share };
model.IsValid = message.IsValid;
model = new LinkModel
{
MessageModel = new MessageModel { Code = code, Text = message.Text }, Share = share,
IsValid = message.IsValid
};


if (model.IsValid) if (model.IsValid)
{ {
else else
{ {
//ONE_TIME sharing: make the message invalid now so that it can't be accessed next time //ONE_TIME sharing: make the message invalid now so that it can't be accessed next time
if (share == null || share.Value == false) await _messageService.InvalidateMessage(message.Id);
if (share is null or false) await _messageService.InvalidateMessage(message.Id);
} }
} }
} }

+ 1
- 0
SecureSharing/Models/MessageModel.cs 查看文件

{ {
public int Id { get; set; } public int Id { get; set; }
public string Text { get; set; } public string Text { get; set; }
public Guid Code { get; set; }
public PeriodOfValidity ChosenPeriod { get; set; } public PeriodOfValidity ChosenPeriod { get; set; }


// public Dictionary<int, string> AvailablePeriods { get; set; } // public Dictionary<int, string> AvailablePeriods { get; set; }

+ 1
- 1
SecureSharing/Views/Home/Link.cshtml 查看文件

</div> </div>
<div id="div-link" class="label-text link-show"> <div id="div-link" class="label-text link-show">
@{ @{
var link = Url.Action("Link", "Home", new { id = Model.MessageModel.Id }, "https");
var link = Url.Action("Link", "Home", new { code = Model.MessageModel.Code }, "https");
} }
<a id="a-link" href="@link">@link</a> <a id="a-link" href="@link">@link</a>
</div> </div>

Loading…
取消
儲存