Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b637de188 | |||
| c0f49ab8de | |||
| c23e128d2e | |||
| b611173ea9 |
+132
-18
@@ -206,6 +206,7 @@ PERMISSION_ACTION_ENDPOINTS = {
|
||||
'register': 'can_manage_users',
|
||||
'admin_reset_user_password': 'can_manage_users',
|
||||
'admin_update_user_permissions': 'can_manage_users',
|
||||
'admin_anonymize_names': 'can_manage_users',
|
||||
'home_admin': 'can_manage_settings',
|
||||
'upload_admin': 'can_manage_settings',
|
||||
'library_admin': 'can_manage_settings',
|
||||
@@ -297,7 +298,7 @@ def _enforce_user_permissions():
|
||||
return jsonify({'ok': False, 'message': message}), 403
|
||||
|
||||
flash(message, 'error')
|
||||
fallback_endpoint = _permission_denied_fallback_endpoint(permissions)
|
||||
fallback_endpoint = _permission_denied_fallback_endpoint(permissions, current_endpoint=endpoint)
|
||||
return redirect(url_for(fallback_endpoint))
|
||||
|
||||
action_key = PERMISSION_ACTION_ENDPOINTS.get(endpoint)
|
||||
@@ -307,7 +308,7 @@ def _enforce_user_permissions():
|
||||
return jsonify({'ok': False, 'message': message}), 403
|
||||
|
||||
flash(message, 'error')
|
||||
fallback_endpoint = _permission_denied_fallback_endpoint(permissions)
|
||||
fallback_endpoint = _permission_denied_fallback_endpoint(permissions, current_endpoint=endpoint)
|
||||
return redirect(url_for(fallback_endpoint))
|
||||
|
||||
return None
|
||||
@@ -375,8 +376,16 @@ def _action_access_allowed(permissions, action_key):
|
||||
return bool(action_permissions.get(action_key, True))
|
||||
|
||||
|
||||
def _permission_denied_fallback_endpoint(permissions):
|
||||
for candidate in ('home', 'my_borrowed_items', 'tutorial_page', 'notifications_view', 'impressum'):
|
||||
def _permission_denied_fallback_endpoint(permissions, current_endpoint=None):
|
||||
username = session.get('username')
|
||||
is_admin_user = bool(username and us.check_admin(username))
|
||||
admin_home_allowed = _page_access_allowed(permissions, 'home_admin') and _action_access_allowed(permissions, 'can_manage_settings')
|
||||
|
||||
for candidate in ('my_borrowed_items', 'tutorial_page', 'notifications_view', 'impressum', 'home'):
|
||||
if current_endpoint and candidate == current_endpoint:
|
||||
continue
|
||||
if candidate == 'home' and is_admin_user and not admin_home_allowed:
|
||||
continue
|
||||
if _page_access_allowed(permissions, candidate):
|
||||
return candidate
|
||||
return 'logout'
|
||||
@@ -1588,14 +1597,25 @@ def _student_card_id_slug(value):
|
||||
return re.sub(r'[^a-z0-9]+', '', normalized).upper()
|
||||
|
||||
|
||||
def _name_to_alias(full_name):
|
||||
"""Convert clear names to deterministic aliases, e.g. Simon Frings -> SimFri."""
|
||||
text = sanitize_form_value(full_name)
|
||||
if not text:
|
||||
return 'User'
|
||||
|
||||
parts = [p for p in re.split(r'\s+', text) if p]
|
||||
if len(parts) >= 2:
|
||||
return us.build_name_synonym(parts[0], parts[-1])
|
||||
return us.build_name_synonym(parts[0], '')
|
||||
|
||||
|
||||
def _build_student_card_excel_id(student_name, class_name, row_number, used_ids):
|
||||
"""Create a stable student-card ID when the spreadsheet does not provide one."""
|
||||
name_slug = _student_card_id_slug(student_name)
|
||||
"""Create a stable student-card ID without embedding personal names."""
|
||||
class_slug = _student_card_id_slug(class_name)
|
||||
|
||||
base_parts = [part for part in (class_slug, name_slug) if part]
|
||||
base_parts = [part for part in (class_slug,) if part]
|
||||
if base_parts:
|
||||
base_id = f"SC-{'-'.join(base_parts[:2])}"
|
||||
base_id = f"SC-{'-'.join(base_parts[:1])}-ROW-{row_number}"
|
||||
else:
|
||||
base_id = f"SC-ROW-{row_number}"
|
||||
|
||||
@@ -1715,6 +1735,8 @@ def _upload_student_cards_excel():
|
||||
student_name = f'{first_name} {last_name}'.strip()
|
||||
validation_warnings.append((row_number, 'Schülername wurde aus Vorname und Nachname zusammengesetzt'))
|
||||
|
||||
student_name_alias = _name_to_alias(student_name)
|
||||
|
||||
if not ausweis_id and not student_name and not class_name:
|
||||
continue
|
||||
|
||||
@@ -1739,7 +1761,7 @@ def _upload_student_cards_excel():
|
||||
planned_rows.append({
|
||||
'row_number': row_number,
|
||||
'ausweis_id': ausweis_id,
|
||||
'student_name': student_name,
|
||||
'student_name': student_name_alias,
|
||||
'class_name': class_name,
|
||||
'notes': notes,
|
||||
'default_borrow_days': default_borrow_days,
|
||||
@@ -2393,7 +2415,14 @@ def home():
|
||||
student_max_borrow_days=cfg.STUDENT_MAX_BORROW_DAYS
|
||||
)
|
||||
else:
|
||||
return redirect(url_for('home_admin'))
|
||||
permissions = _get_current_user_permissions() or us.build_default_permission_payload('standard_user')
|
||||
if _page_access_allowed(permissions, 'home_admin') and _action_access_allowed(permissions, 'can_manage_settings'):
|
||||
return redirect(url_for('home_admin'))
|
||||
|
||||
fallback_endpoint = _permission_denied_fallback_endpoint(permissions, current_endpoint='home')
|
||||
if fallback_endpoint == 'logout':
|
||||
flash('Für diesen Benutzer sind aktuell keine Seiten freigegeben.', 'error')
|
||||
return redirect(url_for(fallback_endpoint))
|
||||
|
||||
|
||||
@app.route('/home_admin')
|
||||
@@ -3172,6 +3201,7 @@ def student_cards_admin():
|
||||
action = request.form.get('action', 'add')
|
||||
ausweis_id = request.form.get('ausweis_id', '').strip().upper()
|
||||
student_name = request.form.get('student_name', '').strip()
|
||||
student_name_alias = _name_to_alias(student_name)
|
||||
default_borrow_days = request.form.get('default_borrow_days', 14)
|
||||
class_name = request.form.get('class_name', '').strip()
|
||||
notes = request.form.get('notes', '').strip()
|
||||
@@ -3198,7 +3228,7 @@ def student_cards_admin():
|
||||
else:
|
||||
encrypted_payload = encrypt_document_fields(
|
||||
{
|
||||
'SchülerName': student_name,
|
||||
'SchülerName': student_name_alias,
|
||||
'Klasse': class_name,
|
||||
'Notizen': notes,
|
||||
},
|
||||
@@ -3231,7 +3261,7 @@ def student_cards_admin():
|
||||
try:
|
||||
encrypted_payload = encrypt_document_fields(
|
||||
{
|
||||
'SchülerName': student_name,
|
||||
'SchülerName': student_name_alias,
|
||||
'Klasse': class_name,
|
||||
'Notizen': notes,
|
||||
},
|
||||
@@ -3701,7 +3731,11 @@ def login():
|
||||
session['admin'] = is_admin_user
|
||||
session['is_admin'] = is_admin_user
|
||||
if is_admin_user:
|
||||
return redirect(url_for('home_admin'))
|
||||
permissions = us.get_effective_permissions(username)
|
||||
if _page_access_allowed(permissions, 'home_admin') and _action_access_allowed(permissions, 'can_manage_settings'):
|
||||
return redirect(url_for('home_admin'))
|
||||
fallback_endpoint = _permission_denied_fallback_endpoint(permissions, current_endpoint='login')
|
||||
return redirect(url_for(fallback_endpoint))
|
||||
else:
|
||||
return redirect(url_for('home'))
|
||||
else:
|
||||
@@ -6654,8 +6688,8 @@ def register():
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
password = request.form['password']
|
||||
name = request.form['name']
|
||||
last_name = request.form['last-name']
|
||||
permission_preset = (request.form.get('permission_preset') or 'standard_user').strip()
|
||||
use_custom_permissions = request.form.get('use_custom_permissions') == 'on'
|
||||
is_student = bool(request.form.get('is_student')) if cfg.STUDENT_CARDS_MODULE_ENABLED else False
|
||||
student_card_id = us.normalize_student_card_id(request.form.get('student_card_id')) if cfg.STUDENT_CARDS_MODULE_ENABLED else ''
|
||||
max_borrow_days_raw = request.form.get('max_borrow_days') if cfg.STUDENT_CARDS_MODULE_ENABLED else None
|
||||
@@ -6669,6 +6703,17 @@ def register():
|
||||
flash('Passwort ist zu schwach', 'error')
|
||||
return redirect(url_for('register'))
|
||||
|
||||
action_permissions = None
|
||||
page_permissions = None
|
||||
if use_custom_permissions:
|
||||
action_permissions = {}
|
||||
for action_key, _ in PERMISSION_ACTION_OPTIONS:
|
||||
action_permissions[action_key] = request.form.get(f'action_{action_key}') == 'on'
|
||||
|
||||
page_permissions = {}
|
||||
for endpoint_name, _ in PERMISSION_PAGE_OPTIONS:
|
||||
page_permissions[endpoint_name] = request.form.get(f'page_{endpoint_name}') == 'on'
|
||||
|
||||
max_borrow_days = None
|
||||
if is_student:
|
||||
if not student_card_id:
|
||||
@@ -6686,11 +6731,14 @@ def register():
|
||||
us.add_user(
|
||||
username,
|
||||
password,
|
||||
name,
|
||||
last_name,
|
||||
username,
|
||||
'',
|
||||
is_student=is_student,
|
||||
student_card_id=student_card_id if is_student else None,
|
||||
max_borrow_days=max_borrow_days
|
||||
max_borrow_days=max_borrow_days,
|
||||
permission_preset=permission_preset,
|
||||
action_permissions=action_permissions,
|
||||
page_permissions=page_permissions,
|
||||
)
|
||||
return redirect(url_for('home'))
|
||||
return render_template(
|
||||
@@ -7811,6 +7859,72 @@ def admin_update_user_permissions():
|
||||
return redirect(url_for('user_del'))
|
||||
|
||||
|
||||
@app.route('/admin_anonymize_names', methods=['POST'])
|
||||
def admin_anonymize_names():
|
||||
"""Anonymize already stored personal names into short aliases."""
|
||||
if 'username' not in session or not us.check_admin(session['username']):
|
||||
flash('Nicht autorisierter Zugriff', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
client = None
|
||||
try:
|
||||
client = MongoClient(MONGODB_HOST, MONGODB_PORT)
|
||||
db = client[MONGODB_DB]
|
||||
users_col = db['users']
|
||||
student_cards_col = db['student_cards']
|
||||
|
||||
users_updated = 0
|
||||
cards_updated = 0
|
||||
|
||||
for user_doc in users_col.find({}, {'name': 1, 'last_name': 1, 'Username': 1, 'username': 1}):
|
||||
first = str(user_doc.get('name') or '').strip()
|
||||
last = str(user_doc.get('last_name') or '').strip()
|
||||
fallback = str(user_doc.get('Username') or user_doc.get('username') or '').strip()
|
||||
|
||||
alias = us.build_name_synonym(first or fallback, last)
|
||||
result = users_col.update_one(
|
||||
{'_id': user_doc['_id']},
|
||||
{'$set': {'name': alias, 'last_name': ''}}
|
||||
)
|
||||
if result.modified_count > 0:
|
||||
users_updated += 1
|
||||
|
||||
for card_doc in student_cards_col.find({}, {'SchülerName': 1, 'Klasse': 1, 'Notizen': 1}):
|
||||
decrypted = _decrypt_student_card_doc(card_doc)
|
||||
alias = _name_to_alias(decrypted.get('SchülerName', ''))
|
||||
class_name = sanitize_form_value(decrypted.get('Klasse', ''))
|
||||
notes = sanitize_form_value(decrypted.get('Notizen', ''))
|
||||
|
||||
encrypted_payload = encrypt_document_fields(
|
||||
{
|
||||
'SchülerName': alias,
|
||||
'Klasse': class_name,
|
||||
'Notizen': notes,
|
||||
},
|
||||
STUDENT_CARD_ENCRYPTED_FIELDS,
|
||||
)
|
||||
|
||||
result = student_cards_col.update_one(
|
||||
{'_id': card_doc['_id']},
|
||||
{'$set': {'Aktualisiert': datetime.datetime.now(), **encrypted_payload}}
|
||||
)
|
||||
if result.modified_count > 0:
|
||||
cards_updated += 1
|
||||
|
||||
flash(
|
||||
f'Anonymisierung abgeschlossen: {users_updated} Benutzer und {cards_updated} Ausweise aktualisiert.',
|
||||
'success'
|
||||
)
|
||||
except Exception as exc:
|
||||
app.logger.error(f'Error anonymizing names: {exc}')
|
||||
flash('Fehler bei der Anonymisierung der Namen.', 'error')
|
||||
finally:
|
||||
if client:
|
||||
client.close()
|
||||
|
||||
return redirect(url_for('user_del'))
|
||||
|
||||
|
||||
@app.route('/logs')
|
||||
def logs():
|
||||
"""
|
||||
|
||||
+126
-14
@@ -39,16 +39,7 @@
|
||||
<span class="input-icon">👤</span>
|
||||
<input type="text" id="username" name="username" placeholder="Geben Sie einen Benutzernamen ein" required>
|
||||
</div>
|
||||
<label for="name">Name</label>
|
||||
<div class="input-container">
|
||||
<span class="input-icon">👤</span>
|
||||
<input type="text" id="name" name="name" placeholder="Geben Sie den Namen ein" required>
|
||||
</div>
|
||||
<label for="last-name">Nachname</label>
|
||||
<div class="input-container">
|
||||
<span class="input-icon">👤</span>
|
||||
<input type="text" id="last-name" name="last-name" placeholder="Geben Sie den Nachnamen ein" required>
|
||||
</div>
|
||||
<p class="anonymize-hint">Klarnamen werden nicht gespeichert. Ein anonymes Kürzel wird automatisch aus dem Benutzernamen erzeugt.</p>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
@@ -81,6 +72,43 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-group">
|
||||
<label for="permission-preset">Berechtigungs-Preset</label>
|
||||
<select id="permission-preset" name="permission_preset" class="form-select">
|
||||
{% for preset_key, preset in permission_presets.items() %}
|
||||
<option value="{{ preset_key }}">{{ preset.label }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
|
||||
<label style="display:flex; align-items:center; gap:8px; margin-top:12px; color:#1f2937;">
|
||||
<input type="checkbox" id="use-custom-permissions" name="use_custom_permissions" style="width:auto;">
|
||||
Individuelle Berechtigungen statt Preset setzen
|
||||
</label>
|
||||
|
||||
<div id="custom-permissions" style="display:none; margin-top:12px;">
|
||||
<div class="permission-panels">
|
||||
<div class="permission-panel">
|
||||
<h4>Aktionsrechte</h4>
|
||||
{% for action_key, action_label in permission_action_options %}
|
||||
<label class="permission-check">
|
||||
<input type="checkbox" class="permission-action-checkbox" name="action_{{ action_key }}">
|
||||
<span>{{ action_label }}</span>
|
||||
</label>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="permission-panel">
|
||||
<h4>Seitenrechte</h4>
|
||||
{% for endpoint_name, endpoint_label in permission_page_options %}
|
||||
<label class="permission-check">
|
||||
<input type="checkbox" class="permission-page-checkbox" name="page_{{ endpoint_name }}">
|
||||
<span>{{ endpoint_label }}</span>
|
||||
</label>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group form-actions">
|
||||
<button type="submit" class="action-button register-button">Benutzer registrieren</button>
|
||||
</div>
|
||||
@@ -174,7 +202,8 @@ body {
|
||||
}
|
||||
|
||||
input[type="text"],
|
||||
input[type="password"] {
|
||||
input[type="password"],
|
||||
.form-select {
|
||||
width: 100%;
|
||||
padding: 0.8rem 1rem 0.8rem 3rem;
|
||||
border: 1px solid #ddd;
|
||||
@@ -185,12 +214,17 @@ input[type="password"] {
|
||||
}
|
||||
|
||||
input[type="text"]:focus,
|
||||
input[type="password"]:focus {
|
||||
input[type="password"]:focus,
|
||||
.form-select:focus {
|
||||
border-color: var(--primary-color);
|
||||
box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.form-select {
|
||||
padding-left: 1rem;
|
||||
}
|
||||
|
||||
input::placeholder {
|
||||
color: #aaa;
|
||||
}
|
||||
@@ -311,11 +345,89 @@ input::placeholder {
|
||||
.richtlinen{
|
||||
color: #ec0920;
|
||||
}
|
||||
|
||||
.anonymize-hint {
|
||||
margin-top: 10px;
|
||||
margin-bottom: 0;
|
||||
background: #f0f9ff;
|
||||
border: 1px solid #bae6fd;
|
||||
border-radius: 8px;
|
||||
padding: 10px 12px;
|
||||
color: #0c4a6e;
|
||||
font-size: 0.92rem;
|
||||
}
|
||||
|
||||
.permission-panels {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.permission-panel {
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 8px;
|
||||
padding: 10px;
|
||||
background: #f8fafc;
|
||||
}
|
||||
|
||||
.permission-panel h4 {
|
||||
margin: 0 0 8px;
|
||||
font-size: 1rem;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.permission-check {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin: 4px 0;
|
||||
color: #1f2937;
|
||||
}
|
||||
</style>
|
||||
|
||||
{% if student_cards_module_enabled %}
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const permissionPresets = {{ permission_presets | tojson }};
|
||||
const presetSelect = document.getElementById('permission-preset');
|
||||
const useCustomPermissions = document.getElementById('use-custom-permissions');
|
||||
const customPermissions = document.getElementById('custom-permissions');
|
||||
|
||||
function applyPresetToPermissionForm(presetKey) {
|
||||
const preset = permissionPresets[presetKey] || {};
|
||||
const actionDefaults = preset.actions || {};
|
||||
const pageDefaults = preset.pages || {};
|
||||
|
||||
document.querySelectorAll('.permission-action-checkbox').forEach(function (checkbox) {
|
||||
const key = checkbox.name.replace('action_', '');
|
||||
checkbox.checked = !!actionDefaults[key];
|
||||
});
|
||||
|
||||
document.querySelectorAll('.permission-page-checkbox').forEach(function (checkbox) {
|
||||
const key = checkbox.name.replace('page_', '');
|
||||
checkbox.checked = !!pageDefaults[key];
|
||||
});
|
||||
}
|
||||
|
||||
function toggleCustomPermissions() {
|
||||
if (!useCustomPermissions || !customPermissions) {
|
||||
return;
|
||||
}
|
||||
customPermissions.style.display = useCustomPermissions.checked ? 'block' : 'none';
|
||||
}
|
||||
|
||||
if (presetSelect) {
|
||||
presetSelect.addEventListener('change', function () {
|
||||
applyPresetToPermissionForm(this.value);
|
||||
});
|
||||
applyPresetToPermissionForm(presetSelect.value);
|
||||
}
|
||||
|
||||
if (useCustomPermissions) {
|
||||
useCustomPermissions.addEventListener('change', toggleCustomPermissions);
|
||||
toggleCustomPermissions();
|
||||
}
|
||||
|
||||
{% if student_cards_module_enabled %}
|
||||
const studentCheckbox = document.getElementById('is-student');
|
||||
const studentFields = document.getElementById('student-fields');
|
||||
const studentCardInput = document.getElementById('student-card-id');
|
||||
@@ -332,7 +444,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
|
||||
studentCheckbox.addEventListener('change', toggleStudentFields);
|
||||
toggleStudentFields();
|
||||
{% endif %}
|
||||
});
|
||||
</script>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
@@ -17,6 +17,16 @@
|
||||
<div class="user-management-container">
|
||||
<h2>Benutzer</h2>
|
||||
|
||||
<form method="POST" action="{{ url_for('admin_anonymize_names') }}" class="mb-3">
|
||||
<button
|
||||
type="submit"
|
||||
class="btn btn-outline-danger"
|
||||
onclick="return confirm('Sollen alle gespeicherten Klarnamen dauerhaft in Synonym-Kuerzel umgewandelt werden?')"
|
||||
>
|
||||
Gespeicherte Namen anonymisieren
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="filter-bar mb-3">
|
||||
<div class="row g-2 align-items-end">
|
||||
<div class="col-md-3">
|
||||
|
||||
+63
-5
@@ -12,6 +12,7 @@ Provides methods for creating, validating, and retrieving user information.
|
||||
'''
|
||||
import hashlib
|
||||
import copy
|
||||
import re
|
||||
from bson.objectid import ObjectId
|
||||
import settings as cfg
|
||||
from settings import MongoClient
|
||||
@@ -24,6 +25,38 @@ def normalize_student_card_id(card_id):
|
||||
return str(card_id).strip().upper()
|
||||
|
||||
|
||||
def _clean_name_fragment(value):
|
||||
cleaned = re.sub(r'[^A-Za-zÄÖÜäöüß]', '', str(value or '').strip())
|
||||
if not cleaned:
|
||||
return ''
|
||||
replacements = {
|
||||
'ä': 'ae',
|
||||
'ö': 'oe',
|
||||
'ü': 'ue',
|
||||
'ß': 'ss',
|
||||
'Ä': 'Ae',
|
||||
'Ö': 'Oe',
|
||||
'Ü': 'Ue',
|
||||
}
|
||||
for old_char, new_char in replacements.items():
|
||||
cleaned = cleaned.replace(old_char, new_char)
|
||||
return cleaned
|
||||
|
||||
|
||||
def build_name_synonym(first_name, last_name=''):
|
||||
"""Build a deterministic, non-personalized short alias like 'SimFri'."""
|
||||
first = _clean_name_fragment(first_name)
|
||||
last = _clean_name_fragment(last_name)
|
||||
|
||||
if first and last:
|
||||
return (first[:3] + last[:3]).title()
|
||||
|
||||
combined = (first + last)
|
||||
if not combined:
|
||||
return 'User'
|
||||
return combined[:6].title()
|
||||
|
||||
|
||||
ACTION_PERMISSION_KEYS = (
|
||||
'can_borrow',
|
||||
'can_insert',
|
||||
@@ -201,6 +234,10 @@ def get_effective_permissions(username):
|
||||
if not user:
|
||||
return build_default_permission_payload('standard_user')
|
||||
|
||||
# Admin users always have full access, independent of custom presets.
|
||||
if bool(user.get('Admin', False)):
|
||||
return build_default_permission_payload('full_access')
|
||||
|
||||
preset_key = user.get('PermissionPreset') or 'standard_user'
|
||||
payload = build_default_permission_payload(preset_key)
|
||||
payload['actions'] = _normalize_bool_map(user.get('ActionPermissions', {}), payload['actions'])
|
||||
@@ -343,7 +380,18 @@ def check_nm_pwd(username, password):
|
||||
return user
|
||||
|
||||
|
||||
def add_user(username, password, name, last_name, is_student=False, student_card_id=None, max_borrow_days=None):
|
||||
def add_user(
|
||||
username,
|
||||
password,
|
||||
name='',
|
||||
last_name='',
|
||||
is_student=False,
|
||||
student_card_id=None,
|
||||
max_borrow_days=None,
|
||||
permission_preset='standard_user',
|
||||
action_permissions=None,
|
||||
page_permissions=None,
|
||||
):
|
||||
"""
|
||||
Add a new user to the database.
|
||||
|
||||
@@ -359,15 +407,24 @@ def add_user(username, password, name, last_name, is_student=False, student_card
|
||||
users = db['users']
|
||||
if not check_password_strength(password):
|
||||
return False
|
||||
permission_defaults = build_default_permission_payload('standard_user')
|
||||
permission_defaults = build_default_permission_payload(permission_preset)
|
||||
if isinstance(action_permissions, dict):
|
||||
for key, value in action_permissions.items():
|
||||
permission_defaults['actions'][str(key)] = bool(value)
|
||||
if isinstance(page_permissions, dict):
|
||||
for key, value in page_permissions.items():
|
||||
permission_defaults['pages'][str(key)] = bool(value)
|
||||
|
||||
alias_source = name if str(name or '').strip() else username
|
||||
name_alias = build_name_synonym(alias_source, '')
|
||||
|
||||
user_doc = {
|
||||
'Username': username,
|
||||
'Password': hashing(password),
|
||||
'Admin': False,
|
||||
'active_ausleihung': None,
|
||||
'name': name,
|
||||
'last_name': last_name,
|
||||
'name': name_alias,
|
||||
'last_name': '',
|
||||
'IsStudent': bool(is_student),
|
||||
'PermissionPreset': permission_defaults['preset'],
|
||||
'ActionPermissions': permission_defaults['actions'],
|
||||
@@ -714,13 +771,14 @@ def update_user_name(username, name, last_name):
|
||||
bool: True if updated successfully, False otherwise
|
||||
"""
|
||||
try:
|
||||
name_alias = build_name_synonym(name, last_name)
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = client[cfg.MONGODB_DB]
|
||||
users = db['users']
|
||||
|
||||
result = users.update_one(
|
||||
{'Username': username},
|
||||
{'$set': {'name': name, 'last_name': last_name}}
|
||||
{'$set': {'name': name_alias, 'last_name': ''}}
|
||||
)
|
||||
|
||||
client.close()
|
||||
|
||||
Reference in New Issue
Block a user