static void ensureUserCustomFieldExists(
      com.liferay.portal.model.User liferayUser, PortletRequest request)
      throws PortalException, SystemException {
    ExpandoBridge exp = liferayUser.getExpandoBridge();
    if (!exp.hasAttribute(CUSTOM_FIELD_PROJECT_GROUP_FILTER)) {
      exp.addAttribute(CUSTOM_FIELD_PROJECT_GROUP_FILTER, ExpandoColumnConstants.STRING, false);
      long companyId = liferayUser.getCompanyId();

      ExpandoColumn column =
          ExpandoColumnLocalServiceUtil.getColumn(
              companyId,
              exp.getClassName(),
              ExpandoTableConstants.DEFAULT_TABLE_NAME,
              CUSTOM_FIELD_PROJECT_GROUP_FILTER);

      String[] roleNames = new String[] {RoleConstants.USER, RoleConstants.POWER_USER};
      for (String roleName : roleNames) {
        Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
        if (role != null && column != null) {
          ResourcePermissionLocalServiceUtil.setResourcePermissions(
              companyId,
              ExpandoColumn.class.getName(),
              ResourceConstants.SCOPE_INDIVIDUAL,
              String.valueOf(column.getColumnId()),
              role.getRoleId(),
              new String[] {ActionKeys.VIEW, ActionKeys.UPDATE});
        }
      }
    }
  }
Example #2
0
  protected String getUserCSV(User user) {
    StringBundler sb = new StringBundler(PropsValues.USERS_EXPORT_CSV_FIELDS.length * 2);

    for (int i = 0; i < PropsValues.USERS_EXPORT_CSV_FIELDS.length; i++) {
      String field = PropsValues.USERS_EXPORT_CSV_FIELDS[i];

      if (field.equals("fullName")) {
        sb.append(CSVUtil.encode(user.getFullName()));
      } else if (field.startsWith("expando:")) {
        String attributeName = field.substring(8);

        ExpandoBridge expandoBridge = user.getExpandoBridge();

        sb.append(CSVUtil.encode(expandoBridge.getAttribute(attributeName)));
      } else {
        sb.append(CSVUtil.encode(BeanPropertiesUtil.getString(user, field)));
      }

      if ((i + 1) < PropsValues.USERS_EXPORT_CSV_FIELDS.length) {
        sb.append(StringPool.COMMA);
      }
    }

    sb.append(StringPool.NEW_LINE);

    return sb.toString();
  }
  protected void updateExpandoAttributes(User user, LDAPUser ldapUser) throws Exception {

    ExpandoBridge userExpandoBridge = user.getExpandoBridge();

    populateExpandoAttributes(userExpandoBridge, ldapUser.getUserExpandoAttributes());

    Contact contact = user.getContact();

    ExpandoBridge contactExpandoBridge = contact.getExpandoBridge();

    populateExpandoAttributes(contactExpandoBridge, ldapUser.getContactExpandoAttributes());
  }
  protected GoogleDriveSession buildGoogleDriveSession() throws IOException, PortalException {

    long userId = PrincipalThreadLocal.getUserId();

    User user = UserLocalServiceUtil.getUser(userId);

    if (user.isDefaultUser()) {
      throw new PrincipalException("User is not authenticated");
    }

    GoogleCredential.Builder builder = new GoogleCredential.Builder();

    String googleClientId = PrefsPropsUtil.getString(user.getCompanyId(), "google-client-id");
    String googleClientSecret =
        PrefsPropsUtil.getString(user.getCompanyId(), "google-client-secret");

    builder.setClientSecrets(googleClientId, googleClientSecret);

    JacksonFactory jsonFactory = new JacksonFactory();

    builder.setJsonFactory(jsonFactory);

    HttpTransport httpTransport = new NetHttpTransport();

    builder.setTransport(httpTransport);

    GoogleCredential googleCredential = builder.build();

    ExpandoBridge expandoBridge = user.getExpandoBridge();

    String googleAccessToken =
        GetterUtil.getString(expandoBridge.getAttribute("googleAccessToken", false));

    googleCredential.setAccessToken(googleAccessToken);

    String googleRefreshToken =
        GetterUtil.getString(expandoBridge.getAttribute("googleRefreshToken", false));

    googleCredential.setRefreshToken(googleRefreshToken);

    Drive.Builder driveBuilder = new Drive.Builder(httpTransport, jsonFactory, googleCredential);

    Drive drive = driveBuilder.build();

    Drive.About driveAbout = drive.about();

    Drive.About.Get driveAboutGet = driveAbout.get();

    About about = driveAboutGet.execute();

    return new GoogleDriveSession(drive, about.getRootFolderId());
  }
  @Override
  public void addUserAttributes(
      User user, String[] customUserAttributeNames, AssetEntryQuery assetEntryQuery)
      throws Exception {

    if ((user == null) || (customUserAttributeNames.length == 0)) {
      return;
    }

    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(user.getCompanyId());

    long[] allCategoryIds = assetEntryQuery.getAllCategoryIds();

    PrimitiveLongList allCategoryIdsList =
        new PrimitiveLongList(allCategoryIds.length + customUserAttributeNames.length);

    allCategoryIdsList.addAll(allCategoryIds);

    for (String customUserAttributeName : customUserAttributeNames) {
      ExpandoBridge userCustomAttributes = user.getExpandoBridge();

      Serializable userCustomFieldValue =
          userCustomAttributes.getAttribute(customUserAttributeName);

      if (userCustomFieldValue == null) {
        continue;
      }

      String userCustomFieldValueString = userCustomFieldValue.toString();

      List<AssetCategory> assetCategories =
          AssetCategoryLocalServiceUtil.search(
              companyGroup.getGroupId(),
              userCustomFieldValueString,
              new String[0],
              QueryUtil.ALL_POS,
              QueryUtil.ALL_POS);

      for (AssetCategory assetCategory : assetCategories) {
        allCategoryIdsList.add(assetCategory.getCategoryId());
      }
    }

    assetEntryQuery.setAllCategoryIds(allCategoryIdsList.getArray());
  }
 private static ExpandoBridge getUserExpandoBridge(PortletRequest request, User user)
     throws PortalException, SystemException {
   com.liferay.portal.model.User liferayUser = getLiferayUser(request, user);
   ensureUserCustomFieldExists(liferayUser, request);
   return liferayUser.getExpandoBridge();
 }