Skip to content

check project owner is not removed #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 24, 2025
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,19 @@ router.delete('/user/:id', async function(req, res) {
res.status(403).send({ message: 'User owns some web sites, please change owner first!' });
return;
}
let projects = await dbsrv.mongo_projects().find({ owner: uid }).toArray();
if (projects && projects.length > 0) {
res.status(403).send({ message: 'User owns some projects, please change owner first!' });
return;
}
const allprojects = user.projects ? user.projects : [];
for (let project_name of allprojects) {
const users_in_project = await dbsrv.mongo_users().find({ 'projects': project_name }).toArray();
Copy link
Member

@mboudet mboudet Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use .count() instead of toArray, and just check the count value after

dbsrv.mongo_users().find({ 'projects': project_name }).count()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Need to check if it works. It works in mongo directly, but well..)

if (users_in_project.length <= 1) {
res.status(403).send({ message: 'User is the last member of project ${project_name}' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to store project names somewhere, and send the response after will all projects.
(Otherwise, you'll try to delete -> change a project -> try to delete -> etc)

return;
}
}
usrsrv.delete_user(user, session_user.uid, mail_message, mail_send).then(function() {
res.send({ message: 'User deleted' });
return;
Expand Down