Skip to content
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

fix: update member data request pending #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
97 changes: 52 additions & 45 deletions src/controllers/networkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,77 +627,84 @@ exports.easy_post = async function(req, res) {
}

// Easy members auth POST
exports.members = async function(req, res) {
exports.members = async function (req, res) {
const navigate =
{
active: 'networks',
whence: '/controller/networks'
}

let errors = null;

if (req.method === 'POST') {

req.checkBody('id', 'Member ID is required').notEmpty();
req.sanitize('id').trim();
req.sanitize('id').escape();
try {
if (req.method === 'POST') {
// Validate common fields
req.checkBody('id', 'Member ID is required').notEmpty();
req.sanitize('id').trim();
req.sanitize('id').escape();

// Validate optional fields
if (req.body.auth !== undefined) {
req.checkBody('auth', 'Authorization state must be boolean').isBoolean();
req.sanitize('auth').trim();
req.sanitize('auth').escape();
}

if (req.body.auth) {
req.checkBody('auth', 'Authorization state must be boolean').isBoolean();
req.sanitize('auth').trim();
req.sanitize('auth').escape();
if (req.body.activeBridge !== undefined) {
req.checkBody('activeBridge', 'activeBridge state must be boolean').isBoolean();
req.sanitize('activeBridge').trim();
req.sanitize('activeBridge').escape();
}

errors = req.validationErrors();
if (req.body.name !== undefined) {
req.sanitize('name').trim();
req.sanitize('name').escape();
}

if (!errors) {
const auth =
{
authorized: req.body.auth
};
// Collect validation errors
const errors = req.validationErrors();
if (errors) {
return res.status(400).json({ errors: errors.array() });
}

// Simultaneously process all provided fields
if (req.body.auth !== undefined) {
const auth = { authorized: req.body.auth };
try {
const mem = await zt.member_object(req.params.nwid, req.body.id, auth);
await zt.member_object(req.params.nwid, req.body.id, auth);
} catch (err) {
throw err;
console.error('Error processing auth:', err);
return res.status(500).json({ error: 'Failed to update authorization state' });
}
}
} else if (req.body.activeBridge) {
req.checkBody('activeBridge', 'activeBridge state must be boolean').isBoolean();
req.sanitize('activeBridge').trim();
req.sanitize('activeBridge').escape();

errors = req.validationErrors();

if (!errors) {
const activeBridge =
{
activeBridge: req.body.activeBridge
};

if (req.body.activeBridge !== undefined) {
const activeBridge = { activeBridge: req.body.activeBridge };
try {
const mem = await zt.member_object(req.params.nwid, req.body.id, activeBridge);
await zt.member_object(req.params.nwid, req.body.id, activeBridge);
} catch (err) {
throw err;
console.error('Error processing activeBridge:', err);
return res.status(500).json({ error: 'Failed to update activeBridge state' });
}
}
} else if (req.body.name) {
req.sanitize('name').trim();
req.sanitize('name').escape();

errors = req.validationErrors();

if (!errors) {
if (req.body.name !== undefined) {
try {
const ret = await storage.setItem(req.body.id, req.body.name);
await storage.setItem(req.body.id, req.body.name);
} catch (err) {
throw err;
console.error('Error processing name:', err);
return res.status(500).json({ error: 'Failed to update name' });
}
}

// Return success after processing all fields
return res.status(200).json({ success: true });
} else {
return res.redirect(`/controller/network/${nwid}#members`);
}
} else { // GET
res.redirect("/controller/network/" + req.params.nwid + "#members");
} catch (err) {
console.error('Unexpected error:', err);
return res.status(500).json({ error: 'Internal server error', detail: err });
}
}
};

// Member delete GET or POST
exports.member_delete = async function(req, res) {
Expand Down