-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed various authorization issues, limitations listed in TODO
+ Fixed @login_required issues with /search URL + Modified session handling to allow at most one logged user + Added Auth.authorized function to check route-specific authorization (the @login_required decorator right now just checks that someone is logged in)
- Loading branch information
1 parent
e0e7208
commit c46405b
Showing
20 changed files
with
175 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FLASK_APP=chatroom | ||
FLASK_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
from chatroom import db | ||
from functools import wraps | ||
|
||
from flask import g, url_for, redirect | ||
from flask import g, url_for, redirect, session | ||
|
||
# decorator to allow the operation iff the user is logged in to a session | ||
def login_required(f): | ||
@wraps(f) | ||
def login_required(fn): | ||
@wraps(fn) | ||
def login_wrapper(*args, **kwargs): | ||
if 'user_id' in kwargs: | ||
if 'user_id' in session: | ||
# check for a session in the db | ||
cur = db.get_db().execute(''' | ||
SELECT userref FROM Sessions WHERE userref = ?;''', [kwargs['user_id']]) | ||
if cur.fetchone() is not None: | ||
SELECT userref | ||
FROM Sessions | ||
WHERE userref = ?;''', [session['user_id']]) | ||
row = cur.fetchone() | ||
print('user is', row['userref']) | ||
if row is not None: | ||
print('session ok') | ||
return f(*args, **kwargs) | ||
print('session fail') | ||
return redirect(url_for('auth.login')) | ||
return login_wrapper | ||
return fn(*args, **kwargs) | ||
print('session fail') | ||
return redirect(url_for('auth.login')) | ||
return login_wrapper |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
function edit_msg(event) { | ||
var formData = { | ||
msg: $("#message").val(), | ||
}; | ||
|
||
$.ajax({ | ||
type: "PUT", | ||
url: this.action, | ||
data: JSON.stringify(formData), | ||
contentType: "application/json", | ||
dataType: "json", | ||
encode: true, | ||
}).done(function (data) { | ||
window.location.href = data['url'] | ||
//alert("Message updated successfully"); | ||
}).fail(function(data) { | ||
//alert("Failed message update"); | ||
}).always(function(data) { | ||
console.log("msg-edit: ", JSON.stringify(data)); | ||
}); | ||
|
||
event.preventDefault(); | ||
} | ||
|
||
function delete_msg(event) { | ||
$.ajax({ | ||
type: "DELETE", | ||
url: this.href, | ||
contentType: "application/json", | ||
dataType: "json", | ||
encode: true, | ||
}).done(function (data) { | ||
window.location.href = data['url'] | ||
alert("Message deleted successfully"); | ||
}).fail(function(data) { | ||
alert("Failed message deletion"); | ||
}).always(function(data) { | ||
console.log("msg-delete"); | ||
}); | ||
|
||
event.preventDefault(); | ||
} | ||
|
||
function search_user(event) { | ||
var url = document.URL; | ||
|
||
$.ajax({ | ||
type: "GET", | ||
url: "/search/", | ||
data: { "user": $("#query").val()}, | ||
contentType: "application/json", | ||
dataType: "json", | ||
encode: true, | ||
success: function(data) { | ||
// process the json data | ||
matching_users = data | ||
select_node = document.getElementById("matching-users"); | ||
while (select_node.firstChild) { | ||
select_node.removeChild(select_node.lastChild); | ||
} | ||
for (const user of matching_users) { | ||
var opt = document.createElement('option'); | ||
opt.value = user.user_id | ||
opt.innerHTML = user.username + " (user_id: " + user.user_id + ")"; | ||
select_node.appendChild(opt); | ||
} | ||
select_node.firstChild.selected = true; | ||
console.log("user-search: " + query); | ||
} | ||
}).fail(function(data) { | ||
alert("Failed query"); | ||
}).always(function(data) { | ||
console.log("user-search", data); | ||
}); | ||
|
||
event.preventDefault(); | ||
} | ||
|
||
$(document).ready(function () { | ||
$("#msg-edit-form").on("submit", edit_msg); | ||
$("#msg-delete").on("click", delete_msg); | ||
$("#user-search").on("change", search_user); | ||
$("#user-search").on("submit", search_user); | ||
}); | ||
|
Oops, something went wrong.