-
-
Save Spaceghost/754961 to your computer and use it in GitHub Desktop.
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
| protected | |
| def check_perms | |
| #logger.info "------------------\n" | |
| #logger.info controller_name | |
| #logger.info action_name | |
| #logger.info current_user.role.permissions.fetch(controller_name).index(action_name) | |
| #logger.info "------------------\n" | |
| if current_user.role.permissions.fetch(controller_name).index(action_name).nil? | |
| flash[:warning] = "You do not have permission to do that." | |
| redirect_to current_user | |
| 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
| before_filter :check_perms, :only => [:edit, :update, :new] |
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
| <% if current_user.can?("foobar", "edit") %> | |
| some stuff here | |
| <% 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 CreateRoles < ActiveRecord::Migration | |
| def self.up | |
| create_table :roles do |t| | |
| t.string :name | |
| t.text :permissions | |
| t.timestamps | |
| end | |
| end | |
| def self.down | |
| drop_table :roles | |
| 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 Role < ActiveRecord::Base | |
| serialize :permissions | |
| has_many :users | |
| 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
| belongs_to :role | |
| # permissions | |
| def can?(controller, action) | |
| begin | |
| self.role.permissions.fetch(controller).index(action).nil? ? false : true | |
| rescue | |
| false | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment