Skip to content

Commit

Permalink
Merge pull request #88 from kuafuai/feat/user
Browse files Browse the repository at this point in the history
chore(pro)
  • Loading branch information
yakeJiang authored Aug 29, 2023
2 parents e2a5a17 + ebca4e0 commit c33d941
Show file tree
Hide file tree
Showing 19 changed files with 494 additions and 129 deletions.
43 changes: 39 additions & 4 deletions backend/app/controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@

bp = Blueprint('user', __name__, url_prefix='/user')

@bp.route('/register', methods=['POST'])
@json_response
def register():
_ = getI18n("controllers")
data = request.json
username = data['username']
password = data['password']
email = data['email']
phone_number = data['phone']
zone_language = LANGUAGE
if "language" in session:
zone_language = session['language']

if GRADE == "base":
raise Exception("The current version does not support this feature")
else:
# todo 0
current_tenant = 0
user = UserPro.create_user(username, password, phone_number, email, zone_language, current_tenant)

return user.username

@bp.route('/login', methods=['POST'])
@json_response
def login():
Expand All @@ -19,24 +41,32 @@ def login():

if GRADE == "base":
ok = User.checkPassword(username, password)
session['tenant_id'] = 0
else:
ok = UserPro.checkPassword(username, password)
if ok:
userinfo = UserPro.get_user_by_name(username)
session['language'] = userinfo["zone_language"]
session['tenant_id'] = userinfo["current_tenant"]

if ok:
session['logged_in'] = True
session['username'] = username
# todo 1
session['tenant_id'] = 0
session['username'] = username
return {'message': _('Login successful.')}
else:
raise Exception("Invalid username or password")
raise Exception(_("Invalid username or password"))


@bp.route('/logout', methods=['POST'])
@json_response
def logout():
_ = getI18n("controllers")
language = LANGUAGE
if "language" in session:
language = session['language']
session.clear()
session['language'] = language
session.update()
return {'message': _('Logout successful.')}

@bp.route('/change_language', methods=['GET'])
Expand All @@ -53,6 +83,11 @@ def change_language():
session['language'] = "zh"
session.update()

if GRADE != "base" and "username" in session:
username = session['username']
userinfo = UserPro.get_user_by_name(username)
UserPro.update_user(userinfo["user_id"], zone_language=session['language'])

return {'message': _('success.')}


Expand Down
5 changes: 5 additions & 0 deletions backend/app/pkgs/tools/i18b.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ def getFrontendText():
"requirement_satisfaction": _("Requirement satisfaction_rating"),
"opensource_version_1": _("Historical requirement recovery is not supported at this time, please visit workspace to obtain the code results"),
"notice": _("Notice"),
"username": _("Username"),
"password": _("Password"),
"register": _("Register"),
"email": _("Email"),
"phone": _("Phone"),
}
2 changes: 1 addition & 1 deletion backend/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def require_login():
session['tenant_id'] = 0
session[session["username"]] = getEmptyTaskInfo()

