Ejemplo n.º 1
0
    def post(self):
        # register user endpoint
        data = validate_user(request.get_json())
        if data['ok']:
            data = data['data']

            # check if user exists
            user = User.query.filter_by(email=data['email']).first()
            if user:
                return { "message" : "Email has already been taken"}, 400

            # get password
            data['password'] = flask_bcrypt.generate_password_hash(data['password'])

            # find the role of the user
            role = Role.query.filter_by(name=data['role']).first()
            del data['role']
            
            # Add a new patient to db
            user_schema = UserSchema()
            new_user = user_schema.load(data, session=db.session)

            role.users.append(new_user) # add new user to their role

            db.session.add(role) # add user and role
            db.session.commit()

            return {}, 200
        else:
            return {'message': 'Please check the fields'}, 400
Ejemplo n.º 2
0
    def post(self):
        """
        Return the data for the comparisons.

        :returns: error: With a error, data: with data.
        :rtype: json
        """
        args = get_options(['username', 'password'])
        checks = ['username', 'password']
        error = ''
        result = {}

        for check in checks:
            if not args[check] and not error:
                error = '\'{}\' is missing'.format(check)

        if not error:
            user = validate_user(args['username'], args['password'])

            if user is None:
                error = 'Error in login'
            else:
                algs = get_user_algs(user)
                token = user.generate_auth_token(app.config['SECRET_KEY'])
                result.update({
                    'token': token,
                    'username': user.username,
                    'algs': algs
                })

        result.update({'error': error})

        return result
Ejemplo n.º 3
0
def user_change():
    if request.method == 'POST':
        uid = request.form.get('id', '')
        age = request.form.get('age', '')
        state, error = models.validate_user(age=age)
        if state:
            models.user_update(uid, age)
            return redirect(url_for('user'))
        else:
            return render_template('user_modify.html',
                                   id=uid,
                                   age=age,
                                   error=error)
Ejemplo n.º 4
0
    def post(self):
        data = validate_user(request.get_json())
        if data["ok"]:
            data = data["data"]

            user = User.query.filter_by(email=data["email"]).first()

            if user and flask_bcrypt.check_password_hash(
                    user.password, data["password"]):
                del data["password"]

                # setup any extra user params
                roles = []
                if user.roleIds:
                    for role in user.roleIds:
                        roles.append(role.name.name)

                data["roles"] = roles
                data["firstName"] = user.firstName
                data["healthFacilityName"] = user.healthFacilityName
                data["isLoggedIn"] = True
                data["userId"] = user.id

                vhtList = []
                data["vhtList"] = []
                if "CHO" in roles:
                    if user.vhtList:
                        for user in user.vhtList:
                            vhtList.append(user.id)
                        data["vhtList"] = vhtList

                access_token = create_access_token(identity=data)
                refresh_token = create_refresh_token(identity=data)
                data["token"] = access_token
                data["refresh"] = refresh_token

                return data, 200
            else:
                return {"message": "Invalid email or password"}, 401
        else:
            return (
                {
                    "message":
                    "Bad request parameters: {}".format(data["message"])
                },
                400,
            )
Ejemplo n.º 5
0
def user_save():
    if request.method == 'POST':
        username = request.form.get('username', '')
        password = request.form.get('password', '')
        age = request.form.get('age', '')
        state, error = models.validate_user(username=username,
                                            password=password,
                                            age=age)
        if state:
            models.user_add(username, password, age)
            return redirect(url_for('user'))
        else:
            return render_template('user_create.html',
                                   username=username,
                                   password=password,
                                   age=age,
                                   error=error)
Ejemplo n.º 6
0
def login():
    # Retrieve the HTTP POST request parameter value from 'request.form' dictionary
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')

        valid_user = validate_user(username, password)

        if valid_user:
            session['username'] = username
            data = get_data()
            return render_template('response.html', data=data)
        else:
            return render_template('login.html',
                                   message="Please enter correct credentials")
    else:
        return render_template('login.html')
Ejemplo n.º 7
0
def signup():
    '''Display signup page. If user enters valid username and password store the username
    in the session and redirect to the /newpost page.'''

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        verify = request.form['verify']
        existing_user = User.query.filter_by(username=username).first()

        if validate_user(username, password, verify):
            return render_template('signup.html', user_name=username)

        if not existing_user:
            new_user = User(username, password)
            db.session.add(new_user)
            db.session.commit()
            session['username'] = username
            return redirect('/newpost')

    return render_template('signup.html')
Ejemplo n.º 8
0
    def post(self):
        data = validate_user(request.get_json())
        if data['ok']:
            data = data['data']

            user = User.query.filter_by(email=data['email']).first()

            if user and flask_bcrypt.check_password_hash(user.password, data['password']):
                del data['password']

                # setup any extra user params
                roles = []
                if user.roleIds:
                    for role in user.roleIds:
                        roles.append(role.name.name)
                
                data['roles'] = roles
                data['firstName'] = user.firstName
                data['healthFacilityName'] = user.healthFacilityName
                data['isLoggedIn'] = True
                data['userId'] = user.id

                vhtList = []
                data['vhtList'] = []
                if 'CHO' in roles:
                    if user.vhtList:
                        for user in user.vhtList:
                            vhtList.append(user.id)
                        data['vhtList'] = vhtList

                access_token = create_access_token(identity=data)
                refresh_token = create_refresh_token(identity=data)
                data['token'] = access_token
                data['refresh'] = refresh_token

                return data, 200
            else:
                return {'message': 'Invalid email or password'}, 401
        else:
            return {'message': 'Bad request parameters: {}'.format(data['message'])}, 400
Ejemplo n.º 9
0
    def post(self):
        # register user endpoint
        data = validate_user(request.get_json())
        if data["ok"]:
            data = data["data"]

            # check if user exists
            user = User.query.filter_by(email=data["email"]).first()
            if user:
                return {"message": "Email has already been taken"}, 400

            # get password
            data["password"] = flask_bcrypt.generate_password_hash(data["password"])

            # find the role of the user
            role = Role.query.filter_by(name=data["role"]).first()
            if (
                role
                and data["role"] == "ADMIN"
                and data["healthFacilityName"] == "Null"
            ):
                data["healthFacilityName"] = None
            del data["role"]

            # Add a new user to db
            user_schema = UserSchema()
            new_user = user_schema.load(data, session=db.session)

            role.users.append(new_user)  # add new user to their role

            db.session.add(role)  # add user and role
            db.session.commit()

            return new_user.id, 201
        else:
            return {"message": "Please check the fields"}, 400