001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.CustomUserAttributes;
020    import com.liferay.portal.kernel.portlet.UserAttributes;
021    import com.liferay.portal.kernel.security.RandomUtil;
022    
023    import java.util.Map;
024    
025    /**
026     * <p>
027     * A separate instance of this class is created every time
028     * <code>renderRequest.getAttribute(PortletRequest.USER_INFO)</code> is called.
029     * It is safe to cache attributes in this instance because you can assume that
030     * all calls to this instance belong to the same user.
031     * </p>
032     *
033     * @author Brian Wing Shun Chan
034     */
035    public class DefaultCustomUserAttributes implements CustomUserAttributes {
036    
037            @Override
038            public Object clone() {
039                    return new DefaultCustomUserAttributes();
040            }
041    
042            @Override
043            public String getValue(String name, Map<String, String> userInfo) {
044                    if (name == null) {
045                            return null;
046                    }
047    
048                    if (_log.isDebugEnabled()) {
049                            String companyId = userInfo.get(UserAttributes.LIFERAY_COMPANY_ID);
050                            String userId = userInfo.get(UserAttributes.LIFERAY_USER_ID);
051    
052                            _log.debug("Company id " + companyId);
053                            _log.debug("User id " + userId);
054                    }
055    
056                    if (name.equals("user.name.random")) {
057                            String[] names = new String[] {"Aaa", "Bbb", "Ccc"};
058    
059                            return names[RandomUtil.nextInt(3)];
060                    }
061                    else {
062                            return null;
063                    }
064            }
065    
066            private static final Log _log = LogFactoryUtil.getLog(
067                    DefaultCustomUserAttributes.class);
068    
069    }