Tastypie에서 새 사용자를 만들 때 암호가 데이터베이스에 기록되지 않는 이유는 무엇입니까?

데이비드 D.

새 사용자 (+ 프로필)를 만들려고하는데 괜찮은 것 같지만 (응답 201) 데이터베이스에서 암호가 비어 있습니다.

다음 모델이 있습니다.

class CliProfile(models.Model):
    user = models.OneToOneField(User, related_name='cliuser')
    # other stuff

및 다음 리소스 :

class CliProfileResource(ModelResource):

    user = fields.ForeignKey(UserResource, 'user', full=True)

    class Meta(CommonResourceMeta):
        queryset = CliProfile.objects.all()
        resource_name = 'client_infos'
        fields = ['id']
        list_allowed_methods = ['post']


    def obj_create(self, bundle, request=None, **kwargs):
        '''
        We only need the email-adress to register a user. 
        '''

        ema = bundle.data['user']['email']
        usn = ema[:30] # Dirty, will be changed
        raw_pwd = mkpasswd() # Returns a 8 characters random password

        try:
            # Launches model validation
            User(username=usn, email=ema, password=raw_pwd).full_clean()
        except ValidationError as e:
            raise CustomBadRequest(
                code="wrong_user_infos_exception",
                message="Information is wrong as detailed: {0}".
                    format(e))
        else:
            bundle.data['user']['username'] = usn
            bundle.data['user']['password'] = make_password(raw_pwd)
            bundle = super(ClientInfoResource, self).obj_create(bundle, **kwargs)

        return bundle

     # The UserResource is not paste here, but it has nothing special (no override)

POST 요청에 대한 데이터 페이로드는 다음과 같이 간단합니다.

data_post = {
        "user":{
            "email": "[email protected]",
        }

다른 질문. 사용자 생성이 CliProfileResource가 아닌 UserResource에 있어야한다고 생각하기 때문에 내가 잘못하고 있다고 생각합니다. 실제로 BusinessProfileResource (사용자에게도 연결됨)가 있으며이 리소스에 동일한 사용자 생성을 넣어야합니다. 이는 건조하지 않습니다. 분할하는 깨끗한 방법이 있습니까? UserResource의 사용자 생성 및 CliProfileResource의 cliprofile 정보?

감사합니다.

데이비드 D.

이유를 찾았는데 허용 된 필드에 fields포함되지 않은 내 UserResource 필드 때문입니다 password. 추가하면 작동합니다.

그럼에도 불구하고 GET 요청이 암호를 갖는 것이 문제 dehydrate()였기 때문에 UserResource 메서드를 재정의했습니다 .

작동 버전은 다음과 같습니다.

class Meta(CommonResourceMeta):
    queryset = User.objects.all()
    resource_name = 'users'
    fields = ['id', 'username', 'email', 'password', 'bizuser']
    detail_allowed_methods = ['get']

...

def dehydrate(self, bundle):
    '''Remove pwd from data bundle for obvious security purposes'''
    try:
        bundle.data.pop('password')
    except:
        pass
    return bundle

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관