Results 1 to 5 of 5

Thread: Security Help!

  1. #1
    Join Date
    Sep 2009
    Posts
    6

    Exclamation Unanswered: Security Help!

    Afternoon All,

    I need to be able to hide control buttons for certain users, however due to the setup of our company network i am not able to use the security wizard, i have already built the logon page which references to my login details table, also on this table i have a column which shows access level,

    is there anyway that depending on the level i have set in there that i can hide/disable a control box(button)

    regards

    dave

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    There are several solutions.

    One of the easiest is to use the Tag property of each control of a form and assign a security level to it. When the form opens it gets the security level of the current user (several possibilities here too) then show or hide its control according to the user's security level.

    Ex:
    Code:
    Private Sub Form_Load()
    ' or Private Sub Form_Open(Cancel As Integer)
    
        Dim ctl As Control
        Dim lngSecurityLevel As Long
        
        lngSecurityLevel = GetSecurityLevel ' GetSecurityLevel is a function returning the security level of the current user
        For Each ctl In Me.Controls
            If Val(ctl.Tag) <> 0 Then   ' Controls with a zero-length or non numeric beginning tag are ignored
                If ctl.Tag >= lngSecurityLevel Then
                    ctl.Visible = True
                    '
                    ' Could be ctl.Enabled = True
                Else
                    ctl.Visible = False
                    '
                    ' Could be ctl.Enabled = false
                End If
            End If
        Next ctl
            
    End Sub
    Another more complex solution would consists in using a table that would hold a reference to each control subject to permission settings and its security level.
    Have a nice day!

  3. #3
    Join Date
    Sep 2009
    Posts
    6
    sorry i must be a bit of a idiot but im confused what to do!!

    i have a tbl of user ids,passwords and security level needed and thats all so im a lil bit confused!

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    OK!

    Let's imagine that there are two buttons on Form1: Button1 and Button2 that can be visible only if a user has a security level of 4 or higher.

    Set the Tag property of those buttons to 4, write a function GetSercurityLevel that will return the security level of the current user.

    Let's imagine (again) that your security table (named Tbl_Security) has a column named SecurityLevel and a column named User and that you're using the NT (local or Active Directory if it's in a domain) security system to identify the user. The code in the class module of Form1 (the module "attached" to Form1) would be something like this:
    Code:
    Option Compare Database
    Option Explicit
    
    Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Function GetSecurityLevel() As Long
        
        GetSecurityLevel = Nz(DLookup("SecurityLevel", "Tbl_Security", "User = '" & GetNTUser & "'"), 0)
        
    End Function
    
    
    Function GetNTUser() As String
        
        Dim strUserName As String
        Dim lngUserNameSize As Long
        Dim lngApiResult As Long
        
        strUserName = String(255, 0)
        lngUserNameSize = Len(strUserName)
        lngApiResult = GetUserName(strUserName, lngUserNameSize)
        strUserName = Left(strUserName, lngUserNameSize - 1)
        GetNTUser = strUserName
        
    End Function
    
    Private Sub Form_Open(Cancel as Integer)
    
        Dim ctl As Control
        Dim lngSecurityLevel As Long
        
        lngSecurityLevel = GetSecurityLevel
        If lngSecurityLevel > 0 then 
            For Each ctl In Me.Controls
                If Val(ctl.Tag) <> 0 Then
                    If ctl.Tag >= lngSecurityLevel Then
                        ctl.Visible = True
                    Else
                        ctl.Visible = False
                    End If
                End If
            Next ctl
        Else   
            Cancel = True
        End If
    
    End Sub
    Have a nice day!

  5. #5
    Join Date
    Sep 2009
    Posts
    6
    the user is prompted to enter a user name and password

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •