begin process at 2012 02 13 11:52:02
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Sécurité

 > SAVOIR SI UN USAGER A ACCESS À UN FICHIER OU UN DOSSIER

SAVOIR SI UN USAGER A ACCESS À UN FICHIER OU UN DOSSIER


 Information sur la source

Note :
9,5 / 10 - par 2 personnes
9,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Sécurité Niveau :Initié Date de création :17/07/2003 Date de mise à jour :17/07/2003 21:44:44 Vu :3 994

Auteur : dragon

Ecrire un message privé
Site perso
Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

 Description

reçoi un nom de fichier et un mask et permet de savoir si on a acces à ce dossier

j'ai pris une partie du code ici, masi j'ai du modifier beaucoup pour avoir se résultat. par contre, ça risque d,en aider plusieurs

exemple : If GetAccess(fichier, GENERIC_EXECUTE Or GENERIC_READ) Then

Source

  • Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  • Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
  • Declare Function GetFileSecurity Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, pSecurityDescriptor As Byte, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
  • Declare Function GetFileSecurityN Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, ByVal pSecurityDescriptor As Long, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
  • ' The file/security API call constants.
  • ' Refer to the MSDN for more information on how/what these constants
  • ' are used for.
  • Public Const DACL_SECURITY_INFORMATION = &H4
  • Public Const GENERIC_READ = &H80000000
  • Public Const GENERIC_EXECUTE = &H20000000
  • 'savoir si on a acces au fichier ou dossier
  • Public Function GetAccess(ByVal sFileName As String, lMask As Long) As Boolean
  • Dim strUserName As String
  • Dim lResult As Long ' Result of various API calls.
  • Dim bUserSid(255) As Byte ' This will contain your SID.
  • Dim lUserSID As Long ' Used to hold the SID of the
  • ' current user.
  • Dim lSIDType As Long ' The type of SID info we are
  • ' getting back.
  • Dim lFileSDSize As Long ' Size of the File SD.
  • Dim lSizeNeeded As Long ' Size needed for SD for file.
  • Dim sDomainName As String * 255 ' Domain the user belongs to.
  • Dim lDomainNameLength As Long ' Length of domain name needed.
  • Dim bSDBuf() As Byte ' Buffer that holds the security
  • ' descriptor for this file.
  • GetAccess = False
  • Dim temp As Integer
  • If InStr(Mid(sFileName, 2), "*") <> 0 Then
  • sFileName = Mid(sFileName, InStr(Mid(sFileName, 2), "*") + 2)
  • End If
  • 'Create a buffer
  • strUserName = String(100, Chr$(0))
  • 'Get the username
  • GetUserName strUserName, 100
  • 'strip the rest of the buffer
  • strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
  • ' Get the SID of the user. (Refer to the MSDN for more information on SIDs
  • ' and their function/purpose in the operating system.) Get the SID of this
  • ' user by using the LookupAccountName API. In order to use the SID
  • ' of the current user account, call the LookupAccountName API
  • ' twice. The first time is to get the required sizes of the SID
  • ' and the DomainName string. The second call is to actually get
  • ' the desired information.
  • lResult = LookupAccountName(vbNullString, strUserName, _
  • bUserSid(0), 255, sDomainName, lDomainNameLength, _
  • lSIDType)
  • ' Now set the sDomainName string buffer to its proper size before
  • ' calling the API again.
  • sDomainName = Space(lDomainNameLength)
  • ' Call the LookupAccountName again to get the actual SID for user.
  • lResult = LookupAccountName(vbNullString, strUserName, _
  • bUserSid(0), 255, sDomainName, lDomainNameLength, _
  • lSIDType)
  • ' Return value of zero means the call to LookupAccountName failed;
  • ' test for this before you continue.
  • If (lResult = 0) Then
  • MsgBox "Error: Unable to Lookup the Current User Account: " _
  • & sUserName
  • Exit Function
  • End If
  • ' You now have the SID for the user who is logged on.
  • ' The SID is of interest since it will get the security descriptor
  • ' for the file that the user is interested in.
  • ' The GetFileSecurity API will retrieve the Security Descriptor
  • ' for the file. However, you must call this API twice: once to get
  • ' the proper size for the Security Descriptor and once to get the
  • ' actual Security Descriptor information.
  • lResult = GetFileSecurityN(sFileName, DACL_SECURITY_INFORMATION, _
  • 0, 0, lSizeNeeded)
  • ' Redimension the Security Descriptor buffer to the proper size.
  • ReDim bSDBuf(lSizeNeeded)
  • ' Now get the actual Security Descriptor for the file.
  • lResult = GetFileSecurity(sFileName, DACL_SECURITY_INFORMATION, _
  • bSDBuf(0), lSizeNeeded, lSizeNeeded)
  • ' A return code of zero means the call failed; test for this
  • ' before continuing.
  • If (lResult <> 0) Then
  • GetAccess = True
  • End If
  • End Function
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
Declare Function GetFileSecurity Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, pSecurityDescriptor As Byte, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Declare Function GetFileSecurityN Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, ByVal pSecurityDescriptor As Long, ByVal nLength As Long, lpnLengthNeeded As Long) As Long


