2022-08-05 17:05:24 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2023-12-31 23:04:06 +01:00
|
|
|
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
|
2022-08-05 17:05:24 +02:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
ScoDoc 9 API : accès aux utilisateurs
|
2024-07-24 17:34:30 +02:00
|
|
|
|
|
|
|
CATEGORY
|
|
|
|
--------
|
|
|
|
Utilisateurs
|
2022-08-05 17:05:24 +02:00
|
|
|
"""
|
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask import g, request
|
|
|
|
from flask_json import as_json
|
2022-08-05 17:05:24 +02:00
|
|
|
from flask_login import current_user, login_required
|
|
|
|
|
2023-09-29 21:17:31 +02:00
|
|
|
from app import db, log
|
2023-02-22 02:13:06 +01:00
|
|
|
from app.api import api_bp as bp, api_web_bp, API_CLIENT_ERROR
|
2024-07-17 12:03:08 +02:00
|
|
|
from app.api import api_permission_required as permission_required
|
2022-08-05 17:05:24 +02:00
|
|
|
from app.auth.models import User, Role, UserRole
|
2022-08-19 12:46:21 +02:00
|
|
|
from app.auth.models import is_valid_password
|
2024-07-17 12:03:08 +02:00
|
|
|
from app.decorators import scodoc
|
|
|
|
from app.models import Departement
|
2022-08-05 17:05:24 +02:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
|
|
|
from app.scodoc.sco_permissions import Permission
|
2024-01-17 21:58:45 +01:00
|
|
|
from app.scodoc.sco_utils import json_error
|
2022-08-05 17:05:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/user/<int:uid>")
|
|
|
|
@api_web_bp.route("/user/<int:uid>")
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-05 17:05:24 +02:00
|
|
|
def user_info(uid: int):
|
|
|
|
"""
|
2024-07-24 17:34:30 +02:00
|
|
|
Info sur un compte utilisateur ScoDoc.
|
2024-07-27 13:30:02 +02:00
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/user/2
|
2022-08-05 17:05:24 +02:00
|
|
|
"""
|
2023-07-11 06:57:38 +02:00
|
|
|
user: User = db.session.get(User, uid)
|
2022-08-05 17:05:24 +02:00
|
|
|
if user is None:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "user not found")
|
2022-08-05 17:05:24 +02:00
|
|
|
if g.scodoc_dept:
|
2023-09-29 21:17:31 +02:00
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.UsersView)
|
2022-08-19 12:46:21 +02:00
|
|
|
if (None not in allowed_depts) and (user.dept not in allowed_depts):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "user not found")
|
2022-08-05 17:05:24 +02:00
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
return user.to_dict()
|
2022-08-05 17:05:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/users/query")
|
|
|
|
@api_web_bp.route("/users/query")
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-05 17:05:24 +02:00
|
|
|
def users_info_query():
|
|
|
|
"""Utilisateurs, filtrés par dept, active ou début nom
|
2024-07-24 17:34:30 +02:00
|
|
|
|
|
|
|
Exemple:
|
|
|
|
```
|
2022-08-07 11:08:12 +02:00
|
|
|
/users/query?departement=dept_acronym&active=1&starts_with=<string:nom>
|
2024-07-24 17:34:30 +02:00
|
|
|
```
|
2022-08-05 17:05:24 +02:00
|
|
|
|
2022-08-08 10:06:42 +02:00
|
|
|
Seuls les utilisateurs "accessibles" (selon les permissions) sont retournés.
|
|
|
|
Si accès via API web, le département de l'URL est ignoré, seules
|
2022-08-05 17:05:24 +02:00
|
|
|
les permissions de l'utilisateur sont prises en compte.
|
2024-06-23 17:40:48 +02:00
|
|
|
|
|
|
|
QUERY
|
|
|
|
-----
|
2024-07-24 17:34:30 +02:00
|
|
|
active: bool
|
|
|
|
departement: string
|
|
|
|
starts_with: string
|
2024-06-23 17:40:48 +02:00
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
"""
|
|
|
|
query = User.query
|
|
|
|
active = request.args.get("active")
|
|
|
|
if active is not None:
|
|
|
|
active = bool(str(active))
|
|
|
|
query = query.filter_by(active=active)
|
|
|
|
departement = request.args.get("departement")
|
|
|
|
if departement is not None:
|
|
|
|
query = query.filter_by(dept=departement or None)
|
|
|
|
starts_with = request.args.get("starts_with")
|
|
|
|
if starts_with is not None:
|
|
|
|
# remove % and _ for security
|
|
|
|
starts_with = starts_with.translate({ord(c): None for c in "%_"})
|
|
|
|
query = query.filter(User.nom.ilike(starts_with + "%"))
|
|
|
|
# Filtre selon permissions:
|
|
|
|
query = (
|
|
|
|
query.join(UserRole, (UserRole.dept == User.dept) | (UserRole.dept == None))
|
|
|
|
.filter(UserRole.user == current_user)
|
|
|
|
.join(Role, UserRole.role_id == Role.id)
|
2023-09-29 21:17:31 +02:00
|
|
|
.filter(Role.permissions.op("&")(Permission.UsersView) != 0)
|
2022-08-05 17:05:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
query = query.order_by(User.user_name)
|
2023-04-06 16:10:32 +02:00
|
|
|
return [user.to_dict() for user in query]
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
|
2023-11-21 22:28:50 +01:00
|
|
|
def _is_allowed_user_edit(args: dict) -> tuple[bool, str]:
|
|
|
|
"Vrai si on peut"
|
|
|
|
if "cas_id" in args and not current_user.has_permission(
|
|
|
|
Permission.UsersChangeCASId
|
|
|
|
):
|
|
|
|
return False, "non autorise a changer cas_id"
|
|
|
|
|
|
|
|
if not current_user.is_administrator():
|
|
|
|
for field in ("cas_allow_login", "cas_allow_scodoc_login"):
|
|
|
|
if field in args:
|
|
|
|
return False, f"non autorise a changer {field}"
|
|
|
|
return True, ""
|
|
|
|
|
|
|
|
|
2022-08-06 22:31:41 +02:00
|
|
|
@bp.route("/user/create", methods=["POST"])
|
|
|
|
@api_web_bp.route("/user/create", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-06 22:31:41 +02:00
|
|
|
def user_create():
|
|
|
|
"""Création d'un utilisateur
|
2024-07-24 17:34:30 +02:00
|
|
|
|
|
|
|
DATA
|
|
|
|
----
|
|
|
|
```json
|
2022-08-06 22:31:41 +02:00
|
|
|
{
|
2023-11-21 22:28:50 +01:00
|
|
|
"active":bool (default True),
|
2022-08-06 22:31:41 +02:00
|
|
|
"dept": str or null,
|
|
|
|
"nom": str,
|
|
|
|
"prenom": str,
|
2023-11-21 22:28:50 +01:00
|
|
|
"user_name": str,
|
|
|
|
...
|
2022-08-06 22:31:41 +02:00
|
|
|
}
|
2024-07-24 17:34:30 +02:00
|
|
|
```
|
2022-08-06 22:31:41 +02:00
|
|
|
"""
|
2023-11-21 22:28:50 +01:00
|
|
|
args = request.get_json(force=True) # may raise 400 Bad Request
|
|
|
|
user_name = args.get("user_name")
|
2022-08-06 22:31:41 +02:00
|
|
|
if not user_name:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "empty user_name")
|
2022-08-06 22:31:41 +02:00
|
|
|
user = User.query.filter_by(user_name=user_name).first()
|
|
|
|
if user:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, f"user_create: user {user} already exists\n")
|
2023-11-21 22:28:50 +01:00
|
|
|
dept = args.get("dept")
|
2022-08-06 22:31:41 +02:00
|
|
|
if dept == "@all":
|
|
|
|
dept = None
|
2023-09-29 21:17:31 +02:00
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.UsersAdmin)
|
2022-08-19 12:46:21 +02:00
|
|
|
if (None not in allowed_depts) and (dept not in allowed_depts):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(403, "user_create: departement non autorise")
|
2022-08-06 22:31:41 +02:00
|
|
|
if (dept is not None) and (
|
|
|
|
Departement.query.filter_by(acronym=dept).first() is None
|
|
|
|
):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "user_create: departement inexistant")
|
2023-11-21 22:28:50 +01:00
|
|
|
args["dept"] = dept
|
|
|
|
ok, msg = _is_allowed_user_edit(args)
|
|
|
|
if not ok:
|
|
|
|
return json_error(403, f"user_create: {msg}")
|
2023-11-22 17:55:15 +01:00
|
|
|
user = User(user_name=user_name)
|
2023-11-21 22:28:50 +01:00
|
|
|
user.from_dict(args, new_user=True)
|
2022-08-06 22:31:41 +02:00
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return user.to_dict()
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
|
2022-08-13 23:28:20 +02:00
|
|
|
@bp.route("/user/<int:uid>/edit", methods=["POST"])
|
|
|
|
@api_web_bp.route("/user/<int:uid>/edit", methods=["POST"])
|
2022-08-06 22:31:41 +02:00
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-06 22:31:41 +02:00
|
|
|
def user_edit(uid: int):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Modification d'un utilisateur.
|
|
|
|
|
2022-08-06 22:31:41 +02:00
|
|
|
Champs modifiables:
|
2024-07-24 17:34:30 +02:00
|
|
|
```json
|
2022-08-06 22:31:41 +02:00
|
|
|
{
|
|
|
|
"dept": str or null,
|
|
|
|
"nom": str,
|
|
|
|
"prenom": str,
|
|
|
|
"active":bool
|
2023-11-21 22:28:50 +01:00
|
|
|
...
|
2022-08-06 22:31:41 +02:00
|
|
|
}
|
2024-07-24 17:34:30 +02:00
|
|
|
```
|
2022-08-06 22:31:41 +02:00
|
|
|
"""
|
2023-11-21 22:28:50 +01:00
|
|
|
args = request.get_json(force=True) # may raise 400 Bad Request
|
2024-10-29 19:18:36 +01:00
|
|
|
user: User = User.get_or_404(uid)
|
2022-08-06 22:31:41 +02:00
|
|
|
# L'utilisateur doit avoir le droit dans le département de départ et celui d'arrivée
|
|
|
|
orig_dept = user.dept
|
2023-11-21 22:28:50 +01:00
|
|
|
dest_dept = args.get("dept", False)
|
2022-08-06 22:31:41 +02:00
|
|
|
if dest_dept is not False:
|
|
|
|
if dest_dept == "@all":
|
|
|
|
dest_dept = None
|
2023-09-29 21:17:31 +02:00
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.UsersAdmin)
|
2022-08-06 22:31:41 +02:00
|
|
|
if (None not in allowed_depts) and (
|
|
|
|
(orig_dept not in allowed_depts) or (dest_dept not in allowed_depts)
|
|
|
|
):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(403, "user_edit: departement non autorise")
|
2022-08-06 22:31:41 +02:00
|
|
|
if dest_dept != orig_dept:
|
|
|
|
if (dest_dept is not None) and (
|
|
|
|
Departement.query.filter_by(acronym=dest_dept).first() is None
|
|
|
|
):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "user_edit: departement inexistant")
|
2022-08-06 22:31:41 +02:00
|
|
|
user.dept = dest_dept
|
|
|
|
|
2023-11-21 22:28:50 +01:00
|
|
|
ok, msg = _is_allowed_user_edit(args)
|
|
|
|
if not ok:
|
|
|
|
return json_error(403, f"user_edit: {msg}")
|
2022-08-06 22:31:41 +02:00
|
|
|
|
2023-11-21 22:28:50 +01:00
|
|
|
user.from_dict(args)
|
2022-08-06 22:31:41 +02:00
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return user.to_dict()
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
|
2022-08-19 12:46:21 +02:00
|
|
|
@bp.route("/user/<int:uid>/password", methods=["POST"])
|
|
|
|
@api_web_bp.route("/user/<int:uid>/password", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-19 12:46:21 +02:00
|
|
|
def user_password(uid: int):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Modification du mot de passe d'un utilisateur.
|
|
|
|
|
2024-07-27 13:30:02 +02:00
|
|
|
Si le mot de passe ne convient pas, erreur 400.
|
|
|
|
|
|
|
|
DATA
|
|
|
|
----
|
2024-07-24 17:34:30 +02:00
|
|
|
```json
|
2022-08-19 12:46:21 +02:00
|
|
|
{
|
|
|
|
"password": str
|
|
|
|
}
|
2024-07-27 13:30:02 +02:00
|
|
|
```
|
2024-07-24 17:34:30 +02:00
|
|
|
|
2024-07-27 13:30:02 +02:00
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/user/3/password;{""password"" : ""rePlaCemeNT456averylongandcomplicated""}
|
2022-08-19 12:46:21 +02:00
|
|
|
"""
|
|
|
|
data = request.get_json(force=True) # may raise 400 Bad Request
|
2024-10-29 19:18:36 +01:00
|
|
|
user: User = User.get_or_404(uid)
|
2022-08-19 12:46:21 +02:00
|
|
|
password = data.get("password")
|
|
|
|
if not password:
|
|
|
|
return json_error(404, "user_password: missing password")
|
|
|
|
if not is_valid_password(password):
|
2023-02-22 02:13:06 +01:00
|
|
|
return json_error(API_CLIENT_ERROR, "user_password: invalid password")
|
2023-09-29 21:17:31 +02:00
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.UsersAdmin)
|
2022-08-19 12:46:21 +02:00
|
|
|
if (None not in allowed_depts) and ((user.dept not in allowed_depts)):
|
|
|
|
return json_error(403, "user_password: departement non autorise")
|
|
|
|
user.set_password(password)
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return user.to_dict()
|
2022-08-19 12:46:21 +02:00
|
|
|
|
|
|
|
|
2022-08-06 22:31:41 +02:00
|
|
|
@bp.route("/user/<int:uid>/role/<string:role_name>/add", methods=["POST"])
|
|
|
|
@api_web_bp.route("/user/<int:uid>/role/<string:role_name>/add", methods=["POST"])
|
|
|
|
@bp.route(
|
|
|
|
"/user/<int:uid>/role/<string:role_name>/add/departement/<string:dept>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@api_web_bp.route(
|
|
|
|
"/user/<int:uid>/role/<string:role_name>/add/departement/<string:dept>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-06 22:31:41 +02:00
|
|
|
def user_role_add(uid: int, role_name: str, dept: str = None):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Ajoute un rôle à l'utilisateur dans le département donné."""
|
2024-10-29 19:18:36 +01:00
|
|
|
user: User = User.get_or_404(uid)
|
2022-08-06 22:31:41 +02:00
|
|
|
role: Role = Role.query.filter_by(name=role_name).first_or_404()
|
|
|
|
if dept is not None: # check
|
|
|
|
_ = Departement.query.filter_by(acronym=dept).first_or_404()
|
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.ScoSuperAdmin)
|
|
|
|
if (None not in allowed_depts) and (dept not in allowed_depts):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(403, "user_role_add: departement non autorise")
|
2022-08-06 22:31:41 +02:00
|
|
|
user.add_role(role, dept)
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return user.to_dict()
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/user/<int:uid>/role/<string:role_name>/remove", methods=["POST"])
|
|
|
|
@api_web_bp.route("/user/<int:uid>/role/<string:role_name>/remove", methods=["POST"])
|
|
|
|
@bp.route(
|
|
|
|
"/user/<int:uid>/role/<string:role_name>/remove/departement/<string:dept>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@api_web_bp.route(
|
|
|
|
"/user/<int:uid>/role/<string:role_name>/remove/departement/<string:dept>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-06 22:31:41 +02:00
|
|
|
def user_role_remove(uid: int, role_name: str, dept: str = None):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Retire le rôle (dans le département donné) à cet utilisateur."""
|
2024-10-29 19:18:36 +01:00
|
|
|
user: User = User.get_or_404(uid)
|
2022-08-06 22:31:41 +02:00
|
|
|
role: Role = Role.query.filter_by(name=role_name).first_or_404()
|
|
|
|
if dept is not None: # check
|
|
|
|
_ = Departement.query.filter_by(acronym=dept).first_or_404()
|
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.ScoSuperAdmin)
|
|
|
|
if (None not in allowed_depts) and (dept not in allowed_depts):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(403, "user_role_remove: departement non autorise")
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
query = UserRole.query.filter(UserRole.role == role, UserRole.user == user)
|
|
|
|
if dept is not None:
|
|
|
|
query = query.filter(UserRole.dept == dept)
|
|
|
|
user_role = query.first()
|
|
|
|
if user_role:
|
|
|
|
db.session.delete(user_role)
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return user.to_dict()
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
|
2022-08-07 11:08:12 +02:00
|
|
|
@bp.route("/permissions")
|
|
|
|
@api_web_bp.route("/permissions")
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-17 14:58:49 +02:00
|
|
|
def permissions_list():
|
2024-07-27 13:30:02 +02:00
|
|
|
"""Liste des noms de permissions définies.
|
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/permissions
|
|
|
|
"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return list(Permission.permission_by_name.keys())
|
2022-08-07 11:08:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/role/<string:role_name>")
|
|
|
|
@api_web_bp.route("/role/<string:role_name>")
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-17 14:58:49 +02:00
|
|
|
def role_get(role_name: str):
|
2024-07-27 13:30:02 +02:00
|
|
|
"""Un rôle.
|
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/role/Ens
|
|
|
|
"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return Role.query.filter_by(name=role_name).first_or_404().to_dict()
|
2022-08-07 11:08:12 +02:00
|
|
|
|
|
|
|
|
2022-08-06 22:31:41 +02:00
|
|
|
@bp.route("/roles")
|
|
|
|
@api_web_bp.route("/roles")
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.UsersView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-17 14:58:49 +02:00
|
|
|
def roles_list():
|
2024-07-27 13:30:02 +02:00
|
|
|
"""Tous les rôles définis.
|
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/roles
|
|
|
|
"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return [role.to_dict() for role in Role.query]
|
2022-08-06 22:31:41 +02:00
|
|
|
|
|
|
|
|
2022-08-07 11:08:12 +02:00
|
|
|
@bp.route(
|
|
|
|
"/role/<string:role_name>/add_permission/<string:perm_name>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@api_web_bp.route(
|
|
|
|
"/role/<string:role_name>/add_permission/<string:perm_name>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
2022-08-06 22:31:41 +02:00
|
|
|
@login_required
|
|
|
|
@scodoc
|
2022-08-07 11:08:12 +02:00
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-07 11:08:12 +02:00
|
|
|
def role_permission_add(role_name: str, perm_name: str):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Ajoute une permission à un rôle."""
|
2022-08-07 11:08:12 +02:00
|
|
|
role: Role = Role.query.filter_by(name=role_name).first_or_404()
|
|
|
|
permission = Permission.get_by_name(perm_name)
|
|
|
|
if permission is None:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "role_permission_add: permission inconnue")
|
2022-08-07 11:08:12 +02:00
|
|
|
role.add_permission(permission)
|
|
|
|
db.session.add(role)
|
|
|
|
db.session.commit()
|
2023-09-29 21:17:31 +02:00
|
|
|
log(f"role_permission_add({role_name}, {perm_name})")
|
2023-04-06 16:10:32 +02:00
|
|
|
return role.to_dict()
|
2022-08-07 11:08:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route(
|
|
|
|
"/role/<string:role_name>/remove_permission/<string:perm_name>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@api_web_bp.route(
|
|
|
|
"/role/<string:role_name>/remove_permission/<string:perm_name>",
|
|
|
|
methods=["POST"],
|
|
|
|
)
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-07 11:08:12 +02:00
|
|
|
def role_permission_remove(role_name: str, perm_name: str):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Retire une permission d'un rôle."""
|
2022-08-07 11:08:12 +02:00
|
|
|
role: Role = Role.query.filter_by(name=role_name).first_or_404()
|
|
|
|
permission = Permission.get_by_name(perm_name)
|
|
|
|
if permission is None:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "role_permission_remove: permission inconnue")
|
2022-08-07 11:08:12 +02:00
|
|
|
role.remove_permission(permission)
|
|
|
|
db.session.add(role)
|
|
|
|
db.session.commit()
|
2023-09-29 21:17:31 +02:00
|
|
|
log(f"role_permission_remove({role_name}, {perm_name})")
|
2023-04-06 16:10:32 +02:00
|
|
|
return role.to_dict()
|
2022-08-07 11:08:12 +02:00
|
|
|
|
|
|
|
|
2022-08-10 07:29:34 +02:00
|
|
|
@bp.route("/role/create/<string:role_name>", methods=["POST"])
|
|
|
|
@api_web_bp.route("/role/create/<string:role_name>", methods=["POST"])
|
2022-08-07 11:08:12 +02:00
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-07 11:08:12 +02:00
|
|
|
def role_create(role_name: str):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Création d'un nouveau rôle avec les permissions données.
|
|
|
|
|
|
|
|
DATA
|
|
|
|
----
|
|
|
|
```json
|
2022-08-07 11:08:12 +02:00
|
|
|
{
|
|
|
|
"permissions" : [ 'ScoView', ... ]
|
|
|
|
}
|
2024-07-24 17:34:30 +02:00
|
|
|
```
|
2024-07-27 13:30:02 +02:00
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/role/create/customRole;{""permissions"": [""ScoView"", ""UsersView""]}
|
2022-08-07 11:08:12 +02:00
|
|
|
"""
|
|
|
|
role: Role = Role.query.filter_by(name=role_name).first()
|
|
|
|
if role:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "role_create: role already exists")
|
2022-08-07 11:08:12 +02:00
|
|
|
role = Role(name=role_name)
|
|
|
|
data = request.get_json(force=True) # may raise 400 Bad Request
|
|
|
|
permissions = data.get("permissions")
|
|
|
|
if permissions:
|
|
|
|
try:
|
|
|
|
role.set_named_permissions(permissions)
|
|
|
|
except ScoValueError:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "role_create: invalid permissions")
|
2022-08-07 11:08:12 +02:00
|
|
|
db.session.add(role)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return role.to_dict()
|
2022-08-07 11:08:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/role/<string:role_name>/edit", methods=["POST"])
|
|
|
|
@api_web_bp.route("/role/<string:role_name>/edit", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-07 11:08:12 +02:00
|
|
|
def role_edit(role_name: str):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Édition d'un rôle. On peut spécifier un nom et/ou des permissions.
|
|
|
|
|
|
|
|
DATA
|
|
|
|
----
|
|
|
|
```json
|
2022-08-07 11:08:12 +02:00
|
|
|
{
|
|
|
|
"name" : name
|
|
|
|
"permissions" : [ 'ScoView', ... ]
|
|
|
|
}
|
2024-07-24 17:34:30 +02:00
|
|
|
```
|
2022-08-07 11:08:12 +02:00
|
|
|
"""
|
|
|
|
role: Role = Role.query.filter_by(name=role_name).first_or_404()
|
|
|
|
data = request.get_json(force=True) # may raise 400 Bad Request
|
|
|
|
permissions = data.get("permissions", False)
|
|
|
|
if permissions is not False:
|
|
|
|
try:
|
|
|
|
role.set_named_permissions(permissions)
|
|
|
|
except ScoValueError:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "role_create: invalid permissions")
|
2022-08-07 11:08:12 +02:00
|
|
|
role_name = data.get("role_name")
|
|
|
|
if role_name and role_name != role.name:
|
|
|
|
existing_role: Role = Role.query.filter_by(name=role_name).first()
|
|
|
|
if existing_role:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, "role_edit: role name already exists")
|
2022-08-07 11:08:12 +02:00
|
|
|
role.name = role_name
|
|
|
|
db.session.add(role)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return role.to_dict()
|
2022-08-07 11:08:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/role/<string:role_name>/delete", methods=["POST"])
|
|
|
|
@api_web_bp.route("/role/<string:role_name>/delete", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-07 11:08:12 +02:00
|
|
|
def role_delete(role_name: str):
|
2024-07-27 13:30:02 +02:00
|
|
|
"""Suppression d'un rôle.
|
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/role/customRole/delete
|
|
|
|
"""
|
2022-08-07 11:08:12 +02:00
|
|
|
role: Role = Role.query.filter_by(name=role_name).first_or_404()
|
|
|
|
db.session.delete(role)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return {"OK": True}
|
2024-01-17 21:58:45 +01:00
|
|
|
|
|
|
|
|
2024-01-18 00:27:17 +01:00
|
|
|
# @bp.route("/user/<int:uid>/edt")
|
|
|
|
# @api_web_bp.route("/user/<int:uid>/edt")
|
|
|
|
# @login_required
|
|
|
|
# @scodoc
|
|
|
|
# @permission_required(Permission.ScoView)
|
|
|
|
# @as_json
|
|
|
|
# def user_edt(uid: int):
|
|
|
|
# """L'emploi du temps de l'utilisateur.
|
|
|
|
# Si ok, une liste d'évènements. Sinon, une chaine indiquant un message d'erreur.
|
|
|
|
|
|
|
|
# show_modules_titles affiche le titre complet du module (défaut), sinon juste le code.
|
|
|
|
|
|
|
|
# Il faut la permission ScoView + (UsersView ou bien être connecté comme l'utilisateur demandé)
|
|
|
|
# """
|
|
|
|
# if g.scodoc_dept is None: # route API non départementale
|
|
|
|
# if not current_user.has_permission(Permission.UsersView):
|
|
|
|
# return scu.json_error(403, "accès non autorisé")
|
|
|
|
# user: User = db.session.get(User, uid)
|
|
|
|
# if user is None:
|
|
|
|
# return json_error(404, "user not found")
|
|
|
|
# # Check permission
|
|
|
|
# if current_user.id != user.id:
|
|
|
|
# if g.scodoc_dept:
|
|
|
|
# allowed_depts = current_user.get_depts_with_permission(Permission.UsersView)
|
|
|
|
# if (None not in allowed_depts) and (user.dept not in allowed_depts):
|
|
|
|
# return json_error(404, "user not found")
|
|
|
|
|
|
|
|
# show_modules_titles = scu.to_bool(request.args.get("show_modules_titles", False))
|
|
|
|
|
|
|
|
# # Cherche ics
|
|
|
|
# if not user.edt_id:
|
|
|
|
# return json_error(404, "user not configured")
|
|
|
|
# ics_filename = sco_edt_cal.get_ics_user_edt_filename(user.edt_id)
|
|
|
|
# if not ics_filename:
|
|
|
|
# return json_error(404, "no calendar for this user")
|
|
|
|
|
|
|
|
# _, calendar = sco_edt_cal.load_calendar(ics_filename)
|
|
|
|
|
|
|
|
# # TODO:
|
|
|
|
# # - Construire mapping edt2modimpl: edt_id -> modimpl
|
|
|
|
# # pour cela, considérer tous les formsemestres de la période de l'edt
|
|
|
|
# # (soit on considère l'année scolaire du 1er event, ou celle courante,
|
|
|
|
# # soit on cherche min, max des dates des events)
|
|
|
|
# # - Modifier décodage des groupes dans convert_ics pour avoi run mapping
|
|
|
|
# # de groupe par semestre (retrouvé grâce au modimpl associé à l'event)
|
|
|
|
|
|
|
|
# raise NotImplementedError() # TODO XXX WIP
|
|
|
|
|
|
|
|
# events_scodoc, _ = sco_edt_cal.convert_ics(
|
|
|
|
# calendar,
|
|
|
|
# edt2group=edt2group,
|
|
|
|
# default_group=default_group,
|
|
|
|
# edt2modimpl=edt2modimpl,
|
|
|
|
# )
|
|
|
|
# edt_dict = sco_edt_cal.translate_calendar(
|
|
|
|
# events_scodoc, group_ids, show_modules_titles=show_modules_titles
|
|
|
|
# )
|
|
|
|
# return edt_dict
|