mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-10 13:32:45 +00:00
If user is owner, fetch Google Brew with user auth
Fixes the case where a user can see a Google Brew under their account (`listBrew()` uses their personal auth) but can't actually delete it (`getBrew()` only uses the serviceAccount). Occurs if a Google brew has lost its permissions somehow (set to "restricted", etc.) such that serviceAccount can no longer interact with it.
This commit is contained in:
@@ -241,8 +241,8 @@ const GoogleActions = {
|
|||||||
return obj.data.id;
|
return obj.data.id;
|
||||||
},
|
},
|
||||||
|
|
||||||
getGoogleBrew : async (id, accessId, accessType)=>{
|
getGoogleBrew : async (auth, id, accessId, accessType)=>{
|
||||||
const drive = googleDrive.drive({ version: 'v3', auth: defaultAuth });
|
const drive = googleDrive.drive({ version: 'v3', auth: auth || defaultAuth });
|
||||||
|
|
||||||
const obj = await drive.files.get({
|
const obj = await drive.files.get({
|
||||||
fileId : id,
|
fileId : id,
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ const api = {
|
|||||||
stub = stub?.toObject();
|
stub = stub?.toObject();
|
||||||
googleId ??= stub?.googleId;
|
googleId ??= stub?.googleId;
|
||||||
|
|
||||||
|
const isOwner = stub?.authors?.length === 0 || stub?.authors?.[0] === req.account?.username;
|
||||||
const isAuthor = stub?.authors?.includes(req.account?.username);
|
const isAuthor = stub?.authors?.includes(req.account?.username);
|
||||||
const isInvited = stub?.invitedAuthors?.includes(req.account?.username);
|
const isInvited = stub?.invitedAuthors?.includes(req.account?.username);
|
||||||
|
|
||||||
@@ -122,9 +123,10 @@ const api = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If there is a google id, try to find the google brew
|
// If there is a google id, try to find the google brew
|
||||||
const googleBrew = await GoogleActions.getGoogleBrew(googleId || stub?.googleId, id, accessType)
|
|
||||||
if(!stubOnly && googleId) {
|
if(!stubOnly && googleId) {
|
||||||
|
const oAuth2Client = isOwner? GoogleActions.authCheck(req.account, res) : undefined;
|
||||||
|
|
||||||
|
const googleBrew = await GoogleActions.getGoogleBrew(oAuth2Client, googleId, id, accessType)
|
||||||
.catch((googleError)=>{
|
.catch((googleError)=>{
|
||||||
const reason = googleError.errors?.[0].reason;
|
const reason = googleError.errors?.[0].reason;
|
||||||
if(reason == 'notFound')
|
if(reason == 'notFound')
|
||||||
|
|||||||
@@ -298,7 +298,7 @@ describe('Tests for api', ()=>{
|
|||||||
expect(next).toHaveBeenCalled();
|
expect(next).toHaveBeenCalled();
|
||||||
expect(api.getId).toHaveBeenCalledWith(req);
|
expect(api.getId).toHaveBeenCalledWith(req);
|
||||||
expect(model.get).toHaveBeenCalledWith({ shareId: '1' });
|
expect(model.get).toHaveBeenCalledWith({ shareId: '1' });
|
||||||
expect(google.getGoogleBrew).toHaveBeenCalledWith('2', '1', 'share');
|
expect(google.getGoogleBrew).toHaveBeenCalledWith(undefined, '2', '1', 'share');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('access is denied to a locked brew', async()=>{
|
it('access is denied to a locked brew', async()=>{
|
||||||
|
|||||||
Reference in New Issue
Block a user