Skip to content

re-implement the `change_password` function

I have removed the old code for the change_password function:

def change_password(config, dm_user, dm_old_pass, dm_new_pass):
    """
    This function changes a user's password
    """
    ###
    ### Needs testing and might need adopting to DMX
    ###
    authstring = bytes((str(dm_user + ':' + dm_old_pass)), 'UTF-8')
    base64string = (base64.b64encode(authstring)).decode('UTF-8')

    # get id of user_account (not user_name!)
    url = 'core/topic/by_type/dmx.accesscontrol.user_account?children=false'
    topic_id = read_request(config, url)
    print("change Password - Topic ID of user: %s" % topic_id)

    # get id of private workspace
    url = 'core/topic?type_uri=dmx.workspaces.workspace_name&query=Private%%20Workspace'
    wsnameid = read_request(config, url)["topics"][0]["id"]
    url = ('core/topic/%s/related_topics'
           '?assoc_type_uri=dmx.core.composition&my_role_type_uri='
           'dmx.core.child&others_role_type_uri=dmx.core.parent&'
           'others_topic_type_uri=dmx.workspaces.workspace' % str(wsnameid)
          )
    wsid = read_request(config, url)
    print("Change Password WS ID = %s" % wsid)

    # change password
    jsessionid = get_session_id(config)
    url = get_host_url(config) + ('/core/topic/%s' % (topic_id))
    req = urllib.request.Request(url)
    req.add_header("Cookie", "JSESSIONID=%s" % jsessionid)
    req.add_header("Content-Type", "application/json")
    req.get_method = lambda: 'PUT'
    # encrypt the new password
    hash_object = hashlib.sha256(dm_new_pass)
    dm_new_pass = '-SHA256-'+hash_object.hexdigest()
    payload = {
        'children': {
            'dmx.accesscontrol.password': dm_new_pass
        }
    }
    try:
        response = (
            json.loads(
                urllib.request.urlopen(
                    req, (json.dumps(payload))
                ).read()
            )
        )
    except urllib.error.HTTPError as e:
        print('Change Password Error: '+str(e))
    else:
        print(response)

It should be adopted to the latest script version and to the resent DMX api.