Class: SSH::Allow::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh/allow/rule.rb

Direct Known Subclasses

Allow, Deny

Defined Under Namespace

Classes: Allow, Deny

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmds) ⇒ Rule

Returns a new instance of Rule.



6
7
8
9
10
# File 'lib/ssh/allow/rule.rb', line 6

def initialize(cmds)
  @command = [cmds].flatten
  @options = [:none]
  @arguments = [:none]
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



4
5
6
# File 'lib/ssh/allow/rule.rb', line 4

def arguments
  @arguments
end

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/ssh/allow/rule.rb', line 4

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/ssh/allow/rule.rb', line 4

def options
  @options
end

Class Method Details

.allow(*cmds, &block) ⇒ Object



62
63
64
# File 'lib/ssh/allow/rule.rb', line 62

def allow(*cmds, &block)
  create(Rule::Allow.new(cmds.flatten), &block)
end

.create(rule, &block) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/ssh/allow/rule.rb', line 70

def create(rule, &block)
  begin
    rule.instance_eval(&block) if block_given?
    rule
  rescue Exception => e
    false
  end
end

.deny(*cmds, &block) ⇒ Object



66
67
68
# File 'lib/ssh/allow/rule.rb', line 66

def deny(*cmds, &block)
  create(Rule::Deny.new(cmds.flatten), &block)
end

Instance Method Details

#args(arg_text) ⇒ Object



17
18
19
20
# File 'lib/ssh/allow/rule.rb', line 17

def args(arg_text)
  arg_text = Regexp.new(arg_text) if arg_text.is_a?(String)
  push(:arguments, arg_text)
end

#match_arguments?(arg_list) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/ssh/allow/rule.rb', line 35

def match_arguments?(arg_list)
  return arg_list.empty? if none?(arguments)
  return true if any?(arguments)
  return false if arguments.size != arg_list.size
  arg_list.inject(true) { |match, arg| match && match_one_argument?(arg) }
end

#match_command?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/ssh/allow/rule.rb', line 22

def match_command?(name)
  return false if none?(command)
  return true if any?(command)
  command.include?(name)
end

#match_options?(opt_list) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/ssh/allow/rule.rb', line 28

def match_options?(opt_list)
  return opt_list.empty? if none?(options)
  return true if any?(options)
  return false if options.size != opt_list.size
  opt_list.inject(true) { |match, opt| match && options.include?(opt) }
end

#opts(opt_text) ⇒ Object



12
13
14
15
# File 'lib/ssh/allow/rule.rb', line 12

def opts(opt_text)
  opt_text.gsub!(/^--?/, '') if opt_text.is_a?(String)
  push(:options, opt_text)
end