Last active
October 4, 2021 15:52
-
-
Save ziad-saab/41eca04a72d8038493ba to your computer and use it in GitHub Desktop.
Get a list of a user's roles from Parse, including child roles, up to a certain depth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Maximum depth is 3, after that we get a "" error from Parse | |
function getUserRoles(user) { | |
var queries = [ | |
new Parse.Query('_Role').equalTo('users', user) | |
]; | |
for (var i = 0; i < 2; i++) { | |
queries.push(new Parse.Query('_Role').matchesQuery('roles', queries[i])); | |
} | |
return user.rolesPromise = Parse.Query.or.apply(Parse.Query, queries).find().then( | |
function(roles) { | |
return roles.map(function(role) { | |
return role.get('name'); | |
}); | |
} | |
); | |
} |
If anyone is here in 2021 looking for a TS version of this:
static async getUserRoles(user: Parse.User): Promise<string[]> {
const queries = [
new Parse.Query(Parse.Role).equalTo('users', user),
];
for (let i = 0; i < 10; i++) {
queries.push(new Parse.Query(Parse.Role).matchesQuery('roles', queries[i]));
}
const roles = await Parse.Query.or(...queries).find({ useMasterKey: true });
return roles.map((role) => role.getName());
}
Hm it is somewhat confusing that you're asking for a TS version of a TS snippet. What is the real problem/context behind your question?
@coderofsalvation No question from me. I just posted my TS version of the code, in case anyone needed it.
hehe indeed my bad 😂
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@macarthuror i love terse refactors, however the description mentions
, including child roles, up to a certain depth
(which is lacking in your version). Your code only returns unnested roles.