public Object invoke(MethodInvocation invocation) throws Throwable {
    String userId = getUserId();

    boolean setUser = false;
    if (!StringUtils.isEmpty(userId)) {
      final PropertyLayer layer = PropertyLayerProviderInterceptor.getCurrent();

      Map loginProperties = new HashMap();
      LoginUtils.mergeDefaultCredentials(loginProperties);
      /*
               loginProperties.put(SecurityProperties.PARTITION, partitionId);
               loginProperties.put(SecurityProperties.REALM, realmId);
               loginProperties.put(SecurityProperties.DOMAIN, domainId);
      */
      IAuditTrailPartition partition =
          LoginUtils.findPartition(invocation.getParameters(), loginProperties);
      IUserDomain domain =
          LoginUtils.findUserDomain(invocation.getParameters(), partition, loginProperties);

      layer.setProperty(SecurityProperties.CURRENT_PARTITION, partition);
      layer.setProperty(SecurityProperties.CURRENT_PARTITION_OID, new Short(partition.getOID()));
      layer.setProperty(SecurityProperties.CURRENT_DOMAIN, domain);
      layer.setProperty(SecurityProperties.CURRENT_DOMAIN_OID, new Long(domain.getOID()));

      IModel model = ModelManagerFactory.getCurrent().findActiveModel();
      if (model == null) {
        model = ModelManagerFactory.getCurrent().findLastDeployedModel();
      }

      IUser user =
          SynchronizationService.synchronize(
              userId,
              model,
              invocation
                  .getParameters()
                  .getBoolean(SecurityProperties.AUTHORIZATION_SYNC_LOGIN_PROPERTY, true),
              loginProperties);

      layer.setProperty(SecurityProperties.CURRENT_USER, user);

      // clean thread, so nested calls won't reuse the user ID
      setUser = true;
      resetUserId();
    }

    try {
      return invocation.proceed();
    } finally {
      if (setUser) {
        // restore thread status
        setUserId(userId);
      }
    }
  }