Add teams to a user when they are created
This commit is contained in:
1 parent
a6f5c96f93
commit
c39eab49bd
8 files changed
+26
-8
No files matched your search
@@ -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 (
|
||||
<Box
|
||||
@@ -71,7 +75,7 @@ const Account = ({ open = "profile" }) => {
|
||||
</Box>
|
||||
<ProfilePanel />
|
||||
<PasswordPanel />
|
||||
{user.role.includes("admin") && <TeamPanel />}
|
||||
{!hideTeams && <TeamPanel />}
|
||||
</TabContext>
|
||||
</Box>
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -76,6 +76,7 @@ const credentials = joi.object({
|
||||
return value;
|
||||
}),
|
||||
role: joi.array(),
|
||||
teamId: joi.string().allow("").optional(),
|
||||
});
|
||||
|
||||
const monitorValidation = joi.object({
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -37,6 +37,8 @@ const insertUser = async (req, res) => {
|
||||
});
|
||||
teamId = team._id;
|
||||
await team.save();
|
||||
} else {
|
||||
teamId = userData.teamId;
|
||||
}
|
||||
|
||||
const newUser = new UserModel(userData);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in new issue
Block a user