In this episode, we dive deep into how team roles and permissions work in our application. The goal here is to make sure that when a user first signs up and creates their personal team, they're automatically assigned the Team Admin role for that specific team. We walk through writing a test to check this behavior, run into some common issues, and then resolve them.
You'll see how we seed the database with the necessary roles data and discover why just running middleware globally isn't enough—especially when dealing with things like observers and database tests that don't go through HTTP requests. That's where we learn that, in places that aren't part of the usual request cycle, we need to make sure to set the current team ID manually before assigning a role.
We then build a middleware to globally set the current team context for each request, making our roles and permissions system team-aware. Once this is all set up, we try things out in the UI and directly in the database to confirm that role assignments are linked to the correct team IDs. Plus, we test how switching teams changes what permissions the user has, confirming that our middleware keeps everything in the proper context.
By the end, you get a pretty strong understanding of how roles and permissions are managed per team context—so when you check if a user is a team admin, it checks against the right team every time!