001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 * Contributors:
016 *     Thomas Roger <troger@nuxeo.com>
017 */
018
019package org.nuxeo.ecm.user.center;
020
021import static org.jboss.seam.ScopeType.CONVERSATION;
022
023import java.io.Serializable;
024import java.util.Collections;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.NuxeoPrincipal;
035import org.nuxeo.ecm.directory.Session;
036import org.nuxeo.ecm.directory.api.DirectoryService;
037import org.nuxeo.ecm.platform.oauth.tokens.OAuthTokenStoreImpl;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
042 */
043@Name("authorizedApplicationsActions")
044@Scope(CONVERSATION)
045public class AuthorizedApplicationsActions implements Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    @In
050    protected NuxeoPrincipal currentUser;
051
052    public List<DocumentModel> getAuthorizedApplications() {
053        DirectoryService directoryService = Framework.getService(DirectoryService.class);
054        try (Session session = directoryService.open(OAuthTokenStoreImpl.DIRECTORY_NAME)) {
055            Map<String, Serializable> queryFilter = getQueryFilter();
056            Set<String> emptySet = Collections.emptySet();
057            return session.query(queryFilter, emptySet, null, true);
058        }
059    }
060
061    protected Map<String, Serializable> getQueryFilter() {
062        Map<String, Serializable> filter = new HashMap<String, Serializable>();
063        filter.put("clientToken", new Integer(0));
064        filter.put("nuxeoLogin", currentUser.getName());
065        return filter;
066    }
067
068    public void revokeAccess(String id) {
069        DirectoryService directoryService = Framework.getService(DirectoryService.class);
070        try (Session session = directoryService.open(OAuthTokenStoreImpl.DIRECTORY_NAME)) {
071            session.deleteEntry(id);
072        }
073    }
074
075}