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-2015 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.opends.server.admin.server.ConfigurationChangeListener;
025import org.opends.server.admin.std.meta.RootDNCfgDefn;
026import org.opends.server.admin.std.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  /** {@inheritDoc} */
047  @Override
048  public boolean isConfigurationChangeAcceptable(RootDNCfg configuration,
049                      List<LocalizableMessage> unacceptableReasons)
050  {
051    // No special validation is required.
052    return true;
053  }
054
055  /** {@inheritDoc} */
056  @Override
057  public ConfigChangeResult applyConfigurationChange(RootDNCfg configuration)
058  {
059    setDefaultRootPrivileges(configuration);
060    return new ConfigChangeResult();
061  }
062
063  /**
064   * Retrieves the set of privileges that will be automatically granted to root
065   * users.
066   *
067   * @return  The set of privileges that will be automatically granted to root
068   *          users.
069   */
070  public Set<Privilege> getDefaultRootPrivileges()
071  {
072    return defaultRootPrivileges;
073  }
074
075
076
077  /**
078   * Specifies the set of privileges that will be automatically granted to root
079   * users.
080   *
081   * @param  configuration  The configuration object that specifies the set of
082   *                        privileges that will be automatically granted to
083   *                        root users.
084   */
085  void setDefaultRootPrivileges(RootDNCfg configuration)
086  {
087    Set<RootDNCfgDefn.DefaultRootPrivilegeName> configPrivSet =
088         configuration.getDefaultRootPrivilegeName();
089
090    HashSet<Privilege> privSet = new HashSet<>(configPrivSet.size());
091    for (RootDNCfgDefn.DefaultRootPrivilegeName p : configPrivSet)
092    {
093        privSet.add(Privilege.privilegeForName(p.toString()));
094    }
095
096    defaultRootPrivileges = privSet;
097  }
098}