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

feat: set and update user role #151

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 11 additions & 10 deletions server/schema/user/user.datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,32 +178,33 @@ const updateDetails = (id, fields, session, authToken, mid) =>
{ new: true }
);

const getCustomClaims = async (id) => {
const getCustomClaims = async (email) => {
try {
const _user = await UserModel.findById(id, 'email');
if (!_user) {
const _fbUser = await admin.auth().getUserByEmail(email);
if (!_fbUser) {
throw APIError('NOT_FOUND', null, { reason: 'The requested user does not exist.' });
}
const _fbUser = await admin.auth().getUserByEmail(_user.email);
const userRecord = await admin.auth().getUser(_fbUser.uid);
return userRecord.customClaims;
} catch (error) {
throw FirebaseAuthError(error, { reason: "Cannot find user's roles" });
}
};
const updateRoles = async (id, roles) => {
const updateRoles = async (email, roles) => {
try {
const _user = await UserModel.findById(id);
if (!_user) {
console.log('Update roles called');
DesignrKnight marked this conversation as resolved.
Show resolved Hide resolved
const _fbUser = await admin.auth().getUserByEmail(email);
console.log(_fbUser);
if (!_fbUser) {
throw APIError('NOT_FOUND', null, { reason: 'The requested user does not exist.' });
}

const _fbUser = await admin.auth().getUserByEmail(_user.email);
await admin.auth().setCustomUserClaims(_fbUser.uid, {
..._fbUser.customClaims,
roles,
});
return _user;
const customClaims = await getCustomClaims(email);
console.log(customClaims);
return customClaims.roles;
DesignrKnight marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
throw FirebaseAuthError(error, { reason: "The user's roles could not be updated." });
}
Expand Down
8 changes: 6 additions & 2 deletions server/schema/user/user.mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
// GraphQLDate,
// GraphQLTime,
// GraphQLDateTime,
// GraphQLJSON,
GraphQLJSON,
// GraphQLJSONObject,
} = require('../scalars');

Expand Down Expand Up @@ -145,8 +145,12 @@ module.exports = new GraphQLObjectType({
resolve: setUserBan,
},
setUserRoles: {
type: UserType,
type: GraphQLJSON,
rutajdash marked this conversation as resolved.
Show resolved Hide resolved
args: {
email: {
description: "The user's email id",
type: new GraphQLNonNull(GraphQLString),
},
id: {
description: "The user's mongo ID.",
type: new GraphQLNonNull(GraphQLID),
Expand Down
6 changes: 3 additions & 3 deletions server/schema/user/user.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ module.exports = new GraphQLObjectType({
description: 'Get user roles',
type: GraphQLJSON,
rutajdash marked this conversation as resolved.
Show resolved Hide resolved
args: {
id: {
description: "The user's mongo ID.",
type: new GraphQLNonNull(GraphQLID),
email: {
description: "The user's email id",
type: new GraphQLNonNull(GraphQLString),
},
},
resolve: getUserCustomClaims,
Expand Down
11 changes: 5 additions & 6 deletions server/schema/user/user.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const canUpdateUser = (id, mid, session, authToken, decodedToken, fieldNodes, ne

if (
mid !== id &&
_fields.some((item) => !PUBLIC_FIELDS.includes(item)) &&
_fields?.some((item) => !PUBLIC_FIELDS.includes(item)) &&
!UserPermission.exists(session, authToken, decodedToken, 'user.read.all')
) {
throw APIError('FORBIDDEN', null, {
Expand Down Expand Up @@ -522,25 +522,24 @@ module.exports = {
throw FirebaseAuthError(error);
}
},
getUserCustomClaims: async (_parent, { id }, { API: { User } }) => {
getUserCustomClaims: async (_parent, { email }, { API: { User } }) => {
try {
const customClaims = await User.getCustomClaims(id);
const customClaims = await User.getCustomClaims(email);
return customClaims;
} catch (error) {
return FirebaseAuthError(error);
}
},
setUserRoles: async (
_parent,
{ id, roles },
{ email, id, roles },
{ mid, session, authToken, decodedToken, API: { User } },
{ fieldNodes }
) => {
try {
canUpdateUser(id, mid, session, authToken, decodedToken, fieldNodes, true);

const _user = await User.updateRoles(id, roles);

const _user = await User.updateRoles(email, roles);
return _user;
} catch (error) {
return FirebaseAuthError(error);
Expand Down