Merge pull request #25 from Moonlight-Panel/Subscriptions

Update from upstream because of database models
This commit is contained in:
Marcel Baumgartner 2023-04-03 19:31:28 +02:00 committed by GitHub
commit 48a0d3452e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 2828 additions and 448 deletions

View file

@ -35,14 +35,13 @@ public class DataContext : DbContext
public DbSet<SharedDomain> SharedDomains { get; set; }
public DbSet<Domain> Domains { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
public DbSet<SubscriptionLimit> SubscriptionLimits { get; set; }
public DbSet<Revoke> Revokes { get; set; }
public DbSet<NotificationClient> NotificationClients { get; set; }
public DbSet<NotificationAction> NotificationActions { get; set; }
public DbSet<AaPanel> AaPanels { get; set; }
public DbSet<Website> Websites { get; set; }
public DbSet<DdosAttack> DdosAttacks { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

View file

@ -5,7 +5,5 @@ public class Subscription
public int Id { get; set; }
public string Name { get; set; } = "";
public string Description { get; set; } = "";
public string SellPassId { get; set; } = "";
public int Duration { get; set; }
public List<SubscriptionLimit> Limits { get; set; } = new();
public string LimitsJson { get; set; } = "";
}

View file

@ -1,11 +0,0 @@
namespace Moonlight.App.Database.Entities;
public class SubscriptionLimit
{
public int Id { get; set; }
public Image Image { get; set; } = null!;
public int Amount { get; set; }
public int Cpu { get; set; }
public int Memory { get; set; }
public int Disk { get; set; }
}

View file

@ -8,44 +8,21 @@ public class User
public int Id { get; set; }
// Personal data
[Required]
[MinLength(3, ErrorMessage = "Invalid first name")]
[MaxLength(64, ErrorMessage = "Max lenght reached")]
public string FirstName { get; set; } = "";
[Required]
[MinLength(3, ErrorMessage = "Invalid last name")]
[MaxLength(64, ErrorMessage = "Max lenght reached")]
public string LastName { get; set; } = "";
[Required(ErrorMessage = "You need to enter an email address")]
[EmailAddress(ErrorMessage = "You need to enter a valid email address")]
public string Email { get; set; } = "";
[Required(ErrorMessage = "You need to enter a password")]
[MinLength(8, ErrorMessage = "You need to enter a password with minimum 8 characters in lenght")]
public string Password { get; set; } = "";
[Required]
[RegularExpression(@"^(?:[A-Z] \d|[^\W\d_]{2,}\.?)(?:[- &#39;][^\W\d_]+\.?)*\s+[1-9]\d{0,3} ?[a-zA-Z]?(?: ?[/-] ?[1-9]\d{0,3} ?[a-zA-Z]?)?$",
ErrorMessage = "Street and house number required")]
[MaxLength(128, ErrorMessage = "Max lenght reached")]
public string Address { get; set; } = "";
[Required]
[MinLength(3, ErrorMessage = "Invalid city")]
[MaxLength(128, ErrorMessage = "Max lenght reached")]
public string City { get; set; } = "";
[Required]
[MinLength(3, ErrorMessage = "Invalid state")]
[MaxLength(64, ErrorMessage = "Max lenght reached")]
public string State { get; set; } = "";
[Required]
[MinLength(3, ErrorMessage = "Invalid country")]
[MaxLength(64, ErrorMessage = "Max lenght reached")]
public string Country { get; set; } = "";
// States
@ -65,9 +42,10 @@ public class User
// Date stuff
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
// Subscriptions
public Subscription? Subscription { get; set; } = null;
public DateTime? SubscriptionSince { get; set; }
public Subscription? CurrentSubscription { get; set; } = null;
public DateTime SubscriptionSince { get; set; } = DateTime.Now;
public int SubscriptionDuration { get; set; }
}

View file

