Adding Roles to the API - A TimCo Retail Manager Video

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the timko retail manager course this course focuses on real-world application development in this video we're going limiting who can do what by adding in user roles this will prevent just anyone from using our register and will allow us to further lock down those endpoints we just created for only administrators now patreon members don't forget to head over to my patreon site to get the source code if you aren't on patreon and are interested in getting source code for each tIMCO retail manager video head on over inside with a $5 per month level or higher to get the source code this course is closing in on the date when we convert it or a.net core so make sure you're subscribed to this channel so you get alert each time a new video comes out you won't want to miss it when you do convert over we might even throw a party and there might be some type of livestream event we'll see okay so let's move on over to visual studio we want to do is you want to lock down the trm Data Manager or API while lock us down so it not everybody who's authorized can do stuff so let's just make sure we know what's happening right now so we're going to launch this do you want how a comparison to where you want to get to this is always a good first step to take is if you're doing something make sure you know the starting spot before you try and work on at the end spot so you know what changed because maybe it didn't work to begin with but didn't work to begin with then changing something might not be the only thing you have to do so let's make sure it works first so we got a login using Tim I am Tim Cory calm as you can see we're on our sales register or sales page where we can perform actions we can add things the cart we could check things out we could remove things in the cart this means that my user has access to this page and to these actions but maybe not everybody should have access to it one of the questions that came up is well if anybody can can register and then just start using stuff why we ever do registration because that's just useless anybody can do this and yes at our level right now it is not entirely useless but someone because it does take away the anonymity or someone they have to login as them so we know that Tim did this but at the same time we're saying that as long as you logged in then you can do anything you want so at our controller level if we look at let's just say the the sales controller we have the authorized attribute but that's it so as long as you're authorized you can do everything you can post you can get and the same thing is true for the other controllers actually this controller nope this one has authorized inventory authorised that's all they have is authorized so we want to get beyond this because things like the inventory that's not something that everyone should have access to and maybe those different levels for who can do what so let's set up some role based authorizations and then let's talk through what the best practices are for this now you may see tutorials where what they do is they write some code to put in some initialization information into their database in order to create an admin user off the bat we're not gonna do that we've already created a user who has no roles but we're going to do is manually add some roles so I go over to my sequel server object Explorer and go a database and the reason I do this is because I'm not a big fan of putting code in that's gonna run once I rather do it by hand so make sure it's right then have code that might create something that's a backdoor I didn't remember so you have tables and if you look at their roles table let's do the view data here there's nothing in the roles table next let's look at user roles and there's nothing there either but notice it says user ID and role ID so those are the two things in that table let's close out these these classes so this is we're going to do for modification we're going to add some roles to the role table and the role is just a name and we read make sure there's no spaces in these names I'm gonna add three roles we're not gonna go crazy here we're gonna try and keep this as focused as possible so a cashier role that's a person who can use the the sales page essentially or at least the run by somebody else sales page and so if I move off this notice you get this error here it says no rows were updated the data in Row 1 was not committed because you cannot insert a value of null into column ID and you may have seen this error in your own databases or ones that you follow on with me on and you say well what is this issue well the issue is that this ID field is not auto increment so I hit escape here to go away this this is not an auto incrementing ID in fact if we go to look at the designer for this we will see that it's an N varchar' 128 oops that's the user roles let's go rolls itself which is right here and there we go it's still an N mark Schauer 128 now the name is n varchar' 256 so what are these and large are 128 well these are the goods that that we use in the identity system so if we were to go over to the users table and look at its designer will notice the ID is an N varchar' 128 okay and if we look at the values for this we only have I think one row here but the ID itself if we expand this out there you go there is that gooood now it's not a hundred and twenty eight characters long but that is the gooood that they use for this user now goods are not like the auto incrementing IDs for identity where you can say start at one and add one each time Goods have to be generated so not something you add to the necessarily to the the create on its own so what we need to do is we need to insert something into roles and create a GU it so we're actually gonna have to write some sequel so don't get intimidated as we knocking hard let's go our table where our actually data base that level you can say right click and say new query so let's write a little sequel script that's going to be insert into DB o dot asp net rolls over half to specify which fields well as the ID field and the name field i put that in square brackets because name is actually a reserved where it's going to turn blue it won't be the end of the world but i'd prefer to have it in the square brackets and the values are going to be new ID and that's a as a method essentially and then the name of my role so the name of the role to run start with is cashier now let's add a few more so i'm going to copy this and I'll say comma and the next one and then comma the next one now some people say well why would you put the comma at the stereo line and set at the end of the line and here's why if I ever decide I want to get rid of just one of these lines now it's not gonna happen here but if I wanted to I come down here in the middle somewhere and input a comment and knows how it now skips this line entirely and goes to this line and if I comment out this line as well this will still be a valid sequel statement there's no comma at the end here so comment the beginning make it easier to comment out only certain line in your query that's just the reason why we do it that way now I do want to move it out a little bit so we don't want three cashiers that actually wouldn't work because the fact that we have to have different names for our roles they can't have two roles the same name so I'm gonna have cashier I'm gonna have manager and I'm gonna have admin and we're not going to necessarily use all of these today but I want to have these three roles to show some differences so those are three roles I'm going to have I'm going to run this insert statement now right now again roles is empty but if we run this three rows affected and if you come back over here and refresh this we now have three roles with goods for their ID so the GUI we have the name of the the role so admin cashier and manager and now I can start using them well not quite because now we have the idea of the user roles so right now there's nobody in the user ID and role ID match up and this is where when you're first starting out it's a little tricky because of the fact that we're doing this manually this is only to start the SONA setup because what we're gonna do is create some some admin screens it will allow us to to add people to roles add roles the table all these different things we can do without having to drop in a sequel we can do it through the user interface but we definitely wouldn't want to allow that to be done by anybody so we need some administrators first or at least one administrator first before we can allow before you even allow anybody to have that power so read wait to do that and we're going to do it manually at first the benefit here also gives you the opportunity to see what's going on behind the scenes so it's not just this magical things work you can actually see what's happening and why so we have our users table which let's look at the data there so this is my user ID right here I'm going to copy that it's off a scream that's okay and we can put this in for various roles so right now we have a user ID and a role ID so user ID gonna paste in my user ID let's expand that out and expand this out so there's my user ID and now your role ID well what is the role idea want give to my user name let's go to roles and view data we can see we have three admin cashier and manager let's do this for now I'm gonna say that I am an administrator I'm gonna paste this over in the role ID and we're done now my user ID is associated with the role ID of admin this is a main to many relationship so I can add my user ID again down here add a different role ID or I could add a different user ID in the same role ID as I have that's that made of many relationship so for right now we're going to leave it adjust this one role of admin now let's close all this down I'm not gonna say the query so you might want to screenshot this real quick if you had any questions on how it works so this won't be in a source code that you get okay so now we're gonna do is lets unpin this for now and we're gonna go over to our controllers and let's look at some let's go to our our inventory they believe the inventory what's called when we want to load our our user interface so let's look at at what the UI does we've got the the API and we're asking for I believe we're asking for the inventory get a sales endpoint which a sales endpoint gets the or post to sale the product oh we have product okay so product gets all gotcha so this is a little confusing I'd probably document it somewhere the inventory is of things that we've basically put in the warehouse we've bought a lot of 50 widgets as he put 50 widgets in the warehouse the product list is what products do we have and what's the total quantity of those products so if we bought 50 widgets we already had 10 warehouse the product table is gonna say we have 60 total widgets we have the 50 in the 10 so it's the product control that we're all locked down so right now we can get a list of all the products but if I were assay right here after authorize the Parente rolls equals I can lock this down so you know what only cashiers can see this now know as I put in in just text so that that's how rules work we use just tax and we say cashier there are ways we may investigate it let me know if you want to go down that path put a comment down below if your if you want go this way we may may investigate get away from these magic strings and putting some type of you know in oome or a constant in there instead it's a little more tricky it's not as simple as putting in a new here and the reason why is because this cannot be a calculation it has to be a constant but we can do things like this comma space admin actually no space comma admin which means that it's the cashier role or the admin role this is the or so that you can't do the constant as easily there are ways of doing it but again let me know if you want to go down that route or if you want to keep it simple okay so I'm gonna say cashier for now and that's it now you remember I didn't put myself the cashier role I put myself in the administrator role how is that going to work let's find out so we're gonna start this up remember it did work so we now have a baseline for if we have any problems we're gonna find out now let's also look at our let's movies on it where I look at the output here just we can see any issues that pop up I'm gonna say login and I get this unauthorized exception now it drops me the code and it says hey the sales viewmodel we've got an unauthorized exception that's because we're in code and I didn't handle it in any way I didn't capture the exception that says okay if you're unauthorized and what do you do so that's something that I need to do deal with but right now what's saying is it actually crashed the application and it says you can't go on you know your heart will not go on this is this is done because you're unauthorised ideally and when I come back not in this video but another video where it come back and add in that exception handling to deal with what happens in your authorized it shouldn't be the crashes the application it should be that it says hey you can't do that let's not send you this page or let's not even keep the option of viewing a sales page because you don't have authorization to see it now the question may come up well but you're in ministry er it shouldn't administrative you'll do everything and the answer is probably yes but that's not how we set it up the system has no clue who's an administrator and who's not doesn't know that admin means super user role that have can do everything it just knows I have three roles you're not in the role that can use this or can list these products so therefore you can't see this so another another option is we could either do this and say well a cashier or an administrator but it seemed like also that cashier manager and administrator should be able to do this now you see how this is getting a little complicated because now we're having to add all these different roles that might have to be able to be a cashier now we're gonna put that in pause for a minute and make sure this works and then we're going to talk about alternatives to this more complex style here so once the API loads up I'll log in and now I can see my my list again my my sales page comes up I am authorized to start working with us that's because I am in the admin role and that's one of the roles that's authorized to use the product controller so that's one way of going about it is by putting multiple roles on top of an item but there may be a case for not allowing even an administrator to be a cashier if you think about it a cashier might be somewhat of a trained role it's not something where you just step in say because I'm the boss I get to do this there's some training involved in how to a credit card or how to refund a proctor you know whatever those things may be and so maybe that not everybody should be a cashier even if they are higher up in the food chain at the organization so if they make sense that maybe this is just a cashier role now obviously list properly use other places so that might be possible for this specific thing but let's just pretend it is if we say well only cashiers can do this the alternative is to come back over to the user roles and the roles table say okay well this is the cashier role now copy that go to user roles let's expand these out and say I'm gonna creates an authorization for this new role and oops I can't go up one I had to go over to my user ID table and I have to grab this ID and paste the idea it's now my ID is both administrator and a cashier it's both of those things so now even though there's only cashier on this role if I were to run this it should be that I can use the sales page which I login yes I can use the sales page even though I'm an administrator because I'm also a cashier so that's how that's the alternate way of applying these permissions is you could either say here's two or three roles that can do this and we'll do that but the alternate way of doing it is a say or the people that can do this need to be add to that role as well so now I'm a cashier administrator I'm both so let's look at a couple of these endpoints just to cover some other things that we need to do sales page who can buy stuff you know what this should probably cashier only roles equals cashier like so because a cashier should only be able to actually post a sale the way meant though sales report that's that's not really right because a sale report may be all administrators we look at that but not cashiers so now you have a quandary what do you do here for for this where you have this be cashiers and then should be administrators well this actually comes somewhat simple let's copy this and paste it right above post and take away this roles right here and now you happy to use the sales sales control at all but we don't specify what role you have to be in just authorised but now down here for posting a sale you have to be a cashier do that but what about this get sale report well I think we should say that that one should be an admin and maybe a manager - so you're an admin or a manager you can get a sale report that seems most logical let's look at the rest of our controllers to make sure you've locked things down appropriately so let's close out of everything let's go our values controller well values controller is actually that dummy controller we never really got rid of so let's do that now let's delete that user controller user controller is get user by ID that's where we figure out who that user is so it gives us more information about who they are that's just authorized as long as you're authorized you can get the information about yourself and that's all that would be is yourself just getting your user ID and then looking up your information by your user ID so that's just for yourself that's fine we've already touched the sales controller and the product controller now the inventory controller inventory who can get the inventory and who can post inventory well it seemed like again we have this idea of a split responsibilities because I would think that the manager or the admin can give the inventory list or maybe everybody can get inventory who knows but I think that let's start with just manager and ministry because they're the ones managing the warehouse we're at and who can post to the inventory seems like for right now an admin an admin can buy products and put them in the warehouse let's leave with that eventually it may be that we have a warehouse manager or an inventory manager or something like that where there's a person that's doing the purchasing and maybe create a role for them we'll see next let's go to the home controller and the home controller just shows us the home page and maybe she'll give you that and we have the account controller and this is authorized but then notice if we come down here there are certain actions that are actually allowing anonymous so let's come down here change password set password and external login remove login there we go external login allow anonymous so here's the other option as well is if you have a whole controller that is authorized you can pull things out and say well actually that one's not in this case the get external login is allow anonymous you can post your your information order login and of course you're not authorizing to you've actually done it so therefore has to be anonymous until then and there's a couple of those as well there's not allow anonymous so there's another option for us if we aside that certain things you feel will buy everybody because it may be that we creates a webpage where you can sell things directly to users so we have fee authorized maybe they authorize probably but they don't have to authorize in order to list the products so nothing logged in in order to see what we have a cell they just have to be logged in in order to buy something but again not in a role necessarily just hey as long as you log in will let you buy something but to list our products nobody need to be logged in or a see what we have to sell so there's various different options here the one option we didn't cover I do want to talk about even though this is a start to finish course I do want to try and teach as I go and one of the things that we're missing here or not using is the idea of additive roles meaning maybe you have to be a in order to to post here you happy both an administrator and a warehouse worker because only warehouse workers who are admins can add to the inventory so it's not just that you're a a warehouse worker and it's not just your administrator you think both how would you do that because this is an or well the way I do that is to do like this so now you have warehouse worker and admin so you have two both of these roles in order to do a post here not just one of these roles that's the the and relationship okay so those who are ANDed together these two are board meaning either or just want to show you that we don't have a warehouse worker role and I don't think we need one at this point now one of the questions that comes up is well how do you put roles inside out of roles and you don't really and and we have to do is take a step back from this and really realize what we're doing here yes this is a bit simplistic in his management but the same time there's a ton of power here that you're probably not thinking about so with these roles we can use these authorization rules to say yes you can or no you can't and either or or an and if you have an and so you can do a lot on this side and the other thing is we have that many to many relationship so that you can add to as many roles as you want but it gets even more granular in the fact that once we're inside of these calls you can perform more actions now let's go look at the sales controller so the sales controller let's close all of this the sales controller we're getting the user ID so we know who the user is now they're a cashier but we're saying okay specifically who are you so that can record the sale to just you so if we were reporting data so if this into a post was a get if we were reporting data back we could say okay you have to get cashier but just meter a cashier doesn't mean to get every cashiers information you only at your own well that's where this comes in we can find out specifically who the person is and then yes they have to get cashier but we're only gonna return their data for just them not all cashiers so that's how you limit it even more but we also have the idea and I really discourage this but we do have this option here so if we said authorizes be additive notice that role we can say users equals and get set who the user is don't do this okay this would be if you lock it down to a specific user that is just I can't think of a reason why you want to use that it's just to lock down use roles work everything against a role even if you have to create a role that's super admin the only you go in that's fine just create that rule that way later when you decide to sell the company you can put somebody else in that super user group and take yourself out and now they have full control and access and you don't without changing anything in your code you don't give your password of somebody else or anything else ugly like that so there is option as well again I suggest you avoid but with roles between the role ID and the user ID you can limit things so that this filters out the the big chunks okay so it says if you're not a cashier you don't get through this trainer but still let's just say 18 people got through because there are cashiers well yes but if we're returning data here we can say but only return your data not all 18 cashiers data so that is helpful there's one more thing and that is if we get the sales report but we're asking for either an administrator or a manager you might want to know down here well who are they are they a manager or are they an administrator you can get their user ID that's cool but we really want to know hey if they're an administrator maybe you get more on your report than if you're a manager a manager only get certain things on the report what you can do is you can say request context dot principal dot is in role and that will check to see if the person is in the admin role and tickets you could wrap this let's just do real quick some pseudocode so if requested principal is in the role administrator do admin stuff otherwise do manager stuff or you could do the eltz if and say well not admin it's actually manager and then do manager stuff instead and that else if it protect you against if you add a third role up here but that way we can we can change things based upon the role you're in we can know what role you're in based upon that is in role method that we have access to so a lot of power here we can use we're gonna use just the tip of it with just cashier admin privates all but we've added in manager just in case and right now there is no way to programmatically or at least on the friend to add a person to a role so if a user signs up right now and they they register their username and password and it says great you're signed in they still can't do anything because everything is now locked down by role so yes they're authorized cool we can know that you exist and maybe if we had some some methods like buy your own stuff you know for an online web page then they could do those things but for right now they can't do anything even though they can log in so you can create their own they can register for themselves but they can't do anything because they're not one of these roles in order to get in way these roles right now we have to go in a sequel and add that user to the role manually we'll create some some nice admin pages that will allow us to add people to roles easily without having to go to sequel but for right now that is roles so things coming up we need to do probably pretty quick and it may do both at once we'll see how long it takes one we need to work on our DPF front end because right now it's crashing when we're not authorized and that's not cool we need to make sure we give the user a message that says hey you don't have access to this and maybe not even show them the the pages they don't have access to or don't even send them there and then also we need to add that nice user interface for adding in roles and adding roles to users so we'll do that in upcoming videos but for now this is the adding roles to users and let's go ahead and commit our code let's make sure you saved everything that we have and let's review we've got the inventory controller which the difference is is authorization product controller which we've changed from authorized to authorized role of cashier the sales which we add the authorized roles for each of the end points we deleted the value controller and of course we have our changes in RCS per aaj itself because we deleted that file let's stage those and say add user role at usuals add authorization by role to the endpoints sounds good enough we're going to commit to stage we're going to go over to the sink page and I can do a fetch there's nothing I need coming down because we do have you on screen here so therefore we're going to do a push okay once that's done we've now committed our source code pushed up to the server and we are done okay thanks for watching and as always I am Tim quarry [Music] you
Info
Channel: IAmTimCorey
Views: 11,264
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, course, training, how to, tim corey, C# course, C# training, C# tutorial, C# app start to finish, timco, timco retail manager, wpf, asp.net, .net core, asp.net mvc, autofac, c# solid, c# solid principles, dependency injection, webapi, authentication, api authentication, web api authentication example c#, web api authorization and authentication, web api, c# programming
Id: uCWDyluPY8I
Channel Id: undefined
Length: 36min 43sec (2203 seconds)
Published: Mon Sep 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.