protected void updateCustomFields(
      User user, Userinfo userinfo, String accessToken, String refreshToken)
      throws PortalException, SystemException {

    ExpandoValueLocalServiceUtil.addValue(
        user.getCompanyId(),
        User.class.getName(),
        ExpandoTableConstants.DEFAULT_TABLE_NAME,
        GOOGLE_ACCESS_TOKEN,
        user.getUserId(),
        accessToken);

    ExpandoValueLocalServiceUtil.addValue(
        user.getCompanyId(),
        User.class.getName(),
        ExpandoTableConstants.DEFAULT_TABLE_NAME,
        GOOGLE_REFRESH_TOKEN,
        user.getUserId(),
        refreshToken);

    ExpandoValueLocalServiceUtil.addValue(
        user.getCompanyId(),
        User.class.getName(),
        ExpandoTableConstants.DEFAULT_TABLE_NAME,
        GOOGLE_USER_ID,
        user.getUserId(),
        userinfo.getId());
  }
  protected void populateExpandoAttributes(
      ExpandoBridge expandoBridge, Map<String, String[]> expandoAttributes) {

    for (Map.Entry<String, String[]> expandoAttribute : expandoAttributes.entrySet()) {

      String name = expandoAttribute.getKey();

      if (!expandoBridge.hasAttribute(name)) {
        continue;
      }

      int type = expandoBridge.getAttributeType(name);

      Serializable value =
          ExpandoConverterUtil.getAttributeFromStringArray(type, expandoAttribute.getValue());

      try {
        ExpandoValueLocalServiceUtil.addValue(
            expandoBridge.getCompanyId(),
            expandoBridge.getClassName(),
            ExpandoTableConstants.DEFAULT_TABLE_NAME,
            name,
            expandoBridge.getClassPK(),
            value);
      } catch (Exception e) {
        _log.error(e, e);
      }
    }
  }
  protected void updateMPExpandoColumns(long companyId) throws Exception {
    ExpandoTable expandoTable = null;

    try {
      expandoTable = ExpandoTableLocalServiceUtil.getTable(companyId, User.class.getName(), "MP");
    } catch (NoSuchTableException nste) {
      return;
    }

    ExpandoColumn oldExpandoColumn =
        ExpandoColumnLocalServiceUtil.getColumn(
            companyId, User.class.getName(), expandoTable.getName(), "client-id");

    if (oldExpandoColumn == null) {
      return;
    }

    ExpandoColumn newExpandoColumn =
        ExpandoColumnLocalServiceUtil.getColumn(
            companyId, User.class.getName(), expandoTable.getName(), "clientId");

    if (newExpandoColumn == null) {
      newExpandoColumn =
          ExpandoColumnLocalServiceUtil.updateColumn(
              oldExpandoColumn.getColumnId(), "clientId", ExpandoColumnConstants.STRING);
    }

    List<ExpandoValue> expandoValues =
        ExpandoValueLocalServiceUtil.getColumnValues(
            oldExpandoColumn.getColumnId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);

    for (ExpandoValue expandoValue : expandoValues) {
      ExpandoValueLocalServiceUtil.addValue(
          expandoValue.getCompanyId(), User.class.getName(),
          expandoTable.getName(), newExpandoColumn.getName(),
          expandoValue.getClassPK(), expandoValue.getString());
    }

    ExpandoColumnLocalServiceUtil.deleteColumn(oldExpandoColumn.getColumnId());
  }
  public static Date getDateTo(long companyId, long userId) {
    Date result = null;
    try {
      result =
          ExpandoValueLocalServiceUtil.getData(
              companyId,
              User.class.getName(),
              ExpandoTableConstants.DEFAULT_TABLE_NAME,
              ExpandoTableConstants.COLUMN_ACCESS_DATE_TO,
              userId,
              result);
    } catch (Exception e) {
      _log.error("Can't access to user[" + userId + "] attributes", e);
    }

    return result;
  }
  public static boolean isAccessByDateEnabled(long companyId, long userId) {
    boolean result = false;
    try {
      result =
          ExpandoValueLocalServiceUtil.getData(
              companyId,
              User.class.getName(),
              ExpandoTableConstants.DEFAULT_TABLE_NAME,
              ExpandoTableConstants.COLUMN_ACCESS_BY_DATE,
              userId,
              false);
    } catch (Exception e) {
      _log.error("Can't access to user[" + userId + "] attributes", e);
    }

    return result;
  }
  public static int getSessionCount(long companyId, long userId) {
    int result = 0;
    try {
      result =
          ExpandoValueLocalServiceUtil.getData(
              companyId,
              User.class.getName(),
              ExpandoTableConstants.DEFAULT_TABLE_NAME,
              ExpandoTableConstants.COLUMN_SESSION_COUNT,
              userId,
              result);
    } catch (Exception e) {
      _log.error("Can't access to user[" + userId + "] attributes", e);
    }

    return result;
  }
  /**
   * @param companyId
   * @param userId
   * @return empty list, or list with values
   */
  public static List<String[]> getAllowedUserIP(long companyId, long userId) {
    String[] dataArray = null;
    try {
      dataArray =
          ExpandoValueLocalServiceUtil.getData(
              companyId,
              User.class.getName(),
              ExpandoTableConstants.DEFAULT_TABLE_NAME,
              ExpandoTableConstants.COLUMN_ALLOWED_IPS,
              userId,
              dataArray);

      dataArray =
          dataArray != null && dataArray.length > 0
              ? StringUtil.split(dataArray[0], CharPool.NEW_LINE)
              : new String[0];

    } catch (Exception e) {
      _log.error("Can't access to user[" + userId + "] attributes", e);
    }

    List<String[]> result = new LinkedList<String[]>();
    for (String ip : dataArray) {
      if (StringUtils.isBlank(ip)) continue;
      if (ip.contains("-")) {
        String[] range = ip.split("-");
        if (StringUtils.isNotBlank(range[0]) && StringUtils.isNotBlank(range[1])) {
          range[0] = range[0].trim();
          range[1] = range[1].trim();
          result.add(range);
        }
      }
      result.add(new String[] {ip.trim()});
    }
    return result;
  }