Skip to content

Commit 23a3c30

Browse files
author
electis
committed
ValidateMixin, validate password
1 parent 6d8304c commit 23a3c30

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

genesis_core/user_api/iam/api/controllers.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ def enforce(self, rule, do_raise=False, exc=None):
4747
return iam.enforcer.enforce(rule, do_raise, exc)
4848

4949

50+
class ValidationException(ra_e.RestAlchemyException):
51+
code = 400
52+
message = "Validation Error. %(description)s"
53+
54+
5055
class ValidateMixin:
5156
min_length = 8
52-
not_contain: str = string.whitespace
57+
not_contain: list[str] = [string.whitespace]
5358
must_contain: list[str] = None # [digits, ascii_uppercase, punctuation]
5459
regex: str = None
5560

@@ -59,20 +64,25 @@ def validate(self, value):
5964
error = "Value is required"
6065
elif self.min_length and len(value) < self.min_length:
6166
error = f"Value must be at least {self.min_length} characters long"
62-
elif self.not_contain and set(self.not_contain) & set(value):
63-
error = f"Value must not contain {self.not_contain}"
67+
elif self.not_contain:
68+
value_set = set(value)
69+
for not_contain in self.not_contain:
70+
if set(not_contain) & value_set:
71+
error = f"Value must not contain {self.not_contain}"
72+
break
6473
elif self.must_contain:
74+
value_set = set(value)
6575
for required in self.must_contain:
66-
if not set(required) & set(value):
76+
if not set(required) & value_set:
6777
error = f"Value must contain one of {required}"
6878
break
6979
elif self.regex and not re.match(self.regex, value):
7080
error = f"Value must match regex {self.regex}"
7181
if error:
72-
exc = ValidationErrorException()
73-
exc.message = error
74-
raise exc
82+
raise ValidationException(description=error)
7583

84+
85+
class ValidateSecretMixin(ValidateMixin):
7686
def validate_secret(self, kwargs: dict):
7787
if "secret" in kwargs:
7888
self.validate(kwargs["secret"])
@@ -102,7 +112,9 @@ def _get_app_endpoint(req):
102112

103113

104114
class UserController(
105-
controllers.BaseResourceControllerPaginated, EnforceMixin, ValidateMixin
115+
controllers.BaseResourceControllerPaginated,
116+
EnforceMixin,
117+
ValidateSecretMixin,
106118
):
107119
__resource__ = resources.ResourceByModelWithCustomProps(
108120
models.User,

0 commit comments

Comments
 (0)