Class: Checken::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/checken/schema.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Create a new schema



19
20
21
22
23
# File 'lib/checken/schema.rb', line 19

def initialize
  @root_group = PermissionGroup.new(self, nil)
  @config = Config.new
  @schema = {}
end

Class Attribute Details

.instanceObject

This can be used for storing a global instance of a schema for an application that may require such a thing.



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

def instance
  @instance
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#root_groupObject (readonly)

Returns the value of attribute root_group.



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

def root_group
  @root_group
end

Instance Method Details

#check_permission!(permission_path, user_proxy, object = nil, strict: true) ⇒ Object

Does the given user have the appropriate permissions to handle?

Parameters:

  • permission_path (String)
  • user (User)
  • object (Object) (defaults to: nil)


36
37
38
39
40
41
42
# File 'lib/checken/schema.rb', line 36

def check_permission!(permission_path, user_proxy, object = nil, strict: true)
  if strict # permission(s) for the path are expected to be defined within the Checken Schema
    handle_strict_permission_check!(permission_path, user_proxy, object)
  else
    handle_unstrict_permission_check!(permission_path, user_proxy)
  end
end

#configure(&block) ⇒ Object

Add configuration for this schema



26
27
28
29
# File 'lib/checken/schema.rb', line 26

def configure(&block)
  block.call(@config)
  set_namespace if @config.namespace
end

#load_from_directory(path) ⇒ Boolean

Load a set of schema files from a given directory

Parameters:

  • path (String)

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/checken/schema.rb', line 48

def load_from_directory(path)
  # Store the load path for future reload
  @load_path = path

  # If the path doesn't exist, just return false. We won't load anything
  # if the directory hasnt' been loaded yet.
  unless File.exist?(path)
    return false
  end

  # Check that the directory is a a directory
  unless File.directory?(path)
    raise Error, "Path to directory must be a directory. #{path} is not a directory."
  end

  # Read all the files and pass them through the DSL for the root schema.
  # Each directory is a group. Everything in the root will be at the root.
  Dir[File.join(path, "**", "*.rb")].each do |path|
    contents = File.read(path)
    dsl = DSL::GroupDSL.new(@root_group)
    dsl.instance_eval(contents, path)
  end

  logger.info "Loaded permission schema from #{path}"

  true
end

#loggerLogger

Return the logger

Returns:

  • (Logger)


92
93
94
# File 'lib/checken/schema.rb', line 92

def logger
  @config.logger
end

#reloadvoid

This method returns an undefined value.

Reload the schema from the directory if possible



79
80
81
82
83
84
85
86
87
# File 'lib/checken/schema.rb', line 79

def reload
  if @load_path
    @root_group = PermissionGroup.new(self, nil)
    load_from_directory(@load_path)
    true
  else
    raise Error, "Cannot reload a schema that wasn't loaded from a directory"
  end
end

#schemaHash

Return the schema sorted alphabetically by key

Returns:

  • (Hash)


107
108
109
# File 'lib/checken/schema.rb', line 107

def schema
  @schema.sort.to_h
end

#update_schema(entry) ⇒ Hash

Update the schema with the given entry

Parameters:

  • entry (Hash)

Returns:

  • (Hash)


100
101
102
# File 'lib/checken/schema.rb', line 100

def update_schema(entry)
  @schema.merge!(entry)
end