Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Nicolas Parvais
How to change user portrait from my portlet
February 7, 2012 1:31 AM
Answer

Nicolas Parvais

Rank: New Member

Posts: 4

Join Date: October 10, 2011

Recent Posts

I develop a portlet to allow user to change firstname, lastname, password and portrait.

Firstname and Lastname are changed as expected. But Password and portrait doesn't update.

I use same code that Liferay one :
 1
 2UserServiceUtil.updatePassword(user.getUserId(), newPassword, confirmPassword, false);
 3
 4InputStream inputStream = uploadPortletRequest.getFileAsStream(
 5                        PROFILE_IMAGE);
 6
 7                if (Validator.isNotNull(inputStream)) {
 8                    _log.info("Update profile portrait");
 9                    byte[] bytes = FileUtil.getBytes(inputStream);
10                    _log.info("Image bytes [" + bytes + "]");
11
12                    UserServiceUtil.updatePortrait(user.getUserId(), bytes);
13}


What am I doing wrong ?

Best regard.
Nicolas.
David H Nebinger
RE: How to change user portrait from my portlet
February 7, 2012 5:07 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6438

Join Date: September 1, 2006

Recent Posts

Have you tried using the UserLocalServiceUtil class instead?
Hitoshi Ozawa
RE: How to change user portrait from my portlet
February 7, 2012 5:24 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

This has recently been solved in the following thread.

http://www.liferay.com/community/forums/-/message_boards/message/12390208
Nicolas Parvais
RE: How to change user portrait from my portlet
February 8, 2012 5:44 AM
Answer

Nicolas Parvais

Rank: New Member

Posts: 4

Join Date: October 10, 2011

Recent Posts

I already test using LocalService but it doesn't work.

Any other idea ?

Regards.
Hitoshi Ozawa
RE: How to change user portrait from my portlet
February 9, 2012 3:54 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

Have you tried using the code provided in the thread above?

Also, are you getting the same error message when using UserLocalServiceUtil?
Nicolas Parvais
RE: How to change user portrait from my portlet
February 10, 2012 12:50 AM
Answer

Nicolas Parvais

Rank: New Member

Posts: 4

Join Date: October 10, 2011

Recent Posts

Yes, I did it. But it doesn't solve the issue.

There is no errors in log file and I get a success message in my portlet, but password and portrait image do not change. Firstname and Lastname do !

Regards
Nicolas Parvais
RE: How to change user portrait from my portlet
February 10, 2012 2:19 AM
Answer

Nicolas Parvais

Rank: New Member

Posts: 4

Join Date: October 10, 2011

Recent Posts

Finally, we found why it doesn't work in our portlet.

