Skip to content

Commit

Permalink
feat: support for oauth token pathcing
Browse files Browse the repository at this point in the history
Upon the change in the username of an account
  • Loading branch information
joamag committed May 22, 2021
1 parent d2eada1 commit ce42813
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/appier_extras/parts/admin/models/account.py
Expand Up @@ -964,9 +964,23 @@ def fix_children_s(self):
level = 2
)
def change_username_s(self, username):
# retrieves the global reference to the account class so that
# it can be used for the triggering of global operations
cls = self.__class__

# saves the previous username value as it's going to be used
# in the event payload once there's a trigger operation
username_p = self.username

# updates the account's username and then saves the instance
# so that it reflects the new username
self.username = username
self.save()

# triggers the event that indicates the change in the username
# so that any listener can act on it
cls.trigger_g("change_username", self, username_p)

@appier.operation(
name = "Change Email",
description = """Changing the email of an account is
Expand Down
8 changes: 8 additions & 0 deletions src/appier_extras/parts/admin/part.py
Expand Up @@ -192,10 +192,12 @@ def load(self):
self.load_operations()

self.account_c.bind_g("touch_login", self._on_touch_login)
self.account_c.bind_g("change_username", self._on_change_username)

def unload(self):
appier.Part.unload(self)

self.account_c.unbind_g("change_username", self._on_change_username)
self.account_c.unbind_g("touch_login", self._on_touch_login)

self.unload_operations()
Expand Down Expand Up @@ -2689,6 +2691,12 @@ def _on_touch_login(self, account):
self._login_count += 1
self.flush_settings()

def _on_change_username(self, account, username):
oauth_tokens = models.OAuthToken.find(username = username, limit = 0)
for oauth_token in oauth_tokens:
oauth_token.username = account.username
oauth_token.save()

@property
def _last_login_s(self, format = "%Y-%m-%d %H:%M:%S UTC"):
if not self._last_login: return None
Expand Down

0 comments on commit ce42813

Please sign in to comment.