Created
May 14, 2012 13:20
-
-
Save simi/2693934 to your computer and use it in GitHub Desktop.
ActiveController roles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Admin::AdminController < Admin::ApplicationController | |
| role 'Admin' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Admin::ApplicationController < ApplicationController | |
| include Roleable | |
| # Devise | |
| before_filter :authenticate_user! | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # encoding: UTF-8 | |
| module Roleable | |
| extend ActiveSupport::Concern | |
| included do | |
| class_eval do | |
| cattr_accessor :admin_role | |
| before_filter :check_for_role | |
| end | |
| end | |
| def check_for_role | |
| if current_user.in_role?(self.class.admin_role) | |
| return true | |
| else | |
| redirect_to admin_dashboard_path, :notice => 'Nemáte oprávnění' | |
| end | |
| end | |
| module ClassMethods | |
| def role(role) | |
| self.admin_role = role | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User | |
| def in_role?(role) | |
| return true if self.role == 'Admin' | |
| return true if role == nil | |
| return self.role == role | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment