Coding

Laravel Tutorial – ACL (User Roles) – #2 Database Setup & Migrations

  • 00:00:00 I'm going to start implementing my roles
  • 00:00:03 by starting on on the database ads way
  • 00:00:06 adding these roles setting them up in
  • 00:00:08 the database for this to work I will
  • 00:00:11 need three tables I already got one
  • 00:00:14 which is the user table which holds the
  • 00:00:16 users of course I will have a table
  • 00:00:18 holding the roles and then the third
  • 00:00:20 table will hold well kind of the matches
  • 00:00:24 where which basically will hold a user
  • 00:00:27 ID and they're all ready to match users
  • 00:00:29 with roles because this will be a
  • 00:00:31 many-to-many relationship one user may
  • 00:00:34 have multiple roles and one role can of
  • 00:00:37 course be assigned to multiple users
  • 00:00:39 therefore we need free tales I'm going
  • 00:00:42 to use the artisan command to do this so
  • 00:00:46 in my terminal video I'm going to write
  • 00:00:48 PHP artisan make model and first I want
  • 00:00:53 to create a role model
  • 00:00:55 – am to also create a migration file as
  • 00:00:58 you can see this added this role
  • 00:01:02 PHP file this model file here and it
  • 00:01:04 added this migration file let's start up
  • 00:01:07 the migration this is the well
  • 00:01:10 boilerplate level traded for us and I
  • 00:01:12 will need two additional fields here
  • 00:01:15 both will be stringfield's a name of the
  • 00:01:17 role and well though this isn't really
  • 00:01:21 used in this application to be honest a
  • 00:01:23 description just to show how you could
  • 00:01:26 configure your role so it does all I
  • 00:01:28 need for the migration file here the
  • 00:01:32 next thing is I want to go to my role of
  • 00:01:36 PHP Maru file here and I will set up the
  • 00:01:39 relation of this role to the user and
  • 00:01:42 this will just be a public function
  • 00:01:46 which we'll call users because many it's
  • 00:01:50 many users and it will return this
  • 00:01:54 belongs to many app user this is the
  • 00:02:01 relation because as I said my role may
  • 00:02:04 assign too many users therefore it
  • 00:02:06 belongs to many users on the upper hand
  • 00:02:09 in the user model here I will well
  • 00:02:12 basically define the same but
  • 00:02:14 just called rels and this will be
  • 00:02:17 returned this belongs to many and that
  • 00:02:21 app roll now we've got this many-to-many
  • 00:02:25 relationship set up the next step is to
  • 00:02:29 set up this matching table this third
  • 00:02:32 table I was talking of so again back in
  • 00:02:35 the command line here I'm going to write
  • 00:02:38 PHP artisan make migration and then I
  • 00:02:43 will call this create user role table as
  • 00:02:48 he has created this file here this
  • 00:02:51 migration file and inside this file I'll
  • 00:02:54 have to read a little bit more than any
  • 00:02:55 other migration files but I'm just going
  • 00:02:58 to copy this here from my role migration
  • 00:03:01 file down here I'll have schema drop and
  • 00:03:06 table will be called user role here of
  • 00:03:10 course to user role I will have well
  • 00:03:13 increments time stems that is fine
  • 00:03:15 however here I will have an integer
  • 00:03:18 which will be called user ID and I just
  • 00:03:23 copied this line because I'll have the
  • 00:03:25 same for while the role ID the naming
  • 00:03:27 here is important because level will
  • 00:03:30 look for a table called user role and it
  • 00:03:37 will also look for a user and role ID in
  • 00:03:40 this table when we actually use this
  • 00:03:43 relationship we set up in my our model
  • 00:03:46 files here you through levels eloquent
  • 00:03:49 model so this is very important to
  • 00:03:53 understand now you could change this
  • 00:03:54 default behavior by when creating this
  • 00:03:57 relationship adding additional
  • 00:03:58 parameters here where you could say and
  • 00:04:01 let's actually do this even though it's
  • 00:04:03 not necessary here you could say okay
  • 00:04:05 the matching table is called user role
  • 00:04:08 that's just a name I gave this table
  • 00:04:10 here and as a set level will infer this
  • 00:04:14 name by default but if you have a number
  • 00:04:16 table name that's no problem just
  • 00:04:18 specify this an extra parameter and then
  • 00:04:21 as an additional parameter you can say
  • 00:04:24 well the foreign key here in this case
  • 00:04:27 we'll be well user ID also you might say
  • 00:04:32 well the other key of my row will be row
  • 00:04:36 ID now I'm just going to copy all this
  • 00:04:39 and in my role model file I'll insert
  • 00:04:43 this and of course this has to be
  • 00:04:45 changed here in this order so that's
  • 00:04:50 very important to understand to get
  • 00:04:52 right as I said wouldn't have been
  • 00:04:54 necessary here because that is the
  • 00:04:56 default level at first but whenever you
  • 00:04:58 change names does the way to set it up
  • 00:05:02 so what this out of the way the next
  • 00:05:04 thing I want to do is I want to see some
  • 00:05:07 data I want to create some default roles
  • 00:05:10 which are always in there when we start
  • 00:05:13 or create our application so as I
  • 00:05:16 already get my user table cedar which
  • 00:05:18 sets up my users I will create a new
  • 00:05:21 cedar for well my my roles and I will
  • 00:05:26 again oops
  • 00:05:27 do this in the command line here where
  • 00:05:29 typing PHP are titian make seed and then
  • 00:05:33 I will just call this role table cedar
  • 00:05:37 and the name is purely up to you here so
  • 00:05:41 now this cedar here was added and as you
  • 00:05:43 can see it's just an empty function or
  • 00:05:45 the run function and inside this
  • 00:05:48 function I will create a code which I
  • 00:05:50 want you run through upon seeding or
  • 00:05:53 yeah when I basically run the seed
  • 00:05:56 command on the artisan command-line
  • 00:05:59 interface what I actually want to seed
  • 00:06:02 here is I will well say we have the row
  • 00:06:05 for a normal user which will be a new
  • 00:06:07 role and I will import it at the top so
  • 00:06:13 we will use app role so that I can just
  • 00:06:17 write new role here and while this role
  • 00:06:21 user this user role whatever the name of
  • 00:06:25 let's say user and it will have some
  • 00:06:29 kind of description and I will just
  • 00:06:32 write a normal user
  • 00:06:38 and thereafter I will save this now I'm
  • 00:06:45 going to quickly copy this and create
  • 00:06:48 two new users the first one will be my
  • 00:06:52 my offer user here role offer another
  • 00:06:56 will be called offer and here the
  • 00:07:00 structure will be an offer and then I
  • 00:07:02 will have my admin that was the wrong
  • 00:07:04 key
  • 00:07:05 I will have my admin here role admin so
  • 00:07:11 with that we get the free roles we need
  • 00:07:14 for our application to work now the last
  • 00:07:17 thing is in the database cedar file here
  • 00:07:20 I will basically uncommented here
  • 00:07:23 because I already wrote it but we have
  • 00:07:26 to call the role table cedar and then
  • 00:07:29 thereafter the user tables here now the
  • 00:07:32 order here support because I were to
  • 00:07:34 change the change the user table cedar
  • 00:07:37 in a second because in the user table
  • 00:07:39 see the ruler very setup our users I
  • 00:07:42 also want to assign some basic roles to
  • 00:07:44 them I'm currently not doing that but I
  • 00:07:46 told you in a second
  • 00:07:47 therefore it's important to first create
  • 00:07:50 the roles so that they can then use them
  • 00:07:52 in the user table cedar which I'm doing
  • 00:07:55 now assigning roles is pretty easy first
  • 00:08:00 I'm going to retrieve my three roles by
  • 00:08:03 just saying well accessing the database
  • 00:08:05 through a lock when so with the role
  • 00:08:08 model and you're just say well my user
  • 00:08:10 model is where the name equals user it's
  • 00:08:16 a name we just defined and here we will
  • 00:08:18 get the first role and I know I only
  • 00:08:21 have one set us will fetch the only role
  • 00:08:24 I have the only user role now of course
  • 00:08:27 this will change to role offer here and
  • 00:08:31 here it will be rolled admin and here I
  • 00:08:36 need to make these changes you
  • 00:08:38 so this retrieves the roles and now to
  • 00:08:41 assign them to user is super simple
  • 00:08:44 letter well allows me to do the
  • 00:08:47 following I can access the user of
  • 00:08:49 creating here or I already created
  • 00:08:51 saved and the order is important here
  • 00:08:53 again it has to be saved to the database
  • 00:08:55 so the letter can actually create the
  • 00:08:58 relation because remember we have this
  • 00:09:00 third table where we have the role ID
  • 00:09:02 and user ID level can only create an
  • 00:09:07 entry in this table which defines the
  • 00:09:09 actual relation if we have a role as
  • 00:09:13 well as a user in the database because
  • 00:09:15 it needs the IDS of the user enter role
  • 00:09:18 so we have to save the user and
  • 00:09:20 therefore write into the database before
  • 00:09:22 assigning a role and hence creating the
  • 00:09:25 relation that's very important and I
  • 00:09:27 hope it's clear why so what I'm doing
  • 00:09:30 here is I access the roles relation with
  • 00:09:34 parentheses here because we're still in
  • 00:09:37 the query I'm not accessing at it as a
  • 00:09:40 property where I then want to let's say
  • 00:09:42 loop through all the roles this user has
  • 00:09:44 but instead I want to kind of create a
  • 00:09:48 join statement if we were to translate
  • 00:09:51 this into SQL so I use parentheses to
  • 00:09:55 say now connect the user with the roles
  • 00:09:59 table here and then I use the attach
  • 00:10:03 method the rule provides me with the
  • 00:10:08 eloquent model and this allows me to
  • 00:10:10 attach a role because I'm accessing
  • 00:10:13 rules here to this user and the role I
  • 00:10:17 want to attach is role user now of
  • 00:10:19 course attach tests only work with roles
  • 00:10:22 and users it would be the same behavior
  • 00:10:24 if you have posts and comments for
  • 00:10:26 example this basically just says or
  • 00:10:30 tells angular angular wearable that you
  • 00:10:34 create a relation and to create this
  • 00:10:37 entry I was just talking about in this
  • 00:10:39 third table not just copied this here to
  • 00:10:43 my admin where I'm going to assign role
  • 00:10:46 admin and finally to my offer here at
  • 00:10:51 the bottom where I assigned role offer
  • 00:10:56 okay so now let's write all the data to
  • 00:10:59 the database for running PHP our it is
  • 00:11:00 my great – – see
  • 00:11:04 to also run the seeding after the tables
  • 00:11:07 have been created looks good and now
  • 00:11:11 what I did here or with what I do have
  • 00:11:13 here is I have my well this is a sequel
  • 00:11:18 Pro which allows me to connect to my
  • 00:11:19 database and have a look at the contents
  • 00:11:22 and as you can see I got my rolls user
  • 00:11:26 roles and users table in the users table
  • 00:11:28 I got my free users and roles table I
  • 00:11:31 got the free roles I set up and then the
  • 00:11:34 user role table you can see I got the
  • 00:11:36 assignments of user 1 has Row 1 and if
  • 00:11:39 we have a look user one is the normal
  • 00:11:42 user not the admin not the offer and it
  • 00:11:45 has to roll one which is the normal user
  • 00:11:47 role so this works this is how we set up
  • 00:11:50 our database how we see it data and in
  • 00:11:53 the next part we'll have a look at well
  • 00:11:56 the actual work how we protect our
  • 00:11:59 middleware and how we restrict access
  • 00:12:02 based on these roles which just set up
  • 00:12:04 see you there
  • 00:12:05 bye