Debian Codegraph @

azure_functions_devops_build/user/user_manager.py

raw
1 # --------------------------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for license information.
4 # --------------------------------------------------------------------------------------------
5
6 import logging
7 from msrest.service_client import ServiceClient
8 from msrest import Configuration, Deserializer
9 from msrest.exceptions import HttpOperationError
10 from . import models
11
12 class UserManager(object):
13 """ Get details about a user
14
15 Attributes:
16 See BaseManager
17 """
18
19 def __init__(self, base_url='https://peprodscussu2.portalext.visualstudio.com', creds=None):
20 """Inits UserManager as to be able to send the right requests"""
21 self._config = Configuration(base_url=base_url)
22 self._client = ServiceClient(creds, self._config)
23 #create the deserializer for the models
24 client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
25 self._deserialize = Deserializer(client_models)
26
27 # create cache for two user type
28 self._cache_aad_user = None
29 self._cache_msa_user = None
30
31 def is_msa_account(self):
32 user_id_aad = self.get_user(msa=False).id
33 user_id_msa = self.get_user(msa=True).id
34 return user_id_aad != user_id_msa
35
36 def get_user(self, msa=False):
37 # Try to get from cache
38 if msa is True and self._cache_msa_user is not None:
39 return self._cache_msa_user
40 if msa is False and self._cache_aad_user is not None:
41 return self._cache_aad_user
42
43 header_parameters = {}
44 header_parameters['X-VSS-ForceMsaPassThrough'] = 'true' if msa else 'false'
45 header_parameters['Accept'] = 'application/json'
46 request = self._client.get('/_apis/AzureTfs/UserContext')
47 response = self._client.send(request, header_parameters)
48
49 # Handle Response
50 deserialized = None
51 if response.status_code // 100 != 2:
52 logging.error("GET %s", request.url)
53 logging.error("response: %s", response.status_code)
54 logging.error(response.text)
55 raise HttpOperationError(self._deserialize, response)
56 else:
57 deserialized = self._deserialize('User', response)
58
59 # Write to cache
60 if msa is True and self._cache_msa_user is None:
61 self._cache_msa_user = deserialized
62 if msa is False and self._cache_aad_user is None:
63 self._cache_aad_user = deserialized
64
65 return deserialized
66
67 @property
68 def aad_id(self):
69 return self.get_user(msa=False).id
70
71 @property
72 def msa_id(self):
73 return self.get_user(msa=True).id
74
75 def close_connection(self):
76 self._client.close()