-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-trigger Trigger Google Drive shared folder sync on user email edit
- Loading branch information
Showing
7 changed files
with
88 additions
and
41 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Utilities for common-used interactions with Google API.""" | ||
import logging | ||
from typing import List, Optional | ||
from interface.gcp import GCPInterface | ||
from db import DBFacade | ||
from app.model import User, Team | ||
|
||
|
||
def sync_user_email_perms(gcp: Optional[GCPInterface], | ||
db: DBFacade, | ||
user: User): | ||
""" | ||
Refresh Google Drive permissions for a provided user. If no GCP client is | ||
provided, this function is a no-op. | ||
Finds folders for user by checking all teams the user is a part of, and | ||
calling ``sync_team_email_perms()``. | ||
""" | ||
if gcp is None: | ||
logging.debug('GCP not enabled, skipping drive permissions') | ||
return | ||
|
||
if len(user.email) == 0 or len(user.github_id) == 0: | ||
return | ||
|
||
teams_user_is_in = db.query(Team, [('members', user.github_id)]) | ||
for team in teams_user_is_in: | ||
sync_team_email_perms(gcp, db, team) | ||
|
||
|
||
def sync_team_email_perms(gcp: Optional[GCPInterface], | ||
db: DBFacade, | ||
team: Team): | ||
""" | ||
Refresh Google Drive permissions for provided team. If no GCP client | ||
is provided, this function is a no-op. | ||
""" | ||
if gcp is None: | ||
logging.debug("GCP not enabled, skipping drive permissions") | ||
return | ||
|
||
if len(team.folder) == 0: | ||
return | ||
|
||
# Generate who to share with | ||
emails: List[str] = [] | ||
for github_id in team.members: | ||
users = db.query(User, [('github_user_id', github_id)]) | ||
if len(users) != 1: | ||
logging.warn(f"None/multiple users for GitHub ID {github_id}") | ||
|
||
# For now, naiively iterate over all users, due to | ||
# https://github.com/ubclaunchpad/rocket2/issues/493 | ||
for user in users: | ||
if len(user.email) > 0: | ||
emails.append(user.email) | ||
|
||
# Sync permissions | ||
if len(emails) > 0: | ||
logging.info("Synchronizing permissions for " | ||
+ f"{team.github_team_name}'s folder ({team.folder}) " | ||
+ f"to {emails}") | ||
gcp.set_drive_permissions( | ||
team.github_team_name, team.folder, emails) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,7 @@ def test_escape_email(self): | |
email = "<mailto:[email protected]|[email protected]>" | ||
ret = util.escape_email(email) | ||
self.assertEqual(ret, "[email protected]") | ||
|
||
def test_escape_normal_email(self): | ||
email = '[email protected]' | ||
self.assertEqual(util.escape_email(email), email) |
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 |
---|---|---|
|
@@ -99,7 +99,12 @@ def escape_email(email: str) -> str: | |
[email protected] | ||
Does nothing if the email is not escaped. | ||
:param email: email to convert | ||
:return: unescaped email | ||
""" | ||
return email.split('|')[0][8:] | ||
if email.startswith('<'): | ||
return email.split('|')[0][8:] | ||
else: | ||
return email |