-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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: Pass the set of roles the user has to triggers and cloud functions #9299
Open
mstniy
wants to merge
7
commits into
parse-community:alpha
Choose a base branch
from
Ocell-io:cloud_getroles_mr
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fdfb67b
Cloud code and triggers: Pass the set of roles the user has
mstniy 105838f
Update spec/CloudCode.spec.js
mstniy 147060d
Update spec/CloudCode.spec.js
mstniy 2317a95
Use arrow functions
mstniy cbf3eb1
Renamed getRoles() -> getUserRoles()
mstniy 0cbeba6
Also test that getUserRoles is recursive
mstniy dee0d1f
Added a new util, stripACLRolePrefix
mstniy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1163,6 +1163,56 @@ describe('Cloud Code', () => { | |
); | ||
}); | ||
|
||
it('test save triggers get user roles', async () => { | ||
let beforeSaveFlag = false; | ||
let afterSaveFlag = false; | ||
Parse.Cloud.beforeSave('SaveTriggerUserRoles', async req => { | ||
expect(new Set(await req.getUserRoles())).toEqual(new Set(['TestRole1', 'TestRole2'])); | ||
beforeSaveFlag = true; | ||
}); | ||
|
||
Parse.Cloud.afterSave('SaveTriggerUserRoles', async req => { | ||
expect(new Set(await req.getUserRoles())).toEqual(new Set(['TestRole1', 'TestRole2'])); | ||
afterSaveFlag = true; | ||
}); | ||
|
||
const user = new Parse.User(); | ||
user.set('password', 'asdf'); | ||
user.set('email', '[email protected]'); | ||
user.set('username', 'zxcv'); | ||
await user.signUp(); | ||
const role1 = new Parse.Role('TestRole1', new Parse.ACL({ '*': { read: true, write: true } })); | ||
const role2 = new Parse.Role('TestRole2', new Parse.ACL({ '*': { read: true, write: true } })); | ||
await role1.save(); | ||
role2.getRoles().add(role1); | ||
role1.getUsers().add(user); | ||
await Parse.Object.saveAll([role1, role2]); | ||
|
||
const obj = new Parse.Object('SaveTriggerUserRoles'); | ||
await obj.save(); | ||
expect(beforeSaveFlag).toBeTrue(); | ||
expect(afterSaveFlag).toBeTrue(); | ||
}); | ||
|
||
it('should not have user roles for anonymous calls', async () => { | ||
let beforeSaveFlag = false; | ||
let afterSaveFlag = false; | ||
Parse.Cloud.beforeSave('SaveTriggerUserRoles', async req => { | ||
expect(req.getUserRoles).toBeUndefined(); | ||
beforeSaveFlag = true; | ||
}); | ||
|
||
Parse.Cloud.afterSave('SaveTriggerUserRoles', async req => { | ||
expect(req.getUserRoles).toBeUndefined(); | ||
afterSaveFlag = true; | ||
}); | ||
|
||
const obj = new Parse.Object('SaveTriggerUserRoles'); | ||
await obj.save(); | ||
expect(beforeSaveFlag).toBeTrue(); | ||
expect(afterSaveFlag).toBeTrue(); | ||
}); | ||
|
||
it('beforeSave change propagates through the save response', done => { | ||
Parse.Cloud.beforeSave('ChangingObject', function (request) { | ||
request.object.set('foo', 'baz'); | ||
|
@@ -2014,6 +2064,40 @@ describe('cloud functions', () => { | |
|
||
Parse.Cloud.run('myFunction', {}).then(() => done()); | ||
}); | ||
|
||
it('should have user roles', async () => { | ||
let flag = false; | ||
Parse.Cloud.define('myFunction', async req => { | ||
expect(new Set(await req.getUserRoles())).toEqual(new Set(['TestRole1', 'TestRole2'])); | ||
flag = true; | ||
}); | ||
|
||
const user = new Parse.User(); | ||
user.set('password', 'asdf'); | ||
user.set('email', '[email protected]'); | ||
user.set('username', 'zxcv'); | ||
await user.signUp(); | ||
const role1 = new Parse.Role('TestRole1', new Parse.ACL({ '*': { read: true, write: true } })); | ||
const role2 = new Parse.Role('TestRole2', new Parse.ACL({ '*': { read: true, write: true } })); | ||
await role1.save(); | ||
role2.getRoles().add(role1); | ||
role1.getUsers().add(user); | ||
await Parse.Object.saveAll([role1, role2]); | ||
|
||
await Parse.Cloud.run('myFunction', { sessionToken: user.getSessionToken() }); | ||
expect(flag).toBeTrue(); | ||
}); | ||
|
||
it('should not have user roles for anonymous calls', async () => { | ||
let flag = false; | ||
Parse.Cloud.define('myFunction', async req => { | ||
expect(req.getUserRoles).toBeUndefined(); | ||
flag = true; | ||
}); | ||
|
||
await Parse.Cloud.run('myFunction'); | ||
expect(flag).toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe('beforeSave hooks', () => { | ||
|
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if my earlier comment wasn't clear. I meant to add a method
getUserRoleIds()
to wheregetUserRoles()
currently is.