Fix: Identities Group Management - bessere Fehlermeldungen
- add_user_to_group: Exception werfen mit stderr Nachricht - remove_user_from_group: Exception werfen mit stderr Nachricht - text=True für subprocess für besseres Error Handling - Router aktualisiert um Fehlermeldungen an Frontend weiterzugeben - Benutzer sehen jetzt detaillierte Fehlermeldungen beim Gruppe-Entfernen Behebt: 'Failed to remove user from group' verschluckt die echte Fehlermeldung Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -229,12 +229,10 @@ async def add_user_to_group(
|
||||
):
|
||||
"""Add user to group"""
|
||||
try:
|
||||
success = identities_manager.add_user_to_group(username, request.groupname)
|
||||
if not success:
|
||||
raise HTTPException(status_code=400, detail="Failed to add user to group")
|
||||
identities_manager.add_user_to_group(username, request.groupname)
|
||||
return {"status": "added", "username": username, "groupname": request.groupname}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/users/{username}/groups/{groupname}")
|
||||
@@ -245,12 +243,10 @@ async def remove_user_from_group(
|
||||
):
|
||||
"""Remove user from group"""
|
||||
try:
|
||||
success = identities_manager.remove_user_from_group(username, groupname)
|
||||
if not success:
|
||||
raise HTTPException(status_code=400, detail="Failed to remove user from group")
|
||||
identities_manager.remove_user_from_group(username, groupname)
|
||||
return {"status": "removed", "username": username, "groupname": groupname}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
# ============== SAMBA USERS ==============
|
||||
|
||||
@@ -78,6 +78,29 @@ async def create_samba_share(
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.put("/samba/{name}")
|
||||
async def update_samba_share(
|
||||
name: str,
|
||||
request: CreateSambaShareRequest,
|
||||
current_user: str = Depends(get_current_user)
|
||||
):
|
||||
"""Update Samba share"""
|
||||
if not request.name.strip() or not request.path.strip():
|
||||
raise HTTPException(status_code=400, detail="Name and path are required")
|
||||
try:
|
||||
success = share_manager.update_samba_share(
|
||||
name,
|
||||
request.name,
|
||||
request.path,
|
||||
request.comment
|
||||
)
|
||||
if not success:
|
||||
raise HTTPException(status_code=404, detail=f"Samba share '{name}' not found")
|
||||
return {"status": "updated", "name": request.name}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/samba/{name}")
|
||||
async def delete_samba_share(
|
||||
name: str,
|
||||
|
||||
@@ -234,6 +234,7 @@ class IdentitiesManager:
|
||||
result = subprocess.run(
|
||||
["/usr/sbin/usermod", "-aG", groupname, username],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
@@ -241,11 +242,12 @@ class IdentitiesManager:
|
||||
logger.info(f"User {username} added to group {groupname}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"Failed to add user to group: {result.stderr.decode()}")
|
||||
return False
|
||||
error_msg = result.stderr.strip() or result.stdout.strip() or "Unknown error"
|
||||
logger.error(f"Failed to add user to group: {error_msg}")
|
||||
raise Exception(f"Failed to add user to group: {error_msg}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error adding user to group: {e}")
|
||||
return False
|
||||
raise
|
||||
|
||||
def remove_user_from_group(self, username: str, groupname: str) -> bool:
|
||||
"""Remove user from group"""
|
||||
@@ -253,6 +255,7 @@ class IdentitiesManager:
|
||||
result = subprocess.run(
|
||||
["/usr/sbin/gpasswd", "-d", username, groupname],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
@@ -260,11 +263,12 @@ class IdentitiesManager:
|
||||
logger.info(f"User {username} removed from group {groupname}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"Failed to remove user from group: {result.stderr.decode()}")
|
||||
return False
|
||||
error_msg = result.stderr.strip() or result.stdout.strip() or "Unknown error"
|
||||
logger.error(f"Failed to remove user from group: {error_msg}")
|
||||
raise Exception(f"Failed to remove user from group: {error_msg}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error removing user from group: {e}")
|
||||
return False
|
||||
raise
|
||||
|
||||
def change_password(self, username: str, password: str) -> bool:
|
||||
"""Change user password via chpasswd"""
|
||||
|
||||
@@ -73,13 +73,47 @@ class SharesManager:
|
||||
with open(SAMBA_CONFIG, 'a') as f:
|
||||
f.write(section)
|
||||
|
||||
subprocess.run(['smbcontrol', 'smbd', 'reload-config'], capture_output=True, timeout=10)
|
||||
subprocess.run(['/usr/bin/smbcontrol', 'smbd', 'reload-config'], capture_output=True, timeout=10)
|
||||
logger.info(f"Samba share created: {name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating Samba share: {e}")
|
||||
return False
|
||||
|
||||
def update_samba_share(self, old_name: str, new_name: str, path: str, comment: Optional[str] = None) -> bool:
|
||||
"""Update Samba share in /etc/samba/smb.conf"""
|
||||
if not SAMBA_CONFIG.exists():
|
||||
return False
|
||||
|
||||
try:
|
||||
with open(SAMBA_CONFIG, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Find and replace the share section
|
||||
pattern = rf"\n\[{re.escape(old_name)}\].*?(?=\n\[|\Z)"
|
||||
match = re.search(pattern, content, flags=re.DOTALL)
|
||||
|
||||
if not match:
|
||||
return False
|
||||
|
||||
# Build new section
|
||||
section = f"\n[{new_name}]\n path = {path}\n"
|
||||
if comment:
|
||||
section += f" comment = {comment}\n"
|
||||
section += f" browseable = yes\n read only = no\n"
|
||||
|
||||
new_content = re.sub(pattern, section, content, flags=re.DOTALL)
|
||||
|
||||
with open(SAMBA_CONFIG, 'w') as f:
|
||||
f.write(new_content)
|
||||
|
||||
subprocess.run(['/usr/bin/smbcontrol', 'smbd', 'reload-config'], capture_output=True, timeout=10)
|
||||
logger.info(f"Samba share updated: {old_name} → {new_name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating Samba share: {e}")
|
||||
return False
|
||||
|
||||
def delete_samba_share(self, name: str) -> bool:
|
||||
"""Remove Samba share from /etc/samba/smb.conf"""
|
||||
if not SAMBA_CONFIG.exists():
|
||||
@@ -98,7 +132,7 @@ class SharesManager:
|
||||
with open(SAMBA_CONFIG, 'w') as f:
|
||||
f.write(new_content)
|
||||
|
||||
subprocess.run(['smbcontrol', 'smbd', 'reload-config'], capture_output=True, timeout=10)
|
||||
subprocess.run(['/usr/bin/smbcontrol', 'smbd', 'reload-config'], capture_output=True, timeout=10)
|
||||
logger.info(f"Samba share deleted: {name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -148,7 +182,7 @@ class SharesManager:
|
||||
with open(NFS_EXPORTS, 'a') as f:
|
||||
f.write(export_line)
|
||||
|
||||
subprocess.run(['exportfs', '-r'], capture_output=True, timeout=10)
|
||||
subprocess.run(['/usr/sbin/exportfs', '-r'], capture_output=True, timeout=10)
|
||||
logger.info(f"NFS share created: {path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -172,7 +206,7 @@ class SharesManager:
|
||||
with open(NFS_EXPORTS, 'w') as f:
|
||||
f.writelines(new_lines)
|
||||
|
||||
subprocess.run(['exportfs', '-r'], capture_output=True, timeout=10)
|
||||
subprocess.run(['/usr/sbin/exportfs', '-r'], capture_output=True, timeout=10)
|
||||
logger.info(f"NFS share deleted: {path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -277,7 +311,7 @@ class SharesManager:
|
||||
with open(NFS_EXPORTS, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
subprocess.run(['exportfs', '-r'], capture_output=True, timeout=10)
|
||||
subprocess.run(['/usr/sbin/exportfs', '-r'], capture_output=True, timeout=10)
|
||||
logger.info("NFS config updated")
|
||||
return True
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user