@ -0,0 +1,962 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Moonlight.App.Database;
#nullable disable
namespace Moonlight.App.Database.Migrations
{
[DbContext(typeof(DataContext))]
[Migration("20230403130734_RemovedOldSubscriptionData")]
partial class RemovedOldSubscriptionData
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Moonlight.App.Database.Entities.AaPanel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("BaseDomain")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Key")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Url")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("AaPanels");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Database", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("AaPanelId")
.HasColumnType("int");
b.Property<int>("InternalAaPanelId")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("OwnerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("AaPanelId");
b.HasIndex("OwnerId");
b.ToTable("Databases");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.DdosAttack", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<long>("Data")
.HasColumnType("bigint");
b.Property<string>("Ip")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("NodeId")
.HasColumnType("int");
b.Property<bool>("Ongoing")
.HasColumnType("tinyint(1)");
b.HasKey("Id");
b.HasIndex("NodeId");
b.ToTable("DdosAttacks");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.DockerImage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<bool>("Default")
.HasColumnType("tinyint(1)");
b.Property<int?>("ImageId")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("ImageId");
b.ToTable("DockerImages");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Domain", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("OwnerId")
.HasColumnType("int");
b.Property<int>("SharedDomainId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("OwnerId");
b.HasIndex("SharedDomainId");
b.ToTable("Domains");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Image", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("Allocations")
.HasColumnType("int");
b.Property<string>("ConfigFiles")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("InstallDockerImage")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("InstallEntrypoint")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("InstallScript")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Startup")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("StartupDetection")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("StopCommand")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("TagsJson")
.IsRequired()
.HasColumnType("longtext");
b.Property<Guid>("Uuid")
.HasColumnType("char(36)");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ImageTag", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("ImageTags");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ImageVariable", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("DefaultValue")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("ImageId")
.HasColumnType("int");
b.Property<string>("Key")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("ImageId");
b.ToTable("ImageVariables");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.LoadingMessage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Message")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("LoadingMessages");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.LogsEntries.AuditLogEntry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("Ip")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("JsonData")
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("System")
.HasColumnType("tinyint(1)");
b.Property<int>("Type")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("AuditLog");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.LogsEntries.ErrorLogEntry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Class")
.IsRequired()
.HasColumnType("longtext");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("Ip")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("JsonData")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Stacktrace")
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("System")
.HasColumnType("tinyint(1)");
b.HasKey("Id");
b.ToTable("ErrorLog");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.LogsEntries.SecurityLogEntry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("Ip")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("JsonData")
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("System")
.HasColumnType("tinyint(1)");
b.Property<int>("Type")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("SecurityLog");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Node", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Fqdn")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("HttpPort")
.HasColumnType("int");
b.Property<int>("MoonlightDaemonPort")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("SftpPort")
.HasColumnType("int");
b.Property<bool>("Ssl")
.HasColumnType("tinyint(1)");
b.Property<string>("Token")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("TokenId")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("Nodes");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.NodeAllocation", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int?>("NodeId")
.HasColumnType("int");
b.Property<int>("Port")
.HasColumnType("int");
b.Property<int?>("ServerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("NodeId");
b.HasIndex("ServerId");
b.ToTable("NodeAllocations");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Notification.NotificationAction", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Action")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("NotificationClientId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("NotificationClientId");
b.ToTable("NotificationActions");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Notification.NotificationClient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("NotificationClients");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Revoke", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Identifier")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("Revokes");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Server", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("Cpu")
.HasColumnType("int");
b.Property<long>("Disk")
.HasColumnType("bigint");
b.Property<int>("DockerImageIndex")
.HasColumnType("int");
b.Property<int>("ImageId")
.HasColumnType("int");
b.Property<bool>("Installing")
.HasColumnType("tinyint(1)");
b.Property<bool>("IsCleanupException")
.HasColumnType("tinyint(1)");
b.Property<int>("MainAllocationId")
.HasColumnType("int");
b.Property<long>("Memory")
.HasColumnType("bigint");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("NodeId")
.HasColumnType("int");
b.Property<string>("OverrideStartup")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("OwnerId")
.HasColumnType("int");
b.Property<bool>("Suspended")
.HasColumnType("tinyint(1)");
b.Property<Guid>("Uuid")
.HasColumnType("char(36)");
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("MainAllocationId");
b.HasIndex("NodeId");
b.HasIndex("OwnerId");
b.ToTable("Servers");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ServerBackup", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<long>("Bytes")
.HasColumnType("bigint");
b.Property<bool>("Created")
.HasColumnType("tinyint(1)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("ServerId")
.HasColumnType("int");
b.Property<Guid>("Uuid")
.HasColumnType("char(36)");
b.HasKey("Id");
b.HasIndex("ServerId");
b.ToTable("ServerBackups");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ServerVariable", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Key")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("ServerId")
.HasColumnType("int");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("ServerId");
b.ToTable("ServerVariables");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SharedDomain", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("CloudflareId")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("SharedDomains");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SupportMessage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Answer")
.IsRequired()
.HasColumnType("longtext");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<bool>("IsQuestion")
.HasColumnType("tinyint(1)");
b.Property<bool>("IsSupport")
.HasColumnType("tinyint(1)");
b.Property<bool>("IsSystem")
.HasColumnType("tinyint(1)");
b.Property<string>("Message")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("RecipientId")
.HasColumnType("int");
b.Property<int?>("SenderId")
.HasColumnType("int");
b.Property<int>("Type")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("RecipientId");
b.HasIndex("SenderId");
b.ToTable("SupportMessages");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Address")
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("Admin")
.HasColumnType("tinyint(1)");
b.Property<string>("City")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Country")
.IsRequired()
.HasColumnType("longtext");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<long>("DiscordId")
.HasColumnType("bigint");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("State")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<bool>("SupportPending")
.HasColumnType("tinyint(1)");
b.Property<DateTime>("TokenValidTime")
.HasColumnType("datetime(6)");
b.Property<bool>("TotpEnabled")
.HasColumnType("tinyint(1)");
b.Property<string>("TotpSecret")
.IsRequired()
.HasColumnType("longtext");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("datetime(6)");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Website", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("AaPanelId")
.HasColumnType("int");
b.Property<string>("DomainName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("FtpPassword")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("FtpUsername")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("InternalAaPanelId")
.HasColumnType("int");
b.Property<int>("OwnerId")
.HasColumnType("int");
b.Property<string>("PhpVersion")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("AaPanelId");
b.HasIndex("OwnerId");
b.ToTable("Websites");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Database", b =>
{
b.HasOne("Moonlight.App.Database.Entities.AaPanel", "AaPanel")
.WithMany()
.HasForeignKey("AaPanelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.User", "Owner")
.WithMany()
.HasForeignKey("OwnerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("AaPanel");
b.Navigation("Owner");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.DdosAttack", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Node", "Node")
.WithMany()
.HasForeignKey("NodeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Node");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.DockerImage", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Image", null)
.WithMany("DockerImages")
.HasForeignKey("ImageId");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Domain", b =>
{
b.HasOne("Moonlight.App.Database.Entities.User", "Owner")
.WithMany()
.HasForeignKey("OwnerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.SharedDomain", "SharedDomain")
.WithMany()
.HasForeignKey("SharedDomainId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Owner");
b.Navigation("SharedDomain");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ImageVariable", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Image", null)
.WithMany("Variables")
.HasForeignKey("ImageId");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.NodeAllocation", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Node", null)
.WithMany("Allocations")
.HasForeignKey("NodeId");
b.HasOne("Moonlight.App.Database.Entities.Server", null)
.WithMany("Allocations")
.HasForeignKey("ServerId");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Notification.NotificationAction", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Notification.NotificationClient", "NotificationClient")
.WithMany()
.HasForeignKey("NotificationClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("NotificationClient");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Notification.NotificationClient", b =>
{
b.HasOne("Moonlight.App.Database.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Server", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Image", "Image")
.WithMany()
.HasForeignKey("ImageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.NodeAllocation", "MainAllocation")
.WithMany()
.HasForeignKey("MainAllocationId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.Node", "Node")
.WithMany()
.HasForeignKey("NodeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.User", "Owner")
.WithMany()
.HasForeignKey("OwnerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Image");
b.Navigation("MainAllocation");
b.Navigation("Node");
b.Navigation("Owner");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ServerBackup", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Server", null)
.WithMany("Backups")
.HasForeignKey("ServerId");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.ServerVariable", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Server", null)
.WithMany("Variables")
.HasForeignKey("ServerId");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SupportMessage", b =>
{
b.HasOne("Moonlight.App.Database.Entities.User", "Recipient")
.WithMany()
.HasForeignKey("RecipientId");
b.HasOne("Moonlight.App.Database.Entities.User", "Sender")
.WithMany()
.HasForeignKey("SenderId");
b.Navigation("Recipient");
b.Navigation("Sender");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Website", b =>
{
b.HasOne("Moonlight.App.Database.Entities.AaPanel", "AaPanel")
.WithMany()
.HasForeignKey("AaPanelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.User", "Owner")
.WithMany()
.HasForeignKey("OwnerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("AaPanel");
b.Navigation("Owner");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Image", b =>
{
b.Navigation("DockerImages");
b.Navigation("Variables");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Node", b =>
{
b.Navigation("Allocations");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Server", b =>
{
b.Navigation("Allocations");
b.Navigation("Backups");
b.Navigation("Variables");
});
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,269 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Moonlight.App.Database.Migrations
{
/// <inheritdoc />
public partial class RemovedOldSubscriptionData : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Users_Subscriptions_SubscriptionId",
table: "Users");
migrationBuilder.DropTable(
name: "SubscriptionLimits");
migrationBuilder.DropTable(
name: "Subscriptions");
migrationBuilder.DropIndex(
name: "IX_Users_SubscriptionId",
table: "Users");
migrationBuilder.DropColumn(
name: "SubscriptionDuration",
table: "Users");
migrationBuilder.DropColumn(
name: "SubscriptionId",
table: "Users");
migrationBuilder.DropColumn(
name: "SubscriptionSince",
table: "Users");
migrationBuilder.AlterColumn<string>(
name: "State",
table: "Users",
type: "longtext",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar(64)",
oldMaxLength: 64)
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "LastName",
table: "Users",
type: "longtext",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar(64)",
oldMaxLength: 64)
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "FirstName",
table: "Users",
type: "longtext",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar(64)",
oldMaxLength: 64)
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "Country",
table: "Users",
type: "longtext",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar(64)",
oldMaxLength: 64)
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "City",
table: "Users",
type: "longtext",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar(128)",
oldMaxLength: 128)
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "Address",
table: "Users",
type: "longtext",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar(128)",
oldMaxLength: 128)
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "State",
table: "Users",
type: "varchar(64)",
maxLength: 64,
nullable: false,
oldClrType: typeof(string),
oldType: "longtext")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "LastName",
table: "Users",
type: "varchar(64)",
maxLength: 64,
nullable: false,
oldClrType: typeof(string),
oldType: "longtext")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "FirstName",
table: "Users",
type: "varchar(64)",
maxLength: 64,
nullable: false,
oldClrType: typeof(string),
oldType: "longtext")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "Country",
table: "Users",
type: "varchar(64)",
maxLength: 64,
nullable: false,
oldClrType: typeof(string),
oldType: "longtext")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "City",
table: "Users",
type: "varchar(128)",
maxLength: 128,
nullable: false,
oldClrType: typeof(string),
oldType: "longtext")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AlterColumn<string>(
name: "Address",
table: "Users",
type: "varchar(128)",
maxLength: 128,
nullable: false,
oldClrType: typeof(string),
oldType: "longtext")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<int>(
name: "SubscriptionDuration",
table: "Users",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "SubscriptionId",
table: "Users",
type: "int",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "SubscriptionSince",
table: "Users",
type: "datetime(6)",
nullable: true);
migrationBuilder.CreateTable(
name: "Subscriptions",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Description = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Duration = table.Column<int>(type: "int", nullable: false),
Name = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
SellPassId = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_Subscriptions", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "SubscriptionLimits",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
ImageId = table.Column<int>(type: "int", nullable: false),
Amount = table.Column<int>(type: "int", nullable: false),
Cpu = table.Column<int>(type: "int", nullable: false),
Disk = table.Column<int>(type: "int", nullable: false),
Memory = table.Column<int>(type: "int", nullable: false),
SubscriptionId = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_SubscriptionLimits", x => x.Id);
table.ForeignKey(
name: "FK_SubscriptionLimits_Images_ImageId",
column: x => x.ImageId,
principalTable: "Images",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_SubscriptionLimits_Subscriptions_SubscriptionId",
column: x => x.SubscriptionId,
principalTable: "Subscriptions",
principalColumn: "Id");
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_Users_SubscriptionId",
table: "Users",
column: "SubscriptionId");
migrationBuilder.CreateIndex(
name: "IX_SubscriptionLimits_ImageId",
table: "SubscriptionLimits",
column: "ImageId");
migrationBuilder.CreateIndex(
name: "IX_SubscriptionLimits_SubscriptionId",
table: "SubscriptionLimits",
column: "SubscriptionId");
migrationBuilder.AddForeignKey(
name: "FK_Users_Subscriptions_SubscriptionId",
table: "Users",
column: "SubscriptionId",
principalTable: "Subscriptions",
principalColumn: "Id");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,94 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Moonlight.App.Database.Migrations
{
/// <inheritdoc />
public partial class AddedNewSubscriptionData : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "CurrentSubscriptionId",
table: "Users",
type: "int",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "SubscriptionDuration",
table: "Users",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<DateTime>(
name: "SubscriptionSince",
table: "Users",
type: "datetime(6)",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
migrationBuilder.CreateTable(
name: "Subscriptions",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Description = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
LimitsJson = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_Subscriptions", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_Users_CurrentSubscriptionId",
table: "Users",
column: "CurrentSubscriptionId");
migrationBuilder.AddForeignKey(
name: "FK_Users_Subscriptions_CurrentSubscriptionId",
table: "Users",
column: "CurrentSubscriptionId",
principalTable: "Subscriptions",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Users_Subscriptions_CurrentSubscriptionId",
table: "Users");
migrationBuilder.DropTable(
name: "Subscriptions");
migrationBuilder.DropIndex(
name: "IX_Users_CurrentSubscriptionId",
table: "Users");
migrationBuilder.DropColumn(
name: "CurrentSubscriptionId",
table: "Users");
migrationBuilder.DropColumn(
name: "SubscriptionDuration",
table: "Users");
migrationBuilder.DropColumn(
name: "SubscriptionSince",
table: "Users");
}
}
}

