-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
MongoUser.cs
219 lines (203 loc) · 8.01 KB
/
MongoUser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Copyright 2010-2013 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace MongoDB.Driver
{
/// <summary>
/// Represents a MongoDB user.
/// </summary>
[Serializable]
[Obsolete("Use the new user management commands instead.")]
public class MongoUser : IEquatable<MongoUser>
{
// private fields
private string _username;
private string _passwordHash;
private bool _isReadOnly;
// constructors
/// <summary>
/// Creates a new instance of MongoUser.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="isReadOnly">Whether the user has read-only access.</param>
public MongoUser(string username, PasswordEvidence password, bool isReadOnly)
{
if (username == null)
{
throw new ArgumentNullException("username");
}
if (password == null)
{
throw new ArgumentNullException("password");
}
_username = username;
_passwordHash = HashPassword(username, password);
_isReadOnly = isReadOnly;
}
/// <summary>
/// Creates a new instance of MongoUser.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="passwordHash">The password hash.</param>
/// <param name="isReadOnly">Whether the user has read-only access.</param>
public MongoUser(string username, string passwordHash, bool isReadOnly)
{
if (username == null)
{
throw new ArgumentNullException("username");
}
if (passwordHash == null)
{
throw new ArgumentNullException("passwordHash");
}
_username = username;
_passwordHash = passwordHash;
_isReadOnly = isReadOnly;
}
// public properties
/// <summary>
/// Gets or sets the username.
/// </summary>
public string Username
{
get { return _username; }
set {
if (value == null)
{
throw new ArgumentNullException("value");
}
_username = value;
}
}
/// <summary>
/// Gets or sets the password hash.
/// </summary>
public string PasswordHash
{
get { return _passwordHash; }
set {
if (value == null)
{
throw new ArgumentNullException("value");
}
_passwordHash = value;
}
}
/// <summary>
/// Gets or sets whether the user is a read-only user.
/// </summary>
public bool IsReadOnly
{
get { return _isReadOnly; }
set { _isReadOnly = value; }
}
// public operators
/// <summary>
/// Determines whether two specified MongoUser objects have different values.
/// </summary>
/// <param name="lhs">The first value to compare, or null.</param>
/// <param name="rhs">The second value to compare, or null.</param>
/// <returns>True if the value of lhs is different from the value of rhs; otherwise, false.</returns>
public static bool operator !=(MongoUser lhs, MongoUser rhs)
{
return !MongoUser.Equals(lhs, rhs);
}
/// <summary>
/// Determines whether two specified MongoUser objects have the same value.
/// </summary>
/// <param name="lhs">The first value to compare, or null.</param>
/// <param name="rhs">The second value to compare, or null.</param>
/// <returns>True if the value of lhs is the same as the value of rhs; otherwise, false.</returns>
public static bool operator ==(MongoUser lhs, MongoUser rhs)
{
return MongoUser.Equals(lhs, rhs);
}
// public static methods
/// <summary>
/// Determines whether two specified MongoUser objects have the same value.
/// </summary>
/// <param name="lhs">The first value to compare, or null.</param>
/// <param name="rhs">The second value to compare, or null.</param>
/// <returns>True if the value of lhs is the same as the value of rhs; otherwise, false.</returns>
public static bool Equals(MongoUser lhs, MongoUser rhs)
{
if ((object)lhs == null) { return (object)rhs == null; }
return lhs.Equals(rhs);
}
/// <summary>
/// Calculates the password hash.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <returns>The password hash.</returns>
public static string HashPassword(string username, PasswordEvidence password)
{
return password.ComputeMongoCRPasswordDigest(username);
}
/// <summary>
/// Calculates the password hash.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <returns>The password hash.</returns>
public static string HashPassword(string username, string password)
{
return HashPassword(username, new PasswordEvidence(password));
}
// public methods
/// <summary>
/// Determines whether this instance and another specified MongoUser object have the same value.
/// </summary>
/// <param name="rhs">The MongoUser object to compare to this instance.</param>
/// <returns>True if the value of the rhs parameter is the same as this instance; otherwise, false.</returns>
public bool Equals(MongoUser rhs)
{
if ((object)rhs == null || GetType() != rhs.GetType()) { return false; }
if ((object)this == (object)rhs) { return true; }
return _username.Equals(rhs._username) && _passwordHash.Equals(rhs._passwordHash) && _isReadOnly.Equals(rhs._isReadOnly);
}
/// <summary>
/// Determines whether this instance and a specified object, which must also be a MongoUser object, have the same value.
/// </summary>
/// <param name="obj">The MongoUser object to compare to this instance.</param>
/// <returns>True if obj is a MongoUser object and its value is the same as this instance; otherwise, false.</returns>
public override bool Equals(object obj)
{
return Equals(obj as MongoUser); // works even if obj is null or of a different type
}
/// <summary>
/// Returns the hash code for this Class1 object.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
// see Effective Java by Joshua Bloch
int hash = 17;
hash = 37 * hash + _username.GetHashCode();
hash = 37 * hash + _passwordHash.GetHashCode();
hash = 37 * hash + _isReadOnly.GetHashCode();
return hash;
}
/// <summary>
/// Returns a string representation of the user.
/// </summary>
/// <returns>A string representation of the user.</returns>
public override string ToString()
{
return string.Format("User:{0}", _username);
}
}
}