if request.path == '/user/language' or request.path == '/user/login':
if request.path == '/user/language' or request.path == '/user/login' or request.path == '/user/logout' or request.path == '/user/change_language' or request.path == '/user/register':
no = 1
elif 'logged_in' not in session:
return {'success': False, 'error': 'Access denied', 'code': 401}
Expand Down
29 changes: 10 additions & 19 deletions frontend/static/js/coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function sendAjaxRequest(url, method, requestData, successCallback, errorCallbac
} else {
console.log(data.error)
try {
errorCallback(data.error + "<br />" + globalFrontendText["backend_return_error"]);
errorCallback(data.error);
} catch (error) {
myAlert("ERROR", error);
console.error(error);
Expand Down Expand Up @@ -457,23 +457,27 @@ function logincheck() {

successCallback = function(data) {
var username = data.data.username
//const url = window.location;
//const newUrl = url.origin+url.pathname;
//history.pushState('', '', newUrl);
$("#current-username").html(username)
$("#watermark-username").html(username)
}

errorCallback = function(data) {
$("#my-login").modal('show')
username = "Guest"
$("#current-username").html(username)
$("#watermark-username").html(username)
const url = window.location;
const path = url.pathname;
if (path != "/user_login.html" && path != "/user_register.html") {
window.location.href = "user_login.html";
}
}

sendAjaxRequest('/requirement/clear_up', 'GET', info, successCallback, errorCallback, true, false)
}

function logout() {
successCallback = function() {
location.reload();
window.location.href = "user_login.html";
}

sendAjaxRequest('/user/logout', "POST", "", successCallback, alertErrorCallback, true, true)
Expand All @@ -487,20 +491,7 @@ function changeLanguage() {
sendAjaxRequest('/user/change_language', "GET", "", successCallback, alertErrorCallback, true, false)
}

function login() {
var requestData = JSON.stringify({ 'username': $("#login-username").val(), 'password': $("#login-password").val() })

successCallback = function(data) {
location.reload();
}

errorCallback = function(error) {
$("#login-message").html(error)
$("#login-message").fadeOut().fadeIn()
}

sendAjaxRequest('/user/login', "POST", requestData, successCallback, errorCallback, true, false)
}

function myAlert(alert_title, alert_content) {
$("#my-alert-title").html(alert_title)
Expand Down
41 changes: 41 additions & 0 deletions frontend/static/js/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
$(document).ready(function () {
// show dropdown on hover
$('.main.menu .ui.dropdown').dropdown({
on: 'hover'
});
});

function login() {
var requestData = JSON.stringify({ 'username': $("#login-username").val(), 'password': $("#login-password").val() })

successCallback = function(data) {
window.location.href = "index.html";
}

errorCallback = function(error) {
$("#login-message").html(error)
$("#login-message").fadeOut().fadeIn()
}

sendAjaxRequest('/user/login', "POST", requestData, successCallback, errorCallback, true, false)
}

function register() {
var requestData = JSON.stringify({
'username': $("#login-username").val(),
'password': $("#login-password").val(),
'email': $("#login-email").val(),
'phone': $("#login-phone").val(),
})

successCallback = function(data) {
window.location.href = "user_login.html";
}

errorCallback = function(error) {
$("#login-message").html(error)
$("#login-message").fadeOut().fadeIn()
}

sendAjaxRequest('/user/register', "POST", requestData, successCallback, errorCallback, true, false)
}
74 changes: 74 additions & 0 deletions frontend/user_login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="robots" content="noarchive">
<title class="f_title"></title>
<link rel="stylesheet" href="./static/css/style.css?v=6">
<link rel="stylesheet" href="./static/css/semantic.min.css">
<link rel="stylesheet" href="./static/css/codemirror.min.css" />
<link rel="stylesheet" href="./static/css/darcula.min.css" />
<script src="./static/js/codemirror.min.js"></script>
<script src="./static/js/haxe.min.js"></script>
<script src="./static/js/jquery.min.js"></script>
<script src="./static/js/semantic.min.js"></script>
<script src="./static/js/marked.min.js"></script>
<script src="./static/js/diff.min.js"></script>
<script src="./static/js/coder.js?v=75"></script>
<script src="./static/js/user.js?v=75"></script>
</head>

<body>
<div class="ui borderless main menu margin-top-0">
<div class="ui text container ai-textcontainer">
<div class="header item" style="padding-left: 0px;">
<img class="logo" src="./static/image/logo.png" style="margin-right: 5px;">
<span class="f_title"></span>
</div>
<a class="item" href="index.html"><span class="f_start_task"></span></a>
<a class="item" href="requirement.html"><span class="f_requirement_list"></span></a>
<a class="item" href="app.html"><span class="f_app_list"></span></a>
<a class="item" href="setting.html"><span class="f_setting"></span></a>
<a href="#" class="ui right floated dropdown item">
<span class="f_more_operations"></span> <i class="dropdown icon"></i>
<div class="menu">
<div class="item"><span class="f_hello"></span> <span id="current-username"></span></div>
<div class="item" onClick="changeLanguage()"><span class="f_change_language"></span></div>
<div class="item" onClick="logout()"><span class="f_logout"></span></div>
</div>
</a>
</div>
</div>

<div class="ui container">
<div class="ui form">
<div class="field">
<label><span class="f_username"></span></label>
<input type="text" id="login-username" placeholder="">
</div>
<div class="field">
<label><span class="f_password"></span></label>
<input type="password" id="login-password" placeholder="">
</div>
<div class="field">
<label id="login-message" style="color: red;"></label>
</div>
<div class="field">
<a class="ui right floated button blue" href="user_register.html"><i class="id card outline icon"></i><span class="f_register"></span></a>
<button class="ui right floated button green" onClick="login()" type="submit"><i class="user icon"></i><span class="f_submit"></span></div></button>
</div>
</div>
</div>

<div class="ui modal" id="my-alert">
<div class="header" id="my-alert-title"></div>
<div class="content" id="my-alert-content">
</div>
</div>

<div class="ui watermark">
<span id="watermark-username"></span> @ <span class="f_title"></span></div>
</div>
</body>
</html>
82 changes: 82 additions & 0 deletions frontend/user_register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="robots" content="noarchive">
<title class="f_title"></title>
<link rel="stylesheet" href="./static/css/style.css?v=6">
<link rel="stylesheet" href="./static/css/semantic.min.css">
<link rel="stylesheet" href="./static/css/codemirror.min.css" />
<link rel="stylesheet" href="./static/css/darcula.min.css" />
<script src="./static/js/codemirror.min.js"></script>
<script src="./static/js/haxe.min.js"></script>
<script src="./static/js/jquery.min.js"></script>
<script src="./static/js/semantic.min.js"></script>
<script src="./static/js/marked.min.js"></script>
<script src="./static/js/diff.min.js"></script>
<script src="./static/js/coder.js?v=75"></script>
<script src="./static/js/user.js?v=75"></script>
</head>

<body>
<div class="ui borderless main menu margin-top-0">
<div class="ui text container ai-textcontainer">
<div class="header item" style="padding-left: 0px;">
<img class="logo" src="./static/image/logo.png" style="margin-right: 5px;">
<span class="f_title"></span>
</div>
<a class="item" href="index.html"><span class="f_start_task"></span></a>
<a class="item" href="requirement.html"><span class="f_requirement_list"></span></a>
<a class="item" href="app.html"><span class="f_app_list"></span></a>
<a class="item" href="setting.html"><span class="f_setting"></span></a>
<a href="#" class="ui right floated dropdown item">
<span class="f_more_operations"></span> <i class="dropdown icon"></i>
<div class="menu">
<div class="item"><span class="f_hello"></span> <span id="current-username"></span></div>
<div class="item" onClick="changeLanguage()"><span class="f_change_language"></span></div>
<div class="item" onClick="logout()"><span class="f_logout"></span></div>
</div>
</a>
</div>
</div>

<div class="ui container">
<div class="ui form">
<div class="field">
<label><span class="f_username"></span></label>
<input type="text" id="login-username" placeholder="">
</div>
<div class="field">
<label><span class="f_password"></span></label>
<input type="password" id="login-password" placeholder="">
</div>
<div class="field">
<label><span class="f_email"></span></label>
<input type="text" id="login-email" placeholder="">
</div>
<div class="field">
<label><span class="f_phone"></span></label>
<input type="text" id="login-phone" placeholder="">
</div>
<div class="field">
<label id="login-message" style="color: red;"></label>
</div>
<div class="field">
<a class="ui right floated button blue" href="user_register.html"><i class="user icon"></i><span class="f_login"></span></a>
<button class="ui right floated button green" onClick="register()" type="submit"><i class="id card outline icon"></i><span class="f_submit"></span></div></button>
</div>
</div>
</div>

<div class="ui modal" id="my-alert">
<div class="header" id="my-alert-title"></div>
<div class="content" id="my-alert-content">
</div>
</div>

<div class="ui watermark">
<span id="watermark-username"></span> @ <span class="f_title"></span></div>
</div>
</body>
</html>
Loading

0 comments on commit c33d941

Please sign in to comment.