Author Topic: NEW!!! User Management Plugin  (Read 5650 times)

timryder

  • Jr. Member
  • **
  • Posts: 84
  • Still trying to figure it out
    • View Profile
NEW!!! User Management Plugin
« on: December 12, 2017, 12:23:55 PM »
Hey guys,

Per a request from a customer I created a new UserManagement component of my own.  There are plenty of tutorials out there on how to do this in VB.net but for those who aren't code savvy I thought I would share mine and offer any help I can to those who reply to this thread.

First off here is what it looks like on MY user interface.

ScreenShots:




Features:
  • Login/Logout function
  • Local .csv database of users and passwords
  • 3 Levels of privileges Operator, Supervisor, Administrator
  • Passwords are stored as Encrypted strings
  • Public events for Login Logout each page can subscribe to
Usage:
Start by adding the file(s) to your project. Right click on your project name and select "Add --> Existing Item"


Please make sure to select all of the files in the .zip which were including as an attachment in this post.


Login:
Next somewhere on your project add a Button control to your form and double-click on it to add the handler for the Click event and show the related code.
The is a function in the UserManagement Class called "Login" this function when called for the very first time ever on a computer will check if the local database file (.csv) exists yet on the computer.  If it does not then the function will create it and also add the "Admin" user to the database.  The "Admin" user has the default login of User: Admin   Pass: password.  Note that the Username is NOT case sensitive but the Password however is.

Add a call to this Login function using code similar to this.
Code: [Select]
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        frmLogin.ShowDialog()
        If Not UserManagement.currentUser = "" Then
            'Do Something here to handle the login succesfull... maybe update a label.text control with the UserManagement.currentUser.
            'There will also be a UserManagement.currentPrivilage which will be set based on the credentials of the user which logged in.
        End If
    End Sub

When the function is called it will display the Login Dialog.  The Login Dialog will handle the call to the login function within the UserManagement.vb. When the login is successful the function will set 2 shared variables in the UserManagement class for the "currentUser" and "currentPrivilage".  It will also fire a Shared Event for the Login.

It is recommended that you have each form which will dynamically be changing based on the User logged in and their Privileges to subscribe to the public events of "userLoggedIn" and "userLoggedOut".  Here is how to handle those events.

In the Load event for the Form in question, add a handler for each event.
Code: [Select]
        AddHandler UserManagement.userLoggedIn, AddressOf handleUserLoggedIn
        AddHandler UserManagement.userLoggedOut, AddressOf handlerUserLoggedOut

Next create the Subs for each of those events or you can copy this code and add it to your own.
Code: [Select]
    Private Sub handleUserLoggedIn()
        If UserManagement.currentPrivilage = UserManagement.Privilage.Administrator Or UserManagement.currentPrivilage = UserManagement.Privilage.Supervisor Then
            Me.tbManualVinEntry.Enabled = True
        End If
    End Sub
    Private Sub handlerUserLoggedOut()
        If Not UserManagement.currentPrivilage = UserManagement.Privilage.Administrator Or Not UserManagement.currentPrivilage = UserManagement.Privilage.Supervisor Then
            Me.tbManualVinEntry.Enabled = False
        End If
    End Sub

In these examples I am checking the value of the privilege for the current user who is logged in. That is stored in the UserManagement.currentPrivilage variable. Depending on the privilege of the user you can create different methods to turn On or OFF various user controls on the form.  Each form can subscribe to these events and alter the controls within accordingly.

Logout:
Finally back on the main form where the Login button resides.  Add another button for the Logout functionality. Then in the Click event of the button add the following code.
Code: [Select]
        Dim myUserManagement As New UserManagement
        Call myUserManagement.Logout()

Adding a User:
To add a user, somewhere in you project create a button to add a user. Double click on this button to access the click event code.  Add the following code.
Code: [Select]
    Private Sub btnAddNewUser_Click(sender As Object, e As EventArgs) Handles btnAddNewUser.Click
        frmAddUser.ShowDialog()
    End Sub

This will open up the frmAddUser so you can properly add a user to the database.  NOTE: It is suggested that the visibility property of this button be set to False by default and only enabled if the current user logged in has a Privilege of Administrator.  That can be handled by reading the Login section above.


The addUser function will check if the username already exists in the database and if so will prompt if you would like to overwrite it. This gives you the ability to just change the password easily if needed.  After you click "Add User" the function will display a messagebox to inform you if it was a success or a failure.

Deleteing a User:
In my example there is a combo box which I populate on the load event of the form of all of the users in the database. To do this add a ComboBox


To do this, add a Combo Box control to your form, set the Name property to "cboUserList" then open up the Load Event logic of the form.  Add the following code (make sure you change the name of the Combo Box control to match your project if needed)
Code: [Select]
        Me.cboUserList.Items.Clear()
        Dim userList As New List(Of String)
        Dim _userManagement As New UserManagement
        _userManagement.getUserList(userList)

        If Not userList.Count = 0 Then
            For i = 0 To userList.Count - 1
                Me.cboUserList.Items.Add(userList.Item(i).ToString)
            Next
        End If

