Created
July 7, 2015 19:22
-
-
Save sfaxon/8817e279c4b9c154033d to your computer and use it in GitHub Desktop.
Users::Role
This file contains 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
module Users | |
module RolesConfig #:nodoc: | |
# Users::Roles.module_roles returns: | |
# {"Users"=>[:admin], "Events"=>[:admin, :manager]} | |
attr_accessor :roles, :module_roles | |
def configure(&block) | |
if block_given? | |
calling_module = eval("self", block.binding) | |
if calling_module.to_s == "main" | |
raise "Users::Roles.configure must be called in a module" | |
end | |
module_roles = instance_eval(&block) | |
@module_roles ||= {} | |
@module_roles[calling_module.to_s] = module_roles | |
yield self | |
end | |
self | |
end | |
end | |
end | |
module Users | |
# Internally we use the {Pundit gem}[https://github.com/elabs/pundit] for authorization. Each | |
# component that needs authorization access by role must inject the roles that it needs into | |
# this Users component. | |
# | |
# Roles should be injected in the component lib/component_name.rb file. For example, the Events | |
# component lib/events.rb file would declare an admin role with: | |
# | |
# module Events | |
# require "events/engine" | |
# | |
# ROLES = [:admin] | |
# Users::Roles.configure do |config| | |
# config.roles = ROLES | |
# end | |
# end | |
# | |
# With Users::Roles configured the Users will allow roles to be assigned to groups of users. | |
# Components using authorization will also need to include the Users::SessionsHelper and | |
# Users::Authentication module into the components ApplicationController. ex: | |
# | |
# module Events | |
# class ApplicationController < ActionController::Base | |
# include Users::SessionsHelper | |
# | |
# include Users::Authentication | |
# end | |
# end | |
module Roles | |
extend Users::RolesConfig | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment