Exemple #1
0
    def put(self):
        admin = User.find_by_id(get_jwt_identity())
        data = request.get_json()

        user = User.find_by_id(data['user_id'])

        if not user.is_admin:
            return {"msg": "Sie haben nicht die notwendigen Rechte."}, 403

        if not user:
            return {"msg": "User konnte nicht gefunden werden."}, 404

        else:
            for key, value in data.items():
                user[key] = data[key]

        try:
            user.save()
            return {
                "msg":
                "User {username}/{vorname} wurde geupdatet.".format(
                    username=user.username, vorname=user.vorname)
            }
        except:
            return {"msg": "Ein Fehler ist beim Speichern aufgetreten."}, 500
Exemple #2
0
def create_admin_user():
    logger.info("Creating admin")

    logger.info("Username: ?")

    username = input(prompt)
    logger.info("Email: ?")
    email = input(prompt)

    logger.info("Passwort")
    password = input(prompt)
    logger.info("Passwort wiederholen")
    password2 = input(prompt)

    if password != password2:
        logger.info(
            "Passwörter stimmen nicht überein. Bitte den Vorgang wiederholen.")
    else:
        if user.find_by_username(username):
            logger.info("Username ist schon vergeben.")
            break

        user = User(username, password, email)

        try:
            user.is_admin = True
            user.save()
            logger.info("Admin erfolgreich angelegt.")
        except:
            logger.info("Etwas ist beim Speichern der Daten falsch gelaufen.")
Exemple #3
0
    def get(self):
        admin = User.find_by_id(get_jwt_identity())

        if not user.is_admin:
            return {"msg": "Sie haben nicht die notwendigen Rechte."}, 403
        users = User.get_all()

        return {"users": [user.json() for user in users]}
Exemple #4
0
 def get(self):
     response = {}
     user = User.find_by_id(get_jwt_identity())
     if not user.admin:
         response['status'] = "ERROR"
         response['msg'] = "No admin rights"
         return response, 403
     else:
         schema = UserSchema(many=True)
         users = User.get_all()
         return schema.dump(users).data, 200
Exemple #5
0
def test_create_user():
    

    schema = UserSchema()
    
    result = schema.load(user)
    if result.errors == '{}':
        user = User(username, password, email)
        user.save()

        saved_user = User.find_by_username(username=user['username'])
    
        assert user['username'] == saved_user.username
        assert user['email'] == saved_user.email
Exemple #6
0
 def post(self):
     response = {}
     data = request.get_json()
     
     if User.find_by_username(data['username']):
         response['status'] = "ERROR"
         response['message'] = "Username ist bereits vergeben."
         return response, 500
     
     user = User(**data)
     user.save()
     response['status'] = "OK"
     response['message'] = "User wurde erfolgreich angelegt."
     return response, 201
Exemple #7
0
    def put(self):
        # /admin/rechnungslauf
        data = request.get_json()
        settings = SystemSetting.get_settings()

        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host="localhost"))
        channel = connection.channel()
        channel.queue_declare(queue="email")

        for each in data:
            settings = SystemSetting.get_settings()
            logging.warning(f"Using: {each}")
            logging.warning(
                f"username: {each['username']}, coffee_count: {each['coffee_count']}"
            )
            obj = {
                "username": "",
                "email": "",
                "coffee_count": "",
                "betrag": ""
            }
            logging.warning("Rechne new_coffee_count aus")
            new_coffee_count = RechnungsApi.count(each['coffee_count'])
            if new_coffee_count is None:
                new_coffee_count = int(0)
            logging.warning(f"New coffee count: {new_coffee_count}")
            logging.warning("Getting user")
            user = User.find_by_username(each['username'])
            logging.warning(f"Found user: {user.username}")
            logging.warning(f"User coffee count: {user.coffee_count}")
            temp_var = user.coffee_count - new_coffee_count
            logging.warning(f"Temporary coffee_count: {temp_var}")
            user.coffee_count = temp_var
            user.save()
            obj["username"] = user.username
            obj["email"] = user.email
            obj["coffee_count"] = new_coffee_count
            obj["betrag"] = new_coffee_count * settings.kaffee_preis
            logging.warning(f"obj: {obj}")
            user = User.find_by_username(each['username'])
            logging.warning(f"Neuer User coffee counter: {user.coffee_count}")
            channel.basic_publish(exchange="",
                                  routing_key="email",
                                  body=json.dumps(obj))
        connection.close()
        return {
            "message":
            "Die Daten wurden gespeichert und Emails werden versendet."
        }, 201
Exemple #8
0
    def add_claims_to_jwt(identity):
        from app.api.user.models import User
        user = User.find_by_id(identity)

        if user.is_admin:
            return {'roles': "admin"}
        return {'roles': "user"}
Exemple #9
0
    def put(self):
        admin = User.find_by_id(get_jwt_identity())
        systemSetting = SystemSetting.get_setting()

        if not admin.is_admin:
            return {"msg": "Sie haben nicht die notwendigen Rechte."}, 403

        if not sysSetting:
            return {"msg": "Etwas ist schief gelaufen."}, 500

        data = request.get_json()

        for key, value in data.items():
            systemSetting[key] = data[key]

        try:
            sysSetting.save()
            return {
                "msg": "System Einstellungen wurden erfolgreich gespeichert."
            }, 201
        except:
            return {
                "msg":
                "Etwas ist beim Speichern der System Einstellungen schief gelaufen"
            }, 500
Exemple #10
0
def create_admin_user():
    if not User.query.filter_by(is_admin=True).first():
        print("Username: "******"Email: ")
        email = input(prompt)
        print("Password: "******"Password repeat: ")
        pw2 = input(prompt)

        if pw1 == pw2:
            user = User(username, pw1, email)
            user.is_admin = True
            user.save()
            print("Admin user created")
        else:
            print("Admin user already exists")
Exemple #11
0
    def get(self):
        user = User.find_by_id(get_jwt_identity())
        schema = UserSchema()

        response = {
            "status": "OK",
            "user": schema.dump(user).data
        }
        return response, 200
Exemple #12
0
    def put(self):
        user_id = get_jwt_identity()
        user = User.query.filter_by(id=user_id)
        user.update(request.json)
        db.session.commit()

        user = User.find_by_id(get_jwt_identity())
        schema = UserSchema()

        response = {"status": "OK", "user": schema.dump(user).data}
        return response, 200
Exemple #13
0
 def get(self):
     users = User.get_all()
     user_list = []
     for user in users:
         obj = {
             "username": user.username,
             "coffeeCount": user.coffee_count
         }
         user_list.append(obj)
     
     return user_list, 200
Exemple #14
0
    def post(self):

        data = request.get_json()
        logger.info(data)

        if User.find_by_username(data.get('username')):
            logger.info("User not found.")
            return {"msg": "Dieser Username ist bereits vergeben."}, 500

        user = User(**data)
        try:
            logger.info("Saving user data {}".format(data))
            user.save()
            return {"msg": "User wurde erfolgreich angelegt."}
        except:
            logger.info("ERROR while saving user data.")
            return {
                "msg":
                "Der User konnte nicht angelegt werden. Ein Fehler ist auftreten."
            }
Exemple #15
0
    def get(self):
        systemSetting = SystemSetting.get_setting()
        admin = User.find_by_id(get_jwt_identity())

        if not user.is_admin:
            return {"msg": "Sie haben nicht die notwendigen Rechte."}, 403

        if not sysSetting:
            return {"msg": "Etwas ist schief gelaufen."}, 500

        return {"sysSetting": systemSetting.json()}
Exemple #16
0
 def post(self):
     response = {}
     user = User.find_by_id(get_jwt_identity())
     if not user.admin:
         response['status'] = "ERROR"
         response['msg'] = "No admin rights"
         return response, 403
     else:
         schema = UserSchema()
         result = schema.load(request.json)
         if not result.errors:
             if User.find_by_username(username=result.data['username']):
                 response["status"] = "ERROR"
                 response["msg"] = "Username existiert bereits"
                 return response, 400
             else:
                 data = request.get_json()
                 user = User(**data)
                 user.save()
                 response['status'] = "OK"
                 response['msg'] = "User wurde angelegt"
                 return response, 201
         else:
             response['status'] = "ERROR"
             response['msg'] = result.errors
             return response, 300
Exemple #17
0
    def post(self):
        user = User.find_by_username(data.get('username'))

        if user and user.check_password(data.get('password'), user._password):
            logger.info("Creating access_token")
            access_token = create_access_token(identity=str(user.id),
                                               fresh=True)
            return {
                "access_token": access_token,
                "username": user.username
            }, 200

        return {"msg": "Invalid credentials"}, 401
Exemple #18
0
    def post(self):
        response = {}
        data = data.get_json()
        user = User.find_by_username(username=data['username'])

        if user and user.check_pw(user._password, data['password']):
            response['token'] = create_access_token(identity=str(user.id),
                                                    fresh=True)
            response['status'] = "OK"
            response['msg'] = "Du wurdest erfolgreich eingeloggt."
            return response, 200
        else:
            response['status'] = "ERROR"
            response['msg'] = "Username und/oder Passwort falsch."
            return response, 401
Exemple #19
0
    def put(self):
        user = User.find_by_id(get_jwt_identity())
        data = request.get_json()

        if not user:
            return {"msg": "Kein User gefunden."}, 404

        else:
            for key, value in data.items():
                user[key] = data[key]

        try:
            user.save()
            return {"msg": "Daten wurden gespeichert."}, 201
        except:
            return {
                "msg":
                "Etwas ist beim Speichern der User-Daten schief gelaufen."
            }, 500
Exemple #20
0
    def post(self):
        response = {}
        data = request.get_json()
        user = User.find_by_username(data['username'])
        if user and user.check_pw(password=data['password'],
                                  hashed_pw=user._password):
            token = create_access_token(
                identity=str(user.id),
                fresh=True,
                expires_delta=datetime.timedelta(minutes=60))
            response['token'] = token
            response['username'] = user.username
            response['status'] = "OK"
            response['message'] = "User wurde angemeldet"

            return response, 200
        else:
            response['status'] = "ERROR"
            response['message'] = "Username und/oder Passwort sind falsch"
            return response, 403
Exemple #21
0
    def post(self):
        response = {}
        admin = User.find_by_id(get_jwt_identity())
        if not admin.is_admin:
            response['status'] = "ERROR"
            response[
                'message'] = "Sie haben nicht die nötigen Rechte. Bitte wenden sie sich an den Admin."
            return response, 403

        data = request.get_json()
        if User.find_by_username(data['username']):
            response['status'] = "ERROR"
            response['message'] = "Username vergeben"
            return response, 500

        user = User(**data)
        user.save()
        response['status'] = "OK"
        response['message'] = "User wurde angelegt"
        return response, 201
Exemple #22
0
 def get(self):
     #get all users for admin
     schema = UserSchema(many=True)
     users = User.get_all()
     return schema.dump(users).data, 200
Exemple #23
0
    def get(self):
        user = User.find_by_id(get_jwt_identity())

        if not user:
            return {"msg": "User nicht gefunden."}, 404
        return {"user": user.json()}, 200
Exemple #24
0
def test_User_find_by_username():
     
    user = User.find_by_username(username=user['username'])
    
    assert user['username'] == saved_user.username
    assert user['email'] == saved_user.email