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.portal.kernel.test.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.test.randomizerbumpers.NumericStringRandomizerBumper;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Organization;
027    import com.liferay.portal.model.Role;
028    import com.liferay.portal.model.RoleConstants;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.model.UserGroupRole;
031    import com.liferay.portal.service.CompanyLocalServiceUtil;
032    import com.liferay.portal.service.RoleLocalServiceUtil;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
035    import com.liferay.portal.service.UserLocalServiceUtil;
036    import com.liferay.portal.service.UserServiceUtil;
037    
038    import java.util.Calendar;
039    import java.util.List;
040    import java.util.Locale;
041    
042    /**
043     * @author Alberto Chaparro
044     * @author Manuel de la Pe??a
045     * @author Sampsa Sohlman
046     */
047    public class UserTestUtil {
048    
049            public static User addCompanyAdminUser(Company company) throws Exception {
050                    User user = addUser();
051    
052                    user.setCompanyId(company.getCompanyId());
053    
054                    UserLocalServiceUtil.updateUser(user);
055    
056                    Role role = RoleLocalServiceUtil.getRole(
057                            company.getCompanyId(), RoleConstants.ADMINISTRATOR);
058    
059                    UserLocalServiceUtil.addRoleUser(role.getRoleId(), user);
060    
061                    return user;
062            }
063    
064            public static User addGroupAdminUser(Group group) throws Exception {
065                    return UserTestUtil.addGroupUser(
066                            group, RoleConstants.SITE_ADMINISTRATOR);
067            }
068    
069            public static User addGroupOwnerUser(Group group) throws Exception {
070                    return UserTestUtil.addGroupUser(group, RoleConstants.SITE_OWNER);
071            }
072    
073            public static User addGroupUser(Group group, String roleName)
074                    throws Exception {
075    
076                    User groupUser = addUser(group.getGroupId());
077    
078                    Role role = RoleLocalServiceUtil.getRole(
079                            TestPropsValues.getCompanyId(), roleName);
080    
081                    long[] userIds = {groupUser.getUserId()};
082    
083                    UserGroupRoleLocalServiceUtil.addUserGroupRoles(
084                            userIds, group.getGroupId(), role.getRoleId());
085    
086                    return groupUser;
087            }
088    
089            public static User addOmniAdminUser() throws Exception {
090                    Company company = CompanyLocalServiceUtil.getCompanyByMx(
091                            PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
092    
093                    return addCompanyAdminUser(company);
094            }
095    
096            public static User addOrganizationAdminUser(Organization organization)
097                    throws Exception {
098    
099                    return UserTestUtil.addOrganizationUser(
100                            organization, RoleConstants.ORGANIZATION_ADMINISTRATOR);
101            }
102    
103            public static User addOrganizationOwnerUser(Organization organization)
104                    throws Exception {
105    
106                    return UserTestUtil.addOrganizationUser(
107                            organization, RoleConstants.ORGANIZATION_OWNER);
108            }
109    
110            public static User addOrganizationUser(
111                            Organization organization, String roleName)
112                    throws Exception {
113    
114                    User organizationUser = addUser(organization.getGroupId());
115    
116                    long[] userIds = {organizationUser.getUserId()};
117    
118                    UserLocalServiceUtil.addOrganizationUsers(
119                            organization.getOrganizationId(), userIds);
120    
121                    Role role = RoleLocalServiceUtil.getRole(
122                            TestPropsValues.getCompanyId(), roleName);
123    
124                    UserGroupRoleLocalServiceUtil.addUserGroupRoles(
125                            userIds, organization.getGroupId(), role.getRoleId());
126    
127                    return organizationUser;
128            }
129    
130            public static User addUser() throws Exception {
131                    return addUser(
132                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
133                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
134                            LocaleUtil.getDefault(), RandomTestUtil.randomString(),
135                            RandomTestUtil.randomString(),
136                            new long[] {TestPropsValues.getGroupId()},
137                            ServiceContextTestUtil.getServiceContext());
138            }
139    
140            public static User addUser(boolean secure) throws Exception {
141                    boolean autoPassword = true;
142                    String password1 = StringPool.BLANK;
143                    String password2 = StringPool.BLANK;
144                    boolean autoScreenName = true;
145                    String screenName = StringPool.BLANK;
146                    long facebookId = 0;
147                    String openId = StringPool.BLANK;
148                    Locale locale = LocaleUtil.getDefault();
149                    String firstName = "UserServiceTest";
150                    String middleName = StringPool.BLANK;
151                    String lastName = "UserServiceTest";
152                    long prefixId = 0;
153                    long suffixId = 0;
154                    boolean male = true;
155                    int birthdayMonth = Calendar.JANUARY;
156                    int birthdayDay = 1;
157                    int birthdayYear = 1970;
158                    String jobTitle = StringPool.BLANK;
159                    long[] groupIds = null;
160                    long[] organizationIds = null;
161                    long[] roleIds = null;
162                    long[] userGroupIds = null;
163                    boolean sendMail = false;
164    
165                    ServiceContext serviceContext = new ServiceContext();
166    
167                    if (secure) {
168                            String emailAddress =
169                                    "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
170    
171                            return UserServiceUtil.addUser(
172                                    TestPropsValues.getCompanyId(), autoPassword, password1,
173                                    password2, autoScreenName, screenName, emailAddress, facebookId,
174                                    openId, locale, firstName, middleName, lastName, prefixId,
175                                    suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
176                                    jobTitle, groupIds, organizationIds, roleIds, userGroupIds,
177                                    sendMail, serviceContext);
178                    }
179                    else {
180                            String emailAddress =
181                                    "UserServiceTest." + RandomTestUtil.nextLong() + "@test.com";
182    
183                            return UserLocalServiceUtil.addUser(
184                                    TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
185                                    autoPassword, password1, password2, autoScreenName, screenName,
186                                    emailAddress, facebookId, openId, locale, firstName, middleName,
187                                    lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
188                                    birthdayYear, jobTitle, groupIds, organizationIds, roleIds,
189                                    userGroupIds, sendMail, serviceContext);
190                    }
191            }
192    
193            public static User addUser(long... groupIds) throws Exception {
194                    return addUser(
195                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
196                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
197                            LocaleUtil.getDefault(), RandomTestUtil.randomString(),
198                            RandomTestUtil.randomString(), groupIds,
199                            ServiceContextTestUtil.getServiceContext());
200            }
201    
202            public static User addUser(long groupId, Locale locale) throws Exception {
203                    return addUser(
204                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
205                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
206                            locale, RandomTestUtil.randomString(),
207                            RandomTestUtil.randomString(), new long[] {groupId},
208                            ServiceContextTestUtil.getServiceContext());
209            }
210    
211            public static User addUser(
212                            long companyId, long userId, String screenName, Locale locale,
213                            String firstName, String lastName, long[] groupIds,
214                            ServiceContext serviceContext)
215                    throws Exception {
216    
217                    User user = UserLocalServiceUtil.fetchUserByScreenName(
218                            companyId, screenName);
219    
220                    if (user != null) {
221                            return user;
222                    }
223    
224                    boolean autoPassword = true;
225                    String password1 = StringPool.BLANK;
226                    String password2 = StringPool.BLANK;
227                    String emailAddress =
228                            RandomTestUtil.randomString() + RandomTestUtil.nextLong() +
229                                    "@liferay.com";
230                    long facebookId = 0;
231                    String openId = StringPool.BLANK;
232                    String middleName = StringPool.BLANK;
233                    long prefixId = 0;
234                    long suffixId = 0;
235                    boolean male = true;
236                    int birthdayMonth = Calendar.JANUARY;
237                    int birthdayDay = 1;
238                    int birthdayYear = 1970;
239                    String jobTitle = StringPool.BLANK;
240                    long[] organizationIds = null;
241                    long[] roleIds = null;
242                    long[] userGroupIds = null;
243                    boolean sendMail = false;
244    
245                    return UserLocalServiceUtil.addUser(
246                            userId, companyId, autoPassword, password1, password2,
247                            Validator.isNull(screenName), screenName, emailAddress, facebookId,
248                            openId, locale, firstName, middleName, lastName, prefixId, suffixId,
249                            male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
250                            organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
251            }
252    
253            public static User addUser(
254                            String screenName, Locale locale, String firstName, String lastName,
255                            long[] groupIds)
256                    throws Exception {
257    
258                    return addUser(
259                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
260                            screenName, locale, firstName, lastName, groupIds,
261                            ServiceContextTestUtil.getServiceContext());
262            }
263    
264            public static User addUser(String screenName, long... groupIds)
265                    throws Exception {
266    
267                    return addUser(
268                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
269                            screenName, LocaleUtil.getDefault(), RandomTestUtil.randomString(),
270                            RandomTestUtil.randomString(), groupIds,
271                            ServiceContextTestUtil.getServiceContext());
272            }
273    
274            public static User getAdminUser(long companyId) throws PortalException {
275                    Role role = RoleLocalServiceUtil.getRole(
276                            companyId, RoleConstants.ADMINISTRATOR);
277    
278                    List<User> users = UserLocalServiceUtil.getRoleUsers(
279                            role.getRoleId(), 0, 1);
280    
281                    if (!users.isEmpty()) {
282                            return users.get(0);
283                    }
284    
285                    return null;
286            }
287    
288            public static User updateUser(User user) throws Exception {
289                    String oldPassword = StringPool.BLANK;
290                    String newPassword1 = StringPool.BLANK;
291                    String newPassword2 = StringPool.BLANK;
292                    Boolean passwordReset = false;
293                    String reminderQueryQuestion = StringPool.BLANK;
294                    String reminderQueryAnswer = StringPool.BLANK;
295                    String screenName = "TestUser" + RandomTestUtil.nextLong();
296                    String emailAddress =
297                            "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
298                    long facebookId = 0;
299                    String openId = StringPool.BLANK;
300                    String languageId = StringPool.BLANK;
301                    String timeZoneId = StringPool.BLANK;
302                    String greeting = StringPool.BLANK;
303                    String comments = StringPool.BLANK;
304                    String firstName = "UserServiceTest";
305                    String middleName = StringPool.BLANK;
306                    String lastName = "UserServiceTest";
307                    long prefixId = 0;
308                    long suffixId = 0;
309                    boolean male = true;
310                    int birthdayMonth = Calendar.JANUARY;
311                    int birthdayDay = 1;
312                    int birthdayYear = 1970;
313                    String smsSn = StringPool.BLANK;
314                    String aimSn = StringPool.BLANK;
315                    String facebookSn = StringPool.BLANK;
316                    String icqSn = StringPool.BLANK;
317                    String jabberSn = StringPool.BLANK;
318                    String msnSn = StringPool.BLANK;
319                    String mySpaceSn = StringPool.BLANK;
320                    String skypeSn = StringPool.BLANK;
321                    String twitterSn = StringPool.BLANK;
322                    String ymSn = StringPool.BLANK;
323                    String jobTitle = StringPool.BLANK;
324                    long[] groupIds = null;
325                    long[] organizationIds = null;
326                    long[] roleIds = null;
327                    List<UserGroupRole> userGroupRoles = null;
328                    long[] userGroupIds = null;
329    
330                    ServiceContext serviceContext = new ServiceContext();
331    
332                    return UserServiceUtil.updateUser(
333                            user.getUserId(), oldPassword, newPassword1, newPassword2,
334                            passwordReset, reminderQueryQuestion, reminderQueryAnswer,
335                            screenName, emailAddress, facebookId, openId, languageId,
336                            timeZoneId, greeting, comments, firstName, middleName, lastName,
337                            prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
338                            smsSn, aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn,
339                            skypeSn, twitterSn, ymSn, jobTitle, groupIds, organizationIds,
340                            roleIds, userGroupRoles, userGroupIds, serviceContext);
341            }
342    
343    }