001    /**
002     * Copyright (c) 2000-2013 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.portal.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.DigesterUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    import com.liferay.portal.webserver.WebServerServletTokenUtil;
028    
029    /**
030     * @author Amos Fong
031     */
032    public class UserConstants {
033    
034            public static final int FULL_NAME_MAX_LENGTH = 75;
035    
036            public static final String LIST_VIEW_FLAT_ORGANIZATIONS =
037                    "flat-organizations";
038    
039            public static final String LIST_VIEW_FLAT_USER_GROUPS = "flat-user-groups";
040    
041            public static final String LIST_VIEW_FLAT_USERS = "flat-users";
042    
043            public static final String LIST_VIEW_TREE = "tree";
044    
045            public static final long USER_ID_DEFAULT = 0;
046    
047            public static final String USERS_EMAIL_ADDRESS_AUTO_SUFFIX = PropsUtil.get(
048                    PropsKeys.USERS_EMAIL_ADDRESS_AUTO_SUFFIX);
049    
050            public static String getPortraitURL(
051                    String imagePath, boolean male, long portraitId) {
052    
053                    if (!GetterUtil.getBoolean(
054                                    PropsUtil.get(PropsKeys.USERS_IMAGE_CHECK_TOKEN))) {
055    
056                            return getPortraitURL(imagePath, male, portraitId, null);
057                    }
058    
059                    if (portraitId <= 0) {
060                            return getPortraitURL(imagePath, male, 0, StringPool.BLANK);
061                    }
062    
063                    try {
064                            User user = UserLocalServiceUtil.getUserByPortraitId(portraitId);
065    
066                            if (user == null) {
067                                    return getPortraitURL(imagePath, male, 0, StringPool.BLANK);
068                            }
069    
070                            return getPortraitURL(
071                                    imagePath, male, portraitId, user.getUserUuid());
072                    }
073                    catch (Exception e) {
074                            if (_log.isWarnEnabled()) {
075                                    _log.warn(e, e);
076                            }
077                    }
078    
079                    return StringPool.BLANK;
080            }
081    
082            public static String getPortraitURL(
083                    String imagePath, boolean male, long portraitId, String userUuid) {
084    
085                    StringBundler sb = new StringBundler(9);
086    
087                    sb.append(imagePath);
088                    sb.append("/user_");
089    
090                    if (male) {
091                            sb.append("male");
092                    }
093                    else {
094                            sb.append("female");
095                    }
096    
097                    sb.append("_portrait?img_id=");
098                    sb.append(portraitId);
099    
100                    if (GetterUtil.getBoolean(
101                                    PropsUtil.get(PropsKeys.USERS_IMAGE_CHECK_TOKEN))) {
102    
103                            sb.append("&img_id_token=");
104                            sb.append(HttpUtil.encodeURL(DigesterUtil.digest(userUuid)));
105                    }
106    
107                    sb.append("&t=");
108                    sb.append(WebServerServletTokenUtil.getToken(portraitId));
109    
110                    return sb.toString();
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(UserConstants.class);
114    
115    }