User
A User is a non-encryptable, root-level entity that represents an actor that can log in to the system and perform actions according to their permissions.
Only a user that is a data owner (i.e. is associated to a HealthcareParty, Patient, or Device) and has initialized their own encryption keys can perform operations on the encryptable fields of encryptable entities. A user that is an admin or that has the correct permission can access encryptable entities, but they will only be able to read and write the non-encryptable fields.
For more information, check the explanation on data owners and access control.
Login Credentials​
In order to login, a user must provide valid credentials (namely, a login and a password) to the Cardinal SDK. Any of these field of the User entity is valid as login:
idloginemailmobilePhone
As for the password, it is possible both to set a password (in the passwordHash field) or to use a temporary token.
Updating a user's login identifiers​
You can change the email, mobile phone, and password of a user with a regular modifyUser, but this requires the
latest revision of the user, and may fail if the user is modified concurrently (for example by a SmartAuthProvider
that updates the authentication tokens of the user while performing the request).
For this reason, the user API provides dedicated methods that don't require the revision of the user:
modifyUserEmail(userId, newEmail, previousEmail)andmodifyUserMobilePhone(userId, newMobilePhone, previousMobilePhone)(since SDK 2.2.0) replace the email or mobile phone of the user.removeUserMobilePhone(userId, previousMobilePhone)(since SDK 2.11.0) removes the mobile phone of the user.modifyUserPassword(userId, newPassword)(since SDK 2.2.0) sets a new password for the user.
Instead of the revision, the email and mobile phone methods take the value you expect to be currently stored in the
user (the previous parameters are nullable, but not optional): if it doesn't match the stored value, the method fails
with a RevisionConflictException.
modifyUserPassword doesn't require the previous password, so you can also use it to implement a "forgot password"
flow. However, changing the password of a user is a sensitive operation
that requires an elevated security context, so you should use it with an SDK initialized with a SmartAuthProvider
(for example through an authentication secret provider)
that can provide the required credentials when needed.
All these methods except removeUserMobilePhone also have an in-group variant, which takes the id of the group of the
user as first parameter.
To change the roles of a user, see modify user roles.
- Kotlin
- Typescript
- Python
import com.icure.cardinal.sdk.CardinalSdk
import com.icure.cardinal.sdk.model.User
suspend fun updateLoginIdentifiers(
sdk: CardinalSdk,
userId: String,
newEmail: String,
newPassword: String
): User {
val user = sdk.user.getUser(userId)!!
// Fails with a RevisionConflictException if the email of the user is no longer user.email
sdk.user.modifyUserEmail(userId, newEmail, user.email)
if (user.mobilePhone != null) {
sdk.user.removeUserMobilePhone(userId, user.mobilePhone)
}
return sdk.user.modifyUserPassword(userId, newPassword)
}
import {CardinalSdk, User} from "@icure/cardinal-sdk";
async function updateLoginIdentifiers(
sdk: CardinalSdk,
userId: string,
newEmail: string,
newPassword: string
): Promise<User> {
const user = (await sdk.user.getUser(userId))!
// Fails with a RevisionConflictException if the email of the user is no longer user.email
await sdk.user.modifyUserEmail(userId, newEmail, user.email)
if (user.mobilePhone !== undefined) {
await sdk.user.removeUserMobilePhone(userId, user.mobilePhone)
}
return sdk.user.modifyUserPassword(userId, newPassword)
}
from cardinal_sdk import CardinalSdk
from cardinal_sdk.model import User
def update_login_identifiers(sdk: CardinalSdk, user_id: str, new_email: str, new_password: str) -> User:
user = sdk.user.get_user_blocking(user_id)
# Fails with a RevisionConflictException if the email of the user is no longer user.email
sdk.user.modify_user_email_blocking(user_id, new_email, user.email)
if user.mobile_phone is not None:
sdk.user.remove_user_mobile_phone_blocking(user_id, user.mobile_phone)
return sdk.user.modify_user_password_blocking(user_id, new_password)
Roles​
The roles assigned to a User are stored in a nested object inside the systemMetadata property. It has 3 properties:
isAdminis a boolean field that istrueif the user is an admin.rolesis a set of the names of all the roles assigned to the user.inheritsRolesis a boolean field that istrueif the user has no role set and so inherits the roles from the group configuration.
Any update to this property will be prohibited by the backend. To learn how to update the roles on a user, check this how to.
Data Owner Users​
A User is a Data Owner User if exactly one of the following is true:
- They have the id of a valid HealthcareParty in the
healthcarePartyIdfield. - They have the id of a valid Patient in the
patientIdfield. - They have the id of a valid Device in the
deviceIdfield.
A User can be associate either to a HealthcareParty, or to a Patient, or to a Device, and it cannot be associated to more than one of those entities.
When a Data Owner logs in, the SDK loads their available encryption keys or creates new one if no key is available. Then, the user will be able to create, modify and search encrypted data. You can read more about data owner users here
Properties​
Below you will find an explanation of the most commonly used properties in the entity that are not among the shared fields. For a full list, check the reference documentation (🚧).
authenticationTokens​
Contains all the active authentication tokens for the user, where the hash of the token is replaced by the character
*.
This field is useful to check how many tokens are currently active and their duration, and it can be used to remove an active token, by deleting it from the map.
While it is possible to manually creating a token by adding it to this map, it is preferable to use the getToken method
of the SDK. More information about application tokens can be found in this how to.
deviceId​
The id of the Device associated to this User. If this field is not null,
then the healthcarePartyId and patientId fields should be null.
A User where this field is not null is a Data Owner User.
email​
The email of the User. It can be used as username to log in.
groupId​
The id of the group where this user belongs.
healthcarePartyId​
The id of the HealthcareParty associated to this User. If this field
is not null, then the patientId and deviceId fields should be null.
A User where this field is not null is a Data Owner User.
login​
A username for the User. This field can be used in the log in phase.
mobilePhone​
The mobilePhone of the User. It can be used as username to log in.
name​
A free-text field that contains the name of the User.
passwordHash​
If the current User has a password, this field will contain the character *, otherwise it will be null.
This field can be used to create or update the password for the User. When the user is created or updated, if this field
contains a clear-text password, the backend will store it hashed and salted.
If a User logged in with a long token (i.e. an authentication token with a duration > 5 minutes), they will not be able to create or update their own password or the password of any other user, even if they have the permission to do so.
patientId​
The id of the Patient associated to this User. If this field is not null,
then the healthcarePartyId and deviceId fields should be null.
A User where this field is not null is a Data Owner User.
systemMetadata​
This field contains internal information about the User. Its properties are:
isAdminis a boolean field that istrueif the user is an admin.rolesis a set of the ids of all the roles assigned to the user.inheritsRolesis a boolean field that istrueif the user has no role set and so inherits the roles from the group configuration.loginIdentifiersis a list of the identifiers that the user can use to log in.verifiedEmailis a boolean field that istrueif the email of the user has been verified.verifiedMobilePhoneis a boolean field that istrueif the mobile phone of the user has been verified.uses2fais a boolean field that istrueif the user has two-factor authentication enabled for login with password.
Any update to this property will be prohibited by the backend. To learn how to update the roles on a user, check this how to.
status​
The status of the User. The values can be Active, Disabled, or Registering. Note that if a User has a Disabled
status, it will not be able to log in.