示例#1
0
    def validate_username(self, attrs, source):
        username = attrs.get(source, None)
        try:
            validate_user_email_allowed_domains(username)

        except ValidationError:
            # If the validation comes from a request let's check the user is a valid contact
            request = self.context.get("request", None)
            if request is not None and request.user.is_authenticated:
                valid_usernames = request.user.contacts_visible_by_user(
                    request.user).values_list("username", flat=True)
                if username not in valid_usernames:
                    raise ValidationError(
                        _("The user must be a valid contact"))

        user = User.objects.filter(Q(username=username)
                                   | Q(email=username)).first()
        if user is not None:
            email = user.email
            self.user = user

        else:
            email = username

        self.email = email
        self._validate_member_doesnt_exist(attrs, email)
        return attrs
示例#2
0
    def partial_update(self, request, *args, **kwargs):
        """
        We must detect if the user is trying to change his email so we can
        save that value and generate a token that allows him to validate it in
        the new email account
        """
        user = self.get_object()
        self.check_permissions(request, "update", user)

        new_email = request.DATA.pop('email', None)
        if new_email is not None:
            valid_new_email = True
            duplicated_email = models.User.objects.filter(
                email=new_email).exists()

            try:
                validate_email(new_email)
                validate_user_email_allowed_domains(new_email)
            except ValidationError:
                valid_new_email = False

            valid_new_email = valid_new_email and new_email != request.user.email

            if duplicated_email:
                raise exc.WrongArguments(_("Duplicated email"))
            elif not valid_new_email:
                raise exc.WrongArguments(_("Invalid email"))

            # We need to generate a token for the email
            request.user.email_token = str(uuid.uuid4())
            request.user.new_email = new_email
            request.user.save(update_fields=["email_token", "new_email"])
            email = mail_builder.change_email(request.user.new_email, {
                "user": request.user,
                "lang": request.user.lang
            })
            email.send()

        github_username = request.DATA.pop('github_username', None)
        threebot_name = request.DATA.pop('threebot_name', None)
        public_key = request.DATA.pop('public_key', None)

        if github_username:
            request.user.github_username = github_username
            request.user.save(update_fields=["github_username"])

        if threebot_name:
            request.user.threebot_name = threebot_name
            request.user.save(update_fields=["threebot_name"])

        if public_key:
            request.user.public_key = public_key
            request.user.save(update_fields=["public_key"])

        return super().partial_update(request, *args, **kwargs)
示例#3
0
    def validate_username(self, attrs, source):
        username = attrs.get(source)
        try:
            validate_user_email_allowed_domains(username)
        except InvalidEmailValidationError:
            # If the validation comes from a request let's check the user is a valid contact
            request = self.context.get("request", None)
            if request is not None and request.user.is_authenticated:
                valid_usernames = set(request.user.contacts_visible_by_user(request.user).values_list("username", flat=True))
                if username not in valid_usernames:
                    raise ValidationError(_("The user must be a valid contact"))

        return attrs
示例#4
0
    def validate_username(self, attrs, source):
        username = attrs.get(source)
        try:
            validate_user_email_allowed_domains(username)
        except InvalidEmailValidationError:
            # If the validation comes from a request let's check the user is a valid contact
            request = self.context.get("request", None)
            if request is not None and request.user.is_authenticated():
                valid_usernames = set(request.user.contacts_visible_by_user(request.user).values_list("username", flat=True))
                if username not in valid_usernames:
                    raise ValidationError(_("The user must be a valid contact"))

        return attrs
示例#5
0
文件: api.py 项目: 0-T-0/taiga-back
    def partial_update(self, request, *args, **kwargs):
        """
        We must detect if the user is trying to change his email so we can
        save that value and generate a token that allows him to validate it in
        the new email account
        """
        user = self.get_object()
        self.check_permissions(request, "update", user)

        new_email = request.DATA.get('email', None)
        if new_email is not None:
            valid_new_email = True
            duplicated_email = models.User.objects.filter(email=new_email).exists()

            try:
                validate_email(new_email)
                validate_user_email_allowed_domains(new_email)
            except ValidationError:
                valid_new_email = False

            valid_new_email = valid_new_email and new_email != request.user.email

            if duplicated_email:
                raise exc.WrongArguments(_("Duplicated email"))
            elif not valid_new_email:
                raise exc.WrongArguments(_("Not valid email"))

            # We need to generate a token for the email
            request.user.email_token = str(uuid.uuid1())
            request.user.new_email = new_email
            request.user.save(update_fields=["email_token", "new_email"])
            email = mail_builder.change_email(
                request.user.new_email,
                {
                    "user": request.user,
                    "lang": request.user.lang
                }
            )
            email.send()

        ret = super().partial_update(request, *args, **kwargs)

        return ret
示例#6
0
    def validate_username(self, attrs, source):
        username = attrs.get(source, None)
        try:
            validate_user_email_allowed_domains(username)

        except ValidationError:
            # If the validation comes from a request let's check the user is a valid contact
            request = self.context.get("request", None)
            if request is not None and request.user.is_authenticated():
                valid_usernames = request.user.contacts_visible_by_user(request.user).values_list("username", flat=True)
                if username not in valid_usernames:
                    raise ValidationError(_("The user must be a valid contact"))

        user = User.objects.filter(Q(username=username) | Q(email=username)).first()
        if user is not None:
            email = user.email
            self.user = user

        else:
            email = username

        self.email = email
        self._validate_member_doesnt_exist(attrs, email)
        return attrs