コード例 #1
0
  public static void setThreadValues(User user) {
    if (user == null) {
      PrincipalThreadLocal.setName(null);
      PermissionThreadLocal.setPermissionChecker(null);

      return;
    }

    long userId = user.getUserId();

    String name = String.valueOf(userId);

    PrincipalThreadLocal.setName(name);

    try {
      PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

      if (permissionChecker == null) {
        permissionChecker =
            (PermissionChecker) Class.forName(PropsValues.PERMISSIONS_CHECKER).newInstance();
      }

      permissionChecker.init(user);

      PermissionThreadLocal.setPermissionChecker(permissionChecker);
    } catch (Exception e) {
      _log.error(e, e);
    }
  }
コード例 #2
0
  protected void searchRecentEntries() throws Exception {
    long initialSearchGroupEntriesCount = searchGroupEntriesCount(group.getGroupId(), 0);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    BaseModel<?> parentBaseModel1 = getParentBaseModel(group, serviceContext);
    BaseModel<?> parentBaseModel2 = getParentBaseModel(group, serviceContext);

    String name = PrincipalThreadLocal.getName();

    try {
      User user1 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user1.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);

      User user2 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user2.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);
    } finally {
      PrincipalThreadLocal.setName(name);
    }

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));

    moveParentBaseModelToTrash((Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 3, searchGroupEntriesCount(group.getGroupId(), 0));

    TrashHandler parentTrashHandler =
        TrashHandlerRegistryUtil.getTrashHandler(getParentBaseModelClassName());

    parentTrashHandler.restoreTrashEntry(
        TestPropsValues.getUserId(), (Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));
  }
コード例 #3
0
  protected void setPrincipal(long companyId, long userId, String remoteUser, String password) {

    if ((userId == 0) && (remoteUser == null)) {
      return;
    }

    String name = String.valueOf(userId);

    if (PropsValues.PORTAL_JAAS_ENABLE) {
      long remoteUserId = 0;

      try {
        remoteUserId = JAASHelper.getJaasUserId(companyId, remoteUser);
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn(e);
        }
      }

      if (remoteUserId > 0) {
        name = String.valueOf(remoteUserId);
      }
    } else if (remoteUser != null) {
      name = remoteUser;
    }

    PrincipalThreadLocal.setName(name);

    PrincipalThreadLocal.setPassword(password);
  }
コード例 #4
0
  private static void _populateThreadLocalsFromContext(Map<String, Serializable> context) {

    long companyId = GetterUtil.getLong(context.get("companyId"));

    if (companyId > 0) {
      CompanyThreadLocal.setCompanyId(companyId);
    }

    Locale defaultLocale = (Locale) context.get("defaultLocale");

    if (defaultLocale != null) {
      LocaleThreadLocal.setDefaultLocale(defaultLocale);
    }

    long groupId = GetterUtil.getLong(context.get("groupId"));

    if (groupId > 0) {
      GroupThreadLocal.setGroupId(groupId);
    }

    String principalName = GetterUtil.getString(context.get("principalName"));

    if (Validator.isNotNull(principalName)) {
      PrincipalThreadLocal.setName(principalName);
    }

    PermissionChecker permissionChecker = null;

    if (Validator.isNotNull(principalName)) {
      try {
        User user = UserLocalServiceUtil.fetchUser(PrincipalThreadLocal.getUserId());

        permissionChecker = PermissionCheckerFactoryUtil.create(user);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    if (permissionChecker != null) {
      PermissionThreadLocal.setPermissionChecker(permissionChecker);
    }

    String principalPassword = GetterUtil.getString(context.get("principalPassword"));

    if (Validator.isNotNull(principalPassword)) {
      PrincipalThreadLocal.setPassword(principalPassword);
    }

    Locale siteDefaultLocale = (Locale) context.get("siteDefaultLocale");

    if (siteDefaultLocale != null) {
      LocaleThreadLocal.setSiteDefaultLocale(siteDefaultLocale);
    }

    Locale themeDisplayLocale = (Locale) context.get("themeDisplayLocale");

    if (themeDisplayLocale != null) {
      LocaleThreadLocal.setThemeDisplayLocale(themeDisplayLocale);
    }
  }
コード例 #5
0
  @Override
  protected void doUpgrade() throws Exception {
    String name = PrincipalThreadLocal.getName();

    try {
      long companyId = PortalUtil.getDefaultCompanyId();

      long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);

      PrincipalThreadLocal.setName(defaultUserId);

      clearData(companyId);
      setupPermissionsDefined(defaultUserId);
    } finally {
      PrincipalThreadLocal.setName(name);
    }
  }
コード例 #6
0
  @Override
  public void initContextUser(long userId) throws AuthException {
    try {
      User user = UserLocalServiceUtil.getUser(userId);

      CompanyThreadLocal.setCompanyId(user.getCompanyId());

      PrincipalThreadLocal.setName(userId);

      PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user);

      PermissionThreadLocal.setPermissionChecker(permissionChecker);

      AccessControlThreadLocal.setRemoteAccess(false);
    } catch (Exception e) {
      throw new AuthException(e.getMessage(), e);
    }
  }
コード例 #7
0
  protected void initThreadLocals(User user) throws Exception {
    CompanyThreadLocal.setCompanyId(user.getCompanyId());

    PrincipalThreadLocal.setName(user.getUserId());

    if (!_usePermissionChecker) {
      return;
    }

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    if (permissionChecker != null) {
      return;
    }

    permissionChecker = PermissionCheckerFactoryUtil.create(user);

    PermissionThreadLocal.setPermissionChecker(permissionChecker);
  }
