Added new endpoint for roles and updated users endpoints to work with roles

This commit is contained in:
Luka Radenovic 2022-04-14 13:32:35 +02:00
parent 7661088814
commit 10479a625a
10 changed files with 108 additions and 13 deletions

1
app.py
View file

@ -13,6 +13,7 @@ from web import web
from areas import users
from areas import apps
from areas import auth
from areas import roles
from cliapp import cliapp
from web import login

View file

@ -1,4 +1,5 @@
from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from database import db
@ -25,5 +26,7 @@ class AppRole(db.Model):
app_id = db.Column(Integer, ForeignKey("app.id"), primary_key=True)
role_id = db.Column(Integer, ForeignKey("role.id"))
role = relationship("Role")
def __repr__(self):
return f"{self.role} for {self.user_id} on {self.app_id}"
return f"role_id: {self.role_id}, user_id: {self.user_id}, app_id: {self.app_id}, role: {self.role}"

2
areas/roles/__init__.py Normal file
View file

@ -0,0 +1,2 @@
from .roles import *
from .models import *

View file

@ -0,0 +1,8 @@
from .models import Role
class RoleService:
@staticmethod
def get_roles():
roles = Role.query.all()
return [{"id": r.id, "name": r.name} for r in roles]

15
areas/roles/roles.py Normal file
View file

@ -0,0 +1,15 @@
from flask import jsonify, request
from flask_jwt_extended import jwt_required
from flask_cors import cross_origin
from areas import api_v1
from .role_service import RoleService
@api_v1.route("/roles", methods=["GET"])
@jwt_required()
@cross_origin()
def get_roles():
roles = RoleService.get_roles()
return jsonify(roles)

View file

@ -1,2 +1,2 @@
from .users import *
from .models import *
from .user_service import *

View file

@ -0,0 +1,61 @@
import copy
from database import db
from areas.apps import AppRole
from helpers import KratosApi
class UserService:
@staticmethod
def get_users():
res = KratosApi.get("/identities").json()
userList = []
for r in res:
userList.append(UserService.__insertAppRoleToUser(r["id"], r))
return userList
@staticmethod
def get_user(id):
res = KratosApi.get("/identities/{}".format(id)).json()
return UserService.__insertAppRoleToUser(id, res)
@staticmethod
def post_user(data):
kratos_data = {
"schema_id": "default",
"traits": {"email": data["email"], "name": data["name"]},
}
res = KratosApi.post("/identities", kratos_data).json()
appRole = AppRole(
user_id=res["id"],
role_id=data["role_id"] if "role_id" in data else None,
app_id=1,
)
db.session.add(appRole)
db.session.commit()
return UserService.get_user(res["id"])
@staticmethod
def put_user(id, data):
kratos_data = {
"schema_id": "default",
"traits": {"email": data["email"], "name": data["name"]},
}
KratosApi.put("/identities/{}".format(id), kratos_data)
app_role = AppRole.query.filter_by(user_id=id).first()
app_role.role_id = data["role_id"] if "role_id" in data else None
db.session.commit()
return UserService.get_user(id)
@staticmethod
def __insertAppRoleToUser(userId, userRes):
app_role = AppRole.query.filter_by(user_id=userId).first()
userRes["traits"]["app_role_id"] = app_role.role_id if app_role else None
return userRes

View file

@ -5,23 +5,25 @@ from flask_expects_json import expects_json
from areas import api_v1
from helpers import KratosApi
from .validation import schema
from .user_service import UserService
@api_v1.route("/users", methods=["GET"])
@jwt_required()
@cross_origin()
def get_users():
res = KratosApi.get("/identities")
return jsonify(res.json())
res = UserService.get_users()
return jsonify(res)
@api_v1.route("/users/<string:id>", methods=["GET"])
@jwt_required()
@cross_origin()
def get_user(id):
res = KratosApi.get("/identities/{}".format(id))
return jsonify(res.json())
res = UserService.get_user(id)
return jsonify(res)
@api_v1.route("/users", methods=["POST"])
@ -30,9 +32,8 @@ def get_user(id):
@expects_json(schema)
def post_user():
data = request.get_json()
kratos_data = {"schema_id": "default", "traits": data}
res = KratosApi.post("/identities", kratos_data)
return jsonify(res.json()), res.status_code
res = UserService.post_user(data)
return jsonify(res)
@api_v1.route("/users/<string:id>", methods=["PUT"])
@ -41,9 +42,8 @@ def post_user():
@expects_json(schema)
def put_user(id):
data = request.get_json()
kratos_data = {"schema_id": "default", "traits": data}
res = KratosApi.put("/identities/{}".format(id), kratos_data)
return jsonify(res.json()), res.status_code
res = UserService.put_user(id, data)
return jsonify(res)
@api_v1.route("/users/<string:id>", methods=["DELETE"])

View file

@ -8,7 +8,12 @@ schema = {
"description": "Email of the user",
"pattern": r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])",
"minLength": 1,
}
},
"role_id": {
"type": "integer",
"description": "Role of the user",
"minimum": 1,
},
},
"required": ["email"],
}