def get_by_userid(cls, domain, userid): """Return the user with the given ID, or None. :param domain: The domain for the current request, for example: u'hypothes.is'. This must match the domain part of the userid argument - you can't retrieve users who don't belong to the given domain. :type domain: unicode :param userid: A userid unicode string, for example: u'acct:[email protected]' :type userid: unicode :rtype: h.accounts.models.User or None """ try: # pylint: disable=unpacking-non-sequence username, userdomain = util.split_user(userid) except TypeError: # userid didn't match the pattern that split_user() expects. return None if userdomain != domain: return None return cls.get_by_username(username)
def get_user(userid, request): """Return the User object for the given userid, or None. This will also return None if the given userid is None, if it isn't a valid userid, if its domain doesn't match the site's domain, or if there's just no user with that userid. """ if userid is None: return None try: parts = util.split_user(userid) except ValueError: return if parts['domain'] != request.auth_domain: return None return models.User.get_by_username(parts['username'])
def _validate_request(request): """ Check that the passed request is appropriate for proceeding with account claim. Asserts that: - the 'claim' feature is toggled on - no-one is logged in - the claim token is provided and authentic - the user referred to in the token exists - the user referred to in the token has not already claimed their account and raises for redirect or 404 otherwise. """ if not request.feature('claim'): raise exc.HTTPNotFound() # If signed in, redirect to stream if request.authenticated_userid is not None: _perform_logged_in_redirect(request) payload = _validate_token(request) if payload is None: raise exc.HTTPNotFound() try: username = util.split_user(payload['userid'])['username'] except ValueError: log.warn('got claim token with invalid userid=%r', payload['userid']) raise exc.HTTPNotFound() user = User.get_by_username(username) if user is None: log.warn('got claim token with invalid userid=%r', payload['userid']) raise exc.HTTPNotFound() # User already has a password? Claimed already. if user.password: _perform_already_claimed_redirect(request) return user
def _feed_item_from_annotation(annotation, annotation_url): """Return an RSS feed item for the given annotation. :returns: A logical representation of the RSS feed item as a dict, containing all of the data that a template would need to render the feed item to XML. :rtype: dict """ try: name = util.split_user(annotation["user"])["username"] except ValueError: name = annotation["user"] return { "author": {"name": name}, "title": annotation.title, "description": annotation.description, "pubDate": _pubDate_string_from_annotation(annotation), "guid": h.feeds.util.tag_uri_for_annotation( annotation, annotation_url), "link": annotation_url(annotation) }
def _feed_entry_from_annotation( annotation, annotation_url, annotation_api_url=None): """Return an Atom feed entry for the given annotation. :returns: A logical representation of the Atom feed entry as a dict, containing all of the data that a template would need to render the feed item to XML. :rtype: dict """ try: name = util.split_user(annotation["user"])["username"] except ValueError: name = annotation["user"] entry = { "id": h.feeds.util.tag_uri_for_annotation( annotation.annotation, annotation_url), "author": {"name": name}, "title": annotation.title, "updated": annotation["updated"], "published": annotation["created"], "content": annotation.description, "links": [ {"rel": "alternate", "type": "text/html", "href": annotation_url(annotation.annotation)}, ] } if annotation_api_url: entry["links"].append( {"rel": "alternate", "type": "application/json", "href": annotation_api_url(annotation.annotation)} ) entry["links"].extend( [{"rel": "related", "href": link} for link in annotation.target_links]) return entry
def _feed_item_from_annotation(annotation, annotation_url): """Return an RSS feed item for the given annotation. :returns: A logical representation of the RSS feed item as a dict, containing all of the data that a template would need to render the feed item to XML. :rtype: dict """ try: name = util.split_user(annotation["user"])["username"] except ValueError: name = annotation["user"] return { "author": { "name": name }, "title": annotation.title, "description": annotation.description, "pubDate": _pubDate_string_from_annotation(annotation), "guid": h.feeds.util.tag_uri_for_annotation(annotation, annotation_url), "link": annotation_url(annotation) }
def nipsa_index(_): return {"userids": [util.split_user(u)[0] for u in nipsa.index()]}
def nipsa_index(_): return {"usernames": [util.split_user(u)["username"] for u in nipsa.index()]}
def test_split_user(): parts = util.split_user("acct:[email protected]") assert parts == {'username': '******', 'domain': 'hypothes.is'}
def test_split_user_no_match(): with pytest.raises(ValueError): parts = util.split_user("donkeys")
def nipsa_index(_): return { "usernames": [util.split_user(u)["username"] for u in nipsa.index()] }
def test_split_user_no_match(): parts = util.split_user("donkeys") assert parts is None
def test_split_user(): parts = util.split_user("acct:[email protected]") assert parts == ('seanh', 'hypothes.is')