View file

@ -609,14 +609,11 @@ namespace Moonlight.App.Database.Migrations
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("Duration")
.HasColumnType("int");
b.Property<string>("Name")
b.Property<string>("LimitsJson")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("SellPassId")
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
@ -625,39 +622,6 @@ namespace Moonlight.App.Database.Migrations
b.ToTable("Subscriptions");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SubscriptionLimit", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("Amount")
.HasColumnType("int");
b.Property<int>("Cpu")
.HasColumnType("int");
b.Property<int>("Disk")
.HasColumnType("int");
b.Property<int>("ImageId")
.HasColumnType("int");
b.Property<int>("Memory")
.HasColumnType("int");
b.Property<int?>("SubscriptionId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("SubscriptionId");
b.ToTable("SubscriptionLimits");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SupportMessage", b =>
{
b.Property<int>("Id")
@ -710,25 +674,25 @@ namespace Moonlight.App.Database.Migrations
b.Property<string>("Address")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("varchar(128)");
.HasColumnType("longtext");
b.Property<bool>("Admin")
.HasColumnType("tinyint(1)");
b.Property<string>("City")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("varchar(128)");
.HasColumnType("longtext");
b.Property<string>("Country")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
.HasColumnType("longtext");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<int?>("CurrentSubscriptionId")
.HasColumnType("int");
b.Property<long>("DiscordId")
.HasColumnType("bigint");
@ -738,13 +702,11 @@ namespace Moonlight.App.Database.Migrations
b.Property<string>("FirstName")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
.HasColumnType("longtext");
b.Property<string>("LastName")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
.HasColumnType("longtext");
b.Property<string>("Password")
.IsRequired()
@ -752,8 +714,7 @@ namespace Moonlight.App.Database.Migrations
b.Property<string>("State")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
.HasColumnType("longtext");
b.Property<int>("Status")
.HasColumnType("int");
@ -761,10 +722,7 @@ namespace Moonlight.App.Database.Migrations
b.Property<int>("SubscriptionDuration")
.HasColumnType("int");
b.Property<int?>("SubscriptionId")
.HasColumnType("int");
b.Property<DateTime?>("SubscriptionSince")
b.Property<DateTime>("SubscriptionSince")
.HasColumnType("datetime(6)");
b.Property<bool>("SupportPending")
@ -785,7 +743,7 @@ namespace Moonlight.App.Database.Migrations
b.HasKey("Id");
b.HasIndex("SubscriptionId");
b.HasIndex("CurrentSubscriptionId");
b.ToTable("Users");
});
@ -975,21 +933,6 @@ namespace Moonlight.App.Database.Migrations
.HasForeignKey("ServerId");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SubscriptionLimit", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Image", "Image")
.WithMany()
.HasForeignKey("ImageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Moonlight.App.Database.Entities.Subscription", null)
.WithMany("Limits")
.HasForeignKey("SubscriptionId");
b.Navigation("Image");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.SupportMessage", b =>
{
b.HasOne("Moonlight.App.Database.Entities.User", "Recipient")
@ -1007,11 +950,11 @@ namespace Moonlight.App.Database.Migrations
modelBuilder.Entity("Moonlight.App.Database.Entities.User", b =>
{
b.HasOne("Moonlight.App.Database.Entities.Subscription", "Subscription")
b.HasOne("Moonlight.App.Database.Entities.Subscription", "CurrentSubscription")
.WithMany()
.HasForeignKey("SubscriptionId");
.HasForeignKey("CurrentSubscriptionId");
b.Navigation("Subscription");
b.Navigation("CurrentSubscription");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Website", b =>
@ -1053,11 +996,6 @@ namespace Moonlight.App.Database.Migrations
b.Navigation("Variables");
});
modelBuilder.Entity("Moonlight.App.Database.Entities.Subscription", b =>
{
b.Navigation("Limits");
});
#pragma warning restore 612, 618
}
}

