-
Notifications
You must be signed in to change notification settings - Fork 2
/
flask_login_auth.py
88 lines (71 loc) · 2.86 KB
/
flask_login_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# import Psycopg2 as pg
# --------------------------------------------------------------------
import psycopg2 as pg
# --------------------------------------------------------------------
# connection to database----------------------------------------------
def get_connection():
try:
conn = pg.connect(
database='umnsntob',
user='umnsntob',
password='H7mI1xL-pv2MqfDsJNOG4SVDgxkRJJYq',
host='stampy.db.elephantsql.com',
port=5432)
return conn
except Exception as error:
return error
# for authenticating ---------------------------------------------------
# Try except block is used for passing errors
def authenticate(username, password):
try:
connection = get_connection()
cursor = connection.cursor()
query = """SELECT userid, password, role from "public"."user_rec" where userid='%s' and password='%s'""" # noqa
query = query % (username, password)
cursor.execute(query)
rows = cursor.fetchall()
try:
if (rows[0][0] == username) and (rows[0][1] == password):
connection.close()
return 1
else:
connection.close()
return 0
except Exception as error:
return error
except Exception as error:
return error
# -------------------------------------------------------------------------
# Role verification -------------------------------------------------------
def role_authenticate(username, password):
connection = get_connection()
cursor = connection.cursor()
query = """SELECT userid, password, role from "public"."user_rec" where userid='%s' and password='%s'""" # noqa
query = query % (username, password)
cursor.execute(query)
rows = cursor.fetchall()
return rows[0][2]
# -------------------------------------------------------------------------
# Search Box --------------------------------------------------------------
def searchbox(userid):
try:
connection = get_connection()
cursor = connection.cursor()
query = """SELECT * FROM "public"."user_rec" WHERE userid LIKE '%s'"""
query = query % ('%' + userid + '%')
cursor.execute(query)
rows = cursor.fetchall()
connection.close()
return rows
except Exception as error:
return error
# -------------------------------------------------------------------------
# User Blocked ------------------------------------------------------------
def blocked(username, password):
connection = get_connection()
cursor = connection.cursor()
query = """SELECT userid, password, block from "public"."user_rec" where userid='%s' and password='%s'""" # noqa
query = query % (username, password)
cursor.execute(query)
rows = cursor.fetchall()
return rows[0][2]