Exemplo n.º 1
0
def delete(link_id):
  existing_link = g.link

  logging.info('Deleting link: %s' % (convert_entity_to_dict(existing_link, PUBLIC_KEYS)))

  existing_link.delete()

  enqueue_event('link.deleted',
                'link',
                convert_entity_to_dict(existing_link, PUBLIC_KEYS, get_field_conversion_fns()))

  return jsonify({})
Exemplo n.º 2
0
def delete(link_id):
  existing_link = check_authorization(link_id)

  if not existing_link:
    return abort(403)

  logging.info('Deleting link: %s' % (convert_entity_to_dict(existing_link, PUBLIC_KEYS)))

  existing_link.delete()

  enqueue_event('link.deleted',
                'link',
                convert_entity_to_dict(existing_link, PUBLIC_KEYS, get_field_conversion_fns()))

  return jsonify({})
Exemplo n.º 3
0
def get_or_create_user(email, user_org):
    email = email.lower()

    user = models.User.get_by_email(email)

    # TODO: Remove this when all users are backfilled
    if user and (not user.domain_type or not user.organization):
        user.domain_type = _extract_domain_type(email)

        if not user.organization:
            user.organization = user.extract_organization()

        user.put()

    if not user:
        user = models.User(email=email,
                           domain_type=_extract_domain_type(email))
        user.organization = user.extract_organization()
        user.put()

        enqueue_event(
            'user.created', 'user',
            convert_entity_to_dict(user, ['id', 'email', 'organization']))

    return user
Exemplo n.º 4
0
  def get(self):
    links = [convert_entity_to_dict(entity, self.ALLOWED_KEYS, self.get_field_conversion_fns())
             for entity in helpers.get_all_shortlinks_for_org(self.user_org)]

    for link in links:
      link['mine'] = link['owner'] == self.user_email

    self.response.write(json.dumps(links))
Exemplo n.º 5
0
def get_links():
  links = [convert_entity_to_dict(entity, PUBLIC_KEYS, get_field_conversion_fns())
           for entity in helpers.get_all_shortlinks_for_org(current_user.organization)]

  for link in links:
    link['mine'] = link['owner'] == current_user.email

  return jsonify(links)
Exemplo n.º 6
0
def post_link():
  object_data = request.json

  try:
    new_link = helpers.create_short_link(current_user.organization,
                                         current_user.email,
                                         object_data['shortpath'],
                                         object_data['destination'])
  except helpers.LinkCreationException as e:
    return jsonify({
      'error': str(e)
    })

  return jsonify(
    convert_entity_to_dict(new_link, PUBLIC_KEYS, get_field_conversion_fns())
  )
Exemplo n.º 7
0
  def post(self):
    object_data = json.loads(self.request.body)

    try:
      new_link = helpers.create_short_link(self.user_org,
                                           self.user_email,
                                           object_data['shortpath'],
                                           object_data['destination'])
    except helpers.LinkCreationException as e:
      self.response.write(json.dumps({
        'error': str(e)
      }))
      return

    self.response.write(
      json.dumps(convert_entity_to_dict(new_link, self.ALLOWED_KEYS, self.get_field_conversion_fns()))
    )
Exemplo n.º 8
0
def post_link():
    object_data = request.json

    if 'owner' in object_data and not user_helpers.is_user_admin(current_user):
        abort(403)

    try:
        new_link = helpers.create_short_link(
            current_user.organization,
            object_data.get('owner', current_user.email),
            object_data['shortpath'], object_data['destination'])
    except helpers.LinkCreationException as e:
        return jsonify({'error': str(e)})

    logging.info(f'{current_user.email} created go link with ID {new_link.id}')

    return jsonify(
        convert_entity_to_dict(new_link, PUBLIC_KEYS,
                               get_field_conversion_fns())), 201
Exemplo n.º 9
0
 def _write_link_to_response(self, link):
   self.response.write(json.dumps(
     convert_entity_to_dict(link, self.ALLOWED_KEYS, self.get_field_conversion_fns())
   ))
