Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
220: Role-Based Access Control for Admin Panels
You’ve probably already built a few admin panels where you just slapped an is_admin boolean on your User model. For a tiny project, that's totally fine. It's fast, it's easy to understand, and it gets you shipping. But as soon as your product grows, the "boolean trap" starts to close in on you. I've seen this happen a dozen times: first you have admins, then you need "editors" who can change product descriptions but shouldn't touch the payment settings, then you need "support agents" who can issue refunds but shouldn't be able to delete users. Suddenly, your User model is littered with is_admin, is_editor, is_support, and is_super_user.
The Boolean Soup and the Logic Leak
When you rely on boolean flags, your authorization logic inevitably leaks all over your controllers and views. You start seeing code like if current_user.admin? || current_user.editor? scattered everywhere. This is a maintenance nightmare. If the business decides that "Editors" should now be able to do one specific "Admin" task, you have to hunt down every single one of those if statements across your entire codebase. It's brittle, it's prone to bugs, and it makes the codebase feel cluttered.
# The naive way: Leaking logic into the controller
def update
@product = Product.find(params[:id])
if current_user.admin? || current_user.editor?
@product.update(product_params)
redirect_to @product
else
redirect_to root_path, alert: "Not authorized"
end
end
The real problem here isn't just the database schema; it's that you're asking the user who they are instead of what they are allowed to do. In a professional system, the identity of the user should be decoupled from the permission to perform an action.
Shifting to Role-Based Access Control (RBAC)
The better way is to treat roles as first-class citizens. Instead of flags on the user, you create a Role model and a join table (since a user might occasionally need multiple roles). This transforms the question from "Are you an admin?" to "Does your role permit this action?"
I usually recommend pairing this with a Policy object. Instead of putting the logic in the controller, you wrap it in a dedicated class that handles the authorization for a specific resource. This keeps your controllers skinny and your permissions centralized. If the rules for updating a product change, you change them in one file—the ProductPolicy—and the rest of the app just follows along.
# The professional way: Using a Policy object
class ProductPolicy
def initialize(user, product)
@user = user
@product = product
end
def update?
@user.has_role?(:admin) || @user.has_role?(:editor)
end
end
# In your controller
def update
@product = Product.find(params[:id])
authorize! @product, :update? # A helper method that calls ProductPolicy
@product.update(product_params)
end
The Cost of Abstraction
Now, to be fair, this approach has a higher upfront cost. You're writing more classes, managing more database tables, and adding a layer of indirection. If you're building a prototype for a weekend hackathon, RBAC is overkill. But the moment you have more than two levels of permission, the trade-off becomes obvious. You trade a bit of initial speed for a system that doesn't break every time the product manager decides to tweak the "Manager" role.
The beauty of this setup is that it's incredibly easy to test. You don't need to boot up a whole integration test to see if an editor can update a product; you just instantiate a ProductPolicy with a mock user and a mock product and assert that update? returns true. That kind of confidence is what allows a team to move fast without accidentally giving every support agent the power to delete the production database.
📋 Practical Task
Implementing a Tiered Permissions System for a Store Manager
You are tasked with upgrading a legacy e-commerce admin panel. Currently, it uses a simple admin boolean on the User model. You need to migrate this to a proper Role-Based Access Control system.
Requirements:
- Create a
Rolemodel (with anamestring) and aUserRolejoin model to allow aUserto have manyRoles. - Implement a
has_role?(role_name)method on theUsermodel that checks if the user is associated with a role matching that name. - Create a
OrderPolicyclass. This class should have a methodrefund?that returnstrueonly if the user has the:adminor:supportrole. - Create a method
cancel?in theOrderPolicythat returnstrueonly if the user has the:adminor:managerrole.
Deliverable: Provide the code for the User model method, the OrderPolicy class, and a brief example of how you would call the OrderPolicy inside a controller action to protect a refund method.
There are no comments for now.