protected List<NuxeoOAuth2Token> getTokens(String nxuser) { return Framework.doPrivileged(() -> { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(TOKEN_DIR)) { Map<String, Serializable> filter = new HashMap<>(); if (nxuser != null) { filter.put(NuxeoOAuth2Token.KEY_NUXEO_LOGIN, nxuser); } List<DocumentModel> docs = session.query(filter, Collections.emptySet(), Collections.emptyMap(), true, 0, 0); return docs.stream().map(NuxeoOAuth2Token::new).collect(Collectors.toList()); } }); }
protected List<DocumentModel> listCategories() { DirectoryService dm = Framework.getService(DirectoryService.class); try (Session session = dm.open(DIRECTORY_NAME)) { return session.query(Collections.<String, Serializable>emptyMap(), null, Collections.singletonMap("ordering", "ASC")); } }
protected List<DocumentModel> queryProviders(Map<String, Serializable> filter, int limit) { DirectoryService ds = Framework.getService(DirectoryService.class); return Framework.doPrivileged(() -> { try (Session session = ds.open(DIRECTORY_NAME)) { Set<String> fulltext = Collections.emptySet(); Map<String, String> orderBy = Collections.emptyMap(); return session.query(filter, fulltext, orderBy, true, limit, 0); } catch (DirectoryException e) { log.error("Error while fetching provider directory", e); return Collections.emptyList(); } }); }
@Override public List<OAuthToken> listAccessTokenForUser(String login) { List<OAuthToken> result = new ArrayList<OAuthToken>(); DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Serializable> filter = new HashMap<>(); filter.put("nuxeoLogin", login); filter.put("clientToken", 0); DocumentModelList entries = session.query(filter); for (DocumentModel entry : entries) { result.add(new NuxeoOAuthToken(entry)); } } catch (DirectoryException e) { log.error("Error during token listing", e); } return result; }
@Override public DataStore<StoredCredential> delete(String key) throws IOException { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Serializable> filter = new HashMap<>(); filter.put("serviceName", serviceName); filter.put(ENTRY_ID, key); DocumentModelList entries = session.query(filter); for (DocumentModel entry : entries) { session.deleteEntry(entry); } } return this; }
@Override public List<OAuthToken> listAccessTokenForConsumer(String consumerKey) { List<OAuthToken> result = new ArrayList<OAuthToken>(); DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Serializable> filter = new HashMap<String, Serializable>(); filter.put("consumerKey", consumerKey); filter.put("clientToken", 0); DocumentModelList entries = session.query(filter); for (DocumentModel entry : entries) { result.add(new NuxeoOAuthToken(entry)); } } catch (DirectoryException e) { log.error("Error during token listing", e); } return result; }
public boolean canDelete(DirectoryService dirService, String entryId) { try (Session dirSession = dirService.open(targetDirectory)) { // search for given entry id usage in this directory Map<String, Serializable> filter = new HashMap<String, Serializable>(); filter.put(targetDirectoryField, entryId); DocumentModelList res = dirSession.query(filter); if (res.isEmpty()) { return true; } if (log.isDebugEnabled()) { log.debug("Can not delete " + targetDirectory + " " + entryId + ", constraint on " + targetDirectoryField + ":" + res.get(0).getId()); } return false; } }
@Override public List<NuxeoOAuthConsumer> listConsumers() { return Framework.doPrivileged(() -> { try { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { DocumentModelList entries = session.query(Collections.emptyMap()); List<NuxeoOAuthConsumer> result = new ArrayList<NuxeoOAuthConsumer>(); for (DocumentModel entry : entries) { result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null)); } return result; } } catch (DirectoryException e) { log.error("Error while fetching consumer directory", e); return Collections.emptyList(); } }); }
public Object[] getChildren(Object obj) { if (obj == null || obj instanceof DocumentModel) { DocumentModel parent = (DocumentModel) obj; Map<String, Serializable> args = new HashMap<String, Serializable>(); createQuery(parent, args); DocumentModelList list = session.query(args); return list.toArray(new DocumentModel[list.size()]); } return null; }
public void delete(String token, String clientId) { DirectoryService ds = Framework.getService(DirectoryService.class); Framework.doPrivileged(() -> { try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Serializable> filter = new HashMap<String, Serializable>(); filter.put("serviceName", serviceName); filter.put("clientId", clientId); filter.put("accessToken", token); DocumentModelList entries = session.query(filter); for (DocumentModel entry : entries) { session.deleteEntry(entry); } } }); }
@Override public List<NuxeoOAuthServiceProvider> listProviders() { List<NuxeoOAuthServiceProvider> result = new ArrayList<NuxeoOAuthServiceProvider>(); for (NuxeoOAuthServiceProvider provider : inMemoryProviders.values()) { result.add(provider); } DirectoryService ds = Framework.getService(DirectoryService.class); Framework.doPrivileged(() -> { try (Session session = ds.open(DIRECTORY_NAME)) { DocumentModelList entries = session.query(Collections.emptyMap()); for (DocumentModel entry : entries) { result.add(NuxeoOAuthServiceProvider.createFromDirectoryEntry(entry)); } } catch (DirectoryException e) { log.error("Error while fetching provider directory", e); } }); return result; } }
/** * @since 9.10 */ public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy) { DirectoryService ds = Framework.getService(DirectoryService.class); return Framework.doPrivileged(() -> { try (Session session = ds.open(DIRECTORY_NAME)) { filter.put("serviceName", serviceName); return session.query(filter, fulltext, orderBy); } }); }
@Override public List<DirectoryEntry> getCurrentPage() { Object[] parameters = getParameters(); if (parameters == null || parameters.length > 1) { throw new IllegalStateException("Invalid parameters: " + Arrays.toString(parameters)); } if (!(parameters[0] instanceof Directory)) { throw new IllegalStateException("Provided parameter is not a Directory: " + parameters[0]); } Directory directory = (Directory) parameters[0]; try (Session session = directory.getSession()) { return session.query(Collections.emptyMap(), Collections.emptySet(), Collections.emptyMap(), false, (int) getPageSize(), (int) getCurrentPageOffset()) .stream() .map(dir -> new DirectoryEntry(directory.getName(), dir)) .collect(Collectors.toList()); } } }
public boolean clearPersistentList(String userName, String listName) { if (!initPersistentService()) { return false; } try { Map<String, Serializable> filter = new HashMap<>(); filter.put(DIR_COL_LISTID, listName); filter.put(DIR_COL_USERID, userName); try { DocumentModelList entriesToDelete = dirSession.query(filter); for (DocumentModel entry : entriesToDelete) { dirSession.deleteEntry(entry.getId()); } } catch (DirectoryException e) { log.error("Unable to clear DocumentList", e); return false; } return true; } finally { releasePersistenceService(); } }
protected DocumentModelList query(Map<String, Serializable> filter) { DirectoryService ds = Framework.getService(DirectoryService.class); return Framework.doPrivileged(() -> { try (Session session = ds.open(DIRECTORY_NAME)) { return session.query(filter); } }); } }
protected List<DocumentModel> queryClients() { DirectoryService service = Framework.getService(DirectoryService.class); return Framework.doPrivileged(() -> { try (Session session = service.open(OAUTH2CLIENT_DIRECTORY_NAME)) { return session.query(Collections.emptyMap()); } catch (DirectoryException e) { throw new NuxeoException("Error while fetching client directory", e); } }); } }
@Override public void removeClientAccessToken(String appId, String owner) { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Serializable> filter = new HashMap<String, Serializable>(); filter.put("appId", appId); filter.put("clientId", owner); filter.put("clientToken", 1); DocumentModelList entries = session.query(filter); if (entries.size() == 0) { return; } if (entries.size() > 1) { log.error("Found several tokens"); } session.deleteEntry(entries.get(0)); } }
@Override public NuxeoOAuthToken getClientAccessToken(String appId, String owner) { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Serializable> filter = new HashMap<String, Serializable>(); filter.put("appId", appId); filter.put("clientId", owner); filter.put("clientToken", 1); DocumentModelList entries = session.query(filter); if (entries.size() == 0) { return null; } if (entries.size() > 1) { log.error("Found several tokens"); } return getTokenFromDirectoryEntry(entries.get(0)); } }
protected DocumentModel getClientModel(String clientId) { DirectoryService service = Framework.getService(DirectoryService.class); return Framework.doPrivileged(() -> { try (Session session = service.open(OAUTH2CLIENT_DIRECTORY_NAME)) { Map<String, Serializable> filter = Collections.singletonMap("clientId", clientId); DocumentModelList docs = session.query(filter); if (docs.size() == 1) { return docs.get(0); } else if (docs.size() > 1) { throw new NuxeoException( String.format("More than one client registered for the '%s' id", clientId)); } } return null; }); }
public DocumentModelList getEntries() { if (entries == null) { DirectoryService ds = Framework.getService(DirectoryService.class); Framework.doPrivileged(() -> { try (Session session = ds.open(getDirectoryName())) { Map<String, Serializable> emptyMap = getQueryFilter(); Set<String> emptySet = getOrderSet(); entries = session.query(emptyMap, emptySet, null, true); } }); } return entries; }