This episode is for members only

Sign up to access "Laravel Teams" right now.

Get started
Already a member? Sign in to continue
Playing
08. Switching to another team

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
So to switch teams we're going to need to make this a form that patches through so we're going to make a patch request to a controller which basically takes the current user's team and associates it with the team that we've given in the URL. Now it might sound a little bit more complicated than it is we'll get this working first we won't authorize this just yet we'll
00:21
leave that to the next episode and we'll make sure our tests cover that we can actually switch to a team and really the goal is to just make sure we can switch to a team only that we belong to. Okay let's start out with a controller so let's go ahead and make out a controller in here and we'll call this team controller we'll just keep one controller for all of these common actions so
00:44
let's open up team controller and let's create our method in here called set current that kind of makes sense and we'll take in the team that we are switching to with root model binding and for now let's just go ahead and die dump on the team that we are about to switch to and we'll hook this up. Okay so if we head over to our web roots let's go ahead and create out a route for this so we
01:06
need to be authenticated for this so let's do this under this middleware group just here and let's go ahead and create out a patch request so technically what we're doing is we're updating the user you could set this as a post request it doesn't really matter what we do but we just don't want this to be a get request because then it's going to be susceptible to cross-site request forgery.
01:28
Okay let's go ahead and say team slash team and then maybe set underscore current that kind of makes sense and let's reference out that team controller and we'll reference the set current method that we just created. Okay let's go ahead and give this a name and pull that down to the next line to make it a little bit neater and we'll say team dot set and hyphen current. Okay great so
01:53
now what we want to do is reference this over in the navigation where we have each of these drop-down links that we created so because this isn't a get request we can't just link through to it instead what we're going to have to do is build out a form in here that when we press the button it's going to submit through so let's go ahead and create out a form action through to
02:13
that root that we've just created team set current we're going to need to pass the team in there so we know with root model binding which one we are trying to pick up and we'll set a method of post here. Now the really important thing here is to include our cross-site request forgery fields but because we're using patch we're also going to need to use method spoofing to spoof this to a
02:34
patch request and then basically the button that we're going to create here which is going to have a type of submit is basically just going to go through and select that team so when we click this button it's going to submit this form and go through to that route that we've just created. Let's go ahead and try this out so let's go over and click on co-course and there we go so that
02:55
goes ahead and patches this request that's given us the team that we have selected and now we can just take this model and associate it so back over to the team controller let's go and fill this in and then we'll just write a very quick test to verify this so in here let's actually pull in our request as well so we'll do this just here and let's go down and pull out the currently
03:22
authenticated user so request user and we're going to say current team and we're going to associate the team that we have sent through with root model binding and then we're going to save that resource and then we're going to return back that's pretty much all we need to do so whichever page that we're currently on doesn't matter whether it's a page that lists our projects
03:44
when we switch that's going to go back and show the data for that new team now really importantly we need to authorize this so we'll come back to that in the next episode but let's just go ahead and see if this works first of all so we'll keep our database open under here we're currently under current team id of two let's go and switch to Cocos and we already know that works because
04:05
the team has changed in here but as you can see the team has been set in here as well okay let's write a quick test for this because if we change this at any point particularly with authorization it's going to break at some point so let's go ahead and make out a test here so let's say pest test we'll stick this under controllers and let's put this under a team controller test you
04:28
could break these up but i'm just going to put all of the team controller stuff under a single test and of course you can break them up in two different tests if you want so let's say it switches the current team for the user and what do we need to do here well we need to create out a user of course so let's say user factory and create and just make sure we pull in
04:53
the model for the user here we need to specifically attach a team so we could actually use create quietly here so it doesn't create that personal team so i'm going to say user teams and attach and the reason i'm doing it this way rather than saying has and then teams in here is because i need an actual model here so when we attach this i actually need the model to be able to test with
05:18
you could do has and then you could extract that team out by saying user teams first but when i'm writing tests i like things to be as clear as possible so let's go ahead and say team factory and create so that user now belongs to that team and we have a reference to that team and now we want to hit that controller root that we've just created so how do we do that well
05:44
we can say acting as so we need to be authenticated as that user before we make this request we want to send a patch request down to slash teams then the id and then set underscore current so we can just switch this out with the team id that we have here or you could use the root helper to build this up so actually a better way to do this would probably be to say teams set
06:11
current and pass the team in so that looks a little bit neater as well and we basically want to assert a redirect so we want to make sure we're redirected back but then down here we want to make sure that the current team id is different to the one that we originally had so actually let's go ahead and get rid of create quietly because we know this is going to set the current team id
06:35
to the personal team so we can even assert that it exists before and after but let's go ahead and just add in a basic expectation here so we want to make sure that the current team id is the new team that we've just switched to so we could just say to be team id so basically create a user attach a team switch them over using that route that we've just created and make sure that
07:01
it's changed in the database okay let's go ahead and run pests tests and feature controllers and our team controller and let's see great so we know that that is working and of course we already knew it was working directly from the ui so we can now switch between teams by just clicking on any of the teams in the navigation
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!