Repository: internetarchive/openlibrary

Issue:
"Log in as user" feature does not reset printdisabled cookie
Logging in as a user via the admin UI for debugging does not reset the printdisabled cookies, causing some weird issues.

### Steps to Reproduce
1. Log in with admin, printdisabled account
2. "Log in as user" of another user account, not printdisabled accout

* Actual: pd=1 still set in cookies
* Expected: pd should be set to what makes sense for the user.

### Details

- **Logged in (Y/N)?**
- **Browser type/version?**
- **Operating system?**
- **Environment (prod/dev/local)?** prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->

### Stakeholders
<!-- @ tag stakeholders of this bug -->



Discussion:
I would like to take up this issue.

Resolving patch:
diff --git a/openlibrary/accounts/model.py b/openlibrary/accounts/model.py
--- a/openlibrary/accounts/model.py
+++ b/openlibrary/accounts/model.py
@@ -128,6 +128,11 @@ def create_link_doc(key, username, email):
     }
 
 
+def clear_cookies():
+    web.setcookie('pd', "", expires=-1)
+    web.setcookie('sfw', "", expires=-1)
+
+
 class Link(web.storage):
     def get_expiration_time(self):
         d = self['expires_on'].split(".")[0]
diff --git a/openlibrary/plugins/admin/code.py b/openlibrary/plugins/admin/code.py
--- a/openlibrary/plugins/admin/code.py
+++ b/openlibrary/plugins/admin/code.py
@@ -25,6 +25,7 @@
 import openlibrary
 
 from openlibrary import accounts
+from openlibrary.accounts.model import clear_cookies
 from openlibrary.accounts.model import OpenLibraryAccount
 from openlibrary.core import admin as admin_stats, helpers as h, imports, cache
 from openlibrary.core.waitinglist import Stats as WLStats
@@ -465,7 +466,10 @@ def POST_set_bot_flag(self, account, bot):
 
     def POST_su(self, account):
         code = account.generate_login_code()
+        # Clear all existing admin cookies before logging in as another user
+        clear_cookies()
         web.setcookie(config.login_cookie_name, code, expires="")
+
         return web.seeother("/")
 
     def POST_anonymize_account(self, account, test):
diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py
--- a/openlibrary/plugins/upstream/account.py
+++ b/openlibrary/plugins/upstream/account.py
@@ -36,6 +36,7 @@
     OpenLibraryAccount,
     InternetArchiveAccount,
     valid_email,
+    clear_cookies,
 )
 from openlibrary.plugins.upstream import borrow, forms, utils
 from openlibrary.utils.dateutil import elapsed_time
@@ -434,6 +435,23 @@ def POST_resend_verification_email(self, i):
         return render.message(title, message)
 
 
+class account_logout(delegate.page):
+    """Account logout.
+
+    This registers a handler to the /account/logout endpoint in infogami so that additional logic, such as clearing admin cookies,
+    can be handled prior to the calling of infogami's standard logout procedure
+
+    """
+
+    path = "/account/logout"
+
+    def POST(self):
+        clear_cookies()
+        from infogami.core.code import logout as infogami_logout
+
+        return infogami_logout().POST()
+
+
 class account_verify(delegate.page):
     """Verify user account."""
 


Test patch: