示例#1
0
def modify_password(sid, pn, password, role):
    # validate sid to confirm the phone validation process
    session = get_session_dict(sid)
    if session is None:
        raise Error404("Sid does not exist")

    # validate if sid match pn
    if pn != session.get(RegistrationSessionKeys.PHONE_NUMBER):
        raise Error409("Validation code not match")

    if session.get(RegistrationSessionKeys.VALIDATE_STATUS
                   ) == ValidateStatus.VALIDATE_SUCCEEDED:
        # change password and save
        users = User.objects.filter(pn=pn, role=role)
        if len(users) == 0:
            raise Error404("User does not exist")

        if not validators.validate(password, "user password"):
            raise Error403("Format of password not valid")

        users[0].set_password(password)
        users[0].save()
        # destroy sid
        destroy_session(sid)

    else:
        raise Error405("Not validated")
示例#2
0
def register(sid, pn, password, role):
    try:
        session = get_session_dict(sid)
    except KeyError:
        raise Error404("Sid does not exist")

    if pn != session.get(RegistrationSessionKeys.PHONE_NUMBER):
        raise Error409("Phone number and sid do not match.")

    if session.get(RegistrationSessionKeys.VALIDATE_STATUS
                   ) == ValidateStatus.VALIDATE_SUCCEEDED:
        if len(User.objects.filter(pn=pn)) != 0:
            raise Error401("User exists")

        # Validation is moved to serializers, Below is discarded
        # if not validators.validate(password, "user password"):
        #     raise Error403("Format of password not valid")
        #
        # if not role_choice.validate(role):
        #     raise Error400("role is invalid")

        User.objects.create_user(pn=pn, password=password, role=role)
        # destroy sid
        destroy_session(sid)

    else:
        raise Error405("Not validated")
示例#3
0
def validate_sid(sid, pn, vcode):
    try:
        session = get_session_dict(sid)
    except KeyError:
        raise Error404("Sid does not exist")

    if pn != session.get(RegistrationSessionKeys.PHONE_NUMBER):
        raise Error409("pn conflicts with sid")

    if session.get(RegistrationSessionKeys.VALIDATE_STATUS) == ValidateStatus.VALIDATE_FAILED:
        raise Error401("Validate code not match")

    if session.get(RegistrationSessionKeys.VALIDATE_STATUS) == ValidateStatus.VALIDATE_SUCCEEDED:
        return

    if session.get(RegistrationSessionKeys.VALIDATE_STATUS) is None:
        # should not happen but cleanup in case
        destroy_session(sid)
        destroy_session(RegistrationSessionKeys.PN_2_SID % pn)
        raise Error404("Sid does not exist")

    if session.get(RegistrationSessionKeys.VALIDATE_STATUS) == ValidateStatus.VALIDATE_SENT:
        if session.get(RegistrationSessionKeys.VCODE) == vcode:
            session[RegistrationSessionKeys.VALIDATE_STATUS] = ValidateStatus.VALIDATE_SUCCEEDED
            update_session_dict(sid, session)
            return
        else:
            session[RegistrationSessionKeys.VALIDATE_STATUS] = ValidateStatus.VALIDATE_FAILED
            update_session_dict(sid, session)
            raise Error401("Validate code not match")

    raise Error500("Unexpected fork")
示例#4
0
def register(sid, pn, user):
    try:
        user.pn = pn
        user.is_active = True
        user.save()
        # destroy sid
        destroy_session(sid)
    except UserSid.DoesNotExist:
        raise WLException(400, 'user_sid error')