Here is the full code of the update of our profile information :

 1
 2User user;
 3        try {
 4            user = PortalUtil.getSelectedUser(uploadPortletRequest);
 5            _log.info("Update profile [" + user.getUserId() + "]");
 6            if(Validator.isNotNull(user))
 7            {
 8                String firstName = ParamUtil.getString(uploadPortletRequest, PROFILE_FIRSTNAME);
 9                String lastName = ParamUtil.getString(uploadPortletRequest, PROFILE_LASTNAME);
10                String emailAddress = ParamUtil.getString(uploadPortletRequest, PROFILE_EMAILADDRESS);
11                String newPassword = ParamUtil.getString(uploadPortletRequest, PROFILE_NEWPASSWORD);
12                String confirmPassword = ParamUtil.getString(uploadPortletRequest, PROFILE_CONFIRMPASSWORD);
13                if(Validator.isNotNull(firstName))
14                    user.setFirstName(firstName);
15                if(Validator.isNotNull(lastName))
16                    user.setLastName(lastName);
17                if(Validator.isNotNull(emailAddress))
18                    user.setEmailAddress(emailAddress);
19                InputStream inputStream = uploadPortletRequest.getFileAsStream(
20                        PROFILE_IMAGE);
21                if (Validator.isNotNull(inputStream)) {
22                    _log.info("Update profile portrait");
23                    byte[] bytes = FileUtil.getBytes(inputStream);
24                    _log.info("Image bytes [" + bytes + "]");
25
26                    UserLocalServiceUtil.updatePortrait(user.getUserId(), bytes);
27                }
28                if(Validator.isNotNull(newPassword))
29                {
30                    if(Validator.isNotNull(confirmPassword) && confirmPassword.equals(newPassword))
31                    {
32                        _log.info("Update password [" + newPassword + "] + [" + confirmPassword + "]");
33                        UserLocalServiceUtil.updatePassword(user.getUserId(), newPassword, confirmPassword, false);
34                    } else {
35                        SessionErrors.add(request, "profile-wrong-password");
36                    }
37                }
38                UserLocalServiceUtil.updateUser(user);

First, we get the user based on is userid. After we update its portrait and is password. We set new Firstname, Lastname, email to the user we get first. And finally, we update this user. But we didn't change portrait and password from the user we get first. And then, the old password and old portrait overwrite the value modified in our method.
We just need to put updatePassword and updatePortrait outside the other updateUser process.

This works now.
ronnie orvakanti
RE: How to change user portrait from my portlet
May 17, 2012 5:05 AM
Answer

ronnie orvakanti

Rank: Junior Member

Posts: 27

Join Date: February 27, 2012

Recent Posts

Nicolas Parvais:
Finally, we found why it doesn't work in our portlet.

Here is the full code of the update of our profile information :

 1
 2User user;
 3        try {
 4            user = PortalUtil.getSelectedUser(uploadPortletRequest);
 5            _log.info("Update profile [" + user.getUserId() + "]");
 6            if(Validator.isNotNull(user))
 7            {
 8                String firstName = ParamUtil.getString(uploadPortletRequest, PROFILE_FIRSTNAME);
 9                String lastName = ParamUtil.getString(uploadPortletRequest, PROFILE_LASTNAME);
10                String emailAddress = ParamUtil.getString(uploadPortletRequest, PROFILE_EMAILADDRESS);
11                String newPassword = ParamUtil.getString(uploadPortletRequest, PROFILE_NEWPASSWORD);
12                String confirmPassword = ParamUtil.getString(uploadPortletRequest, PROFILE_CONFIRMPASSWORD);
13                if(Validator.isNotNull(firstName))
14                    user.setFirstName(firstName);
15                if(Validator.isNotNull(lastName))
16                    user.setLastName(lastName);
17                if(Validator.isNotNull(emailAddress))
18                    user.setEmailAddress(emailAddress);
19                InputStream inputStream = uploadPortletRequest.getFileAsStream(
20                        PROFILE_IMAGE);
21                if (Validator.isNotNull(inputStream)) {
22                    _log.info("Update profile portrait");
23                    byte[] bytes = FileUtil.getBytes(inputStream);
24                    _log.info("Image bytes [" + bytes + "]");
25
26                    UserLocalServiceUtil.updatePortrait(user.getUserId(), bytes);
27                }
28                if(Validator.isNotNull(newPassword))
29                {
30                    if(Validator.isNotNull(confirmPassword) && confirmPassword.equals(newPassword))
31                    {
32                        _log.info("Update password [" + newPassword + "] + [" + confirmPassword + "]");
33                        UserLocalServiceUtil.updatePassword(user.getUserId(), newPassword, confirmPassword, false);
34                    } else {
35                        SessionErrors.add(request, "profile-wrong-password");
36                    }
37                }
38                UserLocalServiceUtil.updateUser(user);

First, we get the user based on is userid. After we update its portrait and is password. We set new Firstname, Lastname, email to the user we get first. And finally, we update this user. But we didn't change portrait and password from the user we get first. And then, the old password and old portrait overwrite the value modified in our method.
We just need to put updatePassword and updatePortrait outside the other updateUser process.

This works now.


Hi Nicolas,
Do you know how to access a user profile picture?