' The file/security API call constants.
' Refer to the MSDN for more information on how/what these constants
' are used for.
Public Const DACL_SECURITY_INFORMATION = &H4

Public Const GENERIC_READ = &H80000000
Public Const GENERIC_EXECUTE = &H20000000

'savoir si on a acces au fichier ou dossier
Public Function GetAccess(ByVal sFileName As String, lMask As Long) As Boolean

    Dim strUserName As String
    Dim lResult As Long            ' Result of various API calls.
    
    Dim bUserSid(255) As Byte      ' This will contain your SID.
    
    Dim lUserSID As Long           ' Used to hold the SID of the
                                  ' current user.
    Dim lSIDType As Long              ' The type of SID info we are
                                     ' getting back.
    Dim lFileSDSize As Long           ' Size of the File SD.
    Dim lSizeNeeded As Long           ' Size needed for SD for file.
                                     
    
    Dim sDomainName As String * 255   ' Domain the user belongs to.
    Dim lDomainNameLength As Long     ' Length of domain name needed.
    
    Dim bSDBuf() As Byte           ' Buffer that holds the security
                                  ' descriptor for this file.
   
    GetAccess = False
    
    Dim temp As Integer

    If InStr(Mid(sFileName, 2), "*") <> 0 Then
        sFileName = Mid(sFileName, InStr(Mid(sFileName, 2), "*") + 2)
    End If
    'Create a buffer
    strUserName = String(100, Chr$(0))
    'Get the username
    GetUserName strUserName, 100
    'strip the rest of the buffer
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
' Get the SID of the user. (Refer to the MSDN for more information on SIDs
   ' and their function/purpose in the operating system.) Get the SID of this
   ' user by using the LookupAccountName API. In order to use the SID
   ' of the current user account, call the LookupAccountName API
   ' twice. The first time is to get the required sizes of the SID
   ' and the DomainName string. The second call is to actually get
   ' the desired information.

   lResult = LookupAccountName(vbNullString, strUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _
      lSIDType)

   ' Now set the sDomainName string buffer to its proper size before
   ' calling the API again.
   sDomainName = Space(lDomainNameLength)

   ' Call the LookupAccountName again to get the actual SID for user.
   lResult = LookupAccountName(vbNullString, strUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _
      lSIDType)

   ' Return value of zero means the call to LookupAccountName failed;
   ' test for this before you continue.
     If (lResult = 0) Then
        MsgBox "Error: Unable to Lookup the Current User Account: " _
           & sUserName
        Exit Function
     End If

   ' You now have the SID for the user who is logged on.
   ' The SID is of interest since it will get the security descriptor
   ' for the file that the user is interested in.
   ' The GetFileSecurity API will retrieve the Security Descriptor
   ' for the file. However, you must call this API twice: once to get
   ' the proper size for the Security Descriptor and once to get the
   ' actual Security Descriptor information.

   lResult = GetFileSecurityN(sFileName, DACL_SECURITY_INFORMATION, _
      0, 0, lSizeNeeded)

   ' Redimension the Security Descriptor buffer to the proper size.
   ReDim bSDBuf(lSizeNeeded)

   ' Now get the actual Security Descriptor for the file.
   lResult = GetFileSecurity(sFileName, DACL_SECURITY_INFORMATION, _
      bSDBuf(0), lSizeNeeded, lSizeNeeded)

   ' A return code of zero means the call failed; test for this
   ' before continuing.
   If (lResult <> 0) Then
      GetAccess = True
   End If
End Function



 Sources du même auteur

Source avec Zip Source avec une capture Source .NET (Dotnet) RADIOBUTTON EN .NET SUR PLUSIEURS CONTENEURS DIFFÉRENTS
SUPPRIMER LES DOUBLONS D'UNE TABLE (VBA ACCESS)
Source avec Zip Source avec une capture Source .NET (Dotnet) PATRON ITERATEUR, TUTORIAL PATRON 4
Source avec Zip Source avec une capture Source .NET (Dotnet) [VB.NET] FORM AVEC DES SCROLLBARS AUTOMATIQUE
Source avec Zip Source .NET (Dotnet) PATRON OBSERVER, TUTORIAL PATRON 3

 Sources de la même categorie

Source avec Zip Source avec une capture Source .NET (Dotnet) CHIFFREMENT XOR PLUS ROBUSTE par dheroux
Source avec Zip CRYPTAGE MARANT par alpha5
Source avec Zip ACCÈS PAR MOT DE PASSE À FEUILLE EXCEL par mimiZanzan
Source avec Zip CRYPTER-DÉCRYPTER UN TEXTE - TEXTE CRYPTÉ UNIQUEMENT EN MAJ... par Saintache
Source avec Zip Source avec une capture FOLDER PROTECTION par hackoo

Commentaires et avis

Commentaire de isa911 le 07/04/2008 15:28:13 9/10

Ce que je cherchais depuis 3 jours !

Commentaire de mstaub le 04/10/2010 18:06:23

J'ai pas très bien compris le truc !
=> si un fichier est utilisé par une personne ce bout de prog va le signaler ?
ou seulement un accès dossier
dans un intranet ou sur une même machine ?
bon faut que j'essaie mais moi je recherche ça pour un intranet !

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,090 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales