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.security.permission;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.test.util.TestPropsValues;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.security.auth.CompanyThreadLocal;
022    import com.liferay.portal.security.auth.PrincipalThreadLocal;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public abstract class DoAsUserThread extends Thread {
029    
030            public DoAsUserThread(long userId) {
031                    this(userId, 1);
032            }
033    
034            public DoAsUserThread(long userId, int retries) {
035                    _userId = userId;
036    
037                    _retries = retries;
038            }
039    
040            public boolean isSuccess() {
041                    return _success;
042            }
043    
044            @Override
045            public void run() {
046                    for (int i = 0; i < _retries; i++) {
047                            try {
048                                    CompanyThreadLocal.setCompanyId(TestPropsValues.getCompanyId());
049    
050                                    PrincipalThreadLocal.setName(_userId);
051    
052                                    User user = UserLocalServiceUtil.getUserById(_userId);
053    
054                                    PermissionChecker permissionChecker =
055                                            PermissionCheckerFactoryUtil.create(user);
056    
057                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
058    
059                                    doRun();
060    
061                                    _success = true;
062    
063                                    return;
064                            }
065                            catch (Exception e) {
066                                    _log.error(e, e);
067                            }
068                            finally {
069                                    PrincipalThreadLocal.setName(null);
070                                    PermissionThreadLocal.setPermissionChecker(null);
071                            }
072                    }
073            }
074    
075            protected abstract void doRun() throws Exception;
076    
077            private static final Log _log = LogFactoryUtil.getLog(DoAsUserThread.class);
078    
079            private final int _retries;
080            private boolean _success;
081            private final long _userId;
082    
083    }