-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdb.updateUser.txt
343 lines (239 loc) · 8.86 KB
/
db.updateUser.txt
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
===============
db.updateUser()
===============
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.updateUser( username, update, writeConcern )
Updates the user's profile on the database on which you run the method.
An update to a field **completely replaces** the previous field's values.
This includes updates to the user's ``roles`` array.
.. warning::
When you update the ``roles`` array, you completely replace the
previous array's values. To add or remove roles without replacing all
the user's existing roles, use the :method:`db.grantRolesToUser()` or
:method:`db.revokeRolesFromUser()` methods.
The :method:`db.updateUser()` method uses the following syntax:
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
db.updateUser(
"<username>",
{
customData : { <any information> },
roles : [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
pwd: passwordPrompt(), // Or "<cleartext password>"
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>", | "<CIDR range>", ...]
},
...
],
mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ],
passwordDigestor: "<server|client>"
},
writeConcern: { <write concern> }
)
The :method:`db.updateUser()` method has the following arguments.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``username``
- string
- The name of the user to update.
* - ``update``
- document
- A document containing the replacement data for the user. This
data completely replaces the corresponding data for the user.
* - ``writeConcern``
- document
- .. include:: /includes/fact-write-concern-spec-link.rst
The ``update`` document specifies the fields to update and their
new values. All fields in the ``update`` document are optional,
but *must* include at least one field.
The ``update`` document has the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``customData``
- document
- Optional. Any arbitrary information.
* - ``roles``
- array
- Optional. The roles granted to the user. An update to the ``roles`` array
overrides the previous array's values.
* - ``pwd``
- string
- Optional. The user's password. The value can be either:
- the user's password in cleartext string, or
- :method:`passwordPrompt()` to prompt for the user's password.
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
* - ``authenticationRestrictions``
- array
- Optional. The authentication restrictions the server enforces upon the user.
Specifies a list of IP addresses and
:abbr:`CIDR (Classless Inter-Domain Routing)` ranges from which the
user is allowed to connect to the server or from which the server can
accept users.
* - ``mechanisms``
- array
- Optional. The specific SCRAM mechanism or mechanisms for the user credentials.
If :parameter:`authenticationMechanisms` is specified, you can only
specify a subset of the :parameter:`authenticationMechanisms`.
If updating the mechanisms field without the password, you can only
specify a subset of the user's current mechanisms, and only the
existing user credentials for the specified mechanism or mechanisms
are retained.
If updating the password along with the mechanisms, new set of
credentials are stored for the user.
Valid values are:
- ``"SCRAM-SHA-1"``
- Uses the ``SHA-1`` hashing function.
- ``"SCRAM-SHA-256"``
- Uses the ``SHA-256`` hashing function.
- Requires featureCompatibilityVersion set to ``4.0``.
- Requires passwordDigestor to be ``server``.
* - ``passwordDigestor``
- string
- Optional. Indicates whether the server or the client digests the password.
Available values are:
- ``"server"`` (Default)
The server receives undigested password from the client and
digests the password.
- ``"client"`` (Not compatible with ``SCRAM-SHA-256``)
The client digests the password and passes the digested
password to the server.
Roles
~~~~~
.. |local-cmd-name| replace:: :method:`db.updateUser()`
.. include:: /includes/fact-roles-array-contents.rst
Authentication Restrictions
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/fact-auth-restrictions-array-contents.rst
The :method:`db.updateUser()` method wraps the :dbcommand:`updateUser`
command.
Compatibility
-------------
This method is available in deployments hosted in the following
environments:
.. include:: /includes/fact-environments-no-atlas-support.rst
.. include:: /includes/fact-environments-onprem-only.rst
Behavior
--------
Replica set
~~~~~~~~~~~
.. |command| replace:: :method:`db.updateUser()`
.. include:: /includes/fact-management-methods-write-concern.rst
Encryption
~~~~~~~~~~
.. include:: /includes/fact-cleartext-passwords-tls.rst
Required Access
---------------
.. include:: /includes/access-update-user.rst
.. include:: /includes/access-change-own-password-and-custom-data.rst
Example
-------
Given a user ``appClient01`` in the ``products`` database with the following
user info:
.. code-block:: javascript
{
_id : "products.appClient01",
userId : UUID("c5d88855-3f1e-46cb-9c8b-269bef957986"),
user : "appClient01",
db : "products",
customData : { empID : "12345", badge : "9156" },
roles : [
{
role : "readWrite",
db : "products"
},
{
role : "read",
db : "inventory"
}
],
mechanisms : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
],
authenticationRestrictions : [ {
clientSource: ["69.89.31.226"],
serverAddress: ["172.16.254.1"]
} ]
}
The following :method:`db.updateUser()` method **completely** replaces the
user's ``customData`` and ``roles`` data:
.. code-block:: javascript
use products
db.updateUser( "appClient01",
{
customData : { employeeId : "0x3039" },
roles : [
{ role : "read", db : "assets" }
]
} )
The user ``appClient01`` in the ``products`` database now has the following
user information:
.. code-block:: javascript
{
_id : "products.appClient01",
userId : UUID("c5d88855-3f1e-46cb-9c8b-269bef957986"),
user : "appClient01",
db : "products",
customData : { employeeId : "0x3039" },
roles : [
{
role : "read",
db : "assets"
}
],
mechanisms : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
],
authenticationRestrictions : [ {
clientSource: ["69.89.31.226"],
serverAddress: ["172.16.254.1"]
} ]
}
Update User to Use ``SCRAM-SHA-256`` Credentials Only
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. note::
To use :ref:`SCRAM-SHA-256 <authentication-scram-sha-256>`, the
``featureCompatibilityVersion`` must be set to ``4.0``. For more
information on featureCompatibilityVersion, see :ref:`view-fcv` and
:dbcommand:`setFeatureCompatibilityVersion`.
The following operation updates a user who currently have both
``SCRAM-SHA-256`` and ``SCRAM-SHA-1`` credentials to have only
``SCRAM-SHA-256`` credentials.
.. note::
- If the password is not specified along with the ``mechanisms``,
you can only update the ``mechanisms`` to a subset of the current
SCRAM mechanisms for the user.
- If the password is specified along with the ``mechanisms``, you
can specify any supported SCRAM mechanism or mechanisms.
- For ``SCRAM-SHA-256``, the ``passwordDigestor`` must be the
default value ``"server"``.
.. code-block:: javascript
use reporting
db.updateUser(
"reportUser256",
{
mechanisms: [ "SCRAM-SHA-256" ]
}
)