001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.core;
018
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.forgerock.opendj.config.server.ConfigurationChangeListener;
025import org.forgerock.opendj.server.config.meta.RootDNCfgDefn;
026import org.forgerock.opendj.server.config.server.RootDNCfg;
027import org.forgerock.opendj.config.server.ConfigChangeResult;
028import org.opends.server.types.Privilege;
029
030/**
031 * This class defines a data structure that is used to handle changes to the set
032 * of default root privileges.
033 */
034public class RootPrivilegeChangeListener
035       implements ConfigurationChangeListener<RootDNCfg>
036{
037  /** The set of privileges that will be given to root users by default. */
038  private Set<Privilege> defaultRootPrivileges;
039
040  /** Creates a new instance of this root privilege change listener. */
041  public RootPrivilegeChangeListener()
042  {
043    defaultRootPrivileges = Privilege.getDefaultRootPrivileges();
044  }
045
046  @Override
047  public boolean isConfigurationChangeAcceptable(RootDNCfg configuration,
048                      List<LocalizableMessage> unacceptableReasons)
049  {
050    // No special validation is required.
051    return true;
052  }
053
054  @Override
055  public ConfigChangeResult applyConfigurationChange(RootDNCfg configuration)
056  {
057    setDefaultRootPrivileges(configuration);
058    return new ConfigChangeResult();
059  }
060
061  /**
062   * Retrieves the set of privileges that will be automatically granted to root
063   * users.
064   *
065   * @return  The set of privileges that will be automatically granted to root
066   *          users.
067   */
068  public Set<Privilege> getDefaultRootPrivileges()
069  {
070    return defaultRootPrivileges;
071  }
072
073  /**
074   * Specifies the set of privileges that will be automatically granted to root
075   * users.
076   *
077   * @param  configuration  The configuration object that specifies the set of
078   *                        privileges that will be automatically granted to
079   *                        root users.
080   */
081  void setDefaultRootPrivileges(RootDNCfg configuration)
082  {
083    Set<RootDNCfgDefn.DefaultRootPrivilegeName> configPrivSet =
084         configuration.getDefaultRootPrivilegeName();
085
086    HashSet<Privilege> privSet = new HashSet<>(configPrivSet.size());
087    for (RootDNCfgDefn.DefaultRootPrivilegeName p : configPrivSet)
088    {
089        privSet.add(Privilege.privilegeForName(p.toString()));
090    }
091
092    defaultRootPrivileges = privSet;
093  }
094}