-
-
Save thewatts/adaa4e122796dc1ffa14 to your computer and use it in GitHub Desktop.
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
require_relative "roles/config" | |
module Adam | |
class Roles | |
end | |
end |
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 Adam | |
class Roles | |
class Config #:nodoc: | |
# Users::Roles.module_roles returns: | |
# {"Users"=>[:admin], "Events"=>[:admin, :manager]} | |
class << self | |
attr_accessor :roles, :module_roles | |
end | |
def self.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 | |
end |
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 | |
# 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 | |
class Roles < Adam::Roles | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment