Esempio n. 1
0
def check_namespace(user_org, shortpath):
  shortpath_parts = shortpath.split('/', 1)
  if len(shortpath_parts) > 1:
    org_namespaces = config.get_organization_config(user_org).get('namespaces')
    if org_namespaces:
      shortpath_start, shortpath_remainder = shortpath_parts

      if shortpath_start in org_namespaces:
        return shortpath_start, shortpath_remainder

  return config.get_default_namespace(user_org), shortpath
Esempio n. 2
0
def check_namespaces(organization, namespace, shortpath):
  org_namespaces = config.get_organization_config(organization).get('namespaces', [])
  default_namespace = config.get_default_namespace(organization)

  if namespace != default_namespace and namespace not in org_namespaces:
    raise LinkCreationException('"%s" is not a valid link prefix for your organization' % (namespace))

  # If an organization has a namespace of, for example, "eng", go/eng can be created, but go/eng/%s would conflict
  # with eng/something, as {base}/eng/something is how users without an extension installed can still easily
  # access links in the "eng" namespace.
  if namespace == default_namespace:
    shortpath_parts = shortpath.split('/')
    if len(shortpath_parts) > 1 and shortpath_parts[0] in org_namespaces:
      raise LinkCreationException('"%s" is a reserved prefix for your organization' % (shortpath_parts[0]))
Esempio n. 3
0
def home():
  if not current_user.is_authenticated:
    return redirect('https://www.trot.to'
                    if request.host == 'trot.to'
                    else '/_/auth/login')

  from modules.organizations.helpers import get_org_settings

  template = JINJA_ENVIRONMENT.get_template('index.html')

  namespaces = config.get_organization_config(current_user.organization).get('namespaces', [])
  admin_links = get_org_settings(current_user.organization).get('admin_links', [])

  return template.render({'csrf_token': generate_csrf(),
                          'default_namespace': config.get_default_namespace(current_user.organization),
                          'namespaces': json.dumps(namespaces),
                          'admin_links': json.dumps(admin_links)})
Esempio n. 4
0
def get_go_link(path):
  requested_at = time.time()

  provided_shortpath = parse.unquote(path.strip('/'))
  shortpath_parts = provided_shortpath.split('/', 1)
  shortpath = '/'.join([shortpath_parts[0].lower()] + shortpath_parts[1:])

  if not getattr(current_user, 'email', None):
    if request.args.get('s') == 'crx' and request.args.get('sc'):
      # see: go/484356182846856
      return force_to_original_url()

    return redirect('/_/auth/login?%s' % parse.urlencode({'redirect_to': request.full_path}))

  namespace, shortpath = check_namespace(current_user.organization, shortpath)

  matching_shortlink, destination = get_shortlink(current_user.organization, namespace, shortpath)

  if matching_shortlink:
    queue_event(requested_at,
                matching_shortlink.get_id(),
                destination,
                request.args.get('s') or 'other')
    return redirect(str(destination))
  elif request.args.get('s') == 'crx' and request.args.get('sc'):
    return force_to_original_url()
  else:
    for router in ROUTERS:
      response = router(current_user.organization, namespace, shortpath)

      if response:
        return response

    create_link_params = {'sp': shortpath}
    if namespace != config.get_default_namespace(current_user.organization):
      create_link_params['ns'] = namespace

    return redirect(
      '%s/?%s'
      % ('http://localhost:5007' if request.host.startswith('localhost') else '',
         parse.urlencode(create_link_params))
    )
Esempio n. 5
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.get('namespace',
                            get_default_namespace(current_user.organization)),
            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
Esempio n. 6
0
  def test_get_default_namespace__set_at_org_level(self, mock_get_organization_config, _):
    self.assertEqual('yo',
                     config.get_default_namespace('googs.com'))

    mock_get_organization_config.assert_called_once_with('googs.com')