Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask role-based authorization
#2
Although I do not have a role system setup, here is a basic example that I was playing around with a while back.
Hope it helps.
I installed gunicorn in the venv and it ran fine. On a side note you can also use pkill gunicorn to kill the server

database.py
import sqlite3 


class Database:
    def __init__(self):
        self.connection = sqlite3.connect('users.db')
        self.cursor = self.connection.cursor()

    def getuser(self, user, passwd):
        query = f'select user, password from users where user="{user}" and password="{passwd}"'
        return self.cursor.execute(query).fetchone()
app.py
import sqlite3
from flask import (Flask , render_template, url_for, request, 
flash, redirect, session)
from database import Database

app = Flask(__name__)
app.secret_key = 'my secret key'

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/form')
def form():
    return render_template('form.html')

@app.route('/validate', methods=['POST', 'GET'])
def validate():
    if request.method == 'POST':
        user = request.form['user']
        passwd = request.form['mypass']
        db = Database()

        data = db.getuser(user, passwd)
        if data != None:
            if data[0] == user.lower() and data[1] == passwd:
                session['user'] = data[0]
                session['password'] = data[1]
                session['role'] = data[2]
                flash(f'Hello {data[0]}')
                return redirect(url_for('success'))
            
        flash(f'Sorry, username, password combination does not exist.')
        return redirect(url_for('error'))

@app.route('/error')
def error():
    return render_template('error.html')

@app.route('/success')
def success():
    return render_template('success.html')

@app.route('/logout')
def logout():
    session.pop('user', None)
    session.pop('password', None)
    session.pop('role', None)
    return redirect(url_for('index'))


if __name__ == '__main__':
    app.run(debug=True)
In templates folder
error.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {% with messages = get_flashed_messages() %}
    {% if messages %}

        {% for message in messages %}
        <p style="background-color: orange; color: navy; padding:8px;">{{ message }}</p>
        {% endfor %}

    {% endif %}
    {% endwith %}
<a href="{{url_for('form')}}">Login</a><br />
<a href="{{url_for('index')}}">Index</a>
</body>
</html>
form.html
<form action="{{url_for('validate')}}" method="post">
    <table>
        <tr>
            <td>User:</td><td><input type="text", name="user"></td>
        </tr>
        <tr>
            <td>Password:</td><td><input type="password" name="mypass"></td>
        </tr>
        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
</form>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FlaskBlog</title>
</head>
<body>
   <h1>Welcome to Flask</h1>
   <a href="{{url_for('form')}}">Login</a>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Success Page</h1>
    {% with messages = get_flashed_messages() %}
    {% if messages %}

        {% for message in messages %}
        <p style="background-color: orange; color: navy; padding: 8px;">{{ message }}</p>
        {% endfor %}
 
    {% endif %}
    {% endwith %}
   <a href="{{url_for('index')}}">Logout</a>

</body>
</html>
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Messages In This Thread
Flask role-based authorization - by erdemath - May-03-2024, 12:39 PM
RE: Flask role-based authorization - by menator01 - May-03-2024, 07:08 PM
RE: Flask role-based authorization - by erdemath - May-04-2024, 05:11 PM
RE: Flask role-based authorization - by erdemath - May-04-2024, 06:18 PM
RE: Flask role-based authorization - by menator01 - May-04-2024, 06:26 PM
RE: Flask role-based authorization - by menator01 - May-04-2024, 06:19 PM
RE: Flask role-based authorization - by erdemath - May-04-2024, 06:38 PM
RE: Flask role-based authorization - by menator01 - May-04-2024, 06:42 PM
RE: Flask role-based authorization - by erdemath - May-04-2024, 06:45 PM
RE: Flask role-based authorization - by menator01 - May-04-2024, 06:47 PM
RE: Flask role-based authorization - by erdemath - May-04-2024, 06:51 PM
RE: Flask role-based authorization - by menator01 - May-04-2024, 07:02 PM
RE: Flask role-based authorization - by menator01 - May-04-2024, 07:08 PM
RE: Flask role-based authorization - by erdemath - May-04-2024, 07:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask with paramiko based ssh client gives gevent LoopExit exception hbknjr 3 6,215 Dec-25-2018, 07:48 AM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020