Module: AuthorizeRbac

Defined in:
lib/authorize_rbac.rb,
lib/authorize_rbac/version.rb,
lib/authorize_rbac/configuration.rb,
lib/authorize_rbac/authorize_rbac_methods.rb,
lib/generators/authorize_rbac/authorize_rbac_generator.rb

Defined Under Namespace

Modules: AuthorizeRbacMethods Classes: AuthorizeRbacGenerator, Configuration

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationObject



11
12
13
# File 'lib/authorize_rbac.rb', line 11

def self.configuration
  @configration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



15
16
17
# File 'lib/authorize_rbac.rb', line 15

def self.configure
  yield(configuration)
end

.included(base) ⇒ Object



7
8
9
# File 'lib/authorize_rbac.rb', line 7

def self.included(base)
  base.extend(AuthorizeRbacMethods)
end

Instance Method Details

#access_allowed?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/authorize_rbac.rb', line 44

def access_allowed?
  return true if action_roles.nil?

  allowed_from_source = action_roles.include? user_role.to_sym
  allowed_from_db     = user_permissions.include?(permission_name(self.class, action_name))

  allowed_from_source || allowed_from_db
end

#action_nameObject



40
41
42
# File 'lib/authorize_rbac.rb', line 40

def action_name
  request.parameters[:action].to_sym
end

#action_rolesObject



36
37
38
# File 'lib/authorize_rbac.rb', line 36

def action_roles
  self.class.rbac[action_name]
end

#auth_userObject



57
58
59
# File 'lib/authorize_rbac.rb', line 57

def auth_user
  self.send(AuthorizeRbac.configuration.current_user_method)
end

#authorization_filterObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/authorize_rbac.rb', line 19

def authorization_filter
  if access_allowed?
    logger.debug "Authorized to access #{request.original_url}, User: #{auth_user.user_name} (role: #{user_role})"
    return true
  else
    logger.info "#{auth_user.user_name} (role: #{user_role}) attempted to access\
      #{self.class}##{action_name} without the proper permissions."
    flash[:notice] = "Not authorized to access #{request.original_url}!"
    redirect_to :controller => AuthorizeRbac.configuration.default_controller , :action => AuthorizeRbac.configuration.default_action
    return false
  end
end

#permission_name(cotroller, action) ⇒ Object



53
54
55
# File 'lib/authorize_rbac.rb', line 53

def permission_name(cotroller, action)
  "#{cotroller.to_s.chomp("Controller").downcase}_#{action}"
end

#user_permissionsObject



61
62
63
# File 'lib/authorize_rbac.rb', line 61

def user_permissions
  auth_user.role.permissions
end

#user_roleObject



32
33
34
# File 'lib/authorize_rbac.rb', line 32

def user_role
  auth_user.role.nil? ? "user" : auth_user.role.name.to_s
end