Skip to content

Commit

Permalink
tenant selection and user deletion fix for removing from the tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
larinam committed Aug 25, 2024
1 parent e9241b7 commit c63fe14
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
47 changes: 34 additions & 13 deletions backend/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,38 @@ async def update_user(user_id: str, user_update: UserUpdateModel,


@router.delete("/{user_id}")
async def delete_user(user_id: str, current_user: Annotated[User, Depends(get_current_active_user_check_tenant)],
tenant: Annotated[Tenant, Depends(get_tenant)]):
# One should not be able to delete the last user in the tenant.
# There should be another option to destroy the whole tenant with the last user.
if User.objects(tenants__in=[tenant]).count() == 1:
raise HTTPException(status_code=400, detail="Can't delete the last user in the workspace")

result = User.objects(tenants__in=[tenant], id=user_id).delete()
if result == 0:
async def delete_user(
user_id: str,
current_user: Annotated[User, Depends(get_current_active_user_check_tenant)],
tenant: Annotated[Tenant, Depends(get_tenant)]
):
user_to_delete = User.objects(id=user_id).first()

if not user_to_delete:
raise HTTPException(status_code=404, detail="User not found")

return {"message": "User deleted successfully"}
# Check if the user is part of more than one tenant
if len(user_to_delete.tenants) > 1:
try:
user_to_delete.remove_tenant(tenant)
UserInvite.objects(email=user_to_delete.email, tenant=tenant).delete()
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))

return {"message": f"User removed from tenant {tenant.name} successfully"}
else:
# Prevent deletion of the last user in the tenant
# There should be another option to destroy the whole tenant with the last user.
if User.objects(tenants__in=[tenant]).count() == 1:
raise HTTPException(status_code=400, detail="Can't delete the last user in the workspace")

result = user_to_delete.delete()
UserInvite.objects(email=user_to_delete.email, tenant=tenant).delete()

if result == 0:
raise HTTPException(status_code=404, detail="User not found")

return {"message": "User deleted successfully"}


@router.get("/me")
Expand Down Expand Up @@ -323,7 +343,8 @@ async def register_user_via_invite(token: str, user_creation: UserCreationModel)
if user:
# User exists, add the new tenant
if invite.tenant in user.tenants:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User already associated with this tenant")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="User already associated with this tenant")

user.tenants.append(invite.tenant)
user.save()
Expand All @@ -343,8 +364,8 @@ async def register_user_via_invite(token: str, user_creation: UserCreationModel)
# Mark invite as accepted
invite.mark_as_accepted()

return {"message": "User registered successfully" if not user else "Tenant associated successfully with the existing user"}

return {
"message": "User registered successfully" if not user else "Tenant associated successfully with the existing user"}


class UserInviteDTO(BaseModel):
Expand Down
27 changes: 26 additions & 1 deletion frontend/src/components/UserProfileMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {useAuth} from "../contexts/AuthContext";

const UserProfileMenu = ({setShowDropdown}) => {
const navigate = useNavigate();
const {handleLogout, user, currentTenant} = useAuth();
const {handleLogout, user, currentTenant, setCurrentTenant} = useAuth();
const dropdownRef = useRef(null);

useEffect(() => {
Expand All @@ -24,6 +24,14 @@ const UserProfileMenu = ({setShowDropdown}) => {
};
}, [setShowDropdown]);

const handleTenantSwitch = (tenant) => {
if (tenant.identifier !== currentTenant) {
setCurrentTenant(tenant.identifier);
navigate(0); // This triggers a full page reload, or navigate to a specific path if required
}
setShowDropdown(false); // Close the dropdown after switching
};

return (
<div className="dropdownMenu" ref={dropdownRef}>
<div className="dropdownItem" style={{cursor: 'default', backgroundColor: 'transparent', transition: 'none'}}>
Expand All @@ -36,6 +44,23 @@ const UserProfileMenu = ({setShowDropdown}) => {
<FontAwesomeIcon icon={faCog}/>
<span>Settings</span>
</div>

{user.tenants.length > 1 && (
<>
<hr/>
{user.tenants.map((tenant) => (
<div
key={tenant.id}
className="dropdownItem"
onClick={() => handleTenantSwitch(tenant)}
style={{fontWeight: tenant.identifier === currentTenant ? 'bold' : 'normal'}}
>
<span>{tenant.name} ({tenant.identifier})</span>
</div>
))}
<hr/>
</>
)}
<div className="dropdownItem" onClick={() => {
window.open('https://t.me/larinam', '_blank');
setShowDropdown(false); // This will close the dropdown menu when the item is clicked
Expand Down

0 comments on commit c63fe14

Please sign in to comment.