Next add a new button control to your form and set the Text property to "Remove User".  Double click on the button to open the click event code.  Add the following code to yours keeping in mind the name of the Combo Box from the previous step.
Code: [Select]
    Private Sub btnDeleteSelectedUser_Click(sender As Object, e As EventArgs) Handles btnDeleteSelectedUser.Click
        If Not Me.cboUserList.Text = Nothing Then
            Dim _userManagement As New UserManagement
            _userManagement.deleteExistingUser(Me.cboUserList.Text)
            Dim i As Integer = Me.cboUserList.SelectedIndex
            Me.cboUserList.Items.RemoveAt(i)
        End If
    End Sub

This function will find the Username in the database and remove it.  The comboBox will also remove the item from the list.

Please let me know if you have any questions below or of something isn't clear and I will modify this post and elaborate more.

Thanks! and enjoy.

« Last Edit: December 12, 2017, 02:59:16 PM by timryder »
Still just trying to figure out this thing called Life.

Phrog30

  • Guest
Re: NEW!!! User Management Plugin
« Reply #1 on: December 12, 2017, 02:38:08 PM »
Pretty cool.  I would suggest adding security code to your controls, buttons, analogvaluedisplay, basiclabel, etc.  This way there is never any code to write, it's already there.  Just make a property that sets the user and the controls take care of the rest.  I didn't see any mention of it, but you should always have one admin and make sure they can't delete the only one that exists.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #2 on: December 17, 2017, 03:14:51 PM »
There is UserManagementIcon_64x64 missing in the zip file.

Could you possibly provide it?

Attached are a few simple images that can be used instead. They were created in MS Paint so it should be easy to edit them and make changes.
« Last Edit: December 18, 2017, 07:53:59 AM by Godra »

timryder

  • Jr. Member
  • **
  • Posts: 84
  • Still trying to figure it out
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #3 on: December 18, 2017, 10:28:32 AM »
Sure,

Here are the icons I used
Still just trying to figure out this thing called Life.

oqapsking

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #4 on: June 01, 2018, 04:32:33 PM »
very nice work

thanks a lot

i will use it


oqapsking

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #5 on: June 01, 2018, 05:35:14 PM »
i don`t know how to use it
i mean how to put permissions
for example

button 1 is visible to administrator only

how to do it ?

can any one help me how to put permissions according to the type of the account

oqapsking

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #6 on: June 01, 2018, 05:45:33 PM »
i don`t know how to use it
i mean how to put permissions
for example

button 1 is visible to administrator only

how to do it ?

can any one help me how to put permissions according to the type of the account

ok no need for this

i didnt use the handler before

oqapsking

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #7 on: June 01, 2018, 06:18:12 PM »
how to make it when i press on the (x) button in the border to exit the whole project

it only worked with me when i press on (cancel ) button


oqapsking

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #8 on: June 02, 2018, 02:49:15 PM »
hello
i cant make it work properly

what am doing is

using the handler to show or hide the tools according to the user Privilege

like this

Code: [Select]
Private Sub handleUserLoggedIn()
        If UserManagement.currentPrivilage = UserManagement.Privilage.Administrator Then
            btnAddNewUser.Visible = True
            btnDeleteSelectedUser.Visible = True
            btnDeleteSelectedUser.Visible = True
            cboUserList.Visible = True
        Else
            btnAddNewUser.Visible = False
            btnDeleteSelectedUser.Visible = False
            btnDeleteSelectedUser.Visible = False
            cboUserList.Visible = False
        End If

        Me.cboUserList.Items.Clear()
        Dim userList As New List(Of String)
        Dim _userManagement As New UserManagement
        _userManagement.getUserList(userList)

        If Not userList.Count = 0 Then
            For i = 0 To userList.Count - 1
                Me.cboUserList.Items.Add(userList.Item(i).ToString)
            Next
        End If
    End Sub

but it didnt work only the (btnAddNewUser) shows and hides according to the user Privilege

timryder

  • Jr. Member
  • **
  • Posts: 84
  • Still trying to figure it out
    • View Profile
Re: NEW!!! User Management Plugin
« Reply #9 on: June 04, 2018, 09:14:34 AM »
Did you add handlers for the Form Load event?

Code: [Select]
        AddHandler UserManagement.userLoggedIn, AddressOf handleUserLoggedIn
        AddHandler UserManagement.userLoggedOut, AddressOf handlerUserLoggedOut

replace the handleUserLoggedIn and handlerUserLoggedOut with whatever the names of your methods you want to handle the event.
« Last Edit: June 04, 2018, 09:16:22 AM by timryder »
Still just trying to figure out this thing called Life.