Building a C# Winforms Application - Setup User Roles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video we're going to start looking at building functionality that will help us to manage our users now up until now we would have hard-coded one user we called it admin and we hard-coded the password but in reality we're not going to expect that every time we have a user or when we have multiple users for our application that the user is going to have to bring up the database and hard-code it and find the hashing version of the password like what we did and manually put those in that's not the reality what we want to do is create an interface where the user can click and preferably the admin user of course can click manage users get a similar management window like we've done for vehicle listing and for entire records on that list sort of the users in the database who wouldn't want to display their passwords of course and that allows them to maybe deactivate a user versus adding another user and maybe resetting a password or something like that so those are the functionalities that we would love to put in based on what is on the screen however I already modified my main window to have the button manage users and I already created the new form manage users so you can pause the video right here and go ahead and add this new menu option and the new form and write the code that would launch the window you can pause the video try it yourself what I'm still going to go through and show you exactly what I did so in the main window design I just added a new option manage users I didn't put anything underneath it because I want this option to just come up with our when clicked rather come up with the new window to manage the user so I added that new menu item and then I double clicked it to give us the click event and then in the click event I did the usual thing you could even just look at previous code that we've written I didn't put in the check to see if the window is open already so I can put in that check just to make sure that it won't launch twice alright so I'm checking if it is open and I'm checking if manage users is open then if it is not open sorry then you can launch so that we will only get one instance of manage users I already went through and added the new form and remember to add a form you go to solution expert your right-click you say add and then you select form and then you go ahead and add this new form to your project I call the manage users if you want to change the name that's fine as long as you know what name you used and that the name is relevant I'm just going to make some cosmetic changes because you can see that the window title is did well I didn't change it so I'm going to change the text to manage space users so it's more readable more user friendly in its appearance and then on this form we're going to have pretty much the same thing that we had on the other form so I'll just go back to manage maybe manage vehicle listing that design or rental records whichever one because we want to have the same kind of layout so we have that grid I can actually remove this refresh button but just for now we'll have the label the refresh button the grid and all of the buttons so I'm just going to copy all of these controls and then go over to my new window manage users and paste them and then expand the window as much as I need to to facilitate these new controls let me just do that quickly alright I rearrange them and then let me change the text here from manage for entire records to manage users and then because I'm reusing the score of course the the names what is presented on each button one the text and then two the names need to be changed so this one needs to say add new user instead of record alright and then I'm going to change the button name from bTW and add records bTW and add user so I'm going to go through and do that for each of the buttons so let me just do them quickly while on screen so that's edit record and then that's going to be beaten edit user or you know what for edit user I'm actually just going to say reset password so we're changing this functionality from editing the user to reset because for the purpose of this function or this application rather we just want the admin user to be able to reset a user's password that's the only editing quote-unquote that should be able to happen right and then I'm going to change delete to deactivate user so instead of delete user we're going to do deactivate and I'll show you exactly how we can facilitate the activation versus deletion right so BTN the activate user alright and then know that we changed those out I can just do my click event so I'll just double click each button to get those click events let me close that window for no I don't need it double click reset password and the activate user alright so now that we've done those cosmetic things to the form I also want to make some changes to the database table so I brought up my management studio SQL management studio logged into my instance went into my database which is current and then I clicked my table that said and chose design sorry so now I'm in the design of the users table and I'm going to do a few modifications well one modification really and that is to set an active flag so what I'm going to be doing here is I'll be saying is active alright and then based on the naming convention I've been using up until this point anytime you see is something that's me saying I want a boolean in SQL boolean will be bit so the datatype for boolean here is bit and then so this is going to say is the person active yes or no so if it's true then that person is active is if it's false the person isn't active and I will not allow no here either but then there might be I'm going to get an error if I try to save well not alone oh so alone oh and save and then I'll go back and make a modification the reason for the error would be that I already have a user that does not have any beta for this so if I chose to not alone oh then it would have been a problem because then I already have a record that has no so then it would be a data type or constraint mismatch right so moving on I have the user and I'm going to add another table so I'm going to right-click tables go to new go to table and then this one I'm going to call roles so I'm going to have ID and then this is going to be our primary key which is identity so it auto increments and then I'm going to have name a name is just going to be varchar' 50 so name here represents the name of the roles so when we talk about roles and we just save the table and I'll say roles so we talk about roles it means that what role does this user play in this application is this person an admin is this person maybe a data entry clerk is this person maybe just a supervisor you know and then based on the role that they play we can determine what functionality they have access to or what actions they can complete all right so I have users I have roles and then I have to have another table that links the two so I'm going to create another table and then I'm going to give this one an ID and then this is going to be the author incrementing ID and this is really going to be what we call a linker table because we're going to do a many-to-many meaning many users may may have many roles so I'm going to have a user ID and then this is going to be int and then I'm going to have a role ID which is also int alright so this means that any user right now only have admin and that admin user as an idea of one and then let's say I would have two rules so I'm going to have admin and I'm going to have user so you have an admin role I have a regular user role or maybe I can have three one for supervisors whatever the case may be but the point is that when a user with whatever ID gets a or an record goes into this step or other with a particular user ID and a particular role ID that is the association between this particular user and that particular role all right so I'm going to save this table and call it user roles next I'm going to have to create a relationship between the user roles table and the other two tables so because I have user ID that's a foreign key to the users table so I have to go right-click sorry I did that kind of quickly let me do it again I have to right-click in the design view then go to relationships and then well it would have been blank so I click Add and then I'm going to choose the tables and column specifications click this ellipsis button over to the right-hand side and then I'm going to say my primary key table is the users table and I want the ID column from this primary key table and then I want to link it to the foreign key called user ID so that creates a constraint there and then I'll do the same thing just click Add create another one using the ellipsis and then this one would be for the roles table with the ID is connected to the role ID column in the user roles table then click OK click close and then I can save and then it will take a while then it will make sure that I know that I'm about to modify these three tables I can click yes all right and once those changes are saved then we can go about putting in some manual changes so the fact is that the application is yours you can always create your own management screens for whatever data it is like we've been doing so far we've done it for the managing of the car entire records we've done it for the management of the cars we can do it for the users it's up to you to know how granular you want to get with that because then some people allow you to create your own rows in the back end well in the application itself and then they allow you to assign users to the different roles for yourself right so I will show you how to do the assignment but then using the same principles if you want you can allow to create the roles in the application itself but for this course I'm going to just do it manually so I would love for you having done it's in the application that's my challenge to you that you share your code with me I mean I would love to see how it is that you accomplished it and we can you know learn together but for now I'll just do it manually so I'll right-click on roles go to edit top 200 now I know that I only want two roles in my application you might be building your application for a bigger context than mine but then within this context I only want an administrator role so I can have an administrator user or many administrator users which you want to of course you know be careful with that and I'm going to have data entry Clark all right as a matter of fact I'm going to extend this table a bit and I'm going to put in another column so I only have name and ID let me go back to the design and I'm going to put in short name all right so I'm going to put in short name and this one will also be in varchar' 50 can be varchar' 50 very very similar data types what but it's invert or varchar they act very similarly so it's okay if you had either one alright so for roles let me get back to this so let me just close this design view go back to Rose edit top 200 and then for the short names I'm going to put in like a short code name so literally have the name administrator but then the short name could be admin the short name for data entry clerk could be just Clark and then maybe you just have view only so this person wouldn't have any access to actually change data to actually click add or delete or edit or anything like that so view only will only be able to click and see the data that is in the grid right so I'll just save you alright so we have the role names we have the short names and you see the relevance of the short names in a few so those are my rules all right and I'm going to do one more manual addition here so I'm doing these manual additions because we already have the data and I want to show you how it will work before we start building it all right so the user ID here would be the user ID for the existing user which let me just confirm what that is if I select top - sorry tap top 1,000 rows then I see my one admin user and the ID here is one I see is activism no all right so the user ID here is one and then the role ID for administrator let me just select top two cantos and again would be one so that means in user roles user ID one has role ID one and I just changed learn to commit that and so we know that our user role our admin is in the user role administrator with the short name admin now that we've made those modifications oh one more modification my bad has skipped that part I need to let the administrator user know that he is active right so I'm just going to right click users go to edit top 200 rows and then change is active to 1 right so 1 or 0 in database if you say one it's true if you say 0 it's false right so it will always say true so you can write the words or you can just assign the value 1 and the bit will automatically know what is true or false all right so the user admin user is active so let's minimize alright and then in our project we need to update our data context diagram so I need to go over to my admx diagram and then we see all of the the three tables that we left here with what we need to make some modifications so I didn't make any modifications to these two I'm not going to delete those but once I made a modification to well the one I made a modification to would be user so I'm just removing user so I just clicked it and I press delete on my keyboard it's confirming I say yes I do want to delete it and then I'm going to right click and then update model from database which is then going to prompt me do you want to add new table so I'm just going to click tables and I added a few tables just no and then click finish and then give it some time and once that operation is done then you will see your new tables appearing so I created three well two new tables and made an adjustment to the user table and you see the relationships that exist so both user and role are related to the user role table all right know that I have the ability to determine what role the user is in at the time of login I need to be able to see which role this person is in so at the time when I confirmed the person so we know that we have our login form that handles that logic and as soon as I find it here's my login form right so we know that on the button click event where we said login we did that hashing we computed the hash and then we went ahead and said get us the user is there the user and then the user would have said yes or no right so when they said yes right then we would have called the main window passing in an instance of the login window etc now we need to extend this functionality because know I need to know what role this user is in all right so I already have the user object right I already know the user object but remember that I had said that once you get the user object or once you get an object from the database you can actually access the related data directly from this object so I can actually declare a very here and save our roll right roll is equal to and then I can say user because I just got user here so and if it's in the else that means the user is not null so that means that user presumably will have a roll I record in the user roll table so if I say user dot then you're going to notice that I have access to oh my bad so I broke my own rule every time you make an an update to your diagram you should build this I've skip that step so you can see the side effects of this I'm glad this happened so when I just type that code let me just undo and go right back so when you fail to build or fail to update the diagram and then you're ready to write code you're going to see that the code may not reflect the changes so remember that we had added at least one new column to our user table so that means our user objects should have access to that new column at least so we're still seeing the old the same three columns that we had before we change the database before we updated our diagram all because we didn't build so what I'm going to do is just comment out this line and then do ctrl shift and B and allow the project to rebuild all right so our build is completed and let me just uncomment this line and then ctrl and shift after the full stop just to see and there we go so we're seeing here ID ID is active sorry is active password username and then we're seeing user roles so that is our relationship right so remember I said that once something is related to our a foreign key for a tables in another table then we can automatically access the list alright so for car rental the rental record is associated with one car but a car is associated with many records so if you have a car object then you can see all of the records that are associated with it so in this case we have the user and we can see all of the roles that this user might so it could be a situation where you have multiple roles you have multiple users one user can have many roles that is the situation that we facilitated in our database design a simpler way to do that could have been to just add role a role name to the user but then that's kind of limiting because then if you modify the role or you want a new role then you're going to have to go through every single user with that role and make a modification right so if we have that linker table then it's easier to manage the different roles the different users and which role that user might have but within the context we only have one row per user so even though we're facilitating that a user can have multiple roles in our context in this application we're only facilitating one role per user all right so I can say user dot and then I'm getting user roles but I only want the one rule that this person has so I can just save dot and then use link to get the first or default rule that is associated with this user so first or default no I don't need a lambda expression so up here I use the lambda expression because at a condition upon which I wanted the first or I wanted the first record that made this condition in this situation I don't have any condition I just want the first one so I'm saying get me the user and the least quote-unquote list of rows and get me the first one because we know that we only have one rule anyway when we get the first one then we're getting the one role all right so that is a nice quick way to just get the one role that this user has and this is embodied in the object called role all right now when we look at role it is going to be of type user roles so then I can use role and say dot and I can get the details on the role or the details on the user that that this record is associated with second say role dot role and then I can get the name or the short and the reason I created short name is that it would much easier to check the short name than the name so name is the nice official title improving readability but the short name is what we the programmers will be using in the background alright so now that I have gotten the role that this user who is about to login or who has just logged in now that I know the role that they have all right what I'll be doing is passing the well let me say that I only want the short name so I don't want the whole rule object I want the short name so I'm just going to save our role name is equal to role dot and then get the table rule and then say dot again and then get then sharp name all right and then I'm passing this short name into so shortening oh sorry I'm passing role name my bad not sure name so I'm passing so I'm all right let me you know just because I just confuse myself so let me just be clear role short name all right is equal to roll our role object which is users rule dot roll which is our table dot the short name column and then this variable is going to be passed into the main window constructors and I need to modify this constructor so I'm holding down control and then I click it jumps over to that constructor we know it's this one that we need to modify something that you can also accept a string called role short name all right so when we login and we're launching our mean window we're passing in the login object and we're also passing in the role short name so because I'll be using this role all over the place I'm going to do it similar to how I did the login but this time I'm going to make it public so I'm going to say public and string and then I'm going to give it underscore role name all right so my me in window is going to have a publicly accessible property called roll name which will allow any window that needs to know what rule is the currently logged-in person in right so I can just get that and then I'm going to initialize it so I'm going to call this row underscore roll name is going to have the value of the roll short name all right so and then I mean this principle can be used anywhere so it could be that you are going to be passing in maybe user data because you wanted a welcome screen or you know you want some little taskbar at the bottom to say who is logged in you know you could have passed that information just the same way so I could have actually just pass over the entire user object into the main window and then use that data how I need it to there are so many ways you could have done this right well right now I'm just interested in knowing the role of the person all right and then we can move along with that so the person's role now let's do a simple operation so let me just show you exactly why we need the role name and what we can restrict what if anybody who is not admin should not be able to click manage users and you can just think about it we have three users we have a three-year-old sorry we have admin we have our administrator we have data entry clerk and we have view only nobody who is not in the administrator role should be able to come and click manage users it should either not be visible or visible and inactive right so the best way to make sure that when the main window loads we know one what role this person is in and then two we determine what they can and cannot do would be to say unload we want to make these decisions right so I'm going to just create a form loading event on the main window so I just go to properties go over and here it is load so I'm just going to double click in there I have the main window load event in the main window load event I'm going to say if roll short name is equivalent to and this is why I said we can use the short name and sorry I actually have underscore rule name so role short name is unique to just the constructor but then the rest of the application knows underscore rule name right so I'm going to say if underscore rule name has the or is not equal to admin let me say not equal to admin so it is not equal to the short name admin all right so obviously this wouldn't be so clean if somebody went and updated the shortening that would break it all together right but let's just work with it for now so if the roll name is not equal to admin then we want to make a decision the decision we want to make is that the manage users button and let me just find its correct name so at the school with the properties for that button and I see that the name for this button is manage users to strip menu items I can copy that just copy the name so if the role is not equal to admin then I want to say this control by its name dot enable is equal to false all right so you have properties that other it disabled its server but we have that visible also so if you wanted to hide it you could say that visible is false or it could say that enable is false meaning they will see the button but they it's not enable they can't click it they can't interact with it but let me use the visible cause I think that screener instead of show and then something that doesn't work let's just hide it all together so they would never guess that they could have or that that functionality will exist it right so I'm saying that if the role is not equal to admin then this is false all right so let me click start and let's test that out all right so we see that we still see our manage users and that's because all the user we have is the admin user so when it got to this if statement was loading you would have said is the role admin if it is or if it is not admin sorry then don't show it so I'm going to create another user that is not an admin user so this one I'm just going to say user so and then I'm going to use the same password and then this is going to give me an error so let me copy the password first then say user password and is active is true all right and then I'm going to try that again so let me try and login with this new user all right so I'm logging in with user password click login and then it said something went wrong so let us see exactly what our error is so let me go over to my login code let's see if it's something so it's something on the click event let me put a breakpoint here let's try that again and then it gets over to the hashing of the password that's fine let me step through using f11 and then it runs the query where it's saying where if the username is equal to user and the password is equal to the hashed password good let me step through again then it says if the user is equivalent to null else okay so it moved along now we're getting the rules I suspect that my error might be here so rule came back as null all right that's what's wrong so I added the user and I did not set the record inside of the user rules right and you can see even if you hover over it in debug mode that account is zero so that was a problem so let me just click continue and it will so exception handling is why I didn't break right you just said something went wrong please try again and then I'm going to go back to my database and I need to add the new user so my new user is ID 3 let me go to user rules table edit top 200 and I'm going to say that user with the id3 has the roll idea of two because we know that roll ID one is admin so we want it to be anything but admin so I just added that database and I'm going to try and run the application again trying click login and I'm going to remove the breakpoint and press f5 for it to continue that something still went wrong so clearly we still have an arrow that we need to fix so let's try that again so I'm moving the breakpoint down to the if statement check all right so let us try the login operation again so it's good that we're going through this experience together because I think I may have missed a step but I don't want to just tell you the answer let's see it together so f11 and then we get the roll also the roll is still no so it still tell me that the user roles so let me just make sure it's getting the right user so when I hover over the user object I see that the ID is three and everything else is good but then it's still saying zero for the user roles alright so let me stop come out of debug mode and let's just recalibrate it and see and make sure everything is okay so I have user roles I added the user ID three so it just make sure that it was the right user so user ID three and then sorry of some tabs open all right so user ID three and two all right so let me just do some debugging and lets you know what's up all right so I already do a stop it and start it and try it again and try and debug and there was literally no errors so I'll explain what happened then so user password we click login we're going to hit the break point I didn't move the breakpoint but as you can see user Rose is now counting to one if I hover over it it is now seeing one I literally made no changes I think what happened is that when it during runtime it did not go back to the database every single time kind of like to cuss teach like a slap shot of the data at the time of the query or at the time of debugging and it was using that set of data say when we made the changes it didn't reflect in the application at the runtime so what we've done so far should work if it doesn't you can always let me know but I'm just going to remove this breakpoint and I'm just going to continue because everything should work as so we envisioned it and then when I press f5 to continue notice that the menu is not showing manage users so that's because the role that the person who has logged in the role associated with them is not admin so the role is Clark alright if I had left the debug the breakpoint then you see that a rule short name is still seeing Clark all right so then that is host so what we did was we got the role for the person logging in we transferred that role data over to the main window and then in the main window what during load we well we set up a load time event and we said when you load check if it is in admin roles so whatever it is you don't want the non admin persons to be able to do if you had ten many options and they should only be able to access to you can follow the same pattern and just make it not visible just you know what they deactivated if they make it not visible the point is that they can't do it
Info
Channel: Trevoir Williams
Views: 6,655
Rating: 4.9230771 out of 5
Keywords: jamaica, developer, develop, teach, learn, tutorial, music, software, development, open source, php, web, website, application, .net, .net core, core, framework, trevoir, williams, family, house, car, pets, how to, keyboard, guitar, udemy, skillshare, sql, mysql, microsoft, mssql, postgresql, cloud, azure, heroku, wordpress, jquery, ajax, c#, android, package management, programming, xampp, wamp, server, git, github, source control, community, free, hosting, apache, visual studio, visual studio code, ide, bootstrap
Id: 4yfAWVic3is
Channel Id: undefined
Length: 34min 31sec (2071 seconds)
Published: Tue Mar 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.