got login working

This commit is contained in:
Phillip Tarrant 2020-08-13 10:38:36 -04:00
parent 85a69954ea
commit 25affba2e6
12 changed files with 343 additions and 24 deletions

View File

@ -140,6 +140,11 @@ class Helpers:
except: except:
pass pass
@staticmethod
def get_time_as_string():
now = datetime.now()
return now.strftime("%m/%d/%Y, %H:%M:%S")
@staticmethod @staticmethod
def check_file_exists(path: str): def check_file_exists(path: str):
logger.debug('Looking for path: {}'.format(path)) logger.debug('Looking for path: {}'.format(path))

View File

@ -92,7 +92,8 @@ class db_builder:
Webhooks Webhooks
]) ])
def default_settings(self): @staticmethod
def default_settings():
Users.insert({ Users.insert({
Users.username: 'Admin', Users.username: 'Admin',
Users.password: helper.encode_pass('asdfasdf'), Users.password: helper.encode_pass('asdfasdf'),
@ -100,4 +101,11 @@ class db_builder:
Users.enabled: True Users.enabled: True
}).execute() }).execute()
@staticmethod
def is_fresh_install():
if helper.check_file_exists(helper.db_path):
return False
return True
installer = db_builder() installer = db_builder()

View File

@ -0,0 +1,37 @@
import json
import logging
import tornado.web
import tornado.escape
import bleach
from app.classes.shared.console import console
from app.classes.shared.models import Users
from app.classes.web.base_handler import BaseHandler
logger = logging.getLogger(__name__)
class PanelHandler(BaseHandler):
@tornado.web.authenticated
def get(self, page):
# name = tornado.escape.json_decode(self.current_user)
user_data = json.loads(self.get_secure_cookie("user_data"))
template = "panel/denied.html"
page_data = {
'version_data': "version_data_here",
'user_data': user_data
}
if page == 'unauthorized':
template = "panel/denied.html"
elif page == 'dashboard':
template = "panel/dashboard.html"
self.render(
template,
data=page_data
)

View File

@ -1,4 +1,5 @@
import sys import sys
import json
import logging import logging
import tornado.web import tornado.web
import tornado.escape import tornado.escape
@ -6,6 +7,7 @@ import tornado.escape
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
from app.classes.web.base_handler import BaseHandler from app.classes.web.base_handler import BaseHandler
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.shared.models import Users, fn
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -29,7 +31,7 @@ class PublicHandler(BaseHandler):
expire_days = "5" expire_days = "5"
if user: if user:
self.set_secure_cookie("user", tornado.escape.json_encode(user), expires_days=expire_days) self.set_secure_cookie("user", tornado.escape.json_encode(user), expires_days=int(expire_days))
else: else:
self.clear_cookie("user") self.clear_cookie("user")
@ -40,9 +42,6 @@ class PublicHandler(BaseHandler):
# print(page) # print(page)
if page is None:
self.redirect("public/login")
error = bleach.clean(self.get_argument('error', "")) error = bleach.clean(self.get_argument('error', ""))
if error: if error:
@ -50,53 +49,69 @@ class PublicHandler(BaseHandler):
else: else:
error_msg = "" error_msg = ""
# sensible defaults
template = "public/404.html"
page_data = "{}"
# if we have no page, let's go to login
if page is None:
self.redirect("public/login")
if page == "login": if page == "login":
template = "public/login.html" template = "public/login.html"
context = {'error': error_msg} page_data = {'error': error_msg}
# our default 404 template # our default 404 template
else: else:
context = {'error': error_msg} page_data = {'error': error_msg}
self.render(template, data=context) self.render(template, data=page_data)
def post(self, page=None): def post(self, page=None):
if page == 'login': if page == 'login':
next_page = "/public/login" next_page = "/public/login"
entered_email = bleach.clean(self.get_argument('email')) entered_username = bleach.clean(self.get_argument('username'))
entered_password = bleach.clean(self.get_argument('password')) entered_password = bleach.clean(self.get_argument('password'))
user_data = Users.get_or_none(Users.email_address == entered_email) user_data = Users.get_or_none(fn.Lower(Users.username) == entered_username.lower())
# if we already have a user with this email... # if we don't have a user
if not user_data: if not user_data:
next_page = "/public/login?error=Login_Failed" next_page = "/public/login?error=Login_Failed"
self.redirect(next_page) self.redirect(next_page)
return False return False
# if they are disabled
if not user_data.enabled:
next_page = "/public/login?error=Login_Failed"
self.redirect(next_page)
return False
login_result = helper.verify_pass(entered_password, user_data.password) login_result = helper.verify_pass(entered_password, user_data.password)
# Valid Login # Valid Login
if login_result: if login_result:
self.set_current_user(entered_email) self.set_current_user(entered_username)
logger.info("User: {} Logged in from IP: {}".format(entered_email, self.get_remote_ip())) logger.info("User: {} Logged in from IP: {}".format(user_data, self.get_remote_ip()))
# record this login
Users.update({ Users.update({
Users.last_ip: self.get_remote_ip() Users.last_ip: self.get_remote_ip(),
}).execute() Users.last_login: helper.get_time_as_string()
}).where(Users.username == entered_username).execute()
cookie_data = { cookie_data = {
"user_email": user_data.email_address, "username": user_data.username,
"user_id": user_data, "user_id": user_data.id,
"account_type": str(user_data.account_type).upper(), "account_type": user_data.allowed_servers,
} }
self.set_secure_cookie('user_data', json.dumps(cookie_data)) self.set_secure_cookie('user_data', json.dumps(cookie_data))
next_page = "/pro/dashboard" next_page = "/panel/dashboard"
self.redirect(next_page) self.redirect(next_page)
else: else:
self.redirect("/public/login") self.redirect("/public/login")

View File

@ -19,6 +19,7 @@ try:
import tornado.locale import tornado.locale
import tornado.httpserver import tornado.httpserver
from app.classes.web.public_handler import PublicHandler from app.classes.web.public_handler import PublicHandler
from app.classes.web.panel_handler import PanelHandler
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
logger.critical("Import Error: Unable to load {} module".format(e, e.name)) logger.critical("Import Error: Unable to load {} module".format(e, e.name))
@ -118,6 +119,7 @@ class webserver:
handlers = [ handlers = [
(r'/', PublicHandler), (r'/', PublicHandler),
(r'/public/(.*)', PublicHandler), (r'/public/(.*)', PublicHandler),
(r'/panel/(.*)', PanelHandler),
] ]
app = tornado.web.Application( app = tornado.web.Application(

View File

@ -44,7 +44,7 @@
<div class="dropdown-menu dropdown-menu-right navbar-dropdown" aria-labelledby="UserDropdown"> <div class="dropdown-menu dropdown-menu-right navbar-dropdown" aria-labelledby="UserDropdown">
<div class="dropdown-header text-center"> <div class="dropdown-header text-center">
<img class="img-md rounded-circle" src="/static/assets/images/faces-clipart/pic-1.png" alt="Profile image"> <img class="img-md rounded-circle" src="/static/assets/images/faces-clipart/pic-1.png" alt="Profile image">
<p class="mb-1 mt-3 font-weight-semibold">{{ data['user_data']['user_email'] }}</p> <p class="mb-1 mt-3 font-weight-semibold">{{ data['user_data']['username'] }}</p>
<p class="font-weight-light text-muted mb-0">Plan: {{ str(data['user_data']['account_type']).upper() }}</p> <p class="font-weight-light text-muted mb-0">Plan: {{ str(data['user_data']['account_type']).upper() }}</p>
</div> </div>
<a class="dropdown-item"><i class="dropdown-item-icon mdi mdi-account-outline text-primary"></i> My Profile <span class="badge badge-pill badge-danger">1</span></a> <a class="dropdown-item"><i class="dropdown-item-icon mdi mdi-account-outline text-primary"></i> My Profile <span class="badge badge-pill badge-danger">1</span></a>

View File

@ -0,0 +1,250 @@
{% extends ../base.html %}
{% block title %}Crafty Controller - Dashboard{% end %}
{% block content %}
<div class="content-wrapper">
<!-- Page Title Header Starts-->
<div class="row page-title-header">
<div class="col-12">
<div class="page-header">
<h4 class="page-title">Dashboard</h4>
</div>
</div>
</div>
<!-- Page Title Header Ends-->
<div class="row">
<div class="col-md-12 grid-margin">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-lg-3 col-md-6">
<div class="d-flex">
<div class="wrapper">
<h5 class="mb-1 font-weight-medium text-primary"> Nodes</h5>
<h3 class="mb-0 font-weight-semibold">2 </h3>
</div>
<div class="wrapper my-auto ml-auto ml-lg-4">
<p class="mb-0 text-success">1 Online</p>
<p class="mb-0 text-danger">1 Offline</p>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 mt-md-0 mt-4">
<div class="d-flex">
<div class="wrapper">
<h5 class="mb-1 font-weight-medium text-primary">Servers</h5>
<h3 class="mb-0 font-weight-semibold">5</h3>
</div>
<div class="wrapper my-auto ml-auto ml-lg-4">
<p class="mb-0 text-success">3 Online</p>
<p class="mb-0 text-warning">1 Shutdown</p>
<p class="mb-0 text-danger">1 Crashed</p>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 mt-md-0 mt-4">
<div class="d-flex">
<div class="wrapper">
<h5 class="mb-1 font-weight-medium text-primary">Players</h5>
<h3 class="mb-0 font-weight-semibold">18</h3>
</div>
<div class="wrapper my-auto ml-auto ml-lg-4">
<p class="mb-0 text-success">35 Max (24 H)</p>
<p class="mb-0 text-warning">10 Avg (24 H)</p>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 mt-md-0 mt-4">
<div class="d-flex">
<div class="wrapper">
<h5 class="mb-1 font-weight-medium text-primary">Backups</h5>
<h3 class="mb-0 font-weight-semibold">7</h3>
</div>
<div class="wrapper my-auto ml-auto ml-lg-4">
<p class="mb-0 text-success">Last: 11-31-2020</p>
<p class="mb-0 text-success">Next: 12-05-2020</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-lg-12 grid-margin stretch-card">
<div class="card">
<div class="card-header header-sm d-flex justify-content-between align-items-center">
<h4 class="card-title">All Servers</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr class="rounded">
<th>Server</th>
<th>CPU</th>
<th>Memory</th>
<th>Disk</th>
<th>Players</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<i class="fas fa-server"></i> Bedwarz </td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 24%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 75%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
7
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Lobby </td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 60%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
3
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Bedwarz </td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 10%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 12%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
0
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Creative </td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 60%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
5
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Survival </td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 15%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
3
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
{% end %}

View File

@ -33,9 +33,9 @@
<form action="/public/login" method="post"> <form action="/public/login" method="post">
{% raw xsrf_form_html() %} {% raw xsrf_form_html() %}
<div class="form-group"> <div class="form-group">
<label class="label">Email</label> <label class="label">Username</label>
<div class="input-group"> <div class="input-group">
<input type="text" class="form-control" placeholder="user@example.com" name="email" id="email" required="true"> <input type="text" class="form-control" placeholder="username" name="username" id="username" required="true">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">

View File

@ -89,6 +89,8 @@ if __name__ == '__main__':
# this should always be last # this should always be last
tasks_manager.start_main_kill_switch_watcher() tasks_manager.start_main_kill_switch_watcher()
# do our installer stuff
if installer.is_fresh_install():
installer.create_tables() installer.create_tables()
installer.default_settings() installer.default_settings()