Fixed typos and timezone

This commit is contained in:
Ferdinand Thiessen 2020-10-15 02:19:51 +02:00
parent 790e65791d
commit 3f9fdc773c
4 changed files with 14 additions and 11 deletions

View File

@ -36,26 +36,27 @@ def _create_token():
""" Login User
Login in User and create an AccessToken for the User.
Requires POST data {'userid': string, 'password': string}
Returns:
A JSON-File with user information and created token or errors
"""
logger.debug("Start log in.")
data = request.get_json()
username = data['username']
userid = data['userid']
password = data['password']
logger.debug("search user {{ {} }} in database".format(username))
user = userController.login_user(username, password)
logger.debug("search user {{ {} }} in database".format(userid))
user = userController.login_user(userid, password)
if not user:
raise Unauthorized
logger.debug("user is {{ {} }}".format(user))
token = access_controller.create(user, user_agent=request.user_agent)
logger.debug("access token is {{ {} }}".format(token))
logger.info("User {{ {} }} success login.".format(username))
logger.info("User {{ {} }} success login.".format(userid))
# Lets cleanup the DB
access_controller.clear_expired()
return jsonify({"user": user, "token": token.token})
return jsonify({"user": user, "token": token, "permissions": user.get_permissions()})
@auth_bp.route("/auth", methods=['GET'])

View File

@ -3,7 +3,7 @@ from ..models.accessToken import AccessToken
from flaschengeist.system.database import db
from flaschengeist import logger
from werkzeug.exceptions import Forbidden
from datetime import datetime, timedelta
from datetime import datetime, timezone
from . import Singleton

View File

@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from ..database import db
from secrets import compare_digest
from flaschengeist import logger
@ -39,7 +39,7 @@ class AccessToken(db.Model):
"""
return {
"token": self.token,
"expires": self.expires,
"expires": self.expires.replace(tzinfo=timezone.utc),
"lifetime": self.lifetime,
"browser": self.browser,
"platform": self.platform

View File

@ -61,6 +61,9 @@ class User(db.Model):
if 'display_name' in data:
self.display_name = data['display_name']
def get_permissions(self):
return [permission.name for role in self.roles for permission in role.permissions]
def has_permissions(self, permissions):
for role in self.roles:
for permission in role.permissions:
@ -70,13 +73,12 @@ class User(db.Model):
def serialize(self):
return {
# TODO: username should be UID?
"username": self.uid,
"userid": self.uid,
"display_name": self.display_name,
"firstname": self.firstname,
"lastname": self.lastname,
"mail": self.mail,
"roles": ["user"] + [r.name for r in self.roles]
"roles": [r.name for r in self.roles]
}