SCAutolib.models.user

This module defines the User and IPAUser classes, which are designed to represent and manage system and FreeIPA users within the SCAutolib framework.

These classes encapsulate user properties like username and password, and implement methods for common user management operations such as adding and deleting users from either the local system or a specified IPA server.

Classes

class IPAUser(ipa_server, *args, **kwargs)[source]

Represents an IPA (Identity Management for Linux) user. This class extends the base User class to include specific functionalities for managing users within an IPA server environment, primarily through the python_freeipa library.

Initializes an IPAUser object. IPA client should be configured first before creating an IPA user through this class. It requires an IPAServerCA object to facilitate communication with the IPA server and inherits user attributes from the base User class.

Parameters:
  • ipa_server (SCAutolib.models.CA.IPAServerCA) – An IPAServerCA object that provides the necessary IPA server hostname and ClientMeta object for interaction.

  • username (str) – The username for the system user.

  • password (str) – The password for the system user.

Returns:

None

Return type:

None

add_user()[source]

Adds the IPA user to the IPA server using the python_freeipa client. It sets a default password and then changes it to the specified password to avoid requiring a password change on first login.

Returns:

None

Return type:

None

Raises:

SCAutolibException – If the user already exists on the IPA server.

delete_user()[source]

Deletes the IPA user from the IPA server using the python_freeipa client. If the user is not found on the server, the operation is silently ignored.

Returns:

None

Return type:

None

to_dict()[source]

Converts the IPAUser object’s attributes into a dictionary for JSON serialization. It calls the base User.to_dict() method and then removes internal _meta_client and _ipa_hostname attributes, which are not directly serializable.

Returns:

A dictionary representation of the IPA user object’s attributes.

Return type:

dict

class User(username, password)[source]

Represents a general system user, typically a local user account on the machine where SCAutolib is running. It holds user properties like username and password, and provides methods to manage the user’s presence on the local system. User objects can be serialized to and loaded from JSON dump files for persistence across SCAutolib runs.

Initializes a User object for a local system user.

Parameters:
  • username (str) – The username for the system user.

  • password (str) – The password for the system user.

Returns:

None

Return type:

None

add_user()[source]

Adds the user to the local system using the useradd system management command and sets their password via passwd --stdin. It checks if the user already exists to prevent collisions.

Returns:

None

Return type:

None

Raises:

SCAutolibException – If the user already exists on the system.

delete_user()[source]

Deletes the local user from the system using the userdel -f command. It also removes the corresponding JSON dump file for the user.

Returns:

None

Return type:

None

static load(json_file, **kwargs)[source]

Loads user data from a specified JSON file and reconstructs the corresponding User or IPAUser object. It determines the correct class to instantiate based on the user_type field in the JSON content.

Parameters:
  • json_file (pathlib.Path) – The pathlib.Path object pointing to the JSON file from which to read the user’s data.

  • kwargs (dict) – Additional keyword arguments that might be necessary to initialize the user object, particularly for IPAUser which requires an ipa_server object.

Returns:

An initialized User or IPAUser object loaded with data from the JSON file.

Return type:

SCAutolib.models.user.User or SCAutolib.models.user.IPAUser

Raises:

SCAutolibException – If an unknown user type is encountered in the JSON data, or if ipa_server is not provided for an IPA user.

to_dict()[source]

Converts the User object’s attributes into a dictionary suitable for JSON serialization.

Returns:

A dictionary representation of the user object’s attributes.

Return type:

dict