from ..Playbooks.Playbook import PlaybookCredentialCollection, FileCleanupTask
from ...Logging.Logger import log, Severity
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION
from ..SetupStep import SetupStep

class SetLdapPassword(SetupStep):
    def __init__(self, ldap_path: str, password:str):
        super().__init__()
        self.ldap_path = ldap_path
        self.password = password

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        log(f'Changing password for {self.ldap_path}...', Severity.INFO)
        stdout, stderr, retval = MACHINE_COLLECTION.server.execute_shell_command('udm', 'users/user', 'modify', '--dn', self.ldap_path, '--set', f'password={self.password}', '--set', 'overridePWHistory=1', '--set', 'overridePWLength=1')
        if retval == 0:
            log('User modified.', Severity.DEBUG)
            return True
        else:
            log(f'Failed to modify LDAP user: {stderr}', Severity.ERROR)
            return False
