<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Impersonation (via API) and Pages</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=9277967" />
  <subtitle>Impersonation (via API) and Pages</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=9277967</id>
  <updated>2026-04-03T21:41:06Z</updated>
  <dc:date>2026-04-03T21:41:06Z</dc:date>
  <entry>
    <title>RE: Impersonation (via API) and Pages</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112475401" />
    <author>
      <name>Vishnu S Kumar</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112475401</id>
    <updated>2019-02-26T16:08:43Z</updated>
    <published>2019-02-26T16:08:43Z</published>
    <summary type="html"> I implemented a sample portlet using the same code you have given and got the following error when going to the impersonate URL.&lt;br /&gt;&lt;blockquote&gt; Unable to impersonate 36173 because the string cannot be decrypted&lt;br /&gt;&lt;/blockquote&gt;</summary>
    <dc:creator>Vishnu S Kumar</dc:creator>
    <dc:date>2019-02-26T16:08:43Z</dc:date>
  </entry>
  <entry>
    <title>RE: Impersonation (via API) and Pages</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=9310046" />
    <author>
      <name>Chris Chan</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=9310046</id>
    <updated>2011-06-10T19:46:07Z</updated>
    <published>2011-06-10T19:46:07Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Ok, so I've figured it out and just putting how I got this working for future reference.&lt;br&gt;&lt;br&gt;Essentially, in order to impersonate a user in Liferay via the API, you need to first have the userId and the companyId of the person being impersonated.&lt;br&gt;&lt;br&gt;From there, you need to encrypt the userId which can be done by doing something similar to:&lt;br&gt;[codeString encDoAsUserId = Encryptor.encrypt(company.getKeyObj(), String.valueOf(oboId));]&lt;br&gt;where Encryptor is from com.liferay.util.Encryptor&lt;br&gt;&lt;br&gt;Once you have the encrypted userId, you update the ThemeDisplay as follows:&lt;br&gt;&lt;pre&gt;&lt;code&gt;themeDisplay.setDoAsUserId(encDoAsUserId);&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;&lt;br&gt;It's as simple as that.&lt;br&gt;I wish there was some more documentation around this.&lt;br&gt;&lt;br&gt;I also got some of my initial information from this thread:&lt;br&gt;http://www.liferay.com/community/forums/-/message_boards/message/7228942&lt;br&gt;&lt;br&gt;&lt;br&gt;Hope this helps!&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Chris Chan</dc:creator>
    <dc:date>2011-06-10T19:46:07Z</dc:date>
  </entry>
  <entry>
    <title>RE: Impersonation (via API) and Pages</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=9308912" />
    <author>
      <name>Chris Chan</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=9308912</id>
    <updated>2011-06-10T17:59:01Z</updated>
    <published>2011-06-10T17:59:01Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Just looking into this a little more, I see that the ThemeDisplay has the following method defined:&lt;br&gt;&lt;pre&gt;&lt;code&gt;public void setDoAsUserId(String doAsUserId)&lt;/code&gt;&lt;/pre&gt;&lt;br&gt; A couple of questions around this:&lt;br&gt;1. Do I just need to call this and the user will then begin to impersonate?&lt;br&gt;2. Is the doAsUserId string value that's being passed in the encrypted userId?  I assume so, but the javadocs don't provide any information.&lt;br&gt;&lt;br&gt;Thanks in advance!&lt;br&gt;Chris&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Chris Chan</dc:creator>
    <dc:date>2011-06-10T17:59:01Z</dc:date>
  </entry>
  <entry>
    <title>Impersonation (via API) and Pages</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=9277966" />
    <author>
      <name>Chris Chan</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=9277966</id>
    <updated>2011-06-09T16:50:50Z</updated>
    <published>2011-06-09T16:50:50Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hi,&lt;br&gt;&lt;br&gt;I have developed a custom portlet that displays a list of users and when selecting a user from the list, would allow the main user to impersonate the selected user.&lt;br&gt;&lt;br&gt;The following code is how I have coded this part in the portlet:&lt;br&gt;&lt;pre&gt;&lt;code&gt;
public void changeUser(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

		//Get the selected Impersonated user ID from the form 
		long impUserId = ParamUtil.getInteger(actionRequest, "impUserId");
		
		//Get the current instance's company to help build the URL for impersonation
		ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		themeDisplay.setUser(UserLocalServiceUtil.getUser(impUserId));
		Company company = themeDisplay.getCompany();
		
		String companyUrl = company.getHomeURL();
		String doAsURL = company.getHomeURL();
		
		//Encrypt the selected impersonated user's ID
		String encDoAsUserId = Encryptor.encrypt(company.getKeyObj(), String.valueOf(impUserId));
		
		//Redirect the user to the impersonation URL
		actionResponse.sendRedirect(doAsURL, encDoAsUserId);
	}
&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;This method is being referenced as a portlet:actionURL in my jsp's form.&lt;br&gt;Now this works when I've selected a user and it indeed impersonates the user, but when navigating to another page, the impersonation does not persist and it reverts back to the real user.&lt;br&gt;&lt;br&gt;Is there something else that I need to do using the API to do proper full-blown impersonation?&lt;br&gt;&lt;br&gt;Thanks,&lt;br&gt;Chris&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Chris Chan</dc:creator>
    <dc:date>2011-06-09T16:50:50Z</dc:date>
  </entry>
</feed>
