Esempio n. 1
0
def invalid_captcha(captcha_salt: str = None,
                    captcha: str = None,
                    **kwargs) -> None:
    if captcha_salt and captcha_salt not in get_captcha_salts():
        raise ValidationError(["This CAPTCHA has expired. Please try again."])

    if captcha:
        answer = get_captcha_answer(get_captcha_token(captcha_salt))
        if captcha != answer:
            raise ValidationError(["The entered CAPTCHA answer is invalid."])
Esempio n. 2
0
def invalid_password(P: str = str(),
                     C: str = str(),
                     _: l10n.Translator = None,
                     **kwargs) -> None:
    if P:
        if not util.valid_password(P):
            username_min_len = config.getint("options", "username_min_len")
            raise ValidationError([
                _("Your password must be at least %s characters.") %
                (username_min_len)
            ])
        elif not C:
            raise ValidationError(["Please confirm your new password."])
        elif P != C:
            raise ValidationError(["Password fields do not match."])
Esempio n. 3
0
def invalid_suspend_permission(request: Request = None,
                               user: models.User = None,
                               S: str = "False",
                               **kwargs) -> None:
    if not request.user.is_elevated() and strtobool(S) != bool(user.Suspended):
        raise ValidationError(
            ["You do not have permission to suspend accounts."])
Esempio n. 4
0
def is_banned(request: Request = None, **kwargs) -> None:
    host = request.client.host
    exists = db.query(models.Ban, models.Ban.IPAddress == host).exists()
    if db.query(exists).scalar():
        raise ValidationError([
            "Account registration has been disabled for your "
            "IP address, probably due to sustained spam attacks. "
            "Sorry for the inconvenience."
        ])
Esempio n. 5
0
def username_in_use(U: str = str(),
                    user: models.User = None,
                    _: l10n.Translator = None,
                    **kwargs) -> None:
    exists = db.query(models.User).filter(
        and_(models.User.ID != user.ID, models.User.Username == U)).exists()
    if db.query(exists).scalar():
        # If the username already exists...
        raise ValidationError([
            _("The username, %s%s%s, is already in use.") %
            ("<strong>", U, "</strong>")
        ])
Esempio n. 6
0
def email_in_use(E: str = str(),
                 user: models.User = None,
                 _: l10n.Translator = None,
                 **kwargs) -> None:
    exists = db.query(models.User).filter(
        and_(models.User.ID != user.ID, models.User.Email == E)).exists()
    if db.query(exists).scalar():
        # If the email already exists...
        raise ValidationError([
            _("The address, %s%s%s, is already in use.") %
            ("<strong>", E, "</strong>")
        ])
Esempio n. 7
0
def invalid_account_type(T: int = None,
                         request: Request = None,
                         user: models.User = None,
                         _: l10n.Translator = None,
                         **kwargs) -> None:
    if T is not None and (T := int(T)) != user.AccountTypeID:
        name = ACCOUNT_TYPE_NAME.get(T, None)
        has_cred = request.user.has_credential(creds.ACCOUNT_CHANGE_TYPE)
        if name is None:
            raise ValidationError(["Invalid account type provided."])
        elif not has_cred:
            raise ValidationError(
                ["You do not have permission to change account types."])
        elif T > request.user.AccountTypeID:
            # If the chosen account type is higher than the editor's account
            # type, the editor doesn't have permission to set the new type.
            error = _("You do not have permission to change "
                      "this user's account type to %s.") % name
            raise ValidationError([error])

        logger.debug(f"Trusted User '{request.user.Username}' has "
                     f"modified '{user.Username}' account's type to"
                     f" {name}.")
Esempio n. 8
0
def invalid_ssh_pubkey(PK: str = str(),
                       user: models.User = None,
                       _: l10n.Translator = None,
                       **kwargs) -> None:
    if not PK:
        return

    try:
        keys = util.parse_ssh_keys(PK.strip())
    except ValueError as exc:
        raise ValidationError([str(exc)])

    for prefix, key in keys:
        fingerprint = get_fingerprint(f"{prefix} {key}")

        exists = db.query(models.SSHPubKey).filter(
            and_(models.SSHPubKey.UserID != user.ID,
                 models.SSHPubKey.Fingerprint == fingerprint)).exists()
        if db.query(exists).scalar():
            raise ValidationError([
                _("The SSH public key, %s%s%s, is already in use.") %
                ("<strong>", fingerprint, "</strong>")
            ])
Esempio n. 9
0
def request(pkgbase: PackageBase, type: str, comments: str, merge_into: str,
            context: Dict[str, Any]) -> None:
    if not comments:
        raise ValidationError(["The comment field must not be empty."])

    if type == "merge":
        # Perform merge-related checks.
        if not merge_into:
            # TODO: This error needs to be translated.
            raise ValidationError(
                ['The "Merge into" field must not be empty.'])

        target = db.query(PackageBase).filter(
            PackageBase.Name == merge_into).first()
        if not target:
            # TODO: This error needs to be translated.
            raise ValidationError(
                ["The package base you want to merge into does not exist."])

        db.refresh(target)
        if target.ID == pkgbase.ID:
            # TODO: This error needs to be translated.
            raise ValidationError(
                ["You cannot merge a package base into itself."])
Esempio n. 10
0
def invalid_username(request: Request = None,
                     U: str = str(),
                     _: l10n.Translator = None,
                     **kwargs) -> None:
    if not util.valid_username(U):
        username_min_len = config.getint("options", "username_min_len")
        username_max_len = config.getint("options", "username_max_len")
        raise ValidationError([
            "The username is invalid.",
            [
                _("It must be between %s and %s characters long") %
                (username_min_len, username_max_len),
                "Start and end with a letter or number",
                "Can contain only one period, underscore or hyphen.",
            ]
        ])
Esempio n. 11
0
def invalid_email(E: str = str(), **kwargs) -> None:
    if not util.valid_email(E):
        raise ValidationError(["The email address is invalid."])
Esempio n. 12
0
def invalid_user_password(request: Request = None,
                          passwd: str = str(),
                          **kwargs) -> None:
    if request.user.is_authenticated():
        if not request.user.valid_password(passwd):
            raise ValidationError(["Invalid password."])
Esempio n. 13
0
def invalid_pgp_key(K: str = str(), **kwargs) -> None:
    if K and not util.valid_pgp_fingerprint(K):
        raise ValidationError(["The PGP key fingerprint is invalid."])
Esempio n. 14
0
def invalid_timezone(TZ: str = str(), **kwargs) -> None:
    if TZ and TZ not in time.SUPPORTED_TIMEZONES:
        raise ValidationError(["Timezone is not currently supported."])
Esempio n. 15
0
def invalid_language(L: str = str(), **kwargs) -> None:
    if L and L not in l10n.SUPPORTED_LANGUAGES:
        raise ValidationError(["Language is not currently supported."])
Esempio n. 16
0
def invalid_backup_email(BE: str = str(), **kwargs) -> None:
    if BE and not util.valid_email(BE):
        raise ValidationError(["The backup email address is invalid."])
Esempio n. 17
0
def invalid_fields(E: str = str(), U: str = str(), **kwargs) -> None:
    if not E or not U:
        raise ValidationError(["Missing a required field."])
Esempio n. 18
0
def invalid_homepage(HP: str = str(), **kwargs) -> None:
    if HP and not util.valid_homepage(HP):
        raise ValidationError(
            ["The home page is invalid, please specify the full HTTP(s) URL."])