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 hardcoded one user.
We call it admin and we hardcoded 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 hardcoded 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 on users get to a similar management window like we've done for vehicle listing and
for entire records one that lists sold off the users in the database.
Who would 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 user 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 the ah when clicked rather come up with the new window to manage a user.
So I added that new menu item and that I double clicked it to give us the 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 windows open already so I can put in that check just to make
sure that it won't launch twice.
All right.
So I'm checking if it is open and I'm checking if money to 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 explorer
you right click you say ad and then you select form and then you go ahead and add this new form to your
project.
I call it 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.
Well I didn't change it's on with the change of text to manage space users so it's more readable more
user friendly in its appearance.
And then on this forum we're going to have pretty much the same thing that we had on the other forums
so I'll just go back to manage maybe manage vehicle listing that design our entire records whichever
one because we want to have the same kind of labels 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.
Just do that quickly.
All right.
Rearrange them and then let me change the text here from management records to manage users and then
because I'm using this code of course the names what is presented on each button one that takes and
then two the names need to be changed.
So this one needs to say add new user instead of record.
Right.
And then I'm going to change the button name from beat in AD record to beat an AD user someone to go
through and do that for each of the buttons.
So let me just do them quickly while on screen.
So that's at its record.
And then that's going to be beaten at its 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 or other way
you just want the admin user to be able to reset user's password.
That's the only editing quote unquote that should be able to happen.
Right.
And then I went to change delete to the Activate 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.
All right.
So Beatty in the TV user All right.
And then no that we change those old I can just do my click events just little click each button to
get those click events and we close that window for No I don't need it.
They will click reset password and deactivate user.
All right.
So now that we've done those cosmetic things at a forum I also want to make some changes to the database
table.
So I brought up my management studio Cisco management studio logged into my instance went into my database
which just car rental and they interact take to my table that said and chose design.
Sorry.
So now I'm in the design of the user stable and I'm going to do a few modifications while one modification
really and that is to set an active flag.
So what I'm going to be doing here is I'll be seeing is active.
All right.
And then based on the naming convention I've been using up until this point any time you see is something
that me seeing I want a boolean in school boolean will be beat.
So the data type for boolean here is it.
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.
If it's false as the person isn't active and I will not alone know here either but then there might
be I'm going to get an error if I try to save while not following and also alone no and save and then
I go back and make a modification.
The reason for the error would be that I already have a user that does not have any data for this.
So if I chose to not Lowenthal then it would have been a problem because then I already have a record
that has not.
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 directly tables go to new go
to table and then this one I'm going to call the rules somewhat of I.D. 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 and name is
just going to be via char 50.
So name here represents the name of the rules.
So when we talk about rules I mean just save the table and see rules.
So we talk about rules.
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 rules and then I have to have another table that links the to someone to create
another table and then I'm going to give this one an idea and then this is going to be the auto incrementing
I.D..
And this is really going to be what we'll call a linker table because we're going to do a Many to Many.
Meaning many users may have many rules someone of a user I.D. and then this is going to be INT.
And then I went to have a rule I.D. which is also INS.
All right.
So this means that any user right now live admin and that I'd been using as an idea of one and then
let's sandwich of two rules someone to have admin and I'm going to have user.
So you have an odd mineral an app or a regular user rule or maybe a kind of three one four supervisors
whatever the case may be.
But the point is that when I user with whatever I.D. gets assigned or an record goes into this table
or other with articulate user idea and a particular rule I.D. That is association between this particular
user and that particular role.
All right.
So I want to see this table on call it user rules next I'm going to have to create a relationship between
the user rules table and the other two tables so because of user I.D. That's a foreign key to the user's
tables have to go click.
Sorry I did that kind of quickly let me do it again.
Have to right click in the design view.
Then go to relationships and then.
Well it would have been blank.
So I click ad 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 user stable
and I want the I'd call home from this primary table.
And then I want to link it to the foreign key.
Call the user I.D..
So that creates that constraint there.
And then I'll do the same thing just click Add create another one using the ellipses.
And then this one will before the rules table with the idea connected to the role i.e. the column in
the user rules table then click Okay.
Click close and then I can save.
And then it will take awhile.
Then it will make sure that I know that I'm able to modify these three tables I can click.
Yes.
All right.
And once those changes are saved then we can go both to 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 into our 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 rules in the back.
And what in the application itself and then they allow you to assign users to the different rules 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 them to create the rules 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 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 the rules go to edit top 200 now I know that I only want two rules 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 a data entry clerk right.
As a matter of fact I'm going to extend this table a bit and I'm going to put you in another column.
So I only have name and I.D..
Let me go back to the design and then when to put in sharp name.
All right.
So would you put in sharp name and this one will also be in via char 50 can be var char 50 various very
similar data types.
What.
But it's in virtue of Arch I act very similarity so it's okay if you had either one all right.
So for rules I mean get back to this.
So let me just close this design view.
Go back to rules at it top 200 and then for the sharp names I'm going to put in like a shark put name.
So literally of the name administrator but then the short name could be I'd mean the shortening for
data entry clerk could be just clerk and then maybe you just have you only.
So this person wouldn't have any access to actually change data to actually take add or delete or edit
or anything.
That so view only will only be able to click and see the data that is in the greater it so just save
you.
All right so we have the roll names and we have the short names and you'll 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 editions because I already
have the data and I want to show you how it will work.
Before we start building it's old.
Right.
So the user I.D. here would be the user I.D. for the existing user which let me just confirm what that
is.
So if I select up to sorry to top 1000 rules then I see my one admin user and I.D. Here is one I see
is active is no.
All right.
So the user I.D. Here is one and then the role ie the for administrator.
Let we just select top two on those and again would be one.
So that means in user rules user I.D. one has rules I.D. one and I just change line to commit that.
And so we know that our user rule our admin is in the user or administrator with the short name admin
not oh we've made those modifications.
Oh one more modification on my but I skipped that part.
I need to let the administrator user know that e he is active right.
So use going or right click.
Users go to edit top 200 rules and then change is active to 1.
Right so 1 or 0 in database if you say a 1 it's true if you say 0 it's false.
Right.
So it will always ritualistic and write the word jury can just assign the value 1 and the beat will
automatically know what a straw falls.
All right.
So the user admin user is active so let's minimize all right.
And then in our projects we need to update our data context diagram.
So I need to go over to my e DMX there.
GRAHAM And then we see all of the the three tables that we left here with.
But we need to make some modifications so I didn't make any modifications of these two.
I'm not going to delete those but the ones I made on my 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 emperors delete on my keyboard is confirming I say yes I do want to delete
it and then I'm went to right click and then updates model from database which is then going to problem
to me.
Do you want to add new tables I'm just going to click tables.
I know I added a few tables just now 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 or two new tables and made an adjustment to the user table and you see the relationships
that exist.
So both user Andrew and I related to the user rule table.
I know that to have the ability to determine what role the user is in at the time off log in.
I need to be able to see which rule this person is inside at the time.
When I confirmed the person so we know that we have our logging farm that handles that logic and as
soon as I find it here's my log in form right.
So we know that on the button click event where we said log in.
We did that hashing we computed the hash and then we went ahead and said gets us the user is there the
user.
And then the user would've 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
log in window etc..
Now we need to extend this functionality because no 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 variable here and say var url All right roll is equal to and then I can
say user because I just got user here.
So and then if it's in the L's that means the user is not known.
So that means that user presumably will have a rule recording the user rule table.
So if I say user dot then you're going to notice that I have access to all my above say I broke my own
rule.
Every time you make an update to your diagram you should build the site.
Skip that step so you can see the side effects of this.
I'm glad this happened so when I just typed that code let me just undo and go right back.
So when you fail to build or fail to build the diagram on or near it it's a write code you're going
to see that the code may not reflect the changes sermon without we had added at least the one you call
them to our user table.
So that means our user object should have access to that new column at least.
So we're still seeing the all the same three columns that we had before we changed the database before
we updated our diagram all because we didn't build.
So what I'm going to do is a comment on this line and then do control shift and B and allow the project
to rebuild.
All right so our build is completed.
And let me just on this line and then control and shift after the full stop just to see.
And there we go.
So we're seeing here.
I d I d active sorry is active password user name and then we're seeing user rules so that is our relationship
right.
So remember I said that once something is related to r a foreign T for a table is in another table then
we can automatically access the list.
All right.
So far a car into the rental record is associated with one car but our 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 have.
So it could be a situation where you have multiple rules you have multiple users one user can have many
rules that is the situation that we facilitated in our database design a simpler way to do that could
have been to just add rule our own name to the user but then that's kind of limiting because then if
you modify the rule or you want a new rule then you're going to have to go through every single user
with that rule and make a modification right.
So if we have that Linker table then it's easier to manage the different rules the different users and
which rule that user might have.
But within the context we only have one rule per user.
So even though we're facilitating that user can have multiple rules in our context in this application
we're only facilitating one rule per user.
All right.
So I can say user dot and then I'm getting user rules but I only want the one rule that this person
has so I can tell safe dot and then use the link to get the first R default rule that is associated
with this user.
So first are defaults.
No I don't need a lambda expressions up here I use a lambda expression because I had a condition upon
which I wanted the first or I wanted the first record that met 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 list quote unquote list off rules 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 roll.
All right.
So that is a nice.
Quickly it does get the one rule that this user has and this is embodied in the object called roll.
All right.
No.
When we look at rule it is going to be of type user rules.
So then I can use rule and say Dot and I can get the details on the rule are the details on the user
that this record is associated with second C rule DOT rule and then I can get the name or the Sharpe
name and the reason that created a short name is that it would be much easier to check the Sharpe name
than the name.
So name is the nice official title improving readability but the short name is what we the programmers
would be using in the background.
All right.
So now that I have gotten the role that this user who is able to log in or who was just logged in.
Now that's I know the rule that they have.
All right.
What I'll be doing is passing the.
Well let me say that I only want the shark name so I don't want the whole rule object I want to short
name.
So I'm just going to say var rule name is equal to rule dot and then get the table rule and then say
dot again and then get then shark name.
All right.
And then I'm passing this shark name
in two so a shirt name.
Oh sorry.
I'm passing Roland in my bed.
Not sure anymore need some passing so I'm all right.
Let me in or just because I just confused myself.
So let me just be clear role shark name all right is equal to rule our are object which is users rule
that rule which is our table not the shark.
Name column and then this variable is going to be passed into the main window construct us and I need
to modify this construct to someone you don't control and then I click it jump so what's that constructor
we know it's this one that we need to modify.
So I'm saying that you can also accept a string called Rule shark name.
All right.
So when we log in and we're launching our main window we're passing in the log in object.
We're also passing in the rule shaft name so because I'll be using this rule all over the place on when
to do it similar to how I did the log in but this time I want to make it public.
Someone to say public and string and then I'm going to give it the underscore rule name.
All right.
So my main window is going to have a publicly accessible property called a rule name which would allow
any window that needs to know what rule is the current a loving person in right.
So I can just get that and then I'm going to initialize it.
So I'm going to call this rule on the squirrel name is going to have the value of the rule shark name.
All right.
So.
And then I mean this principle can be used anywhere so it could be that you're 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 see who is logged in.
You know you would have passed that information just the same way as I could have actually just possibly
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 it.
But 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 rule
now let's do a simple operation.
So let me just show you exactly why we need the rule 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 Sara.
We have admin.
We have our administrator we have data entry clerk and we have you only nobody who is not in the administrator
role should be able to come and click.
Monday's users it should either not be visible or visible and inactive.
Right.
So the best way to make sure that when the main window lords we know one what rule this person is in
and then to we determine what they can and cannot do would be to see unload.
We want to make these decisions.
All right.
So I'm going to just create a form loading event on the main windows so I just go to properties go over
and here it is load.
So I'm just going to double click in there.
However the main window Load event in the main window Load event.
I'm going to see if roll sharp name is equivalent.
And this is why I said we can use the sharp name and sorry actually have underscore all the names so
roll Rosado name is unique to just the constructor but then the rest of the application knows underscore
rule and name.
Right.
So I want to say if underscore or the name has the R is not equal to admin they miss it not equal to
admin so it is not equal to the sharp name admin.
All right so obviously this wouldn't be so keen if somebody went on updated the short name that would
break it all together.
Right.
But lets us work with it for a..
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 manager uses button.
Let me just find its correct name.
So just go over to the properties for that button and I see that the name for this button is manage
users to strip menu items I can copy it at just copy the name.
So if the rule is not equal to admin then I want to see this control by its name docs enable is equal
to falls.
All right.
So you have properties that allow it to be a bit.
So remember we have dot visible also.
So if you wanted to hide it you could see a visible is fours or you could see a dot enabled as far as
meaning they would see the button but it's not enable that kind of clicking the content directed it.
But let me use the visible because I think that's cleaner instead of shoot and then something that doesn't
work let's just hide it altogether.
So they would never guess that they could have or that that functionality existed right.
So I'm saying that if the role is not equal to admin then this is false side.
So let me click Start I listed that's out.
All right.
So we see that we still see or manage users and that's because all the user we have is the admin user.
So when it got to this statement when it was loading it would have said is the rule 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 went to use the same password and then this is going to give me an error so I make a
copy of the password first then see user password and is active is true all right.
And then I'm going to try that again.
So let me try to log in with this new user.
All right.
So I'm logging in with user password to log in and then it said something went wrong so let us see exactly
what our error is Selimi go over to my log in code and 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 a lot of the hashing of the password.
That's fine.
Let me step through using a few Levin and then it runs the query where it's sitting where if the user
name is equal to user and the password is equal to the hashed password.
Good and then we step through again and then it says if the user is equivalent to no else.
OK.
So it moved along.
Now we're getting the rules.
I suspect that my arrow might be here.
So rule came back as No.
All right.
That's what's so wrong.
So I added the user and I did not set the record instead of the user rules.
All right I ends 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 it didn't break right.
He 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
users I.D. three.
Let me go to user rules table it Top Two hundred and I want to say that user with the I.D. three has
the rule I.D. of two because we know that real I.D. one is admin.
So we want it to be anything but admin.
So I just added that to the database and I'm going to try and run the application again frankly log
in and I want to remove the breakpoint and press F5 for it to continue.
That's something soon 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 though into the if statement check.
All right.
So let us try.
The log in 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 if you live in and then we get the roll out.
So the rule is still no.
So it still to me that the user rules.
So let me just make sure it's getting the right user.
So when I hover over the user object I see that the I.T. is three and everything else is good but then
it's still seeing 0 4 the user rules.
All right.
So let me stop out of debug mode and let's just recalibrate and see and make sure everything is OK.
So I have user rules I added the user I.D. 3.
So just make sure that it was the right user so user I.D. 3 and then sorry of some tabs open.
All right so a user I.D. 3 and 2.
All right.
So let me just do some debugging and let you know what's up.
All right.
So I all I did was stop it and start it and try it again and try and debug.
And there was literally no error.
So I'll explain what happened then.
So user password we clicked plug in we're going to hit the break.
I didn't move the breakpoint but as you can see user rules is no counting to 1 if I hover over it it
is not seeing one I literally made no changes.
I think what happened is that when it's during a runtime it did not go back to the data base every single
time it kind of like took a staged like a snapshot of the data at the time of the query or at the time
of debugging and it was using that set of data saved when we made the changes it didn't reflect in the
application at 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.
So we envisioned it.
And then when I press if I have to continue I noticed that the menu is not showing manage users.
So that's because the role that the person who was loved in the role associated with them is not admin.
So their role is clerk.
All right.
If I had left the debug the breakpoint then you'd see that a rule a short name is still seeing Clark.
All right.
So then that is hollow.
So what we did was we got the rule for the person logging in.
We transferred that rule data over to the main window.
And then in the main window what during load we use.
Well we set up a load time event and we said when you load take if it is in add minerals so whatever
it is you don't want the non admin persons to be able to do if you had 10 menu 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 with a deactivated they make
it not visible.
The point is that they can't do it.