pub async fn assign_role_to_user(
pool: &Pool<MySql>,
user_id: i64,
role_id: i64,
) -> Result<()>
Expand description
Assigns a role to a user.
This function creates an association between a user and a role, granting the user all permissions associated with that role. This is a core operation in the RBAC system for controlling user access.
§Arguments
pool
- Database connection pool for executing the queryuser_id
- Unique identifier of the user to receive the rolerole_id
- Unique identifier of the role to assign
§Returns
Ok(())
- Successfully assigned the role to the userErr(anyhow::Error)
- Failed to assign the role
§Uniqueness
This function assumes that the combination of user_id
and role_id
must be unique in the role_user table. If this association already exists,
the operation will fail with a unique constraint violation.
§Transaction Handling
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
§Note
After this operation, the user will have all permissions associated with the assigned role, as determined by the permissions_role table.