Repository: matrix-org/synapse

Issue:
/_synapse/admin/v2/users is broken
Running the /_synapse/admin/v2/users as documented produces an internal server error

Postgres logs:
```
STATEMENT:  SELECT name, password_hash, is_guest, admin, user_type, deactivated FROM users WHERE is_guest = false AND deactivated = false ORDER BY name ASC LIMIT 10 OFFSET 0
ERROR:  operator does not exist: smallint = boolean at character 95
```

`\d users` shows that `is_guest` and `deactivated` are type `smallint` not `boolean`.
```

                          Table "public.users"
           Column           |   Type   | Collation | Nullable | Default
----------------------------+----------+-----------+----------+---------
 name                       | text     |           |          |
 password_hash              | text     |           |          |
 creation_ts                | bigint   |           |          |
 admin                      | smallint |           | not null | 0
 upgrade_ts                 | bigint   |           |          |
 is_guest                   | smallint |           | not null | 0
 appservice_id              | text     |           |          |
 consent_version            | text     |           |          |
 consent_server_notice_sent | text     |           |          |
 user_type                  | text     |           |          |
 deactivated                | smallint |           | not null | 0
```



Discussion:
@awesome-manuel any chance you could take a look at this?
@richvdh Sure, I can fix it.

Resolving patch:
diff --git a/synapse/storage/data_stores/main/__init__.py b/synapse/storage/data_stores/main/__init__.py
--- a/synapse/storage/data_stores/main/__init__.py
+++ b/synapse/storage/data_stores/main/__init__.py
@@ -526,9 +526,9 @@ def get_users_paginate(
 
         attr_filter = {}
         if not guests:
-            attr_filter["is_guest"] = False
+            attr_filter["is_guest"] = 0
         if not deactivated:
-            attr_filter["deactivated"] = False
+            attr_filter["deactivated"] = 0
 
         return self.db.simple_select_list_paginate(
             desc="get_users_paginate",


Test patch:
diff --git a/tests/rest/admin/test_admin.py b/tests/rest/admin/test_admin.py
--- a/tests/rest/admin/test_admin.py
+++ b/tests/rest/admin/test_admin.py
@@ -341,6 +341,47 @@ def nonce():
         self.assertEqual("Invalid user type", channel.json_body["error"])
 
 
+class UsersListTestCase(unittest.HomeserverTestCase):
+
+    servlets = [
+        synapse.rest.admin.register_servlets,
+        login.register_servlets,
+    ]
+    url = "/_synapse/admin/v2/users"
+
+    def prepare(self, reactor, clock, hs):
+        self.admin_user = self.register_user("admin", "pass", admin=True)
+        self.admin_user_tok = self.login("admin", "pass")
+
+        self.register_user("user1", "pass1", admin=False)
+        self.register_user("user2", "pass2", admin=False)
+
+    def test_no_auth(self):
+        """
+        Try to list users without authentication.
+        """
+        request, channel = self.make_request("GET", self.url, b"{}")
+        self.render(request)
+
+        self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
+        self.assertEqual("M_MISSING_TOKEN", channel.json_body["errcode"])
+
+    def test_all_users(self):
+        """
+        List all users, including deactivated users.
+        """
+        request, channel = self.make_request(
+            "GET",
+            self.url + "?deactivated=true",
+            b"{}",
+            access_token=self.admin_user_tok,
+        )
+        self.render(request)
+
+        self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
+        self.assertEqual(3, len(channel.json_body["users"]))
+
+
 class ShutdownRoomTestCase(unittest.HomeserverTestCase):
     servlets = [
         synapse.rest.admin.register_servlets_for_client_rest_resource,