webui.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. from __future__ import print_function
  21. from bottle import route, run, debug, template, request, redirect, static_file
  22. from pwman.util.crypto import CryptoEngine
  23. import pwman.data.factory
  24. from pwman.data.tags import TagNew
  25. from pwman import parser_options, get_conf_options
  26. from pkg_resources import resource_filename
  27. import itertools
  28. templates_path = [resource_filename('pwman', 'ui/templates')]
  29. statics = [resource_filename('pwman', 'ui/templates/static')][0]
  30. AUTHENTICATED = False
  31. TAGS = None
  32. DB = None
  33. # BUG: Error: SQLite: Incorrect number of bindings supplied.
  34. # The current statement uses 2, and there are 1 supplied.
  35. # When issuing multiple times filter
  36. # WEB GUI shows multiple tags as one tag!
  37. def require_auth(fn):
  38. def check_auth(**kwargs):
  39. if AUTHENTICATED:
  40. return fn(**kwargs)
  41. else:
  42. redirect("/auth")
  43. return check_auth
  44. @route('/node/:no')
  45. @require_auth
  46. def view_node(no):
  47. global DB
  48. node = DB.getnodes([no])
  49. output = template("view.tpl", node=node[0], template_lookup=templates_path)
  50. return output
  51. def submit_node(id, request):
  52. # create new\update node based on request.params.items()
  53. redirect('/')
  54. @route('/new/', method=['GET', 'POST'])
  55. @route('/edit/:no', method=['GET', 'POST'])
  56. @require_auth
  57. def edit_node(no=None):
  58. global DB
  59. if 'POST' in request.method:
  60. submit_node(no, request)
  61. if no:
  62. node = DB.getnodes([no])[0]
  63. else:
  64. class Node(object):
  65. def __init__(self):
  66. self._id = None
  67. self.username = ''
  68. self.password = ''
  69. self.url = ''
  70. self.notes = ''
  71. self.tags = ''
  72. node = Node()
  73. output = template('edit.tpl', node=node,
  74. template_lookup=templates_path)
  75. return output
  76. @route('/forget', method=['GET', 'POST'])
  77. def forget():
  78. global AUTHENTICATED
  79. AUTHENTICATED = False
  80. enc = CryptoEngine.get()
  81. enc.forget()
  82. redirect('/auth')
  83. @route('/auth', method=['GET', 'POST'])
  84. def is_authenticated():
  85. global AUTHENTICATED
  86. crypto = CryptoEngine.get(dbver=0.5)
  87. if request.method == 'POST':
  88. key = request.POST.get('pwd', '')
  89. crypto.auth(key)
  90. AUTHENTICATED = True
  91. redirect('/')
  92. else:
  93. return template("login.tpl", template_lookup=templates_path)
  94. @route('/', method=['GET', 'POST'])
  95. @require_auth
  96. def listnodes(apply=['require_login']):
  97. global AUTHENTICATED, TAGS, DB
  98. _filter = None
  99. if 'POST' in request.method:
  100. _filter = request.POST.get('tag')
  101. if _filter:
  102. DB._filtertags = [TagNew(_filter.strip())]
  103. if _filter == 'None':
  104. DB._filtertags = []
  105. nodeids = DB.listnodes()
  106. nodes = DB.getnodes(nodeids)
  107. nodesd = [''] * len(nodes)
  108. for idx, node in enumerate(nodes):
  109. ntags = [t.strip() for t in filter(None, node.tags)]
  110. nodesd[idx] = ('@'.join((node.username, node.url)),
  111. ', '.join(ntags))
  112. if not TAGS:
  113. t = [node.tags for node in nodes]
  114. t1 = list(itertools.chain.from_iterable(t))
  115. TAGS = list(set(t1))
  116. TAGS.sort()
  117. TAGS.insert(0, 'None')
  118. html_nodes = template("main.tpl", nodes=nodes, tags=TAGS,
  119. template_lookup=[resource_filename('pwman',
  120. 'ui/templates')])
  121. return html_nodes
  122. @route('/static/<filepath:path>')
  123. def server_static(filepath):
  124. return static_file(filepath, root=statics)
  125. if __name__ == '__main__':
  126. OSX = False
  127. args = parser_options().parse_args()
  128. xselpath, dbtype = get_conf_options(args, OSX)
  129. dbver = 0.5
  130. DB = pwman.data.factory.create(dbtype, dbver)
  131. DB.open(dbver=0.5)
  132. crypto = CryptoEngine.get(dbver=0.5)
  133. debug(True)
  134. run(reloader=True, port=9030)