Class: LcpRuby::Generators::Entity::RoleDiscovery

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/lcp_ruby/entity/role_discovery.rb

Constant Summary collapse

FALLBACK =
%w[admin viewer].freeze

Class Method Summary collapse

Class Method Details

.call(destination_root, &on_warning) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/lcp_ruby/entity/role_discovery.rb', line 11

def self.call(destination_root, &on_warning)
  permissions_dir = File.join(destination_root, "config/lcp_ruby/permissions")
  return fallback(&on_warning) unless Dir.exist?(permissions_dir)

  Dir.glob(File.join(permissions_dir, "*.{yml,yaml}")).each do |path|
    # Skip files that fail to parse as YAML, are missing on disk
    # (race with another writer), or use disallowed classes — we
    # only need to find a Hash with the right shape, not validate
    # every permission file in the project.
    yaml = begin
      YAML.safe_load_file(path)
    rescue Psych::SyntaxError, Psych::DisallowedClass, Errno::ENOENT, Errno::EACCES
      nil
    end
    next unless yaml.is_a?(Hash)
    next unless yaml.dig("permissions", "model") == "_default"

    roles = yaml.dig("permissions", "roles")
    return roles.keys if roles.is_a?(Hash) && roles.any?
  end

  fallback(&on_warning)
end

.fallback(&on_warning) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/generators/lcp_ruby/entity/role_discovery.rb', line 35

def self.fallback(&on_warning)
  on_warning&.call(
    "No `_default` permission definition found in config/lcp_ruby/permissions/. " \
    "Falling back to roles: #{FALLBACK.join(', ')}."
  )
  FALLBACK
end