From d621f7e6fc1ac0c67b5dabef7cff7b27c2bea1fd Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 3 Sep 2024 17:18:59 +0200 Subject: [PATCH] AuthUtente x LAND: - aggiunta DbModels - aggiunta controillers - aggiunta servizio --- MP.AppAuth/AppAuthContext.cs | 68 ++++++++++ MP.AppAuth/Controllers/AppAuthController.cs | 133 +++++++++++++++----- MP.AppAuth/Controllers/MPUserController.cs | 59 +++++++++ MP.AppAuth/Models/Permessi2FunzioneModel.cs | 22 ++++ MP.AppAuth/Models/PermessiModel.cs | 30 +++++ MP.AppAuth/Models/UserDirittiModel.cs | 29 +++++ MP.AppAuth/UserAuthContext.cs | 104 +++++++++++++++ 7 files changed, 415 insertions(+), 30 deletions(-) create mode 100644 MP.AppAuth/Controllers/MPUserController.cs create mode 100644 MP.AppAuth/Models/Permessi2FunzioneModel.cs create mode 100644 MP.AppAuth/Models/PermessiModel.cs create mode 100644 MP.AppAuth/Models/UserDirittiModel.cs create mode 100644 MP.AppAuth/UserAuthContext.cs diff --git a/MP.AppAuth/AppAuthContext.cs b/MP.AppAuth/AppAuthContext.cs index 4ea914e2..c8c74357 100644 --- a/MP.AppAuth/AppAuthContext.cs +++ b/MP.AppAuth/AppAuthContext.cs @@ -59,6 +59,8 @@ namespace MP.AppAuth public virtual DbSet DbSetGruppi2Oper { get; set; } public virtual DbSet DbSetUpdMan { get; set; } public virtual DbSet DbSetVocabolario { get; set; } + public virtual DbSet DbSetPermessi { get; set; } = null!; + public virtual DbSet DbSetPermessi2Funzione { get; set; } = null!; #endregion Public Properties @@ -134,6 +136,72 @@ namespace MP.AppAuth entity.Property(e => e.CodGruppo).HasMaxLength(50); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.CodPermesso); + + entity.ToTable("Permessi"); + + entity.Property(e => e.CodPermesso) + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnName("COD_PERMESSO") + .UseCollation("Latin1_General_CI_AS"); + + entity.Property(e => e.Descrizione) + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnName("DESCRIZIONE") + .UseCollation("Latin1_General_CI_AS"); + + entity.Property(e => e.Gruppo).HasColumnName("GRUPPO"); + + entity.Property(e => e.Nome) + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnName("NOME") + .UseCollation("Latin1_General_CI_AS"); + + entity.Property(e => e.Numero).HasColumnName("NUMERO"); + + entity.Property(e => e.Url) + .HasMaxLength(250) + .IsUnicode(false) + .HasColumnName("URL") + .UseCollation("Latin1_General_CI_AS"); + }); + + modelBuilder.Entity(entity => + { + entity.HasKey(e => new { e.CodPermesso, e.CodFunzione }); + + entity.ToTable("Permessi2Funzione"); + + entity.Property(e => e.CodPermesso) + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnName("COD_PERMESSO") + .UseCollation("Latin1_General_CI_AS"); + + entity.Property(e => e.CodFunzione) + .HasMaxLength(31) + .HasColumnName("COD_FUNZIONE") + .UseCollation("Latin1_General_CI_AS"); + + entity.Property(e => e.Readwrite) + .HasMaxLength(1) + .IsUnicode(false) + .HasColumnName("READWRITE") + .IsFixedLength() + .UseCollation("Latin1_General_CI_AS"); + + entity.HasOne(d => d.PermessiNav) + .WithMany(p => p.Permessi2FunzioneNav) + .HasForeignKey(d => d.CodPermesso) + .OnDelete(DeleteBehavior.ClientSetNull) + .HasConstraintName("FK_Permessi2Funzione_Permessi"); + }); + // modelBuilder.Seed(); diff --git a/MP.AppAuth/Controllers/AppAuthController.cs b/MP.AppAuth/Controllers/AppAuthController.cs index d0fce9ae..24757db7 100644 --- a/MP.AppAuth/Controllers/AppAuthController.cs +++ b/MP.AppAuth/Controllers/AppAuthController.cs @@ -11,14 +11,6 @@ namespace MP.AppAuth.Controllers { public class AppAuthController : IDisposable { - #region Private Fields - - private static IConfiguration _configuration; - private static AppAuthContext dbCtx; - private static Logger Log = LogManager.GetCurrentClassLogger(); - - #endregion Private Fields - #region Public Constructors public AppAuthController(IConfiguration configuration) @@ -48,6 +40,7 @@ namespace MP.AppAuth.Controllers } return dbResult; } + /// /// Elenco Record x Gruppi /// @@ -64,28 +57,6 @@ namespace MP.AppAuth.Controllers return dbResult; } - public List AnagOpGetAll(string searchVal) - { - List dbResult = new List(); - using (AppAuthContext localDbCtx = new AppAuthContext(_configuration)) - { - if (!string.IsNullOrEmpty(searchVal)) - { - dbResult = localDbCtx - .DbSetAnagOpr - .Where(x => x.Cognome.Contains(searchVal) || x.Nome.Contains(searchVal)) - .ToList(); - } - else - { - dbResult = localDbCtx - .DbSetAnagOpr - .ToList(); - } - } - // ritorno - return dbResult; - } public List AnagOpByGruppoGetFilt(string codGruppo, string searchVal) { List dbResult = new List(); @@ -122,12 +93,106 @@ namespace MP.AppAuth.Controllers return dbResult; } + public List AnagOpGetAll(string searchVal) + { + List dbResult = new List(); + using (AppAuthContext localDbCtx = new AppAuthContext(_configuration)) + { + if (!string.IsNullOrEmpty(searchVal)) + { + dbResult = localDbCtx + .DbSetAnagOpr + .Where(x => x.Cognome.Contains(searchVal) || x.Nome.Contains(searchVal)) + .ToList(); + } + else + { + dbResult = localDbCtx + .DbSetAnagOpr + .ToList(); + } + } + // ritorno + return dbResult; + } + public void Dispose() { // Clear database context dbCtx.Dispose(); } + /// + /// Elenco completo permessi2funzione + /// + /// + public List Permessi2FunzioneGetAll() + { + List dbResult = new List(); + using (AppAuthContext dbCtx = new AppAuthContext(_configuration)) + { + dbResult = dbCtx + .DbSetPermessi2Funzione + .ToList(); + } + return dbResult; + } + + /// + /// Elenco permessi2funzione filtrato x elenco funzioni + /// + /// + public List Permessi2FunzioneGetFilt(List FunList) + { + List dbResult = new List(); + using (AppAuthContext dbCtx = new AppAuthContext(_configuration)) + { + dbResult = dbCtx + .DbSetPermessi2Funzione + .Where(x => FunList.Contains(x.CodFunzione)) + .ToList(); + } + return dbResult; + } + + /// + /// Elenco completo permessi + /// + /// + public List PermessiGetAll() + { + List dbResult = new List(); + using (AppAuthContext dbCtx = new AppAuthContext(_configuration)) + { + dbResult = dbCtx + .DbSetPermessi + .ToList(); + } + return dbResult; + } + + /// + /// Elenco permessi dato elenco funzioni + /// + /// + /// + public List PermessiGetByFunc(List ListCodFun) + { + List dbResult = new List(); + using (AppAuthContext dbCtx = new AppAuthContext(_configuration)) + { + var listPer = PermessiGetAll(); + var listP2F = Permessi2FunzioneGetFilt(ListCodFun); + + var query = from permesso in listPer + join p2f in listP2F on permesso.CodPermesso equals p2f.CodPermesso + select permesso; + + dbResult = query.ToList(); + } + return dbResult; + } + public void ResetController() { dbCtx = new AppAuthContext(_configuration); @@ -190,5 +255,13 @@ namespace MP.AppAuth.Controllers } #endregion Public Methods + + #region Private Fields + + private static IConfiguration _configuration; + private static AppAuthContext dbCtx; + private static Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields } } \ No newline at end of file diff --git a/MP.AppAuth/Controllers/MPUserController.cs b/MP.AppAuth/Controllers/MPUserController.cs new file mode 100644 index 00000000..ab42c488 --- /dev/null +++ b/MP.AppAuth/Controllers/MPUserController.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Configuration; +using MP.AppAuth.Models; +using NLog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.AppAuth.Controllers +{ + public class MPUserController : IDisposable + { + #region Public Constructors + + public MPUserController(IConfiguration configuration) + { + _configuration = configuration; + Log.Info("Avviata classe MPUserController"); + } + + #endregion Public Constructors + + #region Public Methods + + /// + /// Elenco Diritti utente da modulo + /// + /// UserName cercato + /// Modulo desiderato, se "" allora tutti i diritti + /// + public List DirittiUtente(string UserName, string Modulo) + { + List dbResult = new List(); + using (UserAuthContext dbCtx = new UserAuthContext(_configuration)) + { + dbResult = dbCtx + .DbSetUserDiritti + .Where(x => x.UserName == UserName && (string.IsNullOrEmpty(Modulo) || x.Modulo == Modulo)) + .ToList(); + } + return dbResult; + } + + public void Dispose() + { + GC.Collect(); + } + + #endregion Public Methods + + #region Private Fields + + private static IConfiguration _configuration = null!; + private static Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/MP.AppAuth/Models/Permessi2FunzioneModel.cs b/MP.AppAuth/Models/Permessi2FunzioneModel.cs new file mode 100644 index 00000000..e031e923 --- /dev/null +++ b/MP.AppAuth/Models/Permessi2FunzioneModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.AppAuth.Models +{ + // + // This is here so CodeMaid doesn't reorganize this document + // + [Table("Permessi2Funzione")] + public partial class Permessi2FunzioneModel + { + public string CodPermesso { get; set; } = null!; + public string CodFunzione { get; set; } = null!; + public string? Readwrite { get; set; } + + public virtual PermessiModel PermessiNav { get; set; } = null!; + } +} diff --git a/MP.AppAuth/Models/PermessiModel.cs b/MP.AppAuth/Models/PermessiModel.cs new file mode 100644 index 00000000..007aee5c --- /dev/null +++ b/MP.AppAuth/Models/PermessiModel.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.AppAuth.Models +{ + // + // This is here so CodeMaid doesn't reorganize this document + // + [Table("Permessi")] + public partial class PermessiModel + { + public PermessiModel() + { + Permessi2FunzioneNav = new HashSet(); + } + + public string CodPermesso { get; set; } = null!; + public string Url { get; set; } = null!; + public int? Gruppo { get; set; } + public int? Numero { get; set; } + public string? Nome { get; set; } + public string? Descrizione { get; set; } + + public virtual ICollection Permessi2FunzioneNav { get; set; } + } +} diff --git a/MP.AppAuth/Models/UserDirittiModel.cs b/MP.AppAuth/Models/UserDirittiModel.cs new file mode 100644 index 00000000..f097342e --- /dev/null +++ b/MP.AppAuth/Models/UserDirittiModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.AppAuth.Models +{ + + // + // This is here so CodeMaid doesn't reorganize this document + // + [Table("DIRITTI")] + public partial class UserDirittiModel + { + [Column("USER_NAME"), MaxLength(50)] + public string UserName { get; set; } = ""; + [Column("COD_CDC"), MaxLength(50)] + public string CdC { get; set; } = ""; + [Column("COD_MODULO"), MaxLength(31)] + public string Modulo { get; set; } = ""; + [Column("COD_FUNZIONE"), MaxLength(31)] + public string Funzione { get; set; } = ""; + [Column("VALUE"), MaxLength(255)] + public string? Valore { get; set; } = ""; + } +} diff --git a/MP.AppAuth/UserAuthContext.cs b/MP.AppAuth/UserAuthContext.cs new file mode 100644 index 00000000..8a0823aa --- /dev/null +++ b/MP.AppAuth/UserAuthContext.cs @@ -0,0 +1,104 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using MP.AppAuth.Models; +using NLog; +using NLog.Fluent; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.AppAuth +{ + public partial class UserAuthContext : DbContext + { + #region Public Constructors + + [Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.")] + public UserAuthContext() + { + } + + public UserAuthContext(IConfiguration configuration) + { + _configuration = configuration; + try + { + // se non ci fosse... crea o migra! + Database.Migrate(); + } + catch (Exception exc) + { + Log.Error(exc, "Exception during context initialization 01"); + } + } + + public UserAuthContext(DbContextOptions options) : base(options) + { + try + { + // se non ci fosse... crea o migra! + Database.Migrate(); + } + catch (Exception exc) + { + Log.Error(exc, "Exception during context initialization 02"); + } + } + + #endregion Public Constructors + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + string connString = _configuration.GetConnectionString("MP.Land.Auth"); + if (!string.IsNullOrEmpty(connString)) + { + optionsBuilder.UseSqlServer(connString); + } + else + { + optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=MoonPro_Anagrafica;Trusted_Connection=True;"); + } + } + } + + #region Public Properties + + public virtual DbSet DbSetUserDiritti { get; set; } = null!; + + #endregion Public Properties + + #region Protected Methods + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(entity => + { + entity.HasKey(e => new { e.UserName, e.CdC, e.Modulo, e.Funzione }); + + entity.ToTable("DIRITTI"); + }); + + OnModelCreatingPartial(modelBuilder); + } + + #endregion Protected Methods + + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + private IConfiguration _configuration; + + #endregion Private Fields + + #region Private Methods + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + + #endregion Private Methods + } +} \ No newline at end of file