diff --git a/GWMS.Data/Migrations/20210824143900_UndefRoleAdd.Designer.cs b/GWMS.Data/Migrations/20210824143900_UndefRoleAdd.Designer.cs new file mode 100644 index 0000000..6b5b83a --- /dev/null +++ b/GWMS.Data/Migrations/20210824143900_UndefRoleAdd.Designer.cs @@ -0,0 +1,308 @@ +// +using System; +using GWMS.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace GWMS.User.Migrations +{ + [DbContext(typeof(UserIdentityDbContext))] + [Migration("20210824143900_UndefRoleAdd")] + partial class UndefRoleAdd + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 64) + .HasAnnotation("ProductVersion", "5.0.8"); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .HasColumnType("varchar(255)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("longtext"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("varchar(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("varchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + + b.HasData( + new + { + Id = "bf698c1c-ead1-4d88-83ef-536098876807", + ConcurrencyStamp = "28d97b49-8df4-4d20-8680-1d3abbf4ed94", + Name = "Undef", + NormalizedName = "UNDEF" + }, + new + { + Id = "a1280bf2-7e5a-447b-b070-3bea27ba2239", + ConcurrencyStamp = "d361332c-1ae0-486c-b1d8-1f2c9c36ad88", + Name = "ExtUser", + NormalizedName = "EXTUSER" + }, + new + { + Id = "533995b8-8561-471a-b613-a03685141065", + ConcurrencyStamp = "773db769-a518-406f-9144-31e1fd9f7a76", + Name = "User", + NormalizedName = "USER" + }, + new + { + Id = "d4790e4e-600b-4bae-876d-04c619adc9b0", + ConcurrencyStamp = "70529623-7648-4640-b69e-a1e0ddff10ed", + Name = "Admin", + NormalizedName = "ADMINISTRATOR" + }, + new + { + Id = "3e7ddc7b-94eb-4364-b4d5-311c76dad5b1", + ConcurrencyStamp = "1d59afd3-30f6-492c-87eb-cddf0b251f85", + Name = "SuperAdmin", + NormalizedName = "SUPERADMINISTRATOR" + }); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ClaimType") + .HasColumnType("longtext"); + + b.Property("ClaimValue") + .HasColumnType("longtext"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("varchar(255)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property("Id") + .HasColumnType("varchar(255)"); + + b.Property("AccessFailedCount") + .HasColumnType("int"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("longtext"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("varchar(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnd") + .HasColumnType("datetime(6)"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("varchar(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("varchar(256)"); + + b.Property("PasswordHash") + .HasColumnType("longtext"); + + b.Property("PhoneNumber") + .HasColumnType("longtext"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("SecurityStamp") + .HasColumnType("longtext"); + + b.Property("TwoFactorEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("varchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ClaimType") + .HasColumnType("longtext"); + + b.Property("ClaimValue") + .HasColumnType("longtext"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("varchar(255)"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ProviderKey") + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ProviderDisplayName") + .HasColumnType("longtext"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("varchar(255)"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("varchar(255)"); + + b.Property("RoleId") + .HasColumnType("varchar(255)"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("varchar(255)"); + + b.Property("LoginProvider") + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("Name") + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("Value") + .HasColumnType("longtext"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", 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", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/GWMS.Data/Migrations/20210824143900_UndefRoleAdd.cs b/GWMS.Data/Migrations/20210824143900_UndefRoleAdd.cs new file mode 100644 index 0000000..cdfbf24 --- /dev/null +++ b/GWMS.Data/Migrations/20210824143900_UndefRoleAdd.cs @@ -0,0 +1,81 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace GWMS.User.Migrations +{ + public partial class UndefRoleAdd : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "0a5335f9-64b2-4196-9d5b-db873cb48d94"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "24a790ae-8eac-4c9f-9043-aa688b626169"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "7a024dca-5e5d-47be-b754-75c8921d88bf"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "f6aa57e0-fac5-45b7-8e27-fd01d8eff87d"); + + migrationBuilder.InsertData( + table: "AspNetRoles", + columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" }, + values: new object[,] + { + { "bf698c1c-ead1-4d88-83ef-536098876807", "28d97b49-8df4-4d20-8680-1d3abbf4ed94", "Undef", "UNDEF" }, + { "a1280bf2-7e5a-447b-b070-3bea27ba2239", "d361332c-1ae0-486c-b1d8-1f2c9c36ad88", "ExtUser", "EXTUSER" }, + { "533995b8-8561-471a-b613-a03685141065", "773db769-a518-406f-9144-31e1fd9f7a76", "User", "USER" }, + { "d4790e4e-600b-4bae-876d-04c619adc9b0", "70529623-7648-4640-b69e-a1e0ddff10ed", "Admin", "ADMINISTRATOR" }, + { "3e7ddc7b-94eb-4364-b4d5-311c76dad5b1", "1d59afd3-30f6-492c-87eb-cddf0b251f85", "SuperAdmin", "SUPERADMINISTRATOR" } + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "3e7ddc7b-94eb-4364-b4d5-311c76dad5b1"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "533995b8-8561-471a-b613-a03685141065"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "a1280bf2-7e5a-447b-b070-3bea27ba2239"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "bf698c1c-ead1-4d88-83ef-536098876807"); + + migrationBuilder.DeleteData( + table: "AspNetRoles", + keyColumn: "Id", + keyValue: "d4790e4e-600b-4bae-876d-04c619adc9b0"); + + migrationBuilder.InsertData( + table: "AspNetRoles", + columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" }, + values: new object[,] + { + { "24a790ae-8eac-4c9f-9043-aa688b626169", "c9adff6c-ffac-48e3-899b-8549ca443071", "ExtUser", "EXTUSER" }, + { "7a024dca-5e5d-47be-b754-75c8921d88bf", "3b9aab89-73b6-4051-9825-d5381d2896da", "User", "USER" }, + { "0a5335f9-64b2-4196-9d5b-db873cb48d94", "25097cab-df45-4bdd-8616-041962c72050", "Admin", "ADMINISTRATOR" }, + { "f6aa57e0-fac5-45b7-8e27-fd01d8eff87d", "3c222fe9-d5e5-4ba0-9767-4c1777677105", "SuperAdmin", "SUPERADMINISTRATOR" } + }); + } + } +} diff --git a/GWMS.Data/Migrations/UserIdentityDbContextModelSnapshot.cs b/GWMS.Data/Migrations/UserIdentityDbContextModelSnapshot.cs index c142226..9ca64e6 100644 --- a/GWMS.Data/Migrations/UserIdentityDbContextModelSnapshot.cs +++ b/GWMS.Data/Migrations/UserIdentityDbContextModelSnapshot.cs @@ -45,29 +45,36 @@ namespace GWMS.User.Migrations b.HasData( new { - Id = "24a790ae-8eac-4c9f-9043-aa688b626169", - ConcurrencyStamp = "c9adff6c-ffac-48e3-899b-8549ca443071", + Id = "bf698c1c-ead1-4d88-83ef-536098876807", + ConcurrencyStamp = "28d97b49-8df4-4d20-8680-1d3abbf4ed94", + Name = "Undef", + NormalizedName = "UNDEF" + }, + new + { + Id = "a1280bf2-7e5a-447b-b070-3bea27ba2239", + ConcurrencyStamp = "d361332c-1ae0-486c-b1d8-1f2c9c36ad88", Name = "ExtUser", NormalizedName = "EXTUSER" }, new { - Id = "7a024dca-5e5d-47be-b754-75c8921d88bf", - ConcurrencyStamp = "3b9aab89-73b6-4051-9825-d5381d2896da", + Id = "533995b8-8561-471a-b613-a03685141065", + ConcurrencyStamp = "773db769-a518-406f-9144-31e1fd9f7a76", Name = "User", NormalizedName = "USER" }, new { - Id = "0a5335f9-64b2-4196-9d5b-db873cb48d94", - ConcurrencyStamp = "25097cab-df45-4bdd-8616-041962c72050", + Id = "d4790e4e-600b-4bae-876d-04c619adc9b0", + ConcurrencyStamp = "70529623-7648-4640-b69e-a1e0ddff10ed", Name = "Admin", NormalizedName = "ADMINISTRATOR" }, new { - Id = "f6aa57e0-fac5-45b7-8e27-fd01d8eff87d", - ConcurrencyStamp = "3c222fe9-d5e5-4ba0-9767-4c1777677105", + Id = "3e7ddc7b-94eb-4364-b4d5-311c76dad5b1", + ConcurrencyStamp = "1d59afd3-30f6-492c-87eb-cddf0b251f85", Name = "SuperAdmin", NormalizedName = "SUPERADMINISTRATOR" }); diff --git a/GWMS.Data/RoleConfiguration.cs b/GWMS.Data/RoleConfiguration.cs index c1ca6ad..0e573f6 100644 --- a/GWMS.Data/RoleConfiguration.cs +++ b/GWMS.Data/RoleConfiguration.cs @@ -16,6 +16,7 @@ namespace GWMS.Data public void Configure(EntityTypeBuilder builder) { builder.HasData( + new IdentityRole { Name = "Undef", NormalizedName = "UNDEF" }, new IdentityRole { Name = "ExtUser", NormalizedName = "EXTUSER" }, new IdentityRole { Name = "User", NormalizedName = "USER" }, new IdentityRole { Name = "Admin", NormalizedName = "ADMINISTRATOR" }, diff --git a/GWMS.User/Areas/Identity/Pages/Account/Register.cshtml.cs b/GWMS.User/Areas/Identity/Pages/Account/Register.cshtml.cs index 919ae30..0b07e03 100644 --- a/GWMS.User/Areas/Identity/Pages/Account/Register.cshtml.cs +++ b/GWMS.User/Areas/Identity/Pages/Account/Register.cshtml.cs @@ -19,10 +19,16 @@ namespace GWMS.User.Areas.Identity.Pages.Account [AllowAnonymous] public class RegisterModel : PageModel { + #region Private Fields + + private readonly IEmailSender _emailSender; + private readonly ILogger _logger; private readonly SignInManager _signInManager; private readonly UserManager _userManager; - private readonly ILogger _logger; - private readonly IEmailSender _emailSender; + + #endregion Private Fields + + #region Public Constructors public RegisterModel( UserManager userManager, @@ -36,31 +42,20 @@ namespace GWMS.User.Areas.Identity.Pages.Account _emailSender = emailSender; } + #endregion Public Constructors + + #region Public Properties + + public IList ExternalLogins { get; set; } + [BindProperty] public InputModel Input { get; set; } public string ReturnUrl { get; set; } - public IList ExternalLogins { get; set; } + #endregion Public Properties - public class InputModel - { - [Required] - [EmailAddress] - [Display(Name = "Email")] - public string Email { get; set; } - - [Required] - [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] - [DataType(DataType.Password)] - [Display(Name = "Password")] - public string Password { get; set; } - - [DataType(DataType.Password)] - [Display(Name = "Confirm password")] - [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] - public string ConfirmPassword { get; set; } - } + #region Public Methods public async Task OnGetAsync(string returnUrl = null) { @@ -97,6 +92,10 @@ namespace GWMS.User.Areas.Identity.Pages.Account } else { + // aggiunta ruolo UNDEF (da sistemare poi) + // https://code-maze.com/using-roles-in-blazor-webassembly-hosted-applications/ + await _userManager.AddToRoleAsync(user, "Undef"); + // continuo come default await _signInManager.SignInAsync(user, isPersistent: false); return LocalRedirect(returnUrl); } @@ -110,5 +109,34 @@ namespace GWMS.User.Areas.Identity.Pages.Account // If we got this far, something failed, redisplay form return Page(); } + + #endregion Public Methods + + #region Public Classes + + public class InputModel + { + #region Public Properties + + [DataType(DataType.Password)] + [Display(Name = "Confirm password")] + [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] + public string ConfirmPassword { get; set; } + + [Required] + [EmailAddress] + [Display(Name = "Email")] + public string Email { get; set; } + + [Required] + [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] + [DataType(DataType.Password)] + [Display(Name = "Password")] + public string Password { get; set; } + + #endregion Public Properties + } + + #endregion Public Classes } -} +} \ No newline at end of file diff --git a/GWMS.User/Shared/NavMenu.razor b/GWMS.User/Shared/NavMenu.razor index ff6bc94..63e8b36 100644 --- a/GWMS.User/Shared/NavMenu.razor +++ b/GWMS.User/Shared/NavMenu.razor @@ -17,21 +17,19 @@ Counter + - - - diff --git a/GWMS.User/Startup.cs b/GWMS.User/Startup.cs index 95803f8..b1ab9ef 100644 --- a/GWMS.User/Startup.cs +++ b/GWMS.User/Startup.cs @@ -111,6 +111,10 @@ namespace GWMS.User services.AddRazorPages(); + // rif auth + // https://stackoverflow.com/questions/60687879/require-authorization-on-all-blazor-pages/60688109#60688109 + // https://www.c-sharpcorner.com/article/understand-basic-of-authorization-in-blazor-server-app/#:~:text=Authentication%20is%20a%20process%20of%20validating%20a%20user,UI%20option%20can%20be%20accessible%20by%20the%20user. + // non funziona --> messo attributo in _Imports.razor e esclusione in Index page #if false // richiesta esplicita autorizzazione