If I understand your needs correctly, the st-approach should follow these steps:
- how the administrator can "become" another user to access your application, as if you used his credentials to log into the system
- After "becoming" this user, you want to be able to "become" your real user again whenever you want, without having to log in again.
- Keep admin rights while you become a different user (see answer below for this)
- Avoid logs so that the mention of an action is done by the user you "became"
Become a user
This is necessary if you need to perform actions as another user or "see what he sees"
First you need to implement the official development method (this is pseudocode , I let you adapt it to your personal situation)
# the usage of session[:original_user_id] is explained later def become if admin? or session[:original_user_id] if session[:original_user_id] == params[:id] session[:original_user_id] = nil
As you can see, we "become" the requested user, plus we write your real user.id into the session. Please note: if you choose this solution, you MUST configure the rails to store session data in the database, to prevent this data from being moved to cookies.
Coming back
Secondly, you need the link "back to you" somewhere in your presentation, I suggest the headline as a good place
if session[:original_user_id] link_to 'Be my self', become_path(session[:original_user_id]) end
This will create a link, visible only to the administrator, which has "become" someone else, and this link will allow you to "become" a user with an identifier that matches your real user account. Clicking on it will return your real identity.
Keep admin rights while you are another user
Then you want to preserve administrator privileges, we need to trick the system to make him believe that the user you "became" is an administrator, but we must protect the application so that it does not make unwanted saving of this "fake privilege" in the database
In your application controller, implement before the filter
before_filter :fake_admin def fake_admin if session[:original_user_id] current_user.fake_admin = true current_user.admin = true end end
Then, to protect the user record in order to obtain an undesired administrator privilege, in your user model you can set before_save_filter (or before_validation_filter, depending on your architecture).
before_save :check_for_fake_admin def check_for_fake_admin self.admin = false if self.fake_admin end
Avoid logs to indicate the username you become
You have no choice but to manually hack all the places in your application where the action should contain the administratorโs name and not the username. The template in updated_by records must be set to session[:original_user_id] if it is not nil
Now it is necessary not only in magazines, but also for any other thing. It all depends on your application, and it can become very tedious to maintain.
Legal information
- This is pseudo-code, unsafe, and if you choose this solution, you will need to ensure security
- I answer your question because you asked, but my personal advice: do not implement this solution, because it looks like a big security hole, and you must allow the administrator to log out and log in after they become someone else .