[dd-sso] Adapt admin so it is easily importable
This paves the path forward for thorough testing.GON-3874-DD-moodle
parent
cdfa4c5724
commit
579af2b31c
|
@ -23,42 +23,42 @@ import os
|
||||||
from flask_login import LoginManager, UserMixin
|
from flask_login import LoginManager, UserMixin
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Dict
|
from typing import TYPE_CHECKING, Dict
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from admin.flaskapp import AdminFlaskApp
|
from admin.flaskapp import AdminFlaskApp
|
||||||
|
|
||||||
ram_users = {
|
ram_users = {}
|
||||||
os.environ["ADMINAPP_USER"]: {
|
|
||||||
"id": os.environ["ADMINAPP_USER"],
|
for app, app_role in [
|
||||||
"password": os.environ["ADMINAPP_PASSWORD"],
|
("ADMINAPP", "manager"),
|
||||||
"role": "manager",
|
("KEYCLOAK", "admin"),
|
||||||
},
|
("WORDPRESS_MARIADB", "manager"),
|
||||||
os.environ["KEYCLOAK_USER"]: {
|
]:
|
||||||
"id": os.environ["KEYCLOAK_USER"],
|
k = f"{app}_USER"
|
||||||
"password": os.environ["KEYCLOAK_PASSWORD"],
|
if os.environ.get(k, ""):
|
||||||
"role": "admin",
|
pk = f"{app}_PASSWORD"
|
||||||
},
|
ram_users[os.environ[k]] = {
|
||||||
os.environ["WORDPRESS_MARIADB_USER"]: {
|
"id": os.environ[k],
|
||||||
"id": os.environ["WORDPRESS_MARIADB_USER"],
|
"password": os.environ[pk],
|
||||||
"password": os.environ["WORDPRESS_MARIADB_PASSWORD"],
|
"role": app_role,
|
||||||
"role": "manager",
|
}
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class User(UserMixin):
|
class User(UserMixin):
|
||||||
def __init__(self, id : str, password : str, role : str, active : bool = True) -> None:
|
def __init__(self, id: str, password: str, role: str, active: bool = True) -> None:
|
||||||
self.id = id
|
self.id = id
|
||||||
self.username = id
|
self.username = id
|
||||||
self.password = password
|
self.password = password
|
||||||
self.role = role
|
self.role = role
|
||||||
self.active = active
|
self.active = active
|
||||||
|
|
||||||
def setup_auth(app : "AdminFlaskApp") -> None:
|
|
||||||
|
def setup_auth(app: "AdminFlaskApp") -> None:
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
login_manager.init_app(app)
|
login_manager.init_app(app)
|
||||||
login_manager.login_view = "login"
|
login_manager.login_view = "login"
|
||||||
|
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def user_loader(username : str) -> User:
|
def user_loader(username: str) -> User:
|
||||||
u = ram_users[username]
|
u = ram_users[username]
|
||||||
return User(id = u["id"], password = u["password"], role = u["role"])
|
return User(id=u["id"], password=u["password"], role=u["role"])
|
||||||
|
|
|
@ -161,13 +161,13 @@ class AdminFlaskApp(Flask):
|
||||||
# Move on with settings from the environment
|
# Move on with settings from the environment
|
||||||
self.config.update({
|
self.config.update({
|
||||||
"DOMAIN": self.domain,
|
"DOMAIN": self.domain,
|
||||||
"KEYCLOAK_POSTGRES_USER": os.environ["KEYCLOAK_DB_USER"],
|
"KEYCLOAK_POSTGRES_USER": os.environ.get("KEYCLOAK_DB_USER"),
|
||||||
"KEYCLOAK_POSTGRES_PASSWORD": os.environ["KEYCLOAK_DB_PASSWORD"],
|
"KEYCLOAK_POSTGRES_PASSWORD": os.environ.get("KEYCLOAK_DB_PASSWORD"),
|
||||||
"MOODLE_POSTGRES_USER": os.environ["MOODLE_POSTGRES_USER"],
|
"MOODLE_POSTGRES_USER": os.environ.get("MOODLE_POSTGRES_USER"),
|
||||||
"MOODLE_POSTGRES_PASSWORD": os.environ["MOODLE_POSTGRES_PASSWORD"],
|
"MOODLE_POSTGRES_PASSWORD": os.environ.get("MOODLE_POSTGRES_PASSWORD"),
|
||||||
"NEXTCLOUD_POSTGRES_USER": os.environ["NEXTCLOUD_POSTGRES_USER"],
|
"NEXTCLOUD_POSTGRES_USER": os.environ.get("NEXTCLOUD_POSTGRES_USER"),
|
||||||
"NEXTCLOUD_POSTGRES_PASSWORD": os.environ["NEXTCLOUD_POSTGRES_PASSWORD"],
|
"NEXTCLOUD_POSTGRES_PASSWORD": os.environ.get("NEXTCLOUD_POSTGRES_PASSWORD"),
|
||||||
"VERIFY": os.environ["VERIFY"] == "true",
|
"VERIFY": os.environ.get("VERIFY") == "true",
|
||||||
"API_SECRET": os.environ.get("API_SECRET"),
|
"API_SECRET": os.environ.get("API_SECRET"),
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -68,9 +68,9 @@ if TYPE_CHECKING:
|
||||||
from admin.flaskapp import AdminFlaskApp
|
from admin.flaskapp import AdminFlaskApp
|
||||||
from admin.lib.callbacks import ThirdPartyCallbacks
|
from admin.lib.callbacks import ThirdPartyCallbacks
|
||||||
|
|
||||||
MANAGER = os.environ["CUSTOM_ROLE_MANAGER"]
|
MANAGER = os.environ.get("CUSTOM_ROLE_MANAGER", "manager")
|
||||||
TEACHER = os.environ["CUSTOM_ROLE_TEACHER"]
|
TEACHER = os.environ.get("CUSTOM_ROLE_TEACHER", "teacher")
|
||||||
STUDENT = os.environ["CUSTOM_ROLE_STUDENT"]
|
STUDENT = os.environ.get("CUSTOM_ROLE_STUDENT", "student")
|
||||||
|
|
||||||
DDUser = Dict[str, Any]
|
DDUser = Dict[str, Any]
|
||||||
DDGroup = Dict[str, Any]
|
DDGroup = Dict[str, Any]
|
||||||
|
|
|
@ -57,8 +57,8 @@ class KeycloakClient:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
url : str="http://dd-sso-keycloak:8080/auth/",
|
url : str="http://dd-sso-keycloak:8080/auth/",
|
||||||
username : str=os.environ["KEYCLOAK_USER"],
|
username : str=os.environ.get("KEYCLOAK_USER", ""),
|
||||||
password : str=os.environ["KEYCLOAK_PASSWORD"],
|
password : str=os.environ.get("KEYCLOAK_PASSWORD", ""),
|
||||||
realm : str="master",
|
realm : str="master",
|
||||||
verify : bool=True,
|
verify : bool=True,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -71,8 +71,8 @@ class KeycloakClient:
|
||||||
self.keycloak_pg = Postgres(
|
self.keycloak_pg = Postgres(
|
||||||
"dd-apps-postgresql",
|
"dd-apps-postgresql",
|
||||||
"keycloak",
|
"keycloak",
|
||||||
os.environ["KEYCLOAK_DB_USER"],
|
os.environ.get("KEYCLOAK_DB_USER", ""),
|
||||||
os.environ["KEYCLOAK_DB_PASSWORD"],
|
os.environ.get("KEYCLOAK_DB_PASSWORD", ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
def connect(self) -> None:
|
def connect(self) -> None:
|
||||||
|
|
|
@ -51,8 +51,8 @@ class Nextcloud:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
app : "AdminFlaskApp",
|
app : "AdminFlaskApp",
|
||||||
username : str=os.environ["NEXTCLOUD_ADMIN_USER"],
|
username : str=os.environ.get("NEXTCLOUD_ADMIN_USER", ""),
|
||||||
password : str=os.environ["NEXTCLOUD_ADMIN_PASSWORD"],
|
password : str=os.environ.get("NEXTCLOUD_ADMIN_PASSWORD", ""),
|
||||||
verify : bool=True,
|
verify : bool=True,
|
||||||
) -> None:
|
) -> None:
|
||||||
url = "https://nextcloud." + app.config["DOMAIN"]
|
url = "https://nextcloud." + app.config["DOMAIN"]
|
||||||
|
|
Loading…
Reference in New Issue