Thursday, February 24, 2011

VB Programming - My first step creating User object

I am in the middle of my work and i got bored in a way. So I thought of visiting my blog. Then in a sudden I thought of writing about what I am doing in my work. Yes, why not. In some way, I could share my processes and at the same time, who knows, an expert might visit my page and give/suggest better ideas.


My Program


My system always checks for validation especially the user's access level. There are sub processes in the system that is only accessible by an admin or supervisory account. If I dont make an object of my logged user, and if the access level is needed, I would have to search through my database to retrieve the user's access level every time the the system goes through that process. Thus what I did was, when the user has been granted access to the system, the system creates an object that would hold the information about the user - name, password, accesslevel,etc. But before you can create an object you would have first to create a class of the user.


Create a user class - this is the class that would hold the information about the logged user that you would want to be easily retrieved:




Class clsUser
      Public UserName as string = ""
      Public FullName as string = ""
      Public Designation as string =""
      Public AccessLevel as Integer = 5     'Read-Only User


      Public Sub New(Byval UserName1 as string)
              'I have a shorter version here but since that this is the beginning we will go through th e longer version; 

             
                

                Dim cn As New SqlClient.SqlConnection("Data Source=" & strServername & ";Initial Catalog=" & strDBasename & ";Integrated Security=True")
                Dim cmd As New SqlClient.SqlCommand("SELECT * FROM systemusers WHERE Username = '" & Username1 & "'", cn)    

                Dim myReader As SqlClient.SqlDataReader

                cn.Open()
                                                         
               myReader = cmd.ExecuteReader
                                      
               If myReader.HasRows Then
                       MyReader.Read
                       UserName = myReader("Username").ToString
                       fullName = myReader("Fullname").ToString
                       Designation = myReader("Designation').ToString
                       AccessLevel = iif(myReader("AccessLevel").ToString = "",0,CInt(myReader("AccessLevel").ToString)
               End If
                myReader.Close()
                myReader = Nothing
                cmd = Nothing
                cn.Close()
      End Sub      
End Class

Create a user object - this would hold the information of the logged user

I added this inmy Public Module
             Public objLoggedUser as clsUser

Initialize the user object

       objLoggedUser = new clsUser(txtUsername.text)

Use the user object

I use the user object everytime I check for the access level say in the accessing of the Admin Menu (Level 0)
      

Sub cmdLogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogs.Click    
            If objLoggedUser.AccessLevel = 0 then
                   'allow access
            else
                   MsgBox ("User do not have access to this module.",MsgBoxStyle.Information,"Information")
            End If
End Sub


or to add a record in the database

Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click    
            If objLoggedUser.AccessLevel < 5 then
                   'allow access
            else
                   MsgBox ("User do not have access to this module.",MsgBoxStyle.Critical,"Error")
            End If
End Sub



I also use this object in other modules/methods e.g. change of passwords, edit personal information, etc.


Hope this could be of help to anyone. More ports to come. Ciao
Privatecn As New SqlClient.SqlConnection(connectionString)

No comments:

Post a Comment