001/*
002 * (C) Copyright 2014-2020 Nuxeo (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.dbs;
020
021import static org.nuxeo.ecm.core.storage.dbs.DBSDocument.KEY_ACP;
022import static org.nuxeo.ecm.core.storage.dbs.DBSDocument.KEY_ID;
023import static org.nuxeo.ecm.core.storage.dbs.DBSDocument.KEY_NAME;
024import static org.nuxeo.ecm.core.storage.dbs.DBSDocument.KEY_PRIMARY_TYPE;
025
026import org.nuxeo.ecm.core.api.security.ACE;
027import org.nuxeo.ecm.core.api.security.SecurityConstants;
028import org.nuxeo.ecm.core.api.security.impl.ACLImpl;
029import org.nuxeo.ecm.core.api.security.impl.ACPImpl;
030import org.nuxeo.ecm.core.storage.State;
031
032/**
033 * Interface for a connection to a {@link DBSRepository}. The connection maintains state when it is transactional.
034 *
035 * @since 11.1
036 */
037public abstract class DBSConnectionBase implements DBSConnection {
038
039    protected final DBSRepositoryBase repository;
040
041    public DBSConnectionBase(DBSRepositoryBase repository) {
042        this.repository = repository;
043    }
044
045    @Override
046    public String getRootId() {
047        if (DBSRepositoryBase.DEBUG_UUIDS) {
048            return DBSRepositoryBase.UUID_ZERO_DEBUG;
049        }
050        switch (repository.getIdType()) {
051        case varchar:
052        case uuid:
053            return DBSRepositoryBase.UUID_ZERO;
054        case sequence:
055            return "0";
056        case sequenceHexRandomized:
057            return "0000000000000000";
058        default:
059            throw new UnsupportedOperationException();
060        }
061    }
062
063    /**
064     * Initializes the root and its ACP.
065     */
066    public void initRoot() {
067        State state = new State();
068        state.put(KEY_ID, getRootId());
069        state.put(KEY_NAME, "");
070        state.put(KEY_PRIMARY_TYPE, DBSRepositoryBase.TYPE_ROOT);
071        state.put(KEY_ACP, DBSSession.acpToMem(getRootACP()));
072        createState(state);
073    }
074
075    protected ACPImpl getRootACP() {
076        ACLImpl acl = new ACLImpl();
077        acl.add(new ACE(SecurityConstants.ADMINISTRATORS, SecurityConstants.EVERYTHING, true));
078        acl.add(new ACE(SecurityConstants.ADMINISTRATOR, SecurityConstants.EVERYTHING, true));
079        acl.add(new ACE(SecurityConstants.MEMBERS, SecurityConstants.READ, true));
080        ACPImpl acp = new ACPImpl();
081        acp.addACL(acl);
082        return acp;
083    }
084
085}