Vue.component("UserEdit", { template: `

Berechtigungen


{{ groupName }}

Mitarbeiter-spezifische Felder

Projekt- & Netzwerkzugriff

Passwort & API Key

`, components: { 'collapsible-selection-list': { props: ['title', 'items', 'lookup', 'collapsible'], data: () => ({ collapsed: true }), computed: { selectedItems() { if (!this.items || !this.lookup) return []; const lookupMap = new Map(this.lookup.map(i => [i.value, i.text])); return this.items.map(id => ({ id, text: lookupMap.get(id) || `ID: ${id}` })); } }, template: `
{{ title }} ({{ items.length }})
` } }, data() { return { user: JSON.parse(JSON.stringify(window.TT_CONFIG.USER_DATA)), // Deep copy initialPermissions: {}, lookups: window.TT_CONFIG.LOOKUPS, permissionsConfig: window.TT_CONFIG.PERMISSIONS_CONFIG, password: { new: '', repeat: '' }, passwordFieldType: 'password', selectedTemplate: null, userToLoad: null, isSaving: false, isToggling: false, collapsedSections: { permissions: false, employeeSpecific: true, projects: true, security: true, } } }, computed: { isNewUser() { return !this.user.id; }, templateOptions() { const options = this.lookups.permissionTemplates.map(t => ({ value: t.id, text: t.name })); options.unshift({ value: null, text: 'Vorlage auswählen...' }); return options; }, permissionChangesTooltip() { if (this.isNewUser) return "Ein neuer Benutzer wird erstellt."; const added = []; const removed = []; for (const key in this.user.permissions) { const initial = !!this.initialPermissions[key]; const current = !!this.user.permissions[key]; if (initial !== current) { const permissionLabel = this.findPermissionLabel(key); if (!permissionLabel || /^\d+$/.test(permissionLabel)) continue; if (current) { added.push(`- ${permissionLabel}`); } else { removed.push(`- ${permissionLabel}`); } } } let tooltipText = ''; if (added.length > 0) { tooltipText += 'Hinzugefügt:\n' + added.join('\n'); } if (removed.length > 0) { if (tooltipText.length > 0) { tooltipText += '\n\n'; // Two line breaks } tooltipText += 'Entfernt:\n' + removed.join('\n'); } return tooltipText || 'Keine Berechtigungsänderungen'; } }, methods: { toggleSection(section) { this.collapsedSections[section] = !this.collapsedSections[section]; }, getChevronClass(section) { return this.collapsedSections[section] ? 'fas fa-chevron-down' : 'fas fa-chevron-up'; }, findPermissionLabel(key) { for (const group in this.permissionsConfig) { if (this.permissionsConfig[group][key]) { return this.permissionsConfig[group][key]; } } return key; }, applyTemplate(templateId) { if (!templateId) return; const template = this.lookups.permissionTemplates.find(t => t.id == templateId); template.permissions = typeof template.permissions === 'string' ? JSON.parse(template.permissions) : template.permissions; if (template) { const newPermissions = { ...this.user.permissions }; for (const key in template.permissions) { newPermissions[key] = template.permissions[key]; } this.user.permissions = newPermissions; window.notify('success', `Vorlage "${template.name}" angewendet.`); } setTimeout(() => { this.selectedTemplate = null; this.$refs.templateSelect.selected = null this.$refs.templateSelect.searchQuery = ''; }, 250); }, async loadDataFromUser(userId) { if(!userId) return; try { const response = await axios.get(`/User/getUserDataForTemplate?id=${userId}`); const dataToApply = response.data; // Apply Permissions const newPermissions = { ...this.user.permissions }; for (const key in newPermissions) { newPermissions[key] = dataToApply.permissions[key] === 'true'; } this.user.permissions = newPermissions; // Apply other fields this.$set(this.user, 'preorder_networks', dataToApply.preorder_networks || []); this.$set(this.user, 'constructionconsent_projects', dataToApply.constructionconsent_projects || []); this.$set(this.user, 'vodia_identity_domain', dataToApply.vodia_identity_domain || ''); this.$set(this.user, 'vodia_identity_default', dataToApply.vodia_identity_default || ''); const selectedUser = this.lookups.users.find(u => u.value === userId); window.notify('success', `Daten von "${selectedUser.text}" geladen.`); } catch (error) { window.notify('error', 'Daten konnten nicht geladen werden.'); } finally { this.userToLoad = null; // Reset autocomplete } }, saveUser() { this.isSaving = true; if (this.isNewUser && !this.user.username) { window.notify('error', 'Benutzername ist ein Pflichtfeld.'); this.isSaving = false; return; } if (this.password.new && this.password.new !== this.password.repeat) { window.notify('error', 'Die Passwörter stimmen nicht überein!'); this.isSaving = false; return; } const formData = new FormData(); // Append standard fields const fields = ['id', 'username', 'name', 'email', 'mobile', 'address_id', 'employee_number', 'project_api_key', 'vodia_identity_domain', 'vodia_identity_username', 'vodia_identity_default']; fields.forEach(field => formData.append(field, this.user[field] || '')); if (this.isNewUser) { formData.delete('id'); } // Append booleans as 'true'/'false' strings formData.append('active', this.user.active ? 'true' : 'false'); formData.append('twofactorrequired', this.user.twofactorrequired ? 'true' : 'false'); if (this.password.new) { formData.append('password', this.password.new); formData.append('password2', this.password.repeat); } // Append ONLY TRUE permissions to mimic checkbox form behavior for (const key in this.user.permissions) { if (this.user.permissions[key]) { if (key.startsWith('can')) { formData.append(`can[${key.replace('can', '')}]`, 'true'); } else { formData.append(key, 'true'); } } } (this.user.preorder_networks || []).forEach(val => formData.append('preorder_networks[]', val)); (this.user.constructionconsent_projects || []).forEach(val => formData.append('constructionconsent_projects[]', val)); axios.post(window.TT_CONFIG.SAVE_URL, formData) .then(() => { window.notify('success', 'Benutzer erfolgreich gespeichert.'); setTimeout(() => window.location.href = '/User', 150); }) .catch(error => { window.notify('error', 'Fehler beim Speichern des Benutzers.'); console.error(error); }) .finally(() => { this.isSaving = false; }); }, async generateApiKey() { try { await axios.post(window.TT_CONFIG.API_KEY_URL, new URLSearchParams({id: this.user.id})); window.notify('success', 'Neuer API Key wurde generiert. Seite wird neu geladen...'); setTimeout(() => window.location.reload(), 1500); } catch (error) { window.notify('error', 'API Key konnte nicht generiert werden.'); } }, generatePassword() { const length = 14; const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; let retVal = ""; for (let i = 0, n = charset.length; i < length; ++i) { retVal += charset.charAt(Math.floor(Math.random() * n)); } this.password.new = retVal; this.password.repeat = retVal; window.notify('info', 'Neues Passwort generiert.'); }, togglePasswordVisibility() { this.passwordFieldType = this.passwordFieldType === 'password' ? 'text' : 'password'; }, removeItem(arrayName, valueToRemove) { const index = this.user[arrayName].indexOf(valueToRemove); if (index > -1) { this.user[arrayName].splice(index, 1); } } }, created() { const permissions = {}; Object.values(this.permissionsConfig).forEach(group => { Object.keys(group).forEach(key => { permissions[key] = this.user.permissions[key] === 'true' || this.user.permissions[key] === true; }); }); this.user.permissions = permissions; this.initialPermissions = JSON.parse(JSON.stringify(permissions)); // Deep copy for change tracking this.user.active = this.user.active == 1 || this.isNewUser; this.user.twofactorrequired = this.user.twofactorrequired == 1 || this.isNewUser; // Set default collapse state for new users if (this.isNewUser) { this.collapsedSections = { permissions: false, employeeSpecific: false, projects: false, security: false, }; } } });