コード例 #8
0
  protected HttpServletRequest setCredentials(
      HttpServletRequest request, HttpSession session, long userId) throws Exception {

    User user = UserLocalServiceUtil.getUser(userId);

    String userIdString = String.valueOf(userId);

    request = new ProtectedServletRequest(request, userIdString);

    session.setAttribute(WebKeys.USER, user);
    session.setAttribute(_AUTHENTICATED_USER, userIdString);

    if (_usePermissionChecker) {
      PrincipalThreadLocal.setName(userId);
      PrincipalThreadLocal.setPassword(PortalUtil.getUserPassword(request));

      PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user, false);

      PermissionThreadLocal.setPermissionChecker(permissionChecker);
    }

    return request;
  }
コード例 #9
0
 public static void setName(long name) {
   setName(String.valueOf(name));
 }
コード例 #10
0
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    User user = null;

    try {
      user = _getUser(request);

      PrincipalThreadLocal.setName(user.getUserId());
      PrincipalThreadLocal.setPassword(PortalUtil.getUserPassword(request));

      PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user);

      PermissionThreadLocal.setPermissionChecker(permissionChecker);

      if (_lastModified) {
        long lastModified = getLastModified(request);

        if (lastModified > 0) {
          long ifModifiedSince = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);

          if ((ifModifiedSince > 0) && (ifModifiedSince == lastModified)) {

            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

            return;
          }
        }

        if (lastModified > 0) {
          response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
        }
      }

      String path = HttpUtil.fixPath(request.getPathInfo());
      String[] pathArray = StringUtil.split(path, CharPool.SLASH);

      if (pathArray.length == 0) {
        sendGroups(response, user, request.getServletPath() + StringPool.SLASH + path);
      } else {
        if (_PATH_DDM.equals(pathArray[0])) {
          sendDDMRecordFile(request, response, pathArray);
        } else if (Validator.isNumber(pathArray[0])) {
          sendFile(request, response, user, pathArray);
        } else {
          if (isLegacyImageGalleryImageId(request, response)) {
            return;
          }

          Image image = getImage(request, true);

          if (image != null) {
            writeImage(image, request, response);
          } else {
            sendDocumentLibrary(
                request,
                response,
                user,
                request.getServletPath() + StringPool.SLASH + path,
                pathArray);
          }
        }
      }
    } catch (NoSuchFileEntryException nsfee) {
      PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
    } catch (PrincipalException pe) {
      processPrincipalException(pe, user, request, response);
    } catch (Exception e) {
      PortalUtil.sendError(e, request, response);
    }
  }
コード例 #11
0
  private long _initCompany(ServletContext servletContext, String webId) {

    // Begin initializing company

    if (_log.isDebugEnabled()) {
      _log.debug("Begin initializing company with web id " + webId);
    }

    long companyId = 0;

    try {
      Company company = CompanyLocalServiceUtil.checkCompany(webId);

      companyId = company.getCompanyId();
    } catch (Exception e) {
      _log.error(e, e);
    }

    Long currentThreadCompanyId = CompanyThreadLocal.getCompanyId();

    String currentThreadPrincipalName = PrincipalThreadLocal.getName();

    try {
      CompanyThreadLocal.setCompanyId(companyId);

      String principalName = null;

      try {
        User user = UserLocalServiceUtil.getUser(PrincipalThreadLocal.getUserId());

        if (user.getCompanyId() == companyId) {
          principalName = currentThreadPrincipalName;
        }
      } catch (Exception e) {
      }

      PrincipalThreadLocal.setName(principalName);

      // Initialize display

      if (_log.isDebugEnabled()) {
        _log.debug("Initialize display");
      }

      try {
        String xml =
            HttpUtil.URLtoString(servletContext.getResource("/WEB-INF/liferay-display.xml"));

        PortletCategory portletCategory =
            (PortletCategory) WebAppPool.get(companyId, WebKeys.PORTLET_CATEGORY);

        if (portletCategory == null) {
          portletCategory = new PortletCategory();
        }

        PortletCategory newPortletCategory = PortletLocalServiceUtil.getEARDisplay(xml);

        portletCategory.merge(newPortletCategory);

        for (int i = 0; i < _companyIds.length; i++) {
          long currentCompanyId = _companyIds[i];

          PortletCategory currentPortletCategory =
              (PortletCategory) WebAppPool.get(currentCompanyId, WebKeys.PORTLET_CATEGORY);

          if (currentPortletCategory != null) {
            portletCategory.merge(currentPortletCategory);
          }
        }

        WebAppPool.put(companyId, WebKeys.PORTLET_CATEGORY, portletCategory);
      } catch (Exception e) {
        _log.error(e, e);
      }

      // LDAP import

      try {
        if (LDAPSettingsUtil.isImportOnStartup(companyId)) {
          UserImporterUtil.importUsers(companyId);
        }
      } catch (Exception e) {
        _log.error(e, e);
      }

      // Process application startup events

      if (_log.isDebugEnabled()) {
        _log.debug("Process application startup events");
      }

      try {
        EventsProcessorUtil.process(
            PropsKeys.APPLICATION_STARTUP_EVENTS,
            PropsValues.APPLICATION_STARTUP_EVENTS,
            new String[] {String.valueOf(companyId)});
      } catch (Exception e) {
        _log.error(e, e);
      }

      // End initializing company

      if (_log.isDebugEnabled()) {
        _log.debug(
            "End initializing company with web id " + webId + " and company id " + companyId);
      }

      addCompanyId(companyId);
    } finally {
      CompanyThreadLocal.setCompanyId(currentThreadCompanyId);

      PrincipalThreadLocal.setName(currentThreadPrincipalName);
    }

    return companyId;
  }