Exemplo n.º 10
0
def upsert_short_link(organization, owner, namespace, shortpath, destination, updated_link_object):
  shortpath = shortpath.strip().lower().strip('/')
  destination = _encode_ascii_incompatible_chars(destination.strip())

  if not shortpath:
    raise LinkCreationException('You must provide a path')

  PATH_RESTRICTIONS_ERROR = 'Keywords can include only letters, numbers, hyphens, "/", and "%s" placeholders'

  if shortpath != re.sub('[^0-9a-zA-Z\-\/%]', '', shortpath):
    raise LinkCreationException(PATH_RESTRICTIONS_ERROR)

  check_namespaces(organization, namespace, shortpath)

  if organization != get_organization_id_for_email(owner):
    raise LinkCreationException("The go link's owner must be in the go link's organization")

  shortpath_parts = shortpath.split('/')
  if len(shortpath_parts) > 1:
    placeholder_found = False

    for part in shortpath_parts[1:]:
      if part == '%s':
        placeholder_found = True
      elif placeholder_found:
        raise LinkCreationException('After the first "%s" placeholder, you can only have additional placeholders')

  if '%' in shortpath:
    if '%' in shortpath and shortpath.count('%') != shortpath.count('%s'):
      raise LinkCreationException(PATH_RESTRICTIONS_ERROR)

    if '%s' in shortpath.split('/')[0]:
      raise LinkCreationException('"%s" placeholders must come after a "/". Example: "jira/%s"')

    if shortpath.count('%s') != destination.count('%s'):
      raise LinkCreationException('The keyword and the destination must have the same number of "%s" placeholders')

  if not updated_link_object:
    existing_link, _ = get_shortlink(organization, namespace, shortpath)

    if existing_link:
      error_message = 'That go link already exists. %s/%s points to %s' % (namespace,
                                                                           shortpath,
                                                                           existing_link.destination_url)

      if existing_link.shortpath != shortpath:
        error_message = 'A conflicting go link already exists. %s/%s points to %s' % (namespace,
                                                                                      existing_link.shortpath,
                                                                                      existing_link.destination_url)

      raise LinkCreationException(error_message)

    if '%s' in shortpath:
      conflicting_link = find_conflicting_link(organization, namespace, shortpath)

      if conflicting_link:
        raise LinkCreationException('A conflicting go link already exists. %s/%s points to %s'
                                    % (namespace, conflicting_link.shortpath, conflicting_link.destination_url))

  # Note: urlparse('128.90.0.1:8080/start').scheme returns '128.90.0.1'. Hence the additional checking.
  destination_url_scheme = urlparse(destination).scheme
  if (not destination_url_scheme
      or not destination_url_scheme.strip()
      or not destination.startswith(destination_url_scheme + '://')):
    # default to HTTP (see Slack discussion)
    destination = 'http://' + destination

  if type(validators.url(destination)) is ValidationFailure:
    raise LinkCreationException('You must provide a valid destination URL')

  if updated_link_object:
    link = updated_link_object
  else:
    link_kwargs = {'organization': organization,
                   'owner': owner,
                   'namespace': namespace,
                   'shortpath': shortpath,
                   'destination_url': destination,
                   'shortpath_prefix': shortpath.split('/')[0]}
    link = models.ShortLink(**link_kwargs)

  link.put()

  link_dict = convert_entity_to_dict(link, ['owner', 'shortpath', 'destination_url', 'organization'])
  link_dict['id'] = link.get_id()

  enqueue_event('link.%s' % ('updated' if updated_link_object else 'created'),
                'link',
                link_dict)
  return link
Exemplo n.º 11
0
def upsert_short_link(organization, creator, shortpath, destination,
                      updated_link_object):
    shortpath = shortpath.strip().lower().strip('/')
    destination = _encode_ascii_incompatible_chars(destination.strip())

    # TODO: Cache this.
    user = get_or_create_user(creator, organization)
    if not user.accepted_terms_at:
        user.accepted_terms_at = datetime.datetime.utcnow()
        user.put()

    if not shortpath:
        raise LinkCreationException('You must provide a path')

    PATH_RESTRICTIONS_ERROR = 'Keywords can include only letters, numbers, hyphens, "/", and "%s" placeholders'

    if shortpath != re.sub('[^0-9a-zA-Z\-\/%]', '', shortpath):
        raise LinkCreationException(PATH_RESTRICTIONS_ERROR)

    shortpath_parts = shortpath.split('/')
    if len(shortpath_parts) > 1:
        for part in shortpath_parts[1:]:
            if part != '%s':
                raise LinkCreationException(
                    'Keywords can have only "%s" placeholders after the first "/". Ex: "drive/%s"'
                )

    if '%' in shortpath:
        if '%' in shortpath and shortpath.count('%') != shortpath.count('%s'):
            raise LinkCreationException(PATH_RESTRICTIONS_ERROR)

        if '%s' in shortpath.split('/')[0]:
            raise LinkCreationException(
                '"%s" placeholders must come after a "/". Example: "jira/%s"')

        if shortpath.count('%s') != destination.count('%s'):
            raise LinkCreationException(
                'The keyword and the destination must have the same number of "%s" placeholders'
            )

    if not updated_link_object:
        existing_link, _ = get_shortlink(organization, shortpath)

        if existing_link:
            raise LinkCreationException(
                'That go link already exists. go/%s points to %s' %
                (shortpath, existing_link.destination_url))

    # Note: urlparse('128.90.0.1:8080/start').scheme returns '128.90.0.1'. Hence the additional checking.
    destination_url_scheme = urlparse(destination).scheme
    if (not destination_url_scheme or not destination_url_scheme.strip()
            or not destination.startswith(destination_url_scheme + '://')):
        # default to HTTP (see Slack discussion)
        destination = 'http://' + destination

    if type(validators.url(destination)) is ValidationFailure:
        raise LinkCreationException('You must provide a valid destination URL')

    if updated_link_object:
        link = updated_link_object
    else:
        link_kwargs = {
            'organization': organization,
            'owner': creator,
            'shortpath': shortpath,
            'destination_url': destination,
            'shortpath_prefix': shortpath.split('/')[0]
        }
        link = models.ShortLink(**link_kwargs)

    link.put()

    link_dict = convert_entity_to_dict(
        link, ['owner', 'shortpath', 'destination_url', 'organization'])
    link_dict['id'] = link.get_id()

    enqueue_event(
        'link.%s' % ('updated' if updated_link_object else 'created'), 'link',
        link_dict)
    return link
Exemplo n.º 12
0
def _get_link_response(link):
  return convert_entity_to_dict(link, PUBLIC_KEYS, get_field_conversion_fns())