def get_user() -> Response: @action_logging def _log_get_user(*, user_id: str) -> None: pass # pragma: no cover try: user_id = get_query_param(request.args, 'user_id') url = '{0}{1}/{2}'.format(app.config['METADATASERVICE_BASE'], USER_ENDPOINT, user_id) _log_get_user(user_id=user_id) response = request_metadata(url=url) status_code = response.status_code if status_code == HTTPStatus.OK: message = 'Success' else: message = 'Encountered error: failed to fetch user with user_id: {0}'.format( user_id) logging.error(message) payload = { 'msg': message, 'user': dump_user(load_user(response.json())), } return make_response(jsonify(payload), status_code) except Exception as e: message = 'Encountered exception: ' + str(e) logging.exception(message) payload = jsonify({'msg': message}) return make_response(payload, HTTPStatus.INTERNAL_SERVER_ERROR)
def current_user() -> Response: try: if app.config['AUTH_USER_METHOD']: user = app.config['AUTH_USER_METHOD'](app) else: raise Exception('AUTH_USER_METHOD is not configured') url = '{0}{1}/{2}'.format(app.config['METADATASERVICE_BASE'], USER_ENDPOINT, user.user_id) response = request_metadata(url=url) status_code = response.status_code if status_code == HTTPStatus.OK: message = 'Success' else: message = 'Encountered error: failed to fetch user with user_id: {0}'.format(user.user_id) logging.error(message) payload = { 'msg': message, 'user': dump_user(load_user(response.json())) } return make_response(jsonify(payload), status_code) except Exception as e: message = 'Encountered exception: ' + str(e) logging.exception(message) payload = jsonify({'msg': message}) return make_response(payload, HTTPStatus.INTERNAL_SERVER_ERROR)
def current_user() -> Response: if (app.config['AUTH_USER_METHOD']): user = app.config['AUTH_USER_METHOD'](app) else: user = load_user({'user_id': 'undefined', 'display_name': '*'}) return user.to_json()
def current_user() -> Response: if (app.config['CURRENT_USER_METHOD']): user = app.config['CURRENT_USER_METHOD'](app) else: user = load_user({'display_name': '*'}) return user.to_json()
def test_set_user_id_from_email(self) -> None: """ Deserialization and serialization sets user_id from email if no user_id :return: """ with app.test_request_context(): self.assertEqual(dump_user(load_user({'email': '*****@*****.**'})).get('user_id'), '*****@*****.**')
def test_profile_url(self) -> None: """ Deserialization and serialization sets profile_url from app.config['GET_PROFILE_URL'] if no profile_url provided' :return: """ with app.test_request_context(): self.assertEqual(dump_user(load_user({'email': '*****@*****.**'})).get('profile_url'), 'testUrl')
def test_set_display_name_from_email(self) -> None: """ Deserialization and serialization sets display_name from email if no display_name and full_name is None :return: """ with app.test_request_context(): self.assertEqual(dump_user(load_user({'email': '*****@*****.**'})).get('display_name'), '*****@*****.**')
def get_test_user(app: app) -> User: user_info = { 'email': '*****@*****.**', 'user_id': TEST_USER_ID, 'first_name': 'Firstname', 'last_name': 'Lastname', 'full_name': 'Firstname Lastname', } return load_user(user_info)
def get_auth_user(app: Flask) -> User: """ Retrieves the user information from oidc token, and then makes a dictionary 'UserInfo' from the token information dictionary. We need to convert it to a class in order to use the information in the rest of the Amundsen application. :param app: The instance of the current app. :return: A class UserInfo (Note, there isn't a UserInfo class, so we use Any) """ user_info = load_user(session.get("user")) return user_info
def test_set_display_name_from_email_if_full_name_empty(self) -> None: """ Deserialization and serialization sets display_name from email if no display_name and full_name is '' :return: """ test_user = { 'email': '*****@*****.**', 'full_name': '', } with app.test_request_context(): self.assertEqual(dump_user(load_user(test_user)).get('display_name'), '*****@*****.**')
def test_set_display_name_from_full_name(self) -> None: """ Deserialization and serialization sets display_name from full_name if no display_name and full_name is a non-empty string :return: """ test_user = { 'email': '*****@*****.**', 'full_name': 'Test User', } with app.test_request_context(): self.assertEqual(dump_user(load_user(test_user)).get('display_name'), 'Test User')
def _get_users_from_ids(user_ids: List[str]) -> User: """ Calls get_user metadata API with a user id to retrieve user details. :param user_ids: List of strings representing user ids :return: List of User objects """ users = [] for user_id in user_ids: url = '{0}{1}/{2}'.format(app.config['METADATASERVICE_BASE'], USER_ENDPOINT, user_id) response = request_metadata(url=url) if response.status_code == HTTPStatus.OK: user = load_user(response.json()) if user: users.append(user) return users
def _authorize_access(self, user_id: str) -> None: """ Get Mode user ID via metadata service. Note that metadata service needs to be at least v2.5.2 and Databuilder should also have ingested Mode user. https://github.com/lyft/amundsendatabuilder#modedashboarduserextractor :param user_id: :return: :raise: PermissionError when user is not allowed to access the dashboard """ metadata_svc_url = '{0}{1}/{2}'.format( app.config['METADATASERVICE_BASE'], USER_ENDPOINT, user_id) response = request_metadata(url=metadata_svc_url) response.raise_for_status() user = load_user(response.json()) if user.is_active and user.other_key_values and user.other_key_values.get( 'mode_user_id'): return raise PermissionError( 'User {} is not authorized to preview Mode Dashboard'.format( user_id))
def _map_user_result(result: Dict) -> Dict: user_result = dump_user(load_user(result)) user_result['type'] = 'user' return user_result
def _map_user_object_to_schema(u: Dict) -> Dict: return dump_user(load_user(u))