def wrapper(*args, **kwargs): uid = None sid = None uid = request.args.get('openid') if uid is None: cookie = request.headers.get('Cookie') if cookie is not None: uid, sid = parse_cookie(cookie) if not isinstance( uid, int) and uid is not None and sid is not None: return UserVerifyInfo.COOKIE_ERROR.value if isinstance(uid, int) and sid is not None: f = req_verify_sid(uid, sid) if not f: # 验证不通过 return UserVerifyInfo.UID_ERROR.value if uid is None: return UserVerifyInfo.UID_NONE.value user = User.find_by_uid(uid) if not user: user = User.add(uid=uid) cls.user = user return func(*args, **kwargs)
def wrapper(*args, **kwargs): uid = None sid = None cookie = request.headers.get('Cookie') print('\n', cookie, 'cookie11111111111111111111111111111111111111111') if cookie is not None: uid, sid = parse_cookie(cookie) if not isinstance( uid, int) and uid is not None and sid is not None: return False if isinstance(uid, int) and sid is not None: f = req_verify_sid(uid, sid) if not f: return UserVerifyInfo.UID_ERROR.value if uid is None: return False user = User.find_by_uid(uid) if not user: user = User.add(uid=uid) cls.user = user return func(*args, **kwargs)
def add_user(): body = request.get_json() print(body) email = body.get("email", None) _password = body.get("_password", None) facebook = body.get("facebook", None) instagram = body.get("instagram", None) twitter = body.get("twitter", None) linkedIn = body.get("linkedIn", None) youTube = body.get("youTube", None) is_psychologist = body.get("is_psychologist", None) description = body.get("description", None) if not email or not _password or is_psychologist is None: return "Missing info", 400 password_hashed = generate_password_hash( _password, method='pbkdf2:sha256', salt_length=8) user_id = User.add( email, password_hashed, facebook, instagram, twitter, linkedIn, youTube, is_psychologist, description ) if is_psychologist: psychologist = User_psychologist( name = body.get("name"), lastname = body.get("lastname"), identity_number = body.get("identity_number"), association_number = body.get("association_number") , speciality = body.get("speciality"), user_id=user_id ) psychologist.add() return jsonify(psychologist.to_dict()), 201 company = User_company( company_name = body.get("company_name"), company_number = body.get("company_number"), user_id = user_id ) company.add() return jsonify(company.to_dict()), 201
def current_user(self): try: uid = g.uid sid = g.sid except Exception as e: return error(InterfaceTips.INVALID_COOKIES) user = User.find_by_uid(uid) if uid is None: return error(InterfaceTips.MISSING_COOKIES) if not user: user = User.add(uid=uid) self._current_user = user return self._current_user
def post(self): """ Register a user --- tags: - auth parameters: - in: body name: body required: true description: User's name and password schema: id: register properties: username: type: string default: kevin password: type: string default: P@ssword1 email: type: string default: [email protected] responses: 200: description: A registered user schema: id: user properties: username: type: string default: kevin password: type: string default: P@ssword1 email: type: string default: [email protected] """ request_dict = request.get_json() if not request_dict: response = {'error': 'No input data provided'} abort(status.HTTP_400_BAD_REQUEST, response) errors = user_schema.validate(request_dict) if errors: abort(status.HTTP_400_BAD_REQUEST, errors) try: username = request_dict['username'].lower() if re.match(r'[A-Za-z]+$', username) is None: return {"error": "Non-alphabetic characters for username are not allowed"}, 400 password = request_dict['password'] email = request_dict['email'] except KeyError as error: res = {"error": str(error)} abort(400, res) existing_email = User.query.filter_by(email=email).first() if existing_email: abort(409, {"error": "A user with the same email already exists"}) if not User.is_unique(username=username): response = {"error": "A user with the same name already exists"} abort(status.HTTP_409_CONFLICT, response) error, validated_name = User.validate_data(ctx=username) if validated_name: user = User(username=username, email=email) error_message, password_ok = \ user.check_password_strength_and_hash_if_ok(password) if password_ok: user.add(user) result = {"message": "User successfully registered"} return result, status.HTTP_201_CREATED else: res = {'error': error_message} abort(status.HTTP_400_BAD_REQUEST, res) else: response = {"error": error} abort(response, 400)