// User registry.
// Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Globalization;
namespace GSF.Net.Snmp.Security
{
///
/// A repository to store user information for providers.
///
public sealed class UserRegistry
{
private readonly IDictionary _users = new Dictionary();
///
/// Initializes a new instance of the class.
///
/// The users.
// ReSharper disable ParameterTypeCanBeEnumerable.Local
public UserRegistry(User[] users)
// ReSharper restore ParameterTypeCanBeEnumerable.Local
{
if (users == null)
{
return;
}
foreach (User user in users)
{
Add(user);
}
}
///
/// Initializes a new instance of the class.
///
public UserRegistry() : this(null)
{
}
///
/// Returns the user count.
///
public int Count
{
get { return _users.Count; }
}
///
/// Adds the specified user name.
///
/// Name of the user.
/// The privacy provider.
public UserRegistry Add(OctetString userName, IPrivacyProvider privacy)
{
return Add(new User(userName, privacy));
}
///
/// Adds the specified user.
///
/// The user.
public UserRegistry Add(User user)
{
if (user == null)
{
return this;
}
if (_users.ContainsKey(user.Name))
{
_users.Remove(user.Name);
}
_users.Add(user.Name, user);
return this;
}
///
/// Finds the specified user name.
///
/// Name of the user.
///
public IPrivacyProvider Find(OctetString userName)
{
if (userName == null)
{
throw new ArgumentNullException(nameof(userName));
}
if (userName == OctetString.Empty)
{
// IMPORTANT: used in messagefactory.cs to decrypt discovery messages.
return DefaultPrivacyProvider.DefaultPair;
}
return _users.ContainsKey(userName) ? _users[userName].Privacy : null;
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "User registry: count: {0}", Count);
}
}
}