Module: SuperAuth

Defined in:
lib/super_auth.rb,
lib/super_auth/railtie.rb,
lib/super_auth/version.rb,
app/controllers/super_auth/graph_controller.rb,
lib/generators/super_auth/install/install_generator.rb

Defined Under Namespace

Modules: ActiveRecord, Generators, Nestable Classes: Authorization, Edge, Engine, Error, GraphController, Group, Permission, Railtie, Resource, Role, User

Constant Summary collapse

VERSION =
"0.3.3"

Class Method Summary collapse

Class Method Details

.current_userObject



62
63
64
# File 'lib/super_auth.rb', line 62

def self.current_user
  Thread.current[:super_auth_current_user]
end

.current_user=(user) ⇒ Object



58
59
60
# File 'lib/super_auth.rb', line 58

def self.current_user=(user)
  Thread.current[:super_auth_current_user] = user
end

.dbObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/super_auth.rb', line 66

def self.db
  return @db if !@db.nil?

  if !Gem::Specification.find_all_by_name("activerecord").empty?
    require "active_record"
    extensions = Gem::Specification.find_all_by_name("sequel-activerecord_connection").any? ? { extensions: :activerecord_connection } : {}

    if extensions.empty?
      warn "[SuperAuth] WARNING: Found ActiveRecord but could not find the gem 'sequel-activerecord_connection' installed. SuperAuth may not always work as expected."
    end

    begin
      ::ActiveRecord::Base.establish_connection
    rescue ActiveRecord::AdapterNotSpecified
      if defined?(Rails) && !Rails.env.local?
        raise Error, "SuperAuth could not find a database configuration. " \
          "Please configure ActiveRecord or set SUPER_AUTH_DATABASE_URL."
      end
      warn "[SuperAuth] WARNING: No database configured. Falling back to in-memory SQLite. " \
        "All authorization data will be lost on restart."
      ::ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
    end

    # Walk the ancestor chain so adapter subclasses (e.g. PostGIS, Makara)
    # are recognized as their parent adapter type.
    adapter_ancestors = ::ActiveRecord::Base.adapter_class.ancestors.map(&:to_s)
    if adapter_ancestors.include?("ActiveRecord::ConnectionAdapters::SQLite3Adapter")
      SuperAuth.db = Sequel.sqlite(**extensions)
    elsif adapter_ancestors.include?("ActiveRecord::ConnectionAdapters::PostgreSQLAdapter")
      SuperAuth.db = Sequel.postgres(**extensions)
    elsif adapter_ancestors.include?("ActiveRecord::ConnectionAdapters::Mysql2Adapter")
      SuperAuth.db = Sequel.mysql2(**extensions)
    else
      warn "[SuperAuth] WARNING: Unknown adapter: #{::ActiveRecord::Base.adapter_class}"
    end
  else
    logger =
    if defined?(Rails) && ENV["SUPER_AUTH_LOG_LEVEL"] == "debug"
      { logger: Rails.logger }
    elsif ENV["SUPER_AUTH_LOG_LEVEL"] == "debug"
      require "logger"
      { logger: Logger.new(STDOUT) }
    else
      {} # no logger
    end

    if !ENV['SUPER_AUTH_DATABASE_URL'].nil? && !ENV['SUPER_AUTH_DATABASE_URL'].empty?
      SuperAuth.db = Sequel.connect(ENV['SUPER_AUTH_DATABASE_URL'], **logger)
    else
      if defined?(Rails) && !Rails.env.local?
        raise Error, "SuperAuth could not find a database configuration. " \
          "Please set SUPER_AUTH_DATABASE_URL or configure ActiveRecord."
      end
      warn "[SuperAuth] WARNING: SUPER_AUTH_DATABASE_URL not set. Falling back to in-memory SQLite. " \
        "All authorization data will be lost on restart."
      SuperAuth.db = Sequel.sqlite(**logger)
    end
  end
end

.db=(db) ⇒ Object



126
127
128
# File 'lib/super_auth.rb', line 126

def self.db=(db)
  @db = db
end

.install_migrationsObject



38
39
40
41
42
43
# File 'lib/super_auth.rb', line 38

def self.install_migrations
  Sequel.extension :migration
  require "pathname"
  path = Pathname.new(__FILE__).parent.parent.join("db", "migrate")
  Sequel::Migrator.run(SuperAuth.db, path)
end

.loadObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/super_auth.rb', line 25

def self.load
  require "super_auth/authorization"
  require "super_auth/edge"
  require "super_auth/nestable"
  require "super_auth/group"
  require "super_auth/permission"
  require "super_auth/railtie"
  require "super_auth/resource"
  require "super_auth/role"
  require "super_auth/user"
  require "super_auth/active_record" if defined?(ActiveRecord::Base)
end

.missing_user_behaviorObject

Controls behavior when SuperAuth.current_user is blank in ByCurrentUser scope. :none (default) — returns an empty result set silently :raise — raises SuperAuth::Error



14
15
16
# File 'lib/super_auth.rb', line 14

def self.missing_user_behavior
  @missing_user_behavior || :none
end

.missing_user_behavior=(behavior) ⇒ Object



18
19
20
21
22
23
# File 'lib/super_auth.rb', line 18

def self.missing_user_behavior=(behavior)
  unless %i[none raise].include?(behavior)
    raise ArgumentError, "missing_user_behavior must be :none or :raise, got #{behavior.inspect}"
  end
  @missing_user_behavior = behavior
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (SuperAuth)

    the object that the method was called on



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

def self.setup
  yield self if block_given?
end

.uninstall_migrationsObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/super_auth.rb', line 45

def self.uninstall_migrations
  require "sequel"
  Sequel.extension :migration
  require "pathname"

  path = Pathname.new(__FILE__).parent.parent.join("db", "migrate")
  db = SuperAuth.db

  Sequel::Migrator.run(db, path, target: 0)
rescue => e
  raise Error, "Failed to uninstall migrations: #{e.message}"
end