Skip to content

Commit

Permalink
testing user sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
avayedawadi committed Nov 25, 2023
1 parent a70982b commit d73e22f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
5 changes: 4 additions & 1 deletion server/actions/sso.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ export function validateSAMLResponse(samlResp, certificate) {

const attributes = xml.getElementsByTagName("saml2:Attribute");
let userId;
let permissionLevel;
for (let attribute of attributes) {
if (attribute.getAttribute("Name") === "userId")
userId = attribute.textContent.trim();
if (attribute.getAttribute("Name") === "NetlifyPermissionLevel")
permissionLevel = attribute.textContent.trim();
}

if (!userId) return { error: "Could not find user ID" };

return { userId };
return { userId, permissionLevel };
}
69 changes: 47 additions & 22 deletions server/mongodb/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,43 @@ export async function login({ username, password }) {
};
}

export async function signUp({ username, password, isAdmin }) {
if (username == null || password == null) {
export async function signUp({ username, password, isAdmin, salesforceUserId }) {
if (username == null) {
throw new Error("All parameters must be provided!");
}

await mongoDB();

return bcrypt
.hash(password, 10)
.then((hashedPassword) =>
User.create({
username,
password: hashedPassword,
isAdmin: isAdmin || false,
})
)
.then((user) => {
return {
id: user._id,
isAdmin: user.isAdmin,
password: user.password,
};
});
if (password == null) {
return User.create({
username,
salesforceUserId: salesforceUserId,
isAdmin: isAdmin || false,
})
.then((user) => {
return {
id: user._id,
isAdmin: user.isAdmin,
salesforceUserId: user.salesforceUserId,
};
});
} else {
return bcrypt
.hash(password, 10)
.then((hashedPassword) =>
User.create({
username,
password: hashedPassword,
isAdmin: isAdmin || false,
})
)
.then((user) => {
return {
id: user._id,
isAdmin: user.isAdmin,
password: user.password,
};
});
}
}

export const getUserFromId = async (id) => {
Expand All @@ -72,11 +86,22 @@ export const getUserFromId = async (id) => {
}
};

export const getUserFromSalesforceUserId = async (salesforceUserId) => {
export const getUserFromSalesforceUserId = async (salesforceUserId, permissionLevel) => {
await mongoDB();
try {
const user = await User.findOne({ salesforceUserId });
if (!user) return null;
let user;
user = await User.findOne({ salesforceUserId });
if (!user) {
// We create the user only if they have the correct NetlifyPermissionLevel
if (permissionLevel == "General") {
user = await signUp("Salesforce User", null, false, salesforceUserId);
}
else if (permissionLevel == "Administrator") {
user = await signUp("Salesforce User", null, true, salesforceUserId);
}else {
return null;
}
}

return {
id: user._id,
Expand Down
2 changes: 1 addition & 1 deletion server/mongodb/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const UserSchema = new Schema({
},
password: {
type: String,
required: true,
required: false,
},
isAdmin: {
type: Boolean,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/user/sso/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const handler = async (req, res) => {
});
}

const user = await getUserFromSalesforceUserId(result.userId);
const user = await getUserFromSalesforceUserId(result.userId, result.permissionLevel);
if (!user)
return res.status(404).json({
success: false,
success: result.permissionLevel,
message: "A Southface user has not been provisioned for this user yet",
});

Expand Down

1 comment on commit d73e22f

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for southface ready!

✅ Preview
https://southface-oolo1ggxm-bitsofgood.vercel.app

Built with commit d73e22f.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.