admin.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from twisted.internet import defer
  17. from ._base import BaseHandler
  18. logger = logging.getLogger(__name__)
  19. class AdminHandler(BaseHandler):
  20. def __init__(self, hs):
  21. super(AdminHandler, self).__init__(hs)
  22. @defer.inlineCallbacks
  23. def get_whois(self, user):
  24. connections = []
  25. sessions = yield self.store.get_user_ip_and_agents(user)
  26. for session in sessions:
  27. connections.append({
  28. "ip": session["ip"],
  29. "last_seen": session["last_seen"],
  30. "user_agent": session["user_agent"],
  31. })
  32. ret = {
  33. "user_id": user.to_string(),
  34. "devices": {
  35. "": {
  36. "sessions": [
  37. {
  38. "connections": connections,
  39. }
  40. ]
  41. },
  42. },
  43. }
  44. defer.returnValue(ret)
  45. @defer.inlineCallbacks
  46. def get_users(self):
  47. """Function to reterive a list of users in users table.
  48. Args:
  49. Returns:
  50. defer.Deferred: resolves to list[dict[str, Any]]
  51. """
  52. ret = yield self.store.get_users()
  53. defer.returnValue(ret)
  54. @defer.inlineCallbacks
  55. def get_users_paginate(self, order, start, limit):
  56. """Function to reterive a paginated list of users from
  57. users list. This will return a json object, which contains
  58. list of users and the total number of users in users table.
  59. Args:
  60. order (str): column name to order the select by this column
  61. start (int): start number to begin the query from
  62. limit (int): number of rows to reterive
  63. Returns:
  64. defer.Deferred: resolves to json object {list[dict[str, Any]], count}
  65. """
  66. ret = yield self.store.get_users_paginate(order, start, limit)
  67. defer.returnValue(ret)
  68. @defer.inlineCallbacks
  69. def search_users(self, term):
  70. """Function to search users list for one or more users with
  71. the matched term.
  72. Args:
  73. term (str): search term
  74. Returns:
  75. defer.Deferred: resolves to list[dict[str, Any]]
  76. """
  77. ret = yield self.store.search_users(term)
  78. defer.returnValue(ret)