Exemplo n.º 1
0
    def from_dict(self, *, user_name: str,
                  user_dict: Dict[str, str]) -> "SystemUser":
        raw_scope = user_dict.get("scope", None)
        if raw_scope is None:
            raise errors.SystemUsernamesValidationError(
                name=user_name, message="'scope' is undefined")

        try:
            scope = SystemUserScope[raw_scope.upper()]
        except KeyError:
            raise errors.SystemUsernamesValidationError(
                name=user_name, message=f"scope {raw_scope!r} is invalid")

        return SystemUser(name=user_name, scope=scope)
Exemplo n.º 2
0
    def from_object(cls, *, user_object: Any, user_name: str) -> "SystemUser":
        if user_object is None:
            raise errors.SystemUsernamesValidationError(
                name=user_name, message="undefined user"
            )
        elif isinstance(user_object, str):
            try:
                scope = SystemUserScope[user_object.upper()]
            except KeyError:
                raise errors.SystemUsernamesValidationError(
                    name=user_name, message=f"scope {user_object!r} is invalid"
                )

            return SystemUser(name=user_name, scope=scope)
        elif isinstance(user_object, dict):
            return cls.from_dict(user_dict=user_object, user_name=user_name)

        raise errors.SystemUsernamesValidationError(
            name=user_name, message=f"unknown syntax for {user_object!r}"
        )
Exemplo n.º 3
0
 def validate(self):
     if not isinstance(self.scope, SystemUserScope):
         raise errors.SystemUsernamesValidationError(
             name=self.name, message=f"scope {self.scope!r} is invalid"
         )