Implement a simple user rights management (RBAC) using Python+Flask+Bootstrap (Part3)

Code structure and description

I listed all the files in the first chapter, and I'll cover them in this chapter.

root directory

  1. db_design.txt
    This is the sql script that initializes the database. There is a user.sqlite under the tool_portal directory, and this file is generated by this initialization script.The initialization script includes a series of sql statements.For example:
    a) Establish the user table users, including an automatically generated and unique userid, username, user role userrole, user password
    b) Create a role table roles, including an auto-generated and unique roleid with the role name rolename
    c) Create menu tables menu, including an automatically generated and unique menuid, menuname menu name, parent menu of menu, menu order menuorder
    d) Create a role mapping menu rolemapping, including an auto-generated and unique mappingid, role roleid, and menuid

The specific sql statements are as follows

CREATE TABLE users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL DEFAULT (null) ,userrole Integer,password text);
CREATE TABLE roles (roleid INTEGER PRIMARY KEY AUTOINCREMENT, rolename TEXT NOT NULL DEFAULT (null));
CREATE TABLE menus (menuid INTEGER PRIMARY KEY AUTOINCREMENT, menuname TEXT NOT NULL DEFAULT (null) ,parentmenu integer, menuorder INTEGER);
CREATE TABLE rolemappings (mappingid INTEGER PRIMARY KEY AUTOINCREMENT, roleid INTEGER, menuid INTEGER);

When the table is created, we add some default values to the menu, including
a) Add three roles to the roles table, admin, support, sales
b) Add an admin user to the users table with Password123!
c) Add two parent menus to the menus table, supportmenu and salesmenu, and add two submenu1/2 and salessubmenu1/2 to each of them
The specific sql statements are as follows

insert into roles(rolename) values('admin');
insert into roles(rolename) values('support');
insert into roles(rolename) values('sales');

insert into users(username,userrole,password) values ('admin',1,'63c9c7ffffcae785a032832c03cf0c79da5d5789b6a152aa49751b65');

insert into menus (menuname,menuorder) values ("supportmenu",1);
insert into menus (menuname,menuorder) values ("salesmenu",2);
insert into menus (menuname,parentmenu,menuorder) values ("supportsubmenu1",1,1);
insert into menus (menuname,parentmenu,menuorder) values ("supportsubmenu2",1,2);
insert into menus (menuname,parentmenu,menuorder) values ("salessubmenu1",2,1);
insert into menus (menuname,parentmenu,menuorder) values ("salessubmenu2",2,2);
  1. run.py
    Main program, run this program to start the webserver.As you can see from the source program, we introduced Flask and render_template from flask, app from tool_portal, and login from views
    The last program started at port 5000
#!/usr/bin/env python
from flask import Flask,render_template
from tool_portal import app
import os

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SECRET_KEY'] = os.urandom(24)


from tool_portal.views import login
app.register_blueprint(login.bp)
app.add_url_rule("/", endpoint="login.login")

@app.route('/', methods=['GET', 'POST'])
def index():
    print("here")
    return render_template('index.html')

app.run(host='0.0.0.0',port=5000,debug=True)

tool_portal

Program directory in which all actual programs execute

init.py

The program initializes the file, generates the app, and calls the root directory. The main purpose is to enable the root directory to support many similar applications, such as tool_portal, or to create a parallel directory too_portal_1/2/3, etc.

#!/usr/bin/env python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

def create_app():
    app = Flask(__name__)
    return app

app=create_app()

model.py

Database mapping, mapping the database we created earlier to the program so that the program can operate the database.

from flask_sqlalchemy import SQLAlchemy
from tool_portal import app
import os


USERDBPATH='sqlite:///users.sqlite'
app.config['SQLALCHEMY_BINDS'] = {'userdb':USERDBPATH}
dbusers=SQLAlchemy(app)


class USERS(dbusers.Model):
    __bind_key__ = 'userdb'
    __tablename__ = 'users'
    userid = dbusers.Column(dbusers.Integer, primary_key=True)
    username = dbusers.Column(dbusers.Text, nullable=False)
    userrole = dbusers.Column(dbusers.Integer)
    password=dbusers.Column(dbusers.Text)


class ROLES(dbusers.Model):
    __bind_key__ = 'userdb'
    __tablename__ = 'roles'
    roleid = dbusers.Column(dbusers.Integer, primary_key=True)
    rolename = dbusers.Column(dbusers.Text, nullable=False)

class MENUS(dbusers.Model):
    __bind_key__ = 'userdb'
    __tablename__ = 'menus'
    menuid = dbusers.Column(dbusers.Integer, primary_key=True)
    menuname = dbusers.Column(dbusers.Text, nullable=False)
    parentmenu = dbusers.Column(dbusers.Integer, nullable=False)
    menuorder = dbusers.Column(dbusers.Integer, nullable=False)


class ROLEMAPPINGS(dbusers.Model):
    __bind_key__ = 'userdb'
    __tablename__ = 'rolemappings'
    mappingid=dbusers.Column(dbusers.Integer,primary_key=True)
    roleid = dbusers.Column(dbusers.Integer,nullable=False)
    menuid = dbusers.Column(dbusers.Integer, nullable=False)

static

Static file library, which includes two folders, CSS and JS. The files in these two folders are provided by Bootstrap and jquery. We can use them directly without describing them.

├── static
    │   ├── css
    │   │   ├── bootstrap.min.css
    │   │   └── signin.css
    │   └── js
    │       ├── bootstrap.min.js
    │       └── jquery-3.4.1.min.js

templates

The html template files, which are the content we can see from the browser, may be replaced in key places depending on the content, making the content different for each user.

views

Dynamic files, where the python function script is located, we only have one file for this article, login.py

user.sqlite

The database file, which is a sqlite3 local database file, has been explained in detail how to initialize the db_design.txt of the database in the root directory, and how to connect and operate the database in model.py

Three original articles were published, 0 won and 17 visited
Private letter follow

Tags: Database SQL SQLite Python

Posted on Sun, 23 Feb 2020 21:49:31 -0500 by mynameisbob