mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Implement user session management and JWT refresh tokens
Added user session models, interfaces, repository, and service for managing user sessions and refresh tokens. Refactored authentication flow to return user objects and open sessions with device info, supporting refresh token generation and validation. Updated JWT configuration to separate access and refresh options, and refactored related tests and API contracts. Improved media upload handling and error logging. Migrated dependency references and DI registrations accordingly.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Models.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Govor.Data.Configurations;
|
||||
|
||||
public class UserSessionConfiguration : IEntityTypeConfiguration<UserSession>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<UserSession> builder)
|
||||
{
|
||||
builder.HasKey(us => us.Id);
|
||||
|
||||
builder.Property(us => us.RefreshToken)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(us => us.DeviceInfo)
|
||||
.HasMaxLength(200);
|
||||
|
||||
builder.HasOne<User>()
|
||||
.WithMany()
|
||||
.HasForeignKey(us => us.UserId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Models.Messages;
|
||||
using Govor.Core.Models.Users;
|
||||
@@ -10,6 +9,7 @@ namespace Govor.Data;
|
||||
public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContext(options)
|
||||
{
|
||||
public virtual DbSet<User> Users { get; set; }
|
||||
public virtual DbSet<UserSession> UserSessions { get; set; }
|
||||
public virtual DbSet<Friendship> Friendships { get; set; }
|
||||
public virtual DbSet<PrivateChat> PrivateChats { get; set; }
|
||||
public virtual DbSet<Admin> Admins { get; set; }
|
||||
@@ -29,6 +29,7 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfiguration(new UserSessionConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new FriendshipConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new UserConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new InvitationConfiguration());
|
||||
|
||||
@@ -0,0 +1,639 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Govor.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Govor.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GovorDbContext))]
|
||||
[Migration("20250718130008_usersessions")]
|
||||
partial class usersessions
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.6")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.ChatGroup", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<Guid>("ImageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("IsChannel")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<bool>("IsPrivate")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("ChatGroups");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Friendship", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("AddresseeId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("RequesterId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("AddresseeId");
|
||||
|
||||
b.HasIndex("RequesterId");
|
||||
|
||||
b.ToTable("Friendships");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.GroupAdmins", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("GroupId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("GroupId");
|
||||
|
||||
b.ToTable("GroupAdmins");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.GroupInvitation", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<DateTime>("EndDate")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<Guid>("GroupId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("InvitationCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<int>("MaxParticipants")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<Guid>("UserMakerId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("GroupId");
|
||||
|
||||
b.HasIndex("UserMakerId");
|
||||
|
||||
b.ToTable("GroupInvitations");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.GroupMembership", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("GroupId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid?>("InvitationId")
|
||||
.IsRequired()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("IsBanned")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("GroupId");
|
||||
|
||||
b.HasIndex("InvitationId");
|
||||
|
||||
b.ToTable("GroupMemberships");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Invitation", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Code")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<DateTime>("DateCreated")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<DateTime>("EndDate")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<bool>("IsAdmin")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<int>("MaxParticipants")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Invitations");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.MediaFile", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("DateCreated")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("MediaType")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("MineType")
|
||||
.IsRequired()
|
||||
.HasMaxLength(128)
|
||||
.HasColumnType("varchar(128)");
|
||||
|
||||
b.Property<Guid>("UploaderId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Url")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("MediaFiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.MediaAttachments", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("MediaFileId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("MessageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("MediaFileId");
|
||||
|
||||
b.HasIndex("MessageId");
|
||||
|
||||
b.ToTable("MediaAttachments");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.Message", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid?>("ChatGroupId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime?>("EditedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("EncryptedContent")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<bool>("IsEdited")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<Guid?>("PrivateChatId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("RecipientId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<int>("RecipientType")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<Guid?>("ReplyToMessageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("SenderId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("SentAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ChatGroupId");
|
||||
|
||||
b.HasIndex("PrivateChatId");
|
||||
|
||||
b.ToTable("Messages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.MessageReaction", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("MessageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("ReactedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("ReactionCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.HasIndex("MessageId", "UserId")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("MessageReactions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.MessageView", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("MessageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("ViewedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("MessageId", "UserId")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("MessageViews");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.PrivateChat", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("UserAId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("UserBId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("PrivateChats");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.UserSession", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("DeviceInfo")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<DateTime>("ExpiresAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<bool>("IsRevoked")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("UserSessions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.Admin", b =>
|
||||
{
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("UserId");
|
||||
|
||||
b.ToTable("Admins");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateOnly>("CreatedOn")
|
||||
.HasColumnType("date");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<Guid>("IconId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("InviteId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<DateTime>("WasOnline")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("InviteId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Friendship", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Users.User", "Addressee")
|
||||
.WithMany("ReceivedFriendRequests")
|
||||
.HasForeignKey("AddresseeId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Govor.Core.Models.Users.User", "Requester")
|
||||
.WithMany("SentFriendRequests")
|
||||
.HasForeignKey("RequesterId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Addressee");
|
||||
|
||||
b.Navigation("Requester");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.GroupAdmins", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.ChatGroup", null)
|
||||
.WithMany("Admins")
|
||||
.HasForeignKey("GroupId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.GroupInvitation", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.ChatGroup", null)
|
||||
.WithMany("InviteCodes")
|
||||
.HasForeignKey("GroupId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Govor.Core.Models.Users.User", "UserMaker")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserMakerId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("UserMaker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.GroupMembership", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.ChatGroup", null)
|
||||
.WithMany("Members")
|
||||
.HasForeignKey("GroupId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Govor.Core.Models.GroupInvitation", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("InvitationId")
|
||||
.OnDelete(DeleteBehavior.SetNull)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.MediaAttachments", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.MediaFile", "MediaFile")
|
||||
.WithMany()
|
||||
.HasForeignKey("MediaFileId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Govor.Core.Models.Messages.Message", "Message")
|
||||
.WithMany("MediaAttachments")
|
||||
.HasForeignKey("MessageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("MediaFile");
|
||||
|
||||
b.Navigation("Message");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.Message", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.ChatGroup", null)
|
||||
.WithMany("Messages")
|
||||
.HasForeignKey("ChatGroupId");
|
||||
|
||||
b.HasOne("Govor.Core.Models.PrivateChat", null)
|
||||
.WithMany("Messages")
|
||||
.HasForeignKey("PrivateChatId");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.MessageReaction", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Messages.Message", "Message")
|
||||
.WithMany("Reactions")
|
||||
.HasForeignKey("MessageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Govor.Core.Models.Users.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Message");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.MessageView", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Messages.Message", null)
|
||||
.WithMany("MessageViews")
|
||||
.HasForeignKey("MessageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.UserSession", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Users.User", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.Admin", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Users.User", "User")
|
||||
.WithOne()
|
||||
.HasForeignKey("Govor.Core.Models.Users.Admin", "UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.User", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Invitation", "Invite")
|
||||
.WithMany("Users")
|
||||
.HasForeignKey("InviteId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Invite");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.ChatGroup", b =>
|
||||
{
|
||||
b.Navigation("Admins");
|
||||
|
||||
b.Navigation("InviteCodes");
|
||||
|
||||
b.Navigation("Members");
|
||||
|
||||
b.Navigation("Messages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Invitation", b =>
|
||||
{
|
||||
b.Navigation("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Messages.Message", b =>
|
||||
{
|
||||
b.Navigation("MediaAttachments");
|
||||
|
||||
b.Navigation("MessageViews");
|
||||
|
||||
b.Navigation("Reactions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.PrivateChat", b =>
|
||||
{
|
||||
b.Navigation("Messages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.User", b =>
|
||||
{
|
||||
b.Navigation("ReceivedFriendRequests");
|
||||
|
||||
b.Navigation("SentFriendRequests");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Govor.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class usersessions : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "UserSessions",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
||||
UserId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
||||
RefreshToken = table.Column<string>(type: "longtext", nullable: false)
|
||||
.Annotation("MySql:CharSet", "utf8mb4"),
|
||||
DeviceInfo = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false)
|
||||
.Annotation("MySql:CharSet", "utf8mb4"),
|
||||
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
|
||||
ExpiresAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
|
||||
IsRevoked = table.Column<bool>(type: "tinyint(1)", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_UserSessions", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_UserSessions_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
})
|
||||
.Annotation("MySql:CharSet", "utf8mb4");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserSessions_UserId",
|
||||
table: "UserSessions",
|
||||
column: "UserId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "UserSessions");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -364,6 +364,40 @@ namespace Govor.Data.Migrations
|
||||
b.ToTable("PrivateChats");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.UserSession", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<string>("DeviceInfo")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<DateTime>("ExpiresAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<bool>("IsRevoked")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("UserSessions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.Admin", b =>
|
||||
{
|
||||
b.Property<Guid>("UserId")
|
||||
@@ -529,6 +563,15 @@ namespace Govor.Data.Migrations
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.UserSession", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Users.User", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Govor.Core.Models.Users.Admin", b =>
|
||||
{
|
||||
b.HasOne("Govor.Core.Models.Users.User", "User")
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.UserSessionsRepository;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Govor.Data.Repositories;
|
||||
|
||||
public class UserSessionsRepository : IUserSessionsRepository
|
||||
{
|
||||
private GovorDbContext _context;
|
||||
|
||||
public UserSessionsRepository(GovorDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<List<UserSession>> GetAllAsync()
|
||||
{
|
||||
return await _context.UserSessions
|
||||
.AsNoTracking()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Database is empty"));
|
||||
}
|
||||
|
||||
public async Task<UserSession> GetByIdAsync(Guid sessionId)
|
||||
{
|
||||
return await _context.UserSessions
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(session => session.Id == sessionId)
|
||||
?? throw new NotFoundByKeyException<Guid>(sessionId, "Session with given id does not exist");
|
||||
}
|
||||
|
||||
public async Task<List<UserSession>> GetByUserIdAsync(Guid userId)
|
||||
{
|
||||
return await _context.UserSessions
|
||||
.AsNoTracking()
|
||||
.Where(session => session.UserId == userId)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(userId, "Sessions with given user id does not exist"));
|
||||
}
|
||||
|
||||
public async Task<List<UserSession>> GetByCreatedAtAsync(DateTime createdAt)
|
||||
{
|
||||
return await _context.UserSessions
|
||||
.AsNoTracking()
|
||||
.Where(session => session.CreatedAt == createdAt)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateTime>(createdAt, "Sessions with given created at does not exist"));
|
||||
}
|
||||
|
||||
public async Task<List<UserSession>> GetByExpiresAtAsync(DateTime expiresAt)
|
||||
{
|
||||
return await _context.UserSessions
|
||||
.AsNoTracking()
|
||||
.Where(session => session.ExpiresAt == expiresAt)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateTime>(expiresAt, "Sessions with given expires at does not exist"));
|
||||
}
|
||||
|
||||
public async Task<List<UserSession>> GetByRevokedAsync(bool isRevoked)
|
||||
{
|
||||
return await _context.UserSessions
|
||||
.AsNoTracking()
|
||||
.Where(session => session.IsRevoked == isRevoked)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<bool>(isRevoked, "Sessions is revoked does not exist"));
|
||||
}
|
||||
|
||||
public async Task AddAsync(UserSession userSession)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.UserSessions.Add(userSession);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new AdditionException("Failed to add user session", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(UserSession userSession)
|
||||
{
|
||||
try
|
||||
{
|
||||
//_validator.Validate(user);
|
||||
|
||||
var rowsAffected = await _context.UserSessions
|
||||
.Where(u => u.Id == userSession.Id)
|
||||
.ExecuteUpdateAsync(u => u
|
||||
.SetProperty(a => a.UserId, userSession.UserId)
|
||||
.SetProperty(u => u.RefreshToken, userSession.RefreshToken)
|
||||
.SetProperty(u => u.DeviceInfo, userSession.DeviceInfo)
|
||||
.SetProperty(u => u.CreatedAt, userSession.CreatedAt)
|
||||
.SetProperty(u => u.ExpiresAt, userSession.ExpiresAt)
|
||||
.SetProperty(u => u.IsRevoked, userSession.IsRevoked)
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new UpdateException($"Not found user session by given id {userSession.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UpdateException($"Error when updating the user session {userSession.Id}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(Guid sessionId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await GetByIdAsync(sessionId);
|
||||
|
||||
_context.UserSessions.Remove(result);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new RemoveException($"Not found session by given id {sessionId}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new RemoveException("Error when removing the session", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveByUserIdAsync(Guid userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await GetByUserIdAsync(userId);
|
||||
|
||||
_context.UserSessions.RemoveRange(result);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new RemoveException($"Not found user sessions by given user id {userId}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new RemoveException("Error when removing the user sessions", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Exist(Guid sessionId)
|
||||
{
|
||||
return _context.UserSessions.Any(session => session.Id == sessionId);
|
||||
}
|
||||
|
||||
public bool Exist(string refresh)
|
||||
{
|
||||
return _context.UserSessions.Any(session => session.RefreshToken == refresh);
|
||||
}
|
||||
|
||||
public bool Exist(UserSession userSession)
|
||||
{
|
||||
return _context.UserSessions.Any(session => session.Id == userSession.Id &&
|
||||
session.RefreshToken == userSession.RefreshToken &&
|
||||
session.IsRevoked == userSession.IsRevoked &&
|
||||
session.CreatedAt == userSession.CreatedAt &&
|
||||
session.ExpiresAt == userSession.ExpiresAt &&
|
||||
session.DeviceInfo == userSession.DeviceInfo &&
|
||||
session.UserId == userSession.UserId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user