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.NuxeoPrincipal;
029import org.nuxeo.ecm.platform.usermanager.UserManager;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 *
034 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
035 *
036 */
037@XObject("userFilter")
038public class SegmentIOUserFilter {
039
040    @XNode("enableAnonymous")
041    protected boolean enableAnonymous = false;
042
043    @XNodeList(value = "blackListedUser", type = ArrayList.class, componentType = String.class)
044    protected List<String> blackListedUsers;
045
046    protected String anonymousUserId = null;
047
048    public boolean isEnableAnonymous() {
049        return enableAnonymous;
050    }
051
052    public List<String> getBlackListedUsers() {
053        return blackListedUsers;
054    }
055
056    public String getAnonymousUserId() {
057        if (anonymousUserId == null) {
058            UserManager um = Framework.getLocalService(UserManager.class);
059            if (um==null && Framework.isTestModeSet()) {
060                return "Guest";
061            }
062            anonymousUserId = um.getAnonymousUserId();
063        }
064        return anonymousUserId;
065    }
066
067    public boolean canTrack(String principalName) {
068
069        if (!enableAnonymous && principalName.equals(getAnonymousUserId())) {
070            return false;
071        }
072        if (blackListedUsers != null
073                && blackListedUsers.contains(principalName)) {
074            return false;
075        }
076        return true;
077    }
078
079}