001/*
002 * (C) Copyright 2014 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 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.segment.io;
020
021import java.security.Principal;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeList;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.core.api.NuxeoPrincipal;
030import org.nuxeo.ecm.platform.usermanager.UserManager;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 *
035 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
036 *
037 */
038@XObject("userFilter")
039public class SegmentIOUserFilter {
040
041    @XNode("enableAnonymous")
042    protected boolean enableAnonymous = false;
043
044    @XNodeList(value = "blackListedUser", type = ArrayList.class, componentType = String.class)
045    protected List<String> blackListedUsers;
046
047    protected String anonymousUserId = null;
048
049    public boolean isEnableAnonymous() {
050        return enableAnonymous;
051    }
052
053    public List<String> getBlackListedUsers() {
054        return blackListedUsers;
055    }
056
057    public String getAnonymousUserId() {
058        if (anonymousUserId == null) {
059            UserManager um = Framework.getService(UserManager.class);
060            if (um == null) {
061                if (Framework.isTestModeSet()) {
062                    return "Guest";
063                }
064                throw new NuxeoException("Missing UserManager");
065            }
066            anonymousUserId = um.getAnonymousUserId();
067        }
068        return anonymousUserId;
069    }
070
071    public boolean canTrack(String principalName) {
072
073        if (!enableAnonymous && principalName.equals(getAnonymousUserId())) {
074            return false;
075        }
076        if (blackListedUsers != null
077                && blackListedUsers.contains(principalName)) {
078            return false;
079        }
080        return true;
081    }
082
083}