Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.