|
|
@@ -0,0 +1,106 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Text;
|
|
|
+using System.Configuration;
|
|
|
+using System.IO;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using CG67.FicheCollege.Domaine;
|
|
|
+using CG67.FicheCollege.Interface;
|
|
|
+using System.Security.Principal;
|
|
|
+using CG67.FicheCollege.Tools;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+namespace CG67.FicheCollege.Entrepot
|
|
|
+{
|
|
|
+ public class EntrepotFichiersTravaux: IEntrepotFichiersTravaux
|
|
|
+ {
|
|
|
+ public IList<FichierTravaux> GetByCodeRNE(string codeRNE)
|
|
|
+ {
|
|
|
+
|
|
|
+ IList<FichierTravaux> result = new List<FichierTravaux>();
|
|
|
+
|
|
|
+ //fichier.Fichier=@"067003P";
|
|
|
+
|
|
|
+
|
|
|
+ //On récupère la liste des fichiers
|
|
|
+
|
|
|
+
|
|
|
+ string[] files = (GetProtectedFiles(ConfigurationManager.AppSettings["FileRepository"],
|
|
|
+ "*.*",
|
|
|
+ ConfigurationManager.AppSettings["ImpersonationDomain"],
|
|
|
+ ConfigurationManager.AppSettings["ImpersonationUser"],
|
|
|
+ ConfigurationManager.AppSettings["ImpersonationPassword"]));
|
|
|
+
|
|
|
+ //Pour chaque fichier, on affiche un lien de téléchargement
|
|
|
+ foreach (string file in files)
|
|
|
+ {
|
|
|
+// result.Add(new FichierTravaux (file));
|
|
|
+ string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], codeRNE + ".xls");
|
|
|
+
|
|
|
+ if (file == filePath)
|
|
|
+ result.Add(new FichierTravaux (file));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+// fichier.Fichier = @"\\public\publicng\1PAT\12DIMG\1230SGEPI\PPM\Fichiers DSI (fiches collèges)\0670003P";
|
|
|
+ // result.Add(fichier);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Récupère une liste de fichiers en utilisant un compte d'impersonification
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path">Le chemin du répertoire</param>
|
|
|
+ /// <param name="searchPattern">Le filtre de recherche de fichiers</param>
|
|
|
+ /// <param name="domain">Le domaine du compte à utiliser</param>
|
|
|
+ /// <param name="username">Le compte à utiliser</param>
|
|
|
+ /// <param name="password">Le mot de passe du compte à utiliser</param>
|
|
|
+ /// <returns>La liste des fichiers dans un tableau</returns>
|
|
|
+ private string [] GetProtectedFiles(string path, string searchPattern, string domain, string username, string password)
|
|
|
+ {
|
|
|
+ IntPtr token = IntPtr.Zero;
|
|
|
+ WindowsImpersonationContext impersonatedUser = null;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //On crée un jeton avec l'utilisateur devant accèder à la liste des fichiers
|
|
|
+ bool result = Impersonation.LogonUser(username, domain,
|
|
|
+ password,
|
|
|
+ LogonSessionType.Interactive,
|
|
|
+ LogonProvider.Default,
|
|
|
+ out token);
|
|
|
+
|
|
|
+ //Si le jeton est récupéré
|
|
|
+ if (result)
|
|
|
+ {
|
|
|
+ //On récupère l'identité Windows
|
|
|
+ WindowsIdentity id = new WindowsIdentity(token);
|
|
|
+
|
|
|
+ //On change d'identité
|
|
|
+ impersonatedUser = id.Impersonate();
|
|
|
+ //On récupère la liste des fichiers
|
|
|
+ return Directory.GetFiles(path, searchPattern);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new ApplicationException("Echec de récupération du jeton d'authentification.",
|
|
|
+ new Exception(Marshal.GetLastWin32Error().ToString()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new ApplicationException("Erreur non gérée lors de la récupération de la liste des fichiers.", ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ // Finalement on termine le changement d'identité
|
|
|
+ if (impersonatedUser != null)
|
|
|
+ impersonatedUser.Undo();
|
|
|
+ // On supprime le jeton
|
|
|
+ if (token != IntPtr.Zero)
|
|
|
+ Impersonation.CloseHandle(token);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|