Skip to content

Instantly share code, notes, and snippets.

@thewatts
Forked from sfaxon/gist:8817e279c4b9c154033d
Last active August 29, 2015 14:24
Show Gist options
  • Save thewatts/adaa4e122796dc1ffa14 to your computer and use it in GitHub Desktop.
Save thewatts/adaa4e122796dc1ffa14 to your computer and use it in GitHub Desktop.
require_relative "roles/config"
module Adam
class Roles
end
end
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
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