1
0

user.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 flask import request, g, abort, render_template
  17. from rrd import app
  18. from rrd.model.user import User
  19. from rrd.utils.logger import logging
  20. log = logging.getLogger(__file__)
  21. @app.route("/user/about/<int:user_id>", methods=["GET",])
  22. def user_info(user_id):
  23. if request.method == "GET":
  24. user = User.get_by_id(user_id)
  25. return render_template("user/about.html", **locals())
  26. @app.route("/user/about/<user_name>", methods=["GET",])
  27. def user_info_by_name(user_name):
  28. if request.method == "GET":
  29. user = User.get_by_name(user_name)
  30. return render_template("user/about.html", **locals())
  31. @app.route("/user/profile", methods=["GET", "POST"])
  32. def user_profile():
  33. if request.method == "GET":
  34. current_user = g.user
  35. return render_template("user/profile.html", **locals())
  36. if request.method == "POST":
  37. ret = {"msg":""}
  38. cnname = request.form.get("cnname", "").strip()
  39. email = request.form.get("email", "").strip()
  40. im = request.form.get("im", "").strip()
  41. phone = request.form.get("phone", "").strip()
  42. qq = request.form.get("qq", "").strip()
  43. d = {
  44. "cnname": cnname,
  45. "email": email,
  46. "im": im,
  47. "phone": phone,
  48. "qq": qq,
  49. }
  50. try:
  51. User.update_user_profile(d)
  52. except Exception as e:
  53. ret['msg'] = str(e)
  54. return json.dumps(ret)
  55. @app.route("/user/chpwd", methods=["POST", ])
  56. def user_change_passwd():
  57. if request.method == "POST":
  58. ret = {"msg": ""}
  59. old_password = request.form.get("old_password", "")
  60. new_password = request.form.get("new_password", "")
  61. repeat_password = request.form.get("repeat_password", "")
  62. if not (old_password and new_password and repeat_password):
  63. ret["msg"] = "some form item missing"
  64. return json.dumps(ret)
  65. if new_password != repeat_password:
  66. ret["msg"] = "repeat and new password not equal"
  67. return json.dumps(ret)
  68. try:
  69. User.change_user_passwd(old_password, new_password)
  70. except Exception as e:
  71. ret['msg'] = str(e)
  72. return json.dumps(ret)
  73. @app.route("/user/list", methods=["GET",])
  74. def user_list():
  75. if request.method == "GET":
  76. query_term = request.args.get("query", "")
  77. limit = g.limit or 20
  78. page = g.page or 1
  79. users = User.get_users(query_term, limit, page)
  80. return render_template("user/list.html", **locals())
  81. @app.route("/user/query", methods=["GET",])
  82. def user_query():
  83. if request.method == "GET":
  84. query_term = request.args.get("query", "")
  85. limit = g.limit or 20
  86. page = g.page or 1
  87. ret = {"users":[], "msg":""}
  88. try:
  89. users = User.get_users(query_term, limit, page)
  90. ret['users'] = [u.dict() for u in users]
  91. except Exception as e:
  92. ret['msg'] = str(e)
  93. logging.error(str(e))
  94. return json.dumps(ret)
  95. #anyone can create a new user
  96. @app.route("/user/create", methods=["GET", "POST"])
  97. def user_create():
  98. if request.method == "GET":
  99. return render_template("user/create.html", **locals())
  100. if request.method == "POST":
  101. ret = {"msg":""}
  102. name = request.form.get("name", "").strip()
  103. cnname = request.form.get("cnname", "").strip()
  104. password = request.form.get("password", "")
  105. email = request.form.get("email", "").strip()
  106. phone = request.form.get("phone", "").strip()
  107. im = request.form.get("im", "").strip()
  108. qq = request.form.get("qq", "").strip()
  109. if not name or not cnname or not password or not email:
  110. ret["msg"] = "not all form item entered"
  111. return json.dumps(ret)
  112. try:
  113. User.create_user(name, cnname, password, email, phone, im, qq)
  114. except Exception as e:
  115. ret['msg'] = str(e)
  116. return json.dumps(ret)
  117. ##admin
  118. @app.route("/admin/user/<int:user_id>/edit", methods=["GET", "POST"])
  119. def admin_user_edit(user_id):
  120. if request.method == "GET":
  121. if not (g.user.is_admin() or g.user.is_root()):
  122. abort(403, "no such privilege")
  123. user = User.get_by_id(user_id)
  124. if not user:
  125. abort(404, "no such user where id=%s" % user_id)
  126. return render_template("user/edit.html", **locals())
  127. if request.method == "POST":
  128. ret = {"msg":""}
  129. if not (g.user.is_admin() or g.user.is_root()):
  130. ret["msg"] = "no such privilege"
  131. return json.dumps(ret)
  132. user_id = request.form.get("id", "")
  133. cnname = request.form.get("cnname", "").strip()
  134. email = request.form.get("email", "").strip()
  135. phone = request.form.get("phone", "").strip()
  136. im = request.form.get("im", "").strip()
  137. qq = request.form.get("qq", "").strip()
  138. d = {
  139. "user_id": int(user_id), "cnname": cnname, "email": email, "phone": phone, "im": im, "qq": qq,
  140. }
  141. try:
  142. User.admin_update_user_profile(d)
  143. except Exception as e:
  144. ret['msg'] = str(e)
  145. return json.dumps(ret)
  146. @app.route("/admin/user/<int:user_id>/chpwd", methods=["POST", ])
  147. def admin_user_change_password(user_id):
  148. if request.method == "POST":
  149. ret = {"msg": ""}
  150. if not (g.user.is_admin or g.user.is_root()):
  151. ret["msg"] = "you do not have permissions"
  152. return json.dumps(ret)
  153. password = request.form.get("password")
  154. if not password:
  155. ret["msg"] = "no password entered"
  156. return json.dumps(ret)
  157. try:
  158. User.admin_change_user_passwd(user_id, password)
  159. except Exception as e:
  160. ret['msg'] = str(e)
  161. return json.dumps(ret)
  162. @app.route("/admin/user/<int:user_id>/role", methods=["POST", ])
  163. def admin_user_change_role(user_id):
  164. if request.method == "POST":
  165. ret = {"msg": ""}
  166. if not (g.user.is_admin or g.user.is_root()):
  167. ret["msg"] = "you do not have permissions"
  168. return json.dumps(ret)
  169. role = str(request.form.get("role", ""))
  170. if not role or role not in ['1', '0']:
  171. ret["msg"] = "invalid role"
  172. return json.dumps(ret)
  173. admin = "yes" if role == '1' else "no"
  174. try:
  175. User.admin_change_user_role(user_id, admin)
  176. except Exception as e:
  177. ret['msg'] = str(e)
  178. return json.dumps(ret)
  179. @app.route("/admin/user/<int:user_id>/delete", methods=["POST", ])
  180. def admin_user_delete(user_id):
  181. if request.method == "POST":
  182. ret = {"msg": ""}
  183. if not (g.user.is_admin or g.user.is_root()):
  184. ret["msg"] = "you do not have permissions"
  185. return json.dumps(ret)
  186. try:
  187. User.admin_delete_user(user_id)
  188. except Exception as e:
  189. ret['msg'] = str(e)
  190. return json.dumps(ret)