From c39eab49bd0639f98d24651fac3da4eeac8610dd Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 26 Aug 2024 13:29:11 -0700 Subject: [PATCH] Add teams to a user when they are created --- Client/src/Pages/Account/index.jsx | 8 ++++++-- Client/src/Pages/Auth/Register/Register.jsx | 9 +++++---- Client/src/Validation/validation.js | 1 + Server/controllers/authController.js | 3 ++- Server/db/mongo/modules/inviteModule.js | 1 + Server/db/mongo/modules/userModule.js | 2 ++ Server/models/InviteToken.js | 6 ++++++ Server/validation/joi.js | 4 +++- 8 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Client/src/Pages/Account/index.jsx b/Client/src/Pages/Account/index.jsx index 2fed926b8..e731d231a 100644 --- a/Client/src/Pages/Account/index.jsx +++ b/Client/src/Pages/Account/index.jsx @@ -25,7 +25,11 @@ const Account = ({ open = "profile" }) => { let tabList = ["Profile", "Password", "Team"]; const { user } = useSelector((state) => state.auth); - if (!user.role.includes("admin")) tabList = ["Profile", "Password"]; + const requiredRoles = ["superadmin", "admin"]; + const hideTeams = !requiredRoles.some((role) => user.role.includes(role)); + if (hideTeams) { + tabList = ["Profile", "Password"]; + } return ( { - {user.role.includes("admin") && } + {!hideTeams && } ); diff --git a/Client/src/Pages/Auth/Register/Register.jsx b/Client/src/Pages/Auth/Register/Register.jsx index 4dd8fad24..39a5fdafd 100644 --- a/Client/src/Pages/Auth/Register/Register.jsx +++ b/Client/src/Pages/Auth/Register/Register.jsx @@ -444,24 +444,26 @@ const Register = ({ isSuperAdmin }) => { password: "", confirm: "", role: [], + teamId: "", }); const [errors, setErrors] = useState({}); const [step, setStep] = useState(0); useEffect(() => { const fetchInvite = async () => { + console.log("FETCHING"); if (token !== undefined) { try { const res = await networkService.verifyInvitationToken(token); - const { role, email } = res.data.data; - setForm({ ...form, email, role }); + const { role, email, teamId } = res.data.data; + setForm({ ...form, email, role, teamId }); } catch (error) { logger.error(error); } } }; fetchInvite(); - }, [token, form]); + }, []); /** * Validates the form data against the validation schema. @@ -494,7 +496,6 @@ const Register = ({ isSuperAdmin }) => { const handleStepOne = async (e) => { e.preventDefault(); - let error = validateForm({ firstName: form.firstName, lastName: form.lastName, diff --git a/Client/src/Validation/validation.js b/Client/src/Validation/validation.js index 35d47d6b1..c326e8ee9 100644 --- a/Client/src/Validation/validation.js +++ b/Client/src/Validation/validation.js @@ -76,6 +76,7 @@ const credentials = joi.object({ return value; }), role: joi.array(), + teamId: joi.string().allow("").optional(), }); const monitorValidation = joi.object({ diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 7c053ae56..720dcd08c 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -226,7 +226,8 @@ const inviteController = async (req, res, next) => { try { // Only admins can invite const token = getTokenFromHeaders(req.headers); - const { role, firstname } = jwt.decode(token); + const { role, firstname, teamId } = jwt.decode(token); + req.body.teamId = teamId; try { await inviteRoleValidation.validateAsync({ roles: role }); await inviteBodyValidation.validateAsync(req.body); diff --git a/Server/db/mongo/modules/inviteModule.js b/Server/db/mongo/modules/inviteModule.js index b4404c615..73e883930 100644 --- a/Server/db/mongo/modules/inviteModule.js +++ b/Server/db/mongo/modules/inviteModule.js @@ -8,6 +8,7 @@ const requestInviteToken = async (req, res) => { let inviteToken = new InviteToken({ email: req.body.email, role: req.body.role, + teamId: req.body.teamId, token: crypto.randomBytes(32).toString("hex"), }); await inviteToken.save(); diff --git a/Server/db/mongo/modules/userModule.js b/Server/db/mongo/modules/userModule.js index bc2f83958..2df13ddc7 100644 --- a/Server/db/mongo/modules/userModule.js +++ b/Server/db/mongo/modules/userModule.js @@ -37,6 +37,8 @@ const insertUser = async (req, res) => { }); teamId = team._id; await team.save(); + } else { + teamId = userData.teamId; } const newUser = new UserModel(userData); diff --git a/Server/models/InviteToken.js b/Server/models/InviteToken.js index 10515dadb..5fbd4fc73 100644 --- a/Server/models/InviteToken.js +++ b/Server/models/InviteToken.js @@ -7,6 +7,12 @@ const InviteTokenSchema = mongoose.Schema( required: true, unique: true, }, + teamId: { + type: mongoose.Schema.Types.ObjectId, + ref: "Team", + immutable: true, + required: true, + }, role: { type: Array, required: true, diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 3a9b7e1a1..14654e1ae 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -5,7 +5,7 @@ const joi = require("joi"); //**************************************** const roleValidatior = (role) => (value, helpers) => { - const hasRole = roles.some((role) => value.includes(role)); + const hasRole = role.some((role) => value.includes(role)); if (!hasRole) { throw new Joi.ValidationError( `You do not have the required authorization. Required roles: ${roles.join(", ")}` @@ -72,6 +72,7 @@ const registerValidation = joi.object({ .items(joi.string().valid("superadmin", "admin", "user")) .min(1) .required(), + teamId: joi.string().allow("").required(), }); const editUserParamValidation = joi.object({ @@ -135,6 +136,7 @@ const inviteBodyValidation = joi.object({ "string.email": "Must be a valid email address", }), role: joi.array().required(), + teamId: joi.string().required(), }); const inviteVerifciationBodyValidation = joi.object({