Example #1
0
    def handle(self, *args, **options):
        # type: (*Any, **str) -> None
        realm = get_realm(options["string_id"])
        if options["op"] == "show":
            print("Aliases for %s:" % (realm.domain,))
            for alias in get_realm_aliases(realm):
                print(alias["domain"])
            sys.exit(0)

        domain = options['alias'].strip().lower()
        try:
            validate_domain(domain)
        except ValidationError as e:
            print(e.messages[0])
            sys.exit(1)
        if options["op"] == "add":
            if not can_add_alias(domain):
                print("A Realm already exists for this domain, cannot add it as an alias for another realm!")
                sys.exit(1)
            RealmAlias.objects.create(realm=realm, domain=domain)
            sys.exit(0)
        elif options["op"] == "remove":
            RealmAlias.objects.get(realm=realm, domain=domain).delete()
            sys.exit(0)
        else:
            self.print_help("./manage.py", "realm_alias")
            sys.exit(1)
Example #2
0
    def handle(self, *args, **options):
        # type: (*Any, **str) -> None
        realm = get_realm(options["string_id"])
        if options["op"] == "show":
            print("Aliases for %s:" % (realm.domain, ))
            for alias in get_realm_aliases(realm):
                print(alias["domain"])
            sys.exit(0)

        domain = options['alias'].strip().lower()
        try:
            validate_domain(domain)
        except ValidationError as e:
            print(e.messages[0])
            sys.exit(1)
        if options["op"] == "add":
            if not can_add_alias(domain):
                print(
                    "A Realm already exists for this domain, cannot add it as an alias for another realm!"
                )
                sys.exit(1)
            RealmAlias.objects.create(realm=realm, domain=domain)
            sys.exit(0)
        elif options["op"] == "remove":
            RealmAlias.objects.get(realm=realm, domain=domain).delete()
            sys.exit(0)
        else:
            self.print_help("./manage.py", "realm_alias")
            sys.exit(1)
Example #3
0
def create_alias(request, user_profile, domain=REQ()):
    # type: (HttpRequest, UserProfile, Text) -> (HttpResponse)
    if can_add_alias(domain):
        try:
            alias = do_add_realm_alias(user_profile.realm, domain)
        except ValidationError:
            return json_error(_('Domain can\'t be empty.'))
    else:
        return json_error(_('A Realm for this domain already exists.'))
    return json_success({'new_domain': [alias.id, alias.domain]})
Example #4
0
def create_alias(request, user_profile, domain=REQ(validator=check_string)):
    # type: (HttpRequest, UserProfile, Text) -> (HttpResponse)
    domain = domain.strip().lower()
    try:
        validate_domain(domain)
    except ValidationError as e:
        return json_error(_('Invalid domain: {}').format(e.messages[0]))
    if RealmAlias.objects.filter(realm=user_profile.realm, domain=domain).exists():
        return json_error(_("The domain %(domain)s is already a part of your organization.") % {'domain': domain})
    if not can_add_alias(domain):
        return json_error(_("The domain %(domain)s belongs to another organization.") % {'domain': domain})
    alias = do_add_realm_alias(user_profile.realm, domain)
    return json_success({'new_domain': [alias.id, alias.domain]})
Example #5
0
    def validate_domain(self, domain):
        # type: (str) -> None
        # Domains can't contain whitespace if they are to be used in memcached
        # keys. Seems safer to leave that as the default case regardless of
        # which backing store we use.
        if re.search("\s", domain):
            raise ValueError("Domains can't contain whitespace")

        # Domains must look like domains, ie have the structure of
        # <subdomain(s)>.<tld>. One reason for this is that bots need
        # to have valid looking emails.
        if len(domain.split(".")) < 2:
            raise ValueError("Domains must contain a '.'")

        if not can_add_alias(domain):
            raise ValueError("Domain already assigned to an existing realm")
Example #6
0
    def validate_domain(self, domain):
        # type: (str) -> None
        # Domains can't contain whitespace if they are to be used in memcached
        # keys. Seems safer to leave that as the default case regardless of
        # which backing store we use.
        if re.search("\s", domain):
            raise ValueError("Domains can't contain whitespace")

        # Domains must look like domains, ie have the structure of
        # <subdomain(s)>.<tld>. One reason for this is that bots need
        # to have valid looking emails.
        if len(domain.split(".")) < 2:
            raise ValueError("Domains must contain a '.'")

        if not can_add_alias(domain):
            raise ValueError("Domain already assigned to an existing realm")
Example #7
0
    def handle(self, *args, **options):
        # type: (*Any, **str) -> None
        realm = get_realm(options["string_id"])
        if options["op"] == "show":
            print("Aliases for %s:" % (realm.string_id, ))
            for alias in get_realm_aliases(realm):
                if alias["allow_subdomains"]:
                    print(alias["domain"] + " (subdomains allowed)")
                else:
                    print(alias["domain"] + " (subdomains not allowed)")
            sys.exit(0)

        domain = options['alias'].strip().lower()
        try:
            validate_domain(domain)
        except ValidationError as e:
            print(e.messages[0])
            sys.exit(1)
        if options["op"] == "add":
            try:
                if not can_add_alias(domain):
                    print(
                        _("The domain %(domain)s belongs to another organization."
                          ) % {'domain': domain})
                    sys.exit(1)
                RealmAlias.objects.create(
                    realm=realm,
                    domain=domain,
                    allow_subdomains=options["allow_subdomains"])
                sys.exit(0)
            except IntegrityError:
                print(
                    _("The domain %(domain)s is already a part of your organization."
                      ) % {'domain': domain})
                sys.exit(1)
        elif options["op"] == "remove":
            try:
                RealmAlias.objects.get(realm=realm, domain=domain).delete()
                sys.exit(0)
            except RealmAlias.DoesNotExist:
                print("No such entry found!")
                sys.exit(1)
        else:
            self.print_help("./manage.py", "realm_alias")
            sys.exit(1)
Example #8
0
def create_alias(request, user_profile, domain=REQ(validator=check_string)):
    # type: (HttpRequest, UserProfile, Text) -> (HttpResponse)
    domain = domain.strip().lower()
    try:
        validate_domain(domain)
    except ValidationError as e:
        return json_error(_('Invalid domain: {}').format(e.messages[0]))
    if RealmAlias.objects.filter(realm=user_profile.realm,
                                 domain=domain).exists():
        return json_error(
            _("The domain %(domain)s is already a part of your organization.")
            % {'domain': domain})
    if not can_add_alias(domain):
        return json_error(
            _("The domain %(domain)s belongs to another organization.") %
            {'domain': domain})
    alias = do_add_realm_alias(user_profile.realm, domain)
    return json_success({'new_domain': [alias.id, alias.domain]})
Example #9
0
    def handle(self, *args, **options):
        # type: (*Any, **str) -> None
        realm = get_realm(options["domain"])
        if options["op"] == "show":
            print("Aliases for %s:" % (realm.domain,))
            for alias in realm_aliases(realm):
                print(alias)
            sys.exit(0)

        alias = options['alias'].lower()
        if options["op"] == "add":
            if not can_add_alias(alias):
                print("A Realm already exists for this domain, cannot add it as an alias for another realm!")
                sys.exit(1)
            RealmAlias.objects.create(realm=realm, domain=alias)
            sys.exit(0)
        elif options["op"] == "remove":
            RealmAlias.objects.get(realm=realm, domain=alias).delete()
            sys.exit(0)
        else:
            self.print_help("python manage.py", "realm_alias")
            sys.exit(1)