user.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #-*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  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 json
  16. from rrd import corelib
  17. from rrd import config
  18. from rrd.utils.logger import logging
  19. log = logging.getLogger(__file__)
  20. class UserToken(object):
  21. def __init__(self, name, sig):
  22. self.name = name
  23. self.sig = sig
  24. def __repr__(self):
  25. return "<UserToken name=%s, sig=%s>" % (self.name, self.sig)
  26. __str__ = __repr__
  27. class User(object):
  28. def __init__(self, id, name, cnname, email, phone, im, qq, role):
  29. self.id = id
  30. self.name = name
  31. self.cnname = cnname
  32. self.email = email
  33. self.phone = phone
  34. self.im = im
  35. self.qq = qq
  36. self.role = role
  37. def __repr__(self):
  38. return "<User id=%s, name=%s, cnname=%s>" \
  39. % (self.id, self.name, self.cnname)
  40. __str__ = __repr__
  41. def dict(self):
  42. return {
  43. 'id': self.id,
  44. 'name': self.name,
  45. 'cnname': self.cnname,
  46. 'email': self.email,
  47. 'phone': self.phone,
  48. 'im': self.im,
  49. 'qq': self.qq,
  50. 'role': self.role,
  51. }
  52. def is_root(self):
  53. return str(self.role) == "2"
  54. def is_admin(self):
  55. return str(self.role) == "1"
  56. def in_teams(self, groups=[]):
  57. if not groups:
  58. return False
  59. r = corelib.auth_requests("GET", '%s/user/u/%s/in_teams?team_names=%s' \
  60. % (config.API_ADDR, self.id, ','.join(groups)))
  61. log.debug("%s:%s" %(r.status_code, r.text))
  62. if r.status_code != 200:
  63. return False
  64. j = r.json()
  65. return j["message"] == "true"
  66. @classmethod
  67. def get_by_id(cls, user_id):
  68. h = {"Content-type": "application/json"}
  69. r = corelib.auth_requests("GET", "%s/user/u/%s" %(config.API_ADDR, user_id), headers=h)
  70. log.debug("%s:%s" %(r.status_code, r.text))
  71. if r.status_code != 200:
  72. raise Exception("%s %s" %(r.status_code, r.text))
  73. j = r.json()
  74. return j and cls(j['id'], j['name'], j['cnname'], j['email'], j['phone'], j['im'], j['qq'], j['role'])
  75. @classmethod
  76. def get_by_name(cls, name):
  77. h = {"Content-type": "application/json"}
  78. r = corelib.auth_requests("GET", "%s/user/name/%s" %(config.API_ADDR, name), headers=h)
  79. log.debug("%s:%s" %(r.status_code, r.text))
  80. if r.status_code != 200:
  81. raise Exception("%s %s" %(r.status_code, r.text))
  82. j = r.json()
  83. return j and cls(j['id'], j['name'], j['cnname'], j['email'], j['phone'], j['im'], j['qq'], j['role'])
  84. @classmethod
  85. def update_user_profile(cls, data={}):
  86. h = {"Content-type": "application/json"}
  87. r = corelib.auth_requests("PUT", "%s/user/update" %(config.API_ADDR,), \
  88. data=json.dumps(data), headers=h)
  89. log.debug("%s:%s" %(r.status_code, r.text))
  90. if r.status_code != 200:
  91. raise Exception("%s %s" %(r.status_code, r.text))
  92. return r.text
  93. @classmethod
  94. def change_user_passwd(cls, old_password, new_password):
  95. h = {"Content-type":"application/json"}
  96. d = {
  97. "old_password": old_password,
  98. "new_password": new_password,
  99. }
  100. r = corelib.auth_requests("PUT", "%s/user/cgpasswd" %(config.API_ADDR,), \
  101. data=json.dumps(d), headers=h)
  102. log.debug("%s:%s" %(r.status_code, r.text))
  103. if r.status_code != 200:
  104. raise Exception("%s %s" %(r.status_code, r.text))
  105. return r.text
  106. @classmethod
  107. def get_users(cls, query_term, limit=20, page=1):
  108. users = []
  109. if not query_term:
  110. query_term = '.'
  111. d = {
  112. "q": query_term,
  113. "limit": limit,
  114. "page": page,
  115. }
  116. h = {"Content-type":"application/json"}
  117. r = corelib.auth_requests("GET", "%s/user/users" \
  118. %(config.API_ADDR,), params=d, headers=h)
  119. log.debug("%s:%s" %(r.status_code, r.text))
  120. if r.status_code != 200:
  121. raise Exception("%s %s" %(r.status_code, r.text))
  122. j = r.json() or []
  123. for x in j:
  124. u = cls(x["id"], x["name"], x["cnname"], x["email"], x["phone"], x["im"], x["qq"], x["role"])
  125. users.append(u)
  126. return users
  127. #anyone can create user
  128. @classmethod
  129. def create_user(cls, name, cnname, password, email, phone="", im="", qq=""):
  130. h = {"Content-type": "application/json"}
  131. d = {
  132. "name": name, "cnname": cnname, "password": password, "email": email, "phone": phone, "im": im, "qq": qq,
  133. }
  134. r = corelib.auth_requests("POST", "%s/user/create" %(config.API_ADDR,), \
  135. data=json.dumps(d), headers=h)
  136. log.debug("%s:%s" %(r.status_code, r.text))
  137. if r.status_code != 200:
  138. raise Exception("%s %s" %(r.status_code, r.text))
  139. return r.json()
  140. @classmethod
  141. def admin_update_user_profile(cls, data={}):
  142. h = {"Content-type": "application/json"}
  143. r = corelib.auth_requests("PUT", "%s/admin/change_user_profile" %(config.API_ADDR,), \
  144. data=json.dumps(data), headers=h)
  145. log.debug("%s:%s" %(r.status_code, r.text))
  146. if r.status_code != 200:
  147. raise Exception("%s %s" %(r.status_code, r.text))
  148. return r.text
  149. @classmethod
  150. def admin_change_user_passwd(cls, user_id, password):
  151. h = {"Content-type": "application/json"}
  152. d = {
  153. "user_id": user_id, "password": password,
  154. }
  155. r = corelib.auth_requests("PUT", "%s/admin/change_user_passwd" %(config.API_ADDR,), \
  156. data=json.dumps(d), headers=h)
  157. log.debug("%s:%s" %(r.status_code, r.text))
  158. if r.status_code != 200:
  159. raise Exception("%s %s" %(r.status_code, r.text))
  160. return r.text
  161. @classmethod
  162. def admin_change_user_role(cls, user_id, admin):
  163. h = {"Content-type":"application/json"}
  164. d = {"admin": admin, "user_id": user_id}
  165. r = corelib.auth_requests("PUT", "%s/admin/change_user_role" \
  166. %(config.API_ADDR,), data=json.dumps(d), headers=h)
  167. log.debug("%s:%s" %(r.status_code, r.text))
  168. if r.status_code != 200:
  169. raise Exception("%s %s" %(r.status_code, r.text))
  170. return r.text
  171. @classmethod
  172. def admin_delete_user(cls, user_id):
  173. h = {"Content-type":"application/json"}
  174. d = {"user_id": int(user_id)}
  175. r = corelib.auth_requests("DELETE", "%s/admin/delete_user" \
  176. %(config.API_ADDR,), data=json.dumps(d), headers=h)
  177. log.debug("%s:%s" %(r.status_code, r.text))
  178. if r.status_code != 200:
  179. raise Exception("%s %s" %(r.status_code, r.text))
  180. return r.text