How to add methods/modules access control in Daemons?

142 views Asked by At

I want disable the rm\_rf method of the FileUtils module in Ruby scripts.

When foo.rb contains:

FileUtils.rm_rf(file)

It should not be run by:

Daemons.run("foo.rb", some_options)

and should give an error message instead.

Does Daemons can do this? Or can some other libraries can do this simply and effectively?

1

There are 1 answers

0
Dave Newton On BEST ANSWER

Here's a rough outline of what you want to do.

Using alias_method is likely not a good idea unless you define the old method to something else; here methods are defined away. The danger of this approach is that internal behavior may be affected in anticipated ways, e.g., an allowed method uses a disallowed method internally.

The following is for singleton (class) methods, the same logic may be used for instance methods. There are several ways this could be implemented, this is just one, meant as a guide.

> FileUtils.pwd
=> "/home/dave"
> FileUtils.cp '.bashrc', 'tmpbashrc'
=> nil
> class Object
*   def deimplement_singleton_methods *methods
*     methods.each do |msym|
*       define_singleton_method msym do |*args|
*         raise NotImplementedError.new "#{msym} not implemented"
*       end
*     end
*   end
* end
> FileUtils.deimplement_singleton_methods :cp
> FileUtils.pwd
=> "/home/dave"
> FileUtils.cp '.bashrc', 'tmpbashrc'
NotImplementedError: cp not implemented
from (pry):10:in `block (2 levels) in deimplement_singleton_methods'

There's also Module::undef_method and Module::remove_method, which may or may not provide the behavior you want (not sure what you need it to do).