This episode is for members only

Sign up to access "Laravel Teams" right now.

Get started
Already a member? Sign in to continue
Playing
11. Basic roles and permissions setup

Episodes

0%
Your progress
  • Total: 4h 36m
  • Played: 0m
  • Remaining: 4h 36m
Join or sign in to track your progress
01. Introduction and demo
4m 49s
0%
02. Setup with Pest
4m 2s
0%
03. Building the user teams relations
6m 5s
0%
04. Creating a personal team when registering
6m 3s
0%
05. Leaving all teams when an account is deleted
3m 55s
0%
06. Tracking the current team
4m 50s
0%
07. Showing team details in the UI
2m 55s
0%
08. Switching to another team
7m 24s
0%
09. Authorising team switching
6m 53s
0%
10. Updating a team name
10m 48s
0%
11. Basic roles and permissions setup
5m 26s
0%
12. Team roles and permissions middleware
9m 18s
0%
13. Authorising current team updates
3m 43s
0%
14. Testing team permissions through HTTP requests
3m 58s
0%
15. Leaving a team
10m 50s
0%
16. Displaying team members
8m 42s
0%
17. Making team members look better
5m 3s
0%
18. Removing a team member
15m 18s
0%
19. Preventing self removal from a team
4m 7s
0%
20. Storing invitations
13m 31s
0%
21. Validating invitations
6m 38s
0%
22. Authorising team invitation creation
6m 22s
0%
23. Displaying invitations
3m 16s
0%
24. Revoking invitations
12m 46s
0%
25. Sending an invitation email
13m 6s
0%
26. Accepting an invitation
12m 22s
0%
27. Displaying a modal to change a member’s role
10m 31s
0%
28. Updating a member’s role
9m 5s
0%
29. More authorisation and checks for role changing
10m 14s
0%
30. Fixing up the email sending test details
49s
0%
31. Fixing and validating email addresses for invites
1m 32s
0%
32. Tidying up @can directive checks
3m 25s
0%
33. Detaching roles when removing users
5m
0%
34. Adding an extra layer of protection to the team middleware
6m 35s
0%
35. Getting related models through teams
5m 37s
0%
36. Building a helper to access the current team
10m 47s
0%
37. Getting all related models through all teams
7m 15s
0%
38. Creating new teams
13m 7s
0%

Transcript

00:00
There are several options when it comes to roles and permissions packages for Laravel, but I like to use the Sparxy Laravel permission package.
00:06
This also supports team functionality as well, which of course is what we're working on. So let's just take this episode to get this installed, have a look around, but more importantly get this initially set up with team functionality. Okay, let's head over to the installation in Laravel section,
00:23
and let's go down to the composer require command and just pull this directly into our project. Before we do anything else, it's really important to make sure that we enable team functionality. So let's go ahead and publish the migration and the config file first of all. Let's head over to the create permissions table migration and have a look,
00:45
and you'll notice several references to teams in our permissions config. What this will do is it will create the migrations based on whether you have teams enabled or not. So this is why it's really important. Let's head over to config and permission, which we've just published.
01:00
Let's search for teams and we should see this teams feature section, which we're going to set to true. So now when we go ahead and migrate, this is going to include team functionality. Now another really important part of this is the team foreign ID. So for us on the user model, if we just take a look at this,
01:18
we know that we've got this set to current team ID. That kind of makes sense for our user model. So what we're going to do is say current team ID in here. So when the migrations get published, it uses this foreign key for the user.
01:32
That's all we need to do. Now we can go ahead and migrate what we have here, and this will include team functionality. Okay, let's have a look at the database and just see what this has done. So let's start over in roles.
01:44
Roles are just high level things that have permissions. We're not in this course, and I don't recommend doing this anywhere. Going to be looking at checking against individual team permissions. We're going to have roles for users within teams.
01:59
So you can see here that we've got an ID of the role. We've got a current team ID, which will be the role that this belongs to. The name of it, the guard name, which is going to be web in our case. Then we have permissions, which are individual permissions that we can assign
02:13
to a role via this role has permissions table. Now we're not going to have to fill in any of this really manually ourselves. We could create a seeder to set up our application with all of the roles and permissions that we need.
02:25
And we also have model has roles. So the team user who actually has a role and we have model has permissions as well, which we're going to leave alone. It's much better to assign roles who have permissions
02:37
rather than individual permissions per user. Okay, so now that we've worked this out, I'm going to go ahead and create our a seeder, which is going to create some sensible defaults for our role and all of our permissions. Now, this is going to be a seeder, which you could run in production
02:52
once you launch your app. So let's go ahead and call this admin role seeder. And we're also going to have a member role seeder as well. We'll fill this one in, then we'll just pretty much copy and paste this over.
03:06
So this seeder is going to go ahead and run. And first of all, create our role. So we're going to say role first or create, because we don't want to override it if we already have it.
03:17
And let's give this a name. So let's say that this is a team admin. Now a role is going to have multiple permissions. Again, let's just make sure we put in the model here.
03:28
And let's say role give permission to, and then we're going to create a permission. So let's create our permission again, using that model from that package. And again, we're going to say first or create.
03:41
So we don't create duplicates. So let's say name. And the first one that we're going to work on is going to be update team. Let's just leave it like that for now.
03:51
And then we'll come back and we'll add more permissions in here for a team admin as we go through each feature of the course. Okay, let's go ahead and run this. So phpr and db seed, and let's say admin role seeder.
04:03
And that should create that out for us. And let's take a look. So we've got a role now in here called team admin. Role has these permissions, which are defined under permissions.
04:14
And one of our permissions that we've just created is update team. So now we have a role with a single permission, which is a good start. Let's do this for a basic user as well. So we'll make a member role seeder.
04:25
And we'll pretty much just do exactly the same thing and just assign any permissions that we need. So let's open up that member role seeder. And again, we'll just paste this directly in here.
04:35
And this is going to be a team member. Now, at the moment, at least a team member has no permissions, but we can add some permissions down here a little bit later, like viewing all of the team members inside of a team.
04:48
Or if a team member had any additional permissions, you could assign them here as well. Okay, let's go ahead and run our seeder for our member role seeder. And we're good.
04:58
Great. So we've got another role in there now. Before we forget, the last thing that we want to make sure that we do is over in the user model as part of this package.
05:06
So we have all of this functionality on a user. Is use the has roles trait. And that's all we need to do. Okay.
05:15
So there is our basic roles and permissions functionality set up within the context of teams, which we're going to look at in a bit more detail over the next couple of episodes.
38 episodes4 hrs 36 mins

Overview

Need team functionality in your Laravel application? Let’s build it from scratch.

We’ll cover the basics of creating teams, switching between them, sending secure team invites by email, and managing team members.

Powering everything will be roles and permissions for each member, with the ability to switch roles directly from your team dashboard.

Once you’re done, you’ll have mastered team functionality in Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!