001/*
002 * (C) Copyright 2006-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 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.security;
020
021import java.security.Principal;
022
023import org.nuxeo.ecm.core.query.sql.model.Operator;
024import org.nuxeo.ecm.core.query.sql.model.Predicate;
025import org.nuxeo.ecm.core.query.sql.model.Reference;
026import org.nuxeo.ecm.core.query.sql.model.SQLQuery;
027import org.nuxeo.ecm.core.query.sql.model.SQLQuery.Transformer;
028import org.nuxeo.ecm.core.query.sql.model.StringLiteral;
029import org.nuxeo.ecm.core.query.sql.model.WhereClause;
030
031/**
032 * Dummy security policy denying all access to File objects with a query transformer.
033 *
034 * @author Florent Guillaume
035 */
036public class NoFile2SecurityPolicy extends NoFileSecurityPolicy {
037
038    @Override
039    public boolean isExpressibleInQuery() {
040        return true;
041    }
042
043    /**
044     * Transformer that adds {@code AND ecm:primaryType <> 'File'} to the query.
045     */
046    public static class NoFileTransformer implements Transformer {
047        private static final long serialVersionUID = 1L;
048
049        public static final Predicate NO_FILE = new Predicate(new Reference("ecm:primaryType"), Operator.NOTEQ,
050                new StringLiteral("File"));
051
052        @Override
053        public SQLQuery transform(Principal principal, SQLQuery query) {
054            WhereClause where = query.where;
055            Predicate predicate;
056            if (where == null || where.predicate == null) {
057                predicate = NO_FILE;
058            } else {
059                predicate = new Predicate(NO_FILE, Operator.AND, where.predicate);
060            }
061            SQLQuery newQuery = new SQLQuery(query.select, query.from, new WhereClause(predicate), query.groupBy,
062                    query.having, query.orderBy, query.limit, query.offset);
063            return newQuery;
064        }
065    }
066
067    public static final Transformer NO_FILE_TRANSFORMER = new NoFileTransformer();
068
069    @Override
070    public Transformer getQueryTransformer() {
071        return NO_FILE_TRANSFORMER;
072    }
073
074}