View file

@ -1,50 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Moonlight.App.Services;
namespace Moonlight.App.Http.Controllers.Api.Moonlight;
[ApiController]
[Route("api/moonlight/payments")]
public class PaymentsController : Controller
{
private readonly ConfigService ConfigService;
private readonly SubscriptionService SubscriptionService;
public PaymentsController(ConfigService configService, SubscriptionService subscriptionService)
{
ConfigService = configService;
SubscriptionService = subscriptionService;
}
[HttpGet("generate")]
public async Task<ActionResult> GenerateGet([FromQuery] string key, [FromQuery] int subscriptionId)
{
var validKey = ConfigService
.GetSection("Moonlight")
.GetSection("Payments")
.GetValue<string>("Key");
if (key != validKey)
return StatusCode(403);
var token = await SubscriptionService.ProcessGenerate(subscriptionId);
return Ok(token);
}
[HttpPost("generate")]
public async Task<ActionResult> GeneratePost([FromQuery] string key, [FromQuery] int subscriptionId)
{
var validKey = ConfigService
.GetSection("Moonlight")
.GetSection("Payments")
.GetValue<string>("Key");
if (key != validKey)
return StatusCode(403);
var token = await SubscriptionService.ProcessGenerate(subscriptionId);
return Ok(token);
}
}

View file

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.App.Models.Forms;
public class SubscriptionDataModel
{
[Required(ErrorMessage = "You need to enter a name")]
[MaxLength(32, ErrorMessage = "Max lenght for name is 32")]
public string Name { get; set; } = "";
[Required(ErrorMessage = "You need to enter a description")]
public string Description { get; set; } = "";
}

View file

@ -0,0 +1,14 @@
namespace Moonlight.App.Models.Misc;
public class SubscriptionLimit
{
public string Identifier { get; set; } = "";
public int Amount { get; set; }
public List<LimitOption> Options { get; set; } = new();
public class LimitOption
{
public string Key { get; set; } = "";
public string Value { get; set; } = "";
}
}

View file

@ -2,7 +2,7 @@
using Moonlight.App.Database;
using Moonlight.App.Database.Entities;
namespace Moonlight.App.Repositories.Subscriptions;
namespace Moonlight.App.Repositories;
public class SubscriptionRepository : IDisposable
{

View file

@ -1,44 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Moonlight.App.Database;
using Moonlight.App.Database.Entities;
namespace Moonlight.App.Repositories.Subscriptions;
public class SubscriptionLimitRepository : IDisposable
{
private readonly DataContext DataContext;
public SubscriptionLimitRepository(DataContext dataContext)
{
DataContext = dataContext;
}
public DbSet<SubscriptionLimit> Get()
{
return DataContext.SubscriptionLimits;
}
public SubscriptionLimit Add(SubscriptionLimit subscription)
{
var x = DataContext.SubscriptionLimits.Add(subscription);
DataContext.SaveChanges();
return x.Entity;
}
public void Update(SubscriptionLimit subscription)
{
DataContext.SubscriptionLimits.Update(subscription);
DataContext.SaveChanges();
}
public void Delete(SubscriptionLimit subscription)
{
DataContext.SubscriptionLimits.Remove(subscription);
DataContext.SaveChanges();
}
public void Dispose()
{
DataContext.Dispose();
}
}

View file

@ -0,0 +1,45 @@
using Moonlight.App.Database.Entities;
using Moonlight.App.Models.Misc;
using Moonlight.App.Repositories;
using Newtonsoft.Json;
namespace Moonlight.App.Services;
public class SubscriptionAdminService
{
private readonly SubscriptionRepository SubscriptionRepository;
private readonly OneTimeJwtService OneTimeJwtService;
public SubscriptionAdminService(OneTimeJwtService oneTimeJwtService, SubscriptionRepository subscriptionRepository)
{
OneTimeJwtService = oneTimeJwtService;
SubscriptionRepository = subscriptionRepository;
}
public Task<SubscriptionLimit[]> GetLimits(Subscription subscription)
{
return Task.FromResult(
JsonConvert.DeserializeObject<SubscriptionLimit[]>(subscription.LimitsJson)
?? Array.Empty<SubscriptionLimit>()
);
}
public Task SaveLimits(Subscription subscription, SubscriptionLimit[] limits)
{
subscription.LimitsJson = JsonConvert.SerializeObject(limits);
SubscriptionRepository.Update(subscription);
return Task.CompletedTask;
}
public Task<string> GenerateCode(Subscription subscription, int duration)
{
return Task.FromResult(
OneTimeJwtService.Generate(data =>
{
data.Add("subscription", subscription.Id.ToString());
data.Add("duration", duration.ToString());
}, TimeSpan.FromDays(10324))
);
}
}

View file

@ -3,126 +3,138 @@ using Moonlight.App.Database.Entities;
using Moonlight.App.Exceptions;
using Moonlight.App.Models.Misc;
using Moonlight.App.Repositories;
using Moonlight.App.Repositories.Subscriptions;
using Moonlight.App.Services.LogServices;
using Moonlight.App.Services.Sessions;
using Newtonsoft.Json;
namespace Moonlight.App.Services;
public class SubscriptionService
{
private readonly SubscriptionRepository SubscriptionRepository;
private readonly UserRepository UserRepository;
private readonly IdentityService IdentityService;
private readonly ConfigService ConfigService;
private readonly OneTimeJwtService OneTimeJwtService;
private readonly AuditLogService AuditLogService;
private readonly IdentityService IdentityService;
private readonly UserRepository UserRepository;
private readonly ConfigService ConfigService;
public SubscriptionService(SubscriptionRepository subscriptionRepository,
UserRepository userRepository,
IdentityService identityService,
ConfigService configService,
public SubscriptionService(
SubscriptionRepository subscriptionRepository,
OneTimeJwtService oneTimeJwtService,
AuditLogService auditLogService)
IdentityService identityService,
UserRepository userRepository,
ConfigService configService)
{
SubscriptionRepository = subscriptionRepository;
UserRepository = userRepository;
IdentityService = identityService;
ConfigService = configService;
OneTimeJwtService = oneTimeJwtService;
AuditLogService = auditLogService;
IdentityService = identityService;
UserRepository = userRepository;
ConfigService = configService;
}
public async Task<Subscription?> Get()
public async Task<Subscription?> GetCurrent()
{
var user = await IdentityService.Get();
var advancedUser = UserRepository
.Get()
.Include(x => x.Subscription)
.First(x => x.Id == user!.Id);
var user = await GetCurrentUser();
if (advancedUser.Subscription == null)
if (user == null || user.CurrentSubscription == null)
return null;
return SubscriptionRepository
.Get()
.Include(x => x.Limits)
.Include("Limits.Image")
.First(x => x.Id == advancedUser.Subscription.Id);
}
public async Task Cancel()
{
var user = await IdentityService.Get();
user!.Subscription = null;
UserRepository.Update(user!);
var subscriptionEnd = user.SubscriptionSince.ToUniversalTime().AddDays(user.SubscriptionDuration);
await AuditLogService.Log(AuditLogType.CancelSubscription, new[] { user.Email });
}
public Task<Subscription[]> GetAvailable()
{
return Task.FromResult(
SubscriptionRepository
.Get()
.Include(x => x.Limits)
.ToArray()
);
}
public Task<string> GenerateBuyUrl(Subscription subscription)
{
var url = ConfigService
.GetSection("Moonlight")
.GetSection("Payments")
.GetValue<string>("BaseUrl");
if (subscriptionEnd > DateTime.UtcNow)
{
return user.CurrentSubscription;
}
return Task.FromResult<string>($"{url}/products/{subscription.SellPassId}");
}
public Task<string> ProcessGenerate(int subscriptionId)
{
var subscription = SubscriptionRepository
.Get()
.FirstOrDefault(x => x.Id == subscriptionId);
if (subscription == null)
throw new DisplayException("Unknown subscription id");
var token = OneTimeJwtService.Generate(
options =>
{
options.Add("id", subscription.Id.ToString());
}
);
return Task.FromResult(token);
return null;
}
public async Task ApplyCode(string code)
{
var user = (await IdentityService.Get())!;
var values = await OneTimeJwtService.Validate(code);
var data = await OneTimeJwtService.Validate(code);
if (values == null)
throw new DisplayException("Invalid subscription code");
if (data == null)
throw new DisplayException("Invalid or expired subscription code");
if (!values.ContainsKey("id"))
throw new DisplayException("Subscription code is missing the id");
var id = int.Parse(values["id"]);
var id = int.Parse(data["subscription"]);
var duration = int.Parse(data["duration"]);
var subscription = SubscriptionRepository
.Get()
.FirstOrDefault(x => x.Id == id);
if (subscription == null)
throw new DisplayException("The subscription the code is referring does not exist");
throw new DisplayException("The subscription the code is associated with does not exist");
user.Subscription = subscription;
user.SubscriptionDuration = subscription.Duration;
user.SubscriptionSince = DateTime.Now;
var user = await GetCurrentUser();
if (user == null)
throw new DisplayException("Unable to determine current user");
user.CurrentSubscription = subscription;
user.SubscriptionDuration = duration;
user.SubscriptionSince = DateTime.UtcNow;
UserRepository.Update(user);
await OneTimeJwtService.Revoke(code);
await AuditLogService.Log(AuditLogType.ApplySubscriptionCode, new[] { user.Email, subscription.Id.ToString() });
await OneTimeJwtService.Revoke(code);
}
public async Task<SubscriptionLimit> GetLimit(string identifier)
{
var configSection = ConfigService.GetSection("Moonlight").GetSection("Subscriptions");
var defaultLimits = configSection.GetValue<SubscriptionLimit[]>("defaultLimits");
var subscription = await GetCurrent();
if (subscription == null)
{
var foundDefault = defaultLimits.FirstOrDefault(x => x.Identifier == identifier);
if (foundDefault != null)
return foundDefault;
return new()
{
Identifier = identifier,
Amount = 0
};
}
else
{
var subscriptionLimits =
JsonConvert.DeserializeObject<SubscriptionLimit[]>(subscription.LimitsJson)
?? Array.Empty<SubscriptionLimit>();
var foundLimit = subscriptionLimits.FirstOrDefault(x => x.Identifier == identifier);
if (foundLimit != null)
return foundLimit;
var foundDefault = defaultLimits.FirstOrDefault(x => x.Identifier == identifier);
if (foundDefault != null)
return foundDefault;
return new()
{
Identifier = identifier,
Amount = 0
};
}
}
private async Task<User?> GetCurrentUser()
{
var user = await IdentityService.Get();
if (user == null)
return null;
var userWithData = UserRepository
.Get()
.Include(x => x.CurrentSubscription)
.First(x => x.Id == user.Id);
return userWithData;
}
}

View file

@ -9,7 +9,6 @@ using Moonlight.App.Repositories;
using Moonlight.App.Repositories.Domains;
using Moonlight.App.Repositories.LogEntries;
using Moonlight.App.Repositories.Servers;
using Moonlight.App.Repositories.Subscriptions;
using Moonlight.App.Services;
using Moonlight.App.Services.DiscordBot;
using Moonlight.App.Services.Interop;
@ -58,13 +57,12 @@ namespace Moonlight
builder.Services.AddScoped<SupportMessageRepository>();
builder.Services.AddScoped<DomainRepository>();
builder.Services.AddScoped<SharedDomainRepository>();
builder.Services.AddScoped<SubscriptionRepository>();
builder.Services.AddScoped<SubscriptionLimitRepository>();
builder.Services.AddScoped<RevokeRepository>();
builder.Services.AddScoped<NotificationRepository>();
builder.Services.AddScoped<AaPanelRepository>();
builder.Services.AddScoped<WebsiteRepository>();
builder.Services.AddScoped<DdosAttackRepository>();
builder.Services.AddScoped<SubscriptionRepository>();
builder.Services.AddScoped<AuditLogEntryRepository>();
builder.Services.AddScoped<ErrorLogEntryRepository>();
@ -89,7 +87,6 @@ namespace Moonlight
builder.Services.AddSingleton<ResourceService>();
builder.Services.AddScoped<DomainService>();
builder.Services.AddScoped<OneTimeJwtService>();
builder.Services.AddScoped<SubscriptionService>();
builder.Services.AddSingleton<NotificationServerService>();
builder.Services.AddScoped<NotificationAdminService>();
builder.Services.AddScoped<NotificationClientService>();
@ -98,6 +95,9 @@ namespace Moonlight
builder.Services.AddScoped<GoogleOAuth2Service>();
builder.Services.AddScoped<DiscordOAuth2Service>();
builder.Services.AddScoped<SubscriptionService>();
builder.Services.AddScoped<SubscriptionAdminService>();
builder.Services.AddSingleton<CleanupService>();
// Loggers

View file

@ -0,0 +1,57 @@
@using Moonlight.App.Services
@using Moonlight.App.Services.Interop
@inject SmartTranslateService SmartTranslateService
@inject AlertService AlertService
@if (!Working)
{
<button class="btn btn-danger" @onclick="Do">
<i class="bx bx-trash"></i>
</button>
}
else
{
<button class="btn btn-danger disabled" disabled="">
<span class="spinner-border spinner-border-sm align-middle me-2"></span>
</button>
}
@code
{
private bool Working { get; set; } = false;
[Parameter]
public Func<Task>? OnClick { get; set; }
[Parameter]
public bool Confirm { get; set; } = false;
private async Task Do()
{
Working = true;
StateHasChanged();
await Task.Run(async () =>
{
if (Confirm)
{
var b = await AlertService.YesNo(
SmartTranslateService.Translate("Are you sure?"),
SmartTranslateService.Translate("Do you really want to delete it?"),
SmartTranslateService.Translate("Yes"),
SmartTranslateService.Translate("No")
);
if (b)
{
if(OnClick != null)
await OnClick.Invoke();
}
}
Working = false;
await InvokeAsync(StateHasChanged);
});
}
}

View file

@ -33,13 +33,13 @@
public object Model { get; set; }
[Parameter]
public EventCallback<EditContext> OnValidSubmit { get; set; }
public Func<Task>? OnValidSubmit { get; set; }
[Parameter]
public EventCallback<EditContext> OnInvalidSubmit { get; set; }
public Func<Task>? OnInvalidSubmit { get; set; }
[Parameter]
public EventCallback<EditContext> OnSubmit { get; set; }
public Func<Task>? OnSubmit { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
@ -67,8 +67,14 @@
await Task.Run(async () =>
{
await InvokeAsync(() => OnValidSubmit.InvokeAsync(context));
await InvokeAsync(() => OnSubmit.InvokeAsync(context));
await InvokeAsync(async () =>
{
if (OnValidSubmit != null)
await OnValidSubmit.Invoke();
if (OnSubmit != null)
await OnSubmit.Invoke();
});
Working = false;
await InvokeAsync(StateHasChanged);
@ -87,7 +93,10 @@
await InvokeAsync(StateHasChanged);
await OnInvalidSubmit.InvokeAsync(context);
await OnSubmit.InvokeAsync(context);
if (OnInvalidSubmit != null)
await OnInvalidSubmit.Invoke();
if (OnSubmit != null)
await OnSubmit.Invoke();
}
}

View file

@ -225,6 +225,14 @@ else
<span class="menu-title"><TL>Support</TL></span>
</a>
</div>
<div class="menu-item">
<a class="menu-link" href="/admin/subscriptions">
<span class="menu-icon">
<i class="bx bx-credit-card"></i>
</span>
<span class="menu-title"><TL>Subscriptions</TL></span>
</a>
</div>
<div class="menu-item">
<a class="menu-link" href="/admin/statistics">
<span class="menu-icon">

View file

@ -0,0 +1,72 @@
@page "/admin/subscriptions/"
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Repositories
@using BlazorTable
@inject SmartTranslateService SmartTranslateService
@inject SubscriptionRepository SubscriptionRepository
<OnlyAdmin>
<div class="card">
<LazyLoader @ref="LazyLoader" Load="Load">
<div class="card-header border-0 pt-5">
<h3 class="card-title align-items-start flex-column">
<span class="card-label fw-bold fs-3 mb-1">
<TL>Subscriptions</TL>
</span>
</h3>
<div class="card-toolbar">
<a href="/admin/subscriptions/new" class="btn btn-sm btn-light-success">
<i class="bx bx-credit-card"></i>
<TL>New subscription</TL>
</a>
</div>
</div>
<div class="card-body">
<div class="table-responsive">
<Table TableItem="Subscription" Items="Subscriptions" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
<Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
<Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Name"))" Field="@(x => x.Name)" Sortable="true" Filterable="true"/>
<Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Description"))" Field="@(x => x.Description)" Sortable="true" Filterable="true"/>
<Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Manage"))" Field="@(x => x.Id)" Sortable="false" Filterable="false">
<Template>
<a href="/admin/subscriptions/edit/@(context.Id)/">
<TL>Manage</TL>
</a>
</Template>
</Column>
<Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Manage"))" Field="@(x => x.Id)" Sortable="false" Filterable="false">
<Template>
<DeleteButton Confirm="true" OnClick="() => Delete(context)" />
</Template>
</Column>
<Pager ShowPageNumber="true" ShowTotalCount="true"/>
</Table>
</div>
</div>
</LazyLoader>
</div>
</OnlyAdmin>
@code
{
private Subscription[] Subscriptions;
private LazyLoader LazyLoader;
private Task Load(LazyLoader arg)
{
Subscriptions = SubscriptionRepository
.Get()
.ToArray();
return Task.CompletedTask;
}
private async Task Delete(Subscription subscription)
{
SubscriptionRepository.Delete(subscription);
await LazyLoader.Reload();
}
}

View file

@ -0,0 +1,137 @@
@page "/admin/subscriptions/new"
@using Moonlight.App.Models.Forms
@using Moonlight.App.Models.Misc
@using Moonlight.App.Repositories
@using Moonlight.App.Services
@inject NavigationManager NavigationManager
@inject SubscriptionRepository SubscriptionRepository
@inject SubscriptionAdminService SubscriptionAdminService
<OnlyAdmin>
<div class="card card-body p-10">
<SmartForm Model="Model" OnValidSubmit="OnSubmit">
<label class="form-label">
<TL>Name</TL>
</label>
<div class="input-group mb-5">
<InputText @bind-Value="Model.Name" class="form-control"></InputText>
</div>
<label class="form-label">
<TL>Description</TL>
</label>
<div class="input-group mb-5">
<InputText @bind-Value="Model.Description" class="form-control"></InputText>
</div>
<div>
@foreach (var limitPart in Limits.Chunk(3))
{
<div class="row row-cols-3 mb-5">
@foreach (var limit in limitPart)
{
<div class="col">
<div class="card card-body border">
<label class="form-label">
<TL>Identifier</TL>
</label>
<div class="input-group mb-5">
<input @bind="limit.Identifier" type="text" class="form-control">
</div>
<label class="form-label">
<TL>Amount</TL>
</label>
<div class="input-group mb-5">
<input @bind="limit.Amount" type="number" class="form-control">
</div>
<div class="d-flex flex-column mb-15 fv-row">
<div class="fs-5 fw-bold form-label mb-3">
<TL>Options</TL>
</div>
<div class="table-responsive">
<div class="dataTables_wrapper dt-bootstrap4 no-footer">
<div class="table-responsive">
<table class="table align-middle table-row-dashed fw-semibold fs-6 gy-5 dataTable no-footer">
<thead>
<tr class="text-start text-muted fw-bold fs-7 text-uppercase gs-0">
<th class="pt-0 sorting_disabled">
<TL>Key</TL>
</th>
<th class="pt-0 sorting_disabled">
<TL>Value</TL>
</th>
<th class="pt-0 text-end sorting_disabled">
<TL>Remove</TL>
</th>
</tr>
</thead>
<tbody>
@foreach (var option in limit.Options)
{
<tr class="odd">
<td>
<input @bind="option.Key" type="text" class="form-control form-control-solid">
</td>
<td>
<input @bind="option.Value" type="text" class="form-control form-control-solid">
</td>
<td class="text-end">
<button @onclick="() => limit.Options.Remove(option)" type="button" class="btn btn-icon btn-flex btn-active-light-primary w-30px h-30px me-3" data-kt-action="field_remove">
<i class="bx bx-trash"></i>
</button>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm-12 col-md-5 d-flex align-items-center justify-content-center justify-content-md-start"></div>
<div class="col-sm-12 col-md-7 d-flex align-items-center justify-content-center justify-content-md-end"></div>
</div>
</div>
</div>
<div class="btn-group mt-5">
<button @onclick:preventDefault @onclick="() => limit.Options.Add(new())" type="button" class="btn btn-light-primary me-auto">Add option</button>
<button @onclick:preventDefault @onclick="() => Limits.Remove(limit)" class="btn btn-danger float-end">
<i class="bx bx-trash"></i>
</button>
</div>
</div>
</div>
</div>
}
</div>
}
</div>
<div class="float-end">
<button @onclick:preventDefault @onclick="() => Limits.Add(new())" class="btn btn-primary">
<TL>Add new limit</TL>
</button>
<button type="submit" class="btn btn-success">
<TL>Create subscription</TL>
</button>
</div>
</SmartForm>
</div>
</OnlyAdmin>
@code
{
private SubscriptionDataModel Model = new();
private List<SubscriptionLimit> Limits = new();
private async Task OnSubmit()
{
var sub = SubscriptionRepository.Add(new()
{
Name = Model.Name,
Description = Model.Description
});
await SubscriptionAdminService.SaveLimits(sub, Limits.ToArray());
NavigationManager.NavigateTo("/admin/subscriptions");
}
}

View file

@ -196,16 +196,7 @@ else
</label>
<div class="col-lg-8">
<span class="fw-bold fs-6 text-gray-800">
@if (User.Subscription == null)
{
<span>
<TL>None</TL>
</span>
}
else
{
<span>@(User.Subscription.Name)</span>
}
</span>
</div>
</div>

View file

@ -87,8 +87,10 @@
User = await IdentityService.Get();
}
private void Save()
private Task Save()
{
UserRepository.Update(User);
return Task.CompletedTask;
}
}

View file

@ -1,126 +0,0 @@
@page "/profile/subscriptions"
@using Moonlight.App.Services
@using Moonlight.Shared.Components.Navigations
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@inject SubscriptionService SubscriptionService
@inject SmartTranslateService SmartTranslateService
@inject NavigationManager NavigationManager
<ProfileNavigation Index="2"/>
<LazyLoader @ref="LazyLoader" Load="Load">
<div class="card mb-5 mb-xl-10">
<div class="card-body">
@if (User.Subscription != null)
{
<div class="row">
<div class="col-lg-7">
<h3 class="mb-2">
<TL>Active until</TL> @(Formatter.FormatDate(User.SubscriptionSince!.Value.AddDays(User.SubscriptionDuration)))
</h3>
<p class="fs-6 text-gray-600 fw-semibold mb-6 mb-lg-15">
<TL>We will send you a notification upon subscription expiration</TL>
</p>
</div>
<div class="col-lg-5">
<div class="d-flex justify-content-end pb-0 px-0">
<WButton Text="@(SmartTranslateService.Translate("Cancel Subscription"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn-danger btn-active-light-danger me-2"
OnClick="Cancel">
</WButton>
</div>
</div>
</div>
}
else
{
<div class="row g-3">
<div class="col-auto">
<input @bind="Code" type="text" class="form-control">
</div>
<div class="col-auto">
<WButton Text="@(SmartTranslateService.Translate("Apply"))"
WorkingText="@(SmartTranslateService.Translate("Applying code"))"
CssClasses="btn-success"
OnClick="ApplyCode">
</WButton>
</div>
</div>
<div class="mt-5 row gx-9 gy-6">
@if (Available.Any())
{
@foreach (var sub in Available)
{
<div class="col-xl-6">
<div class="card card-dashed h-xl-100 flex-row flex-stack flex-wrap p-6">
<div class="d-flex flex-column py-2">
<div class="d-flex align-items-center fs-4 fw-bold mb-5">
@(sub.Name)
</div>
<div class="d-flex align-items-center">
<div class="fs-6 fw-semibold">@(sub.Description)</div>
</div>
</div>
<div class="d-flex align-items-center py-2">
<WButton Text="@(SmartTranslateService.Translate("Buy"))"
WorkingText="@(SmartTranslateService.Translate("Redirecting"))"
CssClasses="btn-primary"
OnClick="() => Buy(sub)">
</WButton>
</div>
</div>
</div>
}
}
else
{
<div class="alert alert-primary">
<TL>No subscription available</TL>
</div>
}
</div>
}
</div>
</div>
</LazyLoader>
@code
{
[CascadingParameter]
public User User { get; set; }
private Subscription? Subscription;
private Subscription[] Available;
private string Code = "";
private LazyLoader LazyLoader;
private async Task Load(LazyLoader lazyLoader)
{
Subscription = await SubscriptionService.Get();
Available = await SubscriptionService.GetAvailable();
}
private async Task Cancel()
{
await SubscriptionService.Cancel();
}
private async Task Buy(Subscription subscription)
{
var url = await SubscriptionService.GenerateBuyUrl(subscription);
NavigationManager.NavigateTo(url);
}
private async Task ApplyCode()
{
await SubscriptionService.ApplyCode(Code);
await LazyLoader.Reload();
}
}

View file

@ -454,3 +454,11 @@ Finish activation;Finish activation
New password;New password
Secure your account;Secure your account
2fa adds another layer of security to your account. You have to enter a 6 digit code in order to login.;2fa adds another layer of security to your account. You have to enter a 6 digit code in order to login.
New subscription;New subscription
You need to enter a name;You need to enter a name
You need to enter a description;You need to enter a description
Add new limit;Add new limit
Create subscription;Create subscription
Options;Options
Amount;Amount
Do you really want to delete it?;Do you really want to delete it?