module Strongbolt

Main module

Included in the base class of models (ActiveRecord::Base), this module is the entry point of all authorization.

It implements helper methods that will be used by a lot of other models

Constants

ActionNotConfigured
DirectAssociationNotConfigured
InverseAssociationNotConfigured
ModelNotFound
ModelNotOwned
StrongboltError
TenantError
VERSION
WrongUserClass

Public Class Methods

current_user() click to toggle source

Current User

# File lib/strongbolt.rb, line 82
def self.current_user
  Grant::User.current_user
end
current_user=(user) click to toggle source

We keep an hash so we don't have each time to test if the module is included in the list

# File lib/strongbolt.rb, line 88
def self.current_user=(user)
  # If user is an instance of something and different from what we have
  if user.present?
    # Raise error if wrong user class
    raise Strongbolt::WrongUserClass unless valid_user? user

    # If the user class doesn't have included the module yet
    unless user.class.included_modules.include? Strongbolt::UserAbilities
      user.class.send :include, Strongbolt::UserAbilities
    end
  end

  # Then we call the original grant method
  Grant::User.current_user = user unless Grant::User.current_user == user
end
disable_authorization() click to toggle source

Disable authorization checking

# File lib/strongbolt.rb, line 155
def self.disable_authorization
  Grant::Status.disable_grant
end
disabled?() click to toggle source
# File lib/strongbolt.rb, line 167
def self.disabled?
  !enabled?
end
enable_authorization() click to toggle source
# File lib/strongbolt.rb, line 159
def self.enable_authorization
  Grant::Status.enable_grant
end
enabled?() click to toggle source
# File lib/strongbolt.rb, line 163
def self.enabled?
  Grant::Status.grant_enabled?
end
include_helpers(scope) click to toggle source

Include helpers in the given scope to AC and AV.

# File lib/strongbolt.rb, line 183
def self.include_helpers(scope)
  ActiveSupport.on_load(:action_controller) do
    include scope::UrlHelpers
  end

  ActiveSupport.on_load(:action_view) do
    include scope::UrlHelpers
  end
end
setup(&block) click to toggle source

Setting up Strongbolt

# File lib/strongbolt.rb, line 107
  def self.setup(&block)
    # Configuration by user
    block.call Configuration

    # Include the User::Abilities
    begin
      user_class = Configuration.user_class
      user_class = user_class.constantize if user_class.is_a? String
      user_class.send(:include, Strongbolt::UserAbilities) unless user_class.included_modules.include?(Strongbolt::UserAbilities)
    rescue NameError
      logger.warn "User class #{Configuration.user_class} wasn't found"
    end
  rescue => e
    error = <<~CONTENT
      [ERROR] Strongbolt could not initialized successfully.
        This can happen when running migrations, and in this situation, you can ignore this message.
        If it happens in test, make sure you've run `rake db:test:prepare` so that test database is ready.
        Otherwise, please review the error below to check what happened:

      Error message:
        #{e.message}

        #{e.backtrace.join("\n")}
    CONTENT
    logger.fatal error
    # Display in the console when error test env
    puts error if defined?(Rails) && Rails.env.test?
    # If not being done in a rake task, this should propagate the error
    raise e unless $PROGRAM_NAME =~ /rake$/ # && ARGV.join(" ").include?("db:")
  end
table_name_prefix() click to toggle source
# File lib/strongbolt.rb, line 58
def self.table_name_prefix
  'strongbolt_'
end
with_authorization(&block) click to toggle source

Perform the block with grant

# File lib/strongbolt.rb, line 148
def self.with_authorization(&block)
  Grant::Status.with_grant(&block)
end
without_authorization(&block) click to toggle source

Perform the block without grant

# File lib/strongbolt.rb, line 141
def self.without_authorization(&block)
  Grant::Status.without_grant(&block)
end

Private Class Methods

tenants=(tenants) click to toggle source

Not to use directly, only used in tests

# File lib/strongbolt.rb, line 194
def self.tenants=(tenants)
  @@tenants = tenants
end
valid_user?(user) click to toggle source

Ensures the user instance given is a valid user for that configuration It checks whether the class or the base_class (in case of STI) of the instance class has been configured as the user model

# File lib/strongbolt.rb, line 176
def self.valid_user?(user)
  user.class.name == Strongbolt::Configuration.user_class ||
    user.class.base_class.name == Strongbolt::Configuration.user_class
end