[Doc] User plugin documentation created

This commit is contained in:
Ferdinand Thiessen 2020-10-30 05:53:15 +01:00
parent 8a9776ae0e
commit e0d3b211bb
5 changed files with 73 additions and 33 deletions

View File

@ -43,7 +43,7 @@ def configure_app(app):
user=config["DATABASE"]["user"], user=config["DATABASE"]["user"],
passwd=config["DATABASE"]["password"], passwd=config["DATABASE"]["password"],
host=config["DATABASE"]["host"], host=config["DATABASE"]["host"],
database=config["DATABASE"]["database"] database=config["DATABASE"]["database"],
) )
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

View File

@ -16,6 +16,7 @@ def login_required(permission=None):
Returns: Returns:
Wrapped function with login (and permission) guard Wrapped function with login (and permission) guard
""" """
def wrap(func): def wrap(func):
@wraps(func) @wraps(func)
def wrapped_f(*args, **kwargs): def wrapped_f(*args, **kwargs):

View File

@ -4,8 +4,10 @@ from sqlalchemy.types import DateTime, TypeDecorator
class ModelSerializeMixin: class ModelSerializeMixin:
def serialize(self): def serialize(self):
"""Return: """Serialize class to dict
Dict of all not private or protected annotated member variables.""" Returns:
Dict of all not private or protected annotated member variables.
"""
d = {param: getattr(self, param) for param in self.__class__.__annotations__ if not param.startswith("_")} d = {param: getattr(self, param) for param in self.__class__.__annotations__ if not param.startswith("_")}
if len(d) == 1: if len(d) == 1:
key, value = d.popitem() key, value = d.popitem()

View File

@ -31,7 +31,7 @@ def list_roles(current_session):
current_session: Session sent with Authorization Header current_session: Session sent with Authorization Header
Returns: Returns:
JSON encodes array of `flaschengeist.models.user.Role` JSON encoded array of `flaschengeist.models.user.Role`
""" """
roles = roleController.get_all() roles = roleController.get_all()
return jsonify(roles) return jsonify(roles)

View File

@ -1,3 +1,8 @@
"""Users plugin
Provides routes used to manage users
"""
from flask import Blueprint, request, jsonify from flask import Blueprint, request, jsonify
from werkzeug.exceptions import NotFound, BadRequest, Forbidden from werkzeug.exceptions import NotFound, BadRequest, Forbidden
@ -16,16 +21,6 @@ class UsersPlugin(Plugin):
def __init__(self, config): def __init__(self, config):
super().__init__(blueprint=users_bp, permissions=[_permission_edit, _permission_delete, _permission_set_roles]) super().__init__(blueprint=users_bp, permissions=[_permission_edit, _permission_delete, _permission_set_roles])
#################################################
# Routes #
# #
# /users POST: register new #
# GET: get all users #
# /users/<uid> GET: get user with uid #
# PUT: modify user #
# DELETE: remove user #
#################################################
@users_bp.route("/users", methods=["POST"]) @users_bp.route("/users", methods=["POST"])
def __registration(self): def __registration(self):
@ -35,42 +30,85 @@ def __registration(self):
@users_bp.route("/users", methods=["GET"]) @users_bp.route("/users", methods=["GET"])
@login_required() @login_required()
def __list_users(**kwargs): def list_users(current_session):
"""List all existing users
Route: ``/users`` | Method: ``GET``
Args:
current_session: Session sent with Authorization Header
Returns:
JSON encoded array of `flaschengeist.models.user.User` or HTTP error
"""
logger.debug("Retrieve list of all users") logger.debug("Retrieve list of all users")
users = userController.get_users() users = userController.get_users()
return jsonify(users) return jsonify(users)
@users_bp.route("/users/<uid>", methods=["GET"]) @users_bp.route("/users/<userid>", methods=["GET"])
@login_required() @login_required()
def __get_user(uid, **kwargs): def get_user(userid, current_session):
logger.debug("Get information of user {{ {} }}".format(uid)) """Retrieve user by userid
user = userController.get_user(uid)
if user: Route: ``/users/<userid>`` | Method: ``GET``
return jsonify(user)
raise NotFound Args:
userid: UserID of user to retrieve
current_session: Session sent with Authorization Header
Returns:
JSON encoded `flaschengeist.models.user.User` or HTTP error
"""
logger.debug("Get information of user {{ {} }}".format(userid))
user = userController.get_user(userid)
return jsonify(user)
@users_bp.route("/users/<uid>", methods=["DELETE"]) @users_bp.route("/users/<userid>", methods=["DELETE"])
@login_required(permission=_permission_delete) @login_required(permission=_permission_delete)
def __delete_user(uid, **kwargs): def delete_user(userid, current_session):
logger.debug("Delete user {{ {} }}".format(uid)) """Delete user by userid
user = userController.get_user(uid)
Route: ``/users/<userid>`` | Method: ``DELETE``
Args:
userid: UserID of user to retrieve
current_session: Session sent with Authorization Header
Returns:
HTTP-200 or HTTP error
"""
logger.debug("Delete user {{ {} }}".format(userid))
user = userController.get_user(userid)
userController.delete(user) userController.delete(user)
return jsonify({"ok": "ok"})
@users_bp.route("/users/<uid>", methods=["PUT"]) @users_bp.route("/users/<userid>", methods=["PUT"])
@login_required() @login_required()
def __edit_user(uid, current_session, **kwargs): def edit_user(userid, current_session):
logger.debug("Modify information of user {{ {} }}".format(uid)) """Modify user by userid
user = userController.get_user(uid)
Route: ``/users/<userid>`` | Method: ``PUT``
POST-data: ```{firstname?: string, lastname?: string, display_name?: string, mail?: string,
password?: string, roles?: string[]}```
Args:
userid: UserID of user to retrieve
current_session: Session sent with Authorization Header
Returns:
HTTP-200 or HTTP error
"""
logger.debug("Modify information of user {{ {} }}".format(userid))
user = userController.get_user(userid)
data = request.get_json() data = request.get_json()
password = None password = None
new_password = data["new_password"] if "new_password" in data else None new_password = data["new_password"] if "new_password" in data else None
if uid != current_session._user.userid: if userid != current_session._user.userid:
if not user.has_permission(_permission_edit): if not user.has_permission(_permission_edit):
return Forbidden return Forbidden
else: else:
@ -89,4 +127,3 @@ def __edit_user(uid, current_session, **kwargs):
userController.modify_user(user, password, new_password) userController.modify_user(user, password, new_password)
userController.update_user(user) userController.update_user(user)
return jsonify({"ok": "ok"})