ankane/pretender

Context of `with` proc

abrom opened this issue · 3 comments

abrom commented

At the moment the context of self from within the with proc will be the class in which the impersonates function is called (ie your ApplicationController). Instead of using:

impersonate_with.call(session[session_key])

I'd suggest instead to use

self.instance_exec(session[session_key], &impersonate_with)

which will give the with proc access to everything in the current controller instance.

I'd be happy to put together a PR if it's something you'd be interested in.

Hey @abrom, thanks for the suggestion. Can you give an example where this is useful?

abrom commented

Sure, my use-case is that the attributes/associations of an impersonator and impersonated user can change. Validation of the impersonator's ability to act as another user allows you to be sure the privilege isn't being abused.

I've put together the changes described above, as well as an impersonating_#{scope}? helper method in #27

Closing - see #28 for reference