Skip To Content

示例:转移项目所有权

本示例将一个门户成员拥有的全部内容的所有权转移给另一个成员。如果您试图移除成员(无法移除拥有内容或组的成员),则可能需要转移所有权。

运行此脚本时,您需要指定以下信息:

  • 用于访问 Portal for ArcGIS 的 URL。该 URL 可以是 ArcGIS Web Adaptor URL(例如,https://webadaptorhost.domain.com/webadaptorname),也可以是托管门户的计算机 URL(例如,https://portal.domain.com:7443/arcgis)。指定 URL 时必须将计算机的完全限定域名包括在内。
  • 具有门户管理权限的帐户的用户名和密码。
  • 内容将被转移的帐户的用户名,例如,sarah
  • 将接收所转移内容的帐户的用户名,例如,robert

下面的示例会将成员 sarah 的所有内容转移给成员 robert

python changeOwnership.py https://webadaptorhost.domain.com/webadaptorname admin pass.word sarah robert
#!/usr/bin/env python
# Requires Python 2.7+
# Sample Usage:
# python changeOwnership.py <portal> <username> <password>
#                           <oldOwner> <newOwner>
import urllib
import json
import argparse
def generateToken(username, password, portalUrl):
    '''Retrieves a token to be used with API requests.'''
    parameters = urllib.urlencode({'username' : username,
                                   'password' : password,
                                   'client' : 'referer',
                                   'referer': portalUrl,
                                   'expiration': 60,
                                   'f' : 'json'})
    response = urllib.urlopen(portalUrl + '/sharing/rest/generateToken?',
                              parameters).read()
    try:
        jsonResponse = json.loads(response)
        if 'token' in jsonResponse:
            return jsonResponse['token']
        elif 'error' in jsonResponse:
            print jsonResponse['error']['message']
            for detail in jsonResponse['error']['details']:
                print detail
    except ValueError, e:
        print 'An unspecified error occurred.'
        print e
def changeOwnership(itemId, newOwner, newFolder, token, portalUrl):
    '''
    REQUIRES ADMIN ACCESS.
    Transfers ownership of all content from one user to another.
    Use '/' as the destination folder when moving content into root.
    '''
    itemInfo = getItemInfo(itemId, token, portalUrl)
    params = urllib.urlencode({'targetUsername': newOwner,
                               'targetFoldername': newFolder,
                               'token' : token,
                               'f' : 'json'})
    if not itemInfo['ownerFolder']:
        itemInfo['ownerFolder'] = '/'
    reqUrl = (portalUrl + '/sharing/rest/content/users/' +
              itemInfo['owner'] + '/' + itemInfo['ownerFolder'] +
              '/items/' + itemId + '/reassign?')
    response = urllib.urlopen(reqUrl, params).read()
    try:
        jsonResponse = json.loads(response)
        if 'success' in jsonResponse:
            print 'Item ' + itemId + ' has been transferred.'
        elif 'error' in jsonResponse:
            print 'Error transferring item ' + itemId + '.'
            for detail in jsonResponse['error']['details']:
                print detail
    except ValueError, e:
        print 'An unspecified error occurred.'
        print e
def getUserContent(username, folder, token, portalUrl):
    '''Returns a list of all folders for the specified user.'''
    parameters = urllib.urlencode({'token': token, 'f': 'json'})
    request = (portalUrl + '/sharing/rest/content/users/' + username +
               '/' + folder + '?' + parameters)
    userContent = json.loads(urllib.urlopen(request).read())
    return userContent
def getItemInfo(itemId, token, portalUrl):
    '''Returns general information about the item.'''
    params = urllib.urlencode({'token' : token,
                               'f' : 'json'})
    itemInfo = json.loads(urllib.urlopen(portalUrl +
                                         '/sharing/content/items/' +
                                         itemId + '?' + params).read())
    return itemInfo
# Run the script.
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('portal',
                        help=('url of the Portal (e.g. '
                              'https://portal.domain.com:7443/arcgis)'))
    parser.add_argument('username', help='username')
    parser.add_argument('password', help='password')
    parser.add_argument("oldOwner", help="source account to migrate from")
    parser.add_argument("newOwner", help="destination account to migrate to")
    # Read the command line arguments.
    args = parser.parse_args()
    portal = args.portal
    username = args.username
    password = args.password
    oldOwner = args.oldOwner
    newOwner = args.newOwner
    # Sample usage
    token = generateToken(username=username, password=password,
                          portalUrl=portal)
    # Get a list of the oldOwner's folders and any items in root.
    userContent = getUserContent(oldOwner, '/', token,
                                 portalUrl=portal)
    # *** CAUTION ***
    # The following code will transfer ownership of ALL CONTENT
    # from oldOwner to newOwner.
    # Be sure you are absolutely sure you want to do this before proceeding.
    if not 'items' in userContent:
        print oldOwner + ' doesn\'t have any content visible to this account.'
        print 'Be sure you are signed in as admin.'
    else:
        for item in userContent['items']:
            changeOwnership(item['id'], newOwner, '/', token=token,
                            portalUrl=portal)
        for folder in userContent['folders']:
            folderContent = getUserContent(oldOwner, folder['id'],
                                           token=token, portalUrl=portal)
            for item in folderContent['items']:
                changeOwnership(item['id'], newOwner, folder['title'],
                                token=token, portalUrl=portal)
        print 'Migration completed.'
        print 'All items transferred from {0} to {1}'.format(oldOwner,newOwner)