eventman_server.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. #!/usr/bin/env python
  2. """Event Man(ager)
  3. Your friendly manager of attendees at an event.
  4. Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>
  5. RaspiBO <info@raspibo.org>
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. """
  15. import os
  16. import re
  17. import glob
  18. import json
  19. import time
  20. import string
  21. import random
  22. import logging
  23. import datetime
  24. import tornado.httpserver
  25. import tornado.ioloop
  26. import tornado.options
  27. from tornado.options import define, options
  28. import tornado.web
  29. import tornado.websocket
  30. from tornado import gen, escape, process
  31. import utils
  32. import backend
  33. ENCODING = 'utf-8'
  34. PROCESS_TIMEOUT = 60
  35. API_VERSION = '1.0'
  36. re_env_key = re.compile('[^A-Z_]+')
  37. re_slashes = re.compile(r'//+')
  38. def authenticated(method):
  39. """Decorator to handle authentication."""
  40. original_wrapper = tornado.web.authenticated(method)
  41. @tornado.web.functools.wraps(method)
  42. def my_wrapper(self, *args, **kwargs):
  43. # If no authentication was required from the command line or config file.
  44. if not self.authentication:
  45. return method(self, *args, **kwargs)
  46. # unauthenticated API calls gets redirected to /v1.0/[...]
  47. if self.is_api() and not self.get_.current_user():
  48. self.redirect('/v%s%s' % (API_VERSION, self.get_login_url()))
  49. return
  50. return original_wrapper(self, *args, **kwargs)
  51. return my_wrapper
  52. class BaseHandler(tornado.web.RequestHandler):
  53. """Base class for request handlers."""
  54. permissions = {
  55. 'event|read': True,
  56. 'events|read': True,
  57. 'event:tickets|create': True,
  58. 'event:tickets|update': True,
  59. 'event:tickets|read': True,
  60. 'person|create': True
  61. }
  62. # A property to access the first value of each argument.
  63. arguments = property(lambda self: dict([(k, v[0])
  64. for k, v in self.request.arguments.iteritems()]))
  65. # A property to access both the UUID and the clean arguments.
  66. @property
  67. def uuid_arguments(self):
  68. uuid = None
  69. arguments = self.arguments
  70. if 'uuid' in arguments:
  71. uuid = arguments['uuid']
  72. del arguments['uuid']
  73. return uuid, arguments
  74. _bool_convert = {
  75. '0': False,
  76. 'n': False,
  77. 'f': False,
  78. 'no': False,
  79. 'off': False,
  80. 'false': False,
  81. '1': True,
  82. 'y': True,
  83. 't': True,
  84. 'on': True,
  85. 'yes': True,
  86. 'true': True
  87. }
  88. def is_api(self):
  89. """Return True if the path is from an API call."""
  90. return self.request.path.startswith('/v%s' % API_VERSION)
  91. def tobool(self, obj):
  92. """Convert some textual values to boolean."""
  93. if isinstance(obj, (list, tuple)):
  94. obj = obj[0]
  95. if isinstance(obj, (str, unicode)):
  96. obj = obj.lower()
  97. return self._bool_convert.get(obj, obj)
  98. def arguments_tobool(self):
  99. """Return a dictionary of arguments, converted to booleans where possible."""
  100. return dict([(k, self.tobool(v)) for k, v in self.arguments.iteritems()])
  101. def initialize(self, **kwargs):
  102. """Add every passed (key, value) as attributes of the instance."""
  103. for key, value in kwargs.iteritems():
  104. setattr(self, key, value)
  105. @property
  106. def current_user(self):
  107. """Retrieve current user from the secure cookie."""
  108. return self.get_secure_cookie("user")
  109. @property
  110. def current_user_info(self):
  111. current_user = self.current_user
  112. user_info = {'permissions': set([k for (k, v) in self.permissions.iteritems() if v is True])}
  113. if current_user:
  114. user_info['username'] = current_user
  115. res = self.db.query('users', {'username': current_user})
  116. if res:
  117. user = res[0]
  118. user_info['permissions'].update(set(user.get('permissions') or []))
  119. user_info['permissions'] = list(user_info['permissions'])
  120. return user_info
  121. def has_permission(self, permission):
  122. """Check permissions of the current user.
  123. :param permission: the permission to check
  124. :type permission: str
  125. :returns: True if the user is allowed to perform the action or False
  126. :rtype: bool
  127. """
  128. user_info = self.current_user_info or {}
  129. user_permissions = user_info.get('permissions') or []
  130. global_permission = '%s|all' % permission.split('|')[0]
  131. if 'admin|all' in user_permissions or global_permission in user_permissions or permission in user_permissions:
  132. return True
  133. collection_permission = self.permissions.get(permission)
  134. if isinstance(collection_permission, bool):
  135. return collection_permission
  136. if callable(collection_permission):
  137. return collection_permission(permission)
  138. return False
  139. def build_error(self, message='', status=400):
  140. self.set_status(status)
  141. self.write({'error': True, 'message': message})
  142. def logout(self):
  143. """Remove the secure cookie used fro authentication."""
  144. self.clear_cookie("user")
  145. class RootHandler(BaseHandler):
  146. """Handler for the / path."""
  147. angular_app_path = os.path.join(os.path.dirname(__file__), "angular_app")
  148. @gen.coroutine
  149. @authenticated
  150. def get(self, *args, **kwargs):
  151. # serve the ./angular_app/index.html file
  152. with open(self.angular_app_path + "/index.html", 'r') as fd:
  153. self.write(fd.read())
  154. # Keep track of WebSocket connections.
  155. _ws_clients = {}
  156. class CollectionHandler(BaseHandler):
  157. """Base class for handlers that need to interact with the database backend.
  158. Introduce basic CRUD operations."""
  159. # set of documents we're managing (a collection in MongoDB or a table in a SQL database)
  160. document = None
  161. collection = None
  162. # set of documents used to store incremental sequences
  163. counters_collection = 'counters'
  164. _id_chars = string.ascii_lowercase + string.digits
  165. def get_next_seq(self, seq):
  166. """Increment and return the new value of a ever-incrementing counter.
  167. :param seq: unique name of the sequence
  168. :type seq: str
  169. :return: the next value of the sequence
  170. :rtype: int
  171. """
  172. if not self.db.query(self.counters_collection, {'seq_name': seq}):
  173. self.db.add(self.counters_collection, {'seq_name': seq, 'seq': 0})
  174. merged, doc = self.db.update(self.counters_collection,
  175. {'seq_name': seq},
  176. {'seq': 1},
  177. operation='increment')
  178. return doc.get('seq', 0)
  179. def gen_id(self, seq='ids'):
  180. t = str(time.time())
  181. seq = str(self.get_next_seq(seq)).replace('.', '_')
  182. rand = ''.join([random.choice(self._id_chars) for x in xrange(32)])
  183. return '-'.join((t, seq, rand))
  184. def _filter_results(self, results, params):
  185. """Filter a list using keys and values from a dictionary.
  186. :param results: the list to be filtered
  187. :type results: list
  188. :param params: a dictionary of items that must all be present in an original list item to be included in the return
  189. :type params: dict
  190. :return: list of items that have all the keys with the same values as params
  191. :rtype: list"""
  192. if not params:
  193. return results
  194. params = backend.convert(params)
  195. filtered = []
  196. for result in results:
  197. add = True
  198. for key, value in params.iteritems():
  199. if key not in result or result[key] != value:
  200. add = False
  201. break
  202. if add:
  203. filtered.append(result)
  204. return filtered
  205. def _clean_dict(self, data):
  206. """Filter a dictionary (in place) to remove unwanted keywords.
  207. :param data: dictionary to clean
  208. :type data: dict"""
  209. if isinstance(data, dict):
  210. for key in data.keys():
  211. if isinstance(key, (str, unicode)) and key.startswith('$'):
  212. del data[key]
  213. return data
  214. def _dict2env(self, data):
  215. """Convert a dictionary into a form suitable to be passed as environment variables.
  216. :param data: dictionary to convert
  217. :type data: dict"""
  218. ret = {}
  219. for key, value in data.iteritems():
  220. if isinstance(value, (list, tuple, dict)):
  221. continue
  222. try:
  223. key = key.upper().encode('ascii', 'ignore')
  224. key = re_env_key.sub('', key)
  225. if not key:
  226. continue
  227. ret[key] = unicode(value).encode(ENCODING)
  228. except:
  229. continue
  230. return ret
  231. @gen.coroutine
  232. @authenticated
  233. def get(self, id_=None, resource=None, resource_id=None, acl=True, **kwargs):
  234. if resource:
  235. # Handle access to sub-resources.
  236. permission = '%s:%s|read' % (self.document, resource)
  237. if acl and not self.has_permission(permission):
  238. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  239. method = getattr(self, 'handle_get_%s' % resource, None)
  240. if method and callable(method):
  241. output = method(id_, resource_id, **kwargs)
  242. if output is not None:
  243. self.write(output)
  244. return
  245. return self.build_error(status=404, message='unable to access resource: %s' % resource)
  246. if id_ is not None:
  247. # read a single document
  248. permission = '%s|read' % self.document
  249. if acl and not self.has_permission(permission):
  250. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  251. self.write(self.db.get(self.collection, id_))
  252. else:
  253. # return an object containing the list of all objects in the collection;
  254. # e.g.: {'events': [{'_id': 'obj1-id, ...}, {'_id': 'obj2-id, ...}, ...]}
  255. # Please, never return JSON lists that are not encapsulated into an object,
  256. # to avoid XSS vulnerabilities.
  257. permission = '%s|read' % self.collection
  258. if acl and not self.has_permission(permission):
  259. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  260. self.write({self.collection: self.db.query(self.collection, self.arguments)})
  261. @gen.coroutine
  262. @authenticated
  263. def post(self, id_=None, resource=None, resource_id=None, **kwargs):
  264. data = escape.json_decode(self.request.body or '{}')
  265. self._clean_dict(data)
  266. method = self.request.method.lower()
  267. if resource:
  268. permission = '%s:%s|%s' % (self.document, resource, 'create' if method == 'post' else 'update')
  269. if not self.has_permission(permission):
  270. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  271. # Handle access to sub-resources.
  272. handler = getattr(self, 'handle_%s_%s' % (method, resource), None)
  273. if handler and callable(handler):
  274. self.write(handler(id_, resource_id, data, **kwargs))
  275. return
  276. if id_ is None:
  277. permission = '%s|%s' % (self.document, 'create' if method == 'post' else 'update')
  278. if not self.has_permission(permission):
  279. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  280. newData = self.db.add(self.collection, data, _id=self.gen_id())
  281. else:
  282. permission = '%s|%s' % (self.collection, 'create' if method == 'post' else 'update')
  283. if not self.has_permission(permission):
  284. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  285. merged, newData = self.db.update(self.collection, id_, data)
  286. self.write(newData)
  287. # PUT (update an existing document) is handled by the POST (create a new document) method
  288. put = post
  289. @gen.coroutine
  290. @authenticated
  291. def delete(self, id_=None, resource=None, resource_id=None, **kwargs):
  292. if resource:
  293. # Handle access to sub-resources.
  294. permission = '%s:%s|delete' % (self.document, resource)
  295. if not self.has_permission(permission):
  296. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  297. method = getattr(self, 'handle_delete_%s' % resource, None)
  298. if method and callable(method):
  299. self.write(method(id_, resource_id, **kwargs))
  300. return
  301. if id_:
  302. permission = '%s|delete' % self.document
  303. if not self.has_permission(permission):
  304. return self.build_error(status=401, message='insufficient permissions: %s' % permission)
  305. self.db.delete(self.collection, id_)
  306. self.write({'success': True})
  307. def on_timeout(self, cmd, pipe):
  308. """Kill a process that is taking too long to complete."""
  309. logging.debug('cmd %s is taking too long: killing it' % ' '.join(cmd))
  310. try:
  311. pipe.proc.kill()
  312. except:
  313. pass
  314. def on_exit(self, returncode, cmd, pipe):
  315. """Callback executed when a subprocess execution is over."""
  316. self.ioloop.remove_timeout(self.timeout)
  317. logging.debug('cmd: %s returncode: %d' % (' '.join(cmd), returncode))
  318. @gen.coroutine
  319. def run_subprocess(self, cmd, stdin_data=None, env=None):
  320. """Execute the given action.
  321. :param cmd: the command to be run with its command line arguments
  322. :type cmd: list
  323. :param stdin_data: data to be sent over stdin
  324. :type stdin_data: str
  325. :param env: environment of the process
  326. :type env: dict
  327. """
  328. self.ioloop = tornado.ioloop.IOLoop.instance()
  329. p = process.Subprocess(cmd, close_fds=True, stdin=process.Subprocess.STREAM,
  330. stdout=process.Subprocess.STREAM, stderr=process.Subprocess.STREAM, env=env)
  331. p.set_exit_callback(lambda returncode: self.on_exit(returncode, cmd, p))
  332. self.timeout = self.ioloop.add_timeout(datetime.timedelta(seconds=PROCESS_TIMEOUT),
  333. lambda: self.on_timeout(cmd, p))
  334. yield gen.Task(p.stdin.write, stdin_data or '')
  335. p.stdin.close()
  336. out, err = yield [gen.Task(p.stdout.read_until_close),
  337. gen.Task(p.stderr.read_until_close)]
  338. logging.debug('cmd: %s' % ' '.join(cmd))
  339. logging.debug('cmd stdout: %s' % out)
  340. logging.debug('cmd strerr: %s' % err)
  341. raise gen.Return((out, err))
  342. @gen.coroutine
  343. def run_triggers(self, action, stdin_data=None, env=None):
  344. """Asynchronously execute triggers for the given action.
  345. :param action: action name; scripts in directory ./data/triggers/{action}.d will be run
  346. :type action: str
  347. :param stdin_data: a python dictionary that will be serialized in JSON and sent to the process over stdin
  348. :type stdin_data: dict
  349. :param env: environment of the process
  350. :type stdin_data: dict
  351. """
  352. logging.debug('running triggers for action "%s"' % action)
  353. stdin_data = stdin_data or {}
  354. try:
  355. stdin_data = json.dumps(stdin_data)
  356. except:
  357. stdin_data = '{}'
  358. for script in glob.glob(os.path.join(self.data_dir, 'triggers', '%s.d' % action, '*')):
  359. if not (os.path.isfile(script) and os.access(script, os.X_OK)):
  360. continue
  361. out, err = yield gen.Task(self.run_subprocess, [script], stdin_data, env)
  362. def build_ws_url(self, path, proto='ws', host=None):
  363. """Return a WebSocket url from a path."""
  364. return 'ws://127.0.0.1:%s/ws/%s' % (self.listen_port + 1, path)
  365. @gen.coroutine
  366. def send_ws_message(self, path, message):
  367. """Send a WebSocket message to all the connected clients.
  368. :param path: partial path used to build the WebSocket url
  369. :type path: str
  370. :param message: message to send
  371. :type message: str
  372. """
  373. ws = yield tornado.websocket.websocket_connect(self.build_ws_url(path))
  374. ws.write_message(message)
  375. ws.close()
  376. class PersonsHandler(CollectionHandler):
  377. """Handle requests for Persons."""
  378. document = 'person'
  379. collection = 'persons'
  380. object_id = 'person_id'
  381. def handle_get_events(self, id_, resource_id=None, **kwargs):
  382. # Get a list of events attended by this person.
  383. # Inside the data of each event, a 'person_data' dictionary is
  384. # created, duplicating the entry for the current person (so that
  385. # there's no need to parse the 'persons' list on the client).
  386. #
  387. # If resource_id is given, only the specified event is considered.
  388. #
  389. # If the 'all' parameter is given, every event (also unattended ones) is returned.
  390. args = self.request.arguments
  391. query = {}
  392. if id_ and not self.tobool(args.get('all')):
  393. query = {'persons.person_id': id_}
  394. if resource_id:
  395. query['_id'] = resource_id
  396. events = self.db.query('events', query)
  397. for event in events:
  398. person_data = {}
  399. for persons in event.get('persons') or []:
  400. if str(persons.get('person_id')) == id_:
  401. person_data = persons
  402. break
  403. if 'persons' in event:
  404. del event['persons']
  405. event['person_data'] = person_data
  406. if resource_id and events:
  407. return events[0]
  408. return {'events': events}
  409. class EventsHandler(CollectionHandler):
  410. """Handle requests for Events."""
  411. document = 'event'
  412. collection = 'events'
  413. object_id = 'event_id'
  414. def _get_person_data(self, person_id_or_query, persons):
  415. """Filter a list of persons returning the first item with a given person_id
  416. or which set of keys specified in a dictionary match their respective values."""
  417. for person in persons:
  418. if isinstance(person_id_or_query, dict):
  419. if all(person.get(k) == v for k, v in person_id_or_query.iteritems()):
  420. return person
  421. else:
  422. if str(person.get('person_id')) == person_id_or_query:
  423. return person
  424. return {}
  425. def handle_get_persons(self, id_, resource_id=None):
  426. # Return every person registered at this event, or the information
  427. # about a specific person.
  428. query = {'_id': id_}
  429. event = self.db.query('events', query)[0]
  430. if resource_id:
  431. return {'person': self._get_person_data(resource_id, event.get('persons') or [])}
  432. persons = self._filter_results(event.get('persons') or [], self.arguments)
  433. return {'persons': persons}
  434. def handle_get_tickets(self, id_, resource_id=None):
  435. if resource_id is None and not self.has_permission('event:tickets|all'):
  436. return self.build_error(status=401, message='insufficient permissions: event:tickets|all')
  437. return self.handle_get_persons(id_, resource_id)
  438. def handle_post_persons(self, id_, person_id, data):
  439. # Add a person to the list of persons registered at this event.
  440. uuid, arguments = self.uuid_arguments
  441. self._clean_dict(data)
  442. data['seq'] = self.get_next_seq('event_%s_persons' % id_)
  443. data['seq_hex'] = '%06X' % data['seq']
  444. doc = self.db.query('events',
  445. {'_id': id_, 'persons.person_id': person_id})
  446. ret = {'action': 'add', 'person_id': person_id, 'person': data, 'uuid': uuid}
  447. if '_id' in data:
  448. del data['_id']
  449. self.send_ws_message('event/%s/updates' % id_, json.dumps(ret))
  450. if not doc:
  451. data['_id'] = self.gen_id()
  452. merged, doc = self.db.update('events',
  453. {'_id': id_},
  454. {'persons': data},
  455. operation='appendUnique',
  456. create=False)
  457. return ret
  458. handle_post_tickets = handle_post_persons
  459. def handle_put_persons(self, id_, person_id, data):
  460. # Update an existing entry for a person registered at this event.
  461. self._clean_dict(data)
  462. uuid, arguments = self.uuid_arguments
  463. query = dict([('persons.%s' % k, v) for k, v in arguments.iteritems()])
  464. query['_id'] = id_
  465. if person_id is not None:
  466. query['persons.person_id'] = person_id
  467. old_person_data = {}
  468. current_event = self.db.query(self.collection, query)
  469. if current_event:
  470. current_event = current_event[0]
  471. else:
  472. current_event = {}
  473. old_person_data = self._get_person_data(person_id or self.arguments,
  474. current_event.get('persons') or [])
  475. merged, doc = self.db.update('events', query,
  476. data, updateList='persons', create=False)
  477. new_person_data = self._get_person_data(person_id or self.arguments,
  478. doc.get('persons') or [])
  479. env = self._dict2env(new_person_data)
  480. if person_id is None:
  481. person_id = str(new_person_data.get('person_id'))
  482. env.update({'PERSON_ID': person_id, 'EVENT_ID': id_,
  483. 'EVENT_TITLE': doc.get('title', ''), 'WEB_USER': self.current_user,
  484. 'WEB_REMOTE_IP': self.request.remote_ip})
  485. stdin_data = {'old': old_person_data,
  486. 'new': new_person_data,
  487. 'event': doc,
  488. 'merged': merged
  489. }
  490. self.run_triggers('update_person_in_event', stdin_data=stdin_data, env=env)
  491. if old_person_data and old_person_data.get('attended') != new_person_data.get('attended'):
  492. if new_person_data.get('attended'):
  493. self.run_triggers('attends', stdin_data=stdin_data, env=env)
  494. ret = {'action': 'update', 'person_id': person_id, 'person': new_person_data, 'uuid': uuid}
  495. if old_person_data != new_person_data:
  496. self.send_ws_message('event/%s/updates' % id_, json.dumps(ret))
  497. return ret
  498. handle_put_tickets = handle_put_persons
  499. def handle_delete_persons(self, id_, person_id):
  500. # Remove a specific person from the list of persons registered at this event.
  501. uuid, arguments = self.uuid_arguments
  502. doc = self.db.query('events',
  503. {'_id': id_, 'persons.person_id': person_id})
  504. ret = {'action': 'delete', 'person_id': person_id, 'uuid': uuid}
  505. if doc:
  506. merged, doc = self.db.update('events',
  507. {'_id': id_},
  508. {'persons': {'person_id': person_id}},
  509. operation='delete',
  510. create=False)
  511. self.send_ws_message('event/%s/updates' % id_, json.dumps(ret))
  512. return ret
  513. handle_delete_tickets = handle_delete_persons
  514. class EbCSVImportPersonsHandler(BaseHandler):
  515. """Importer for CSV files exported from eventbrite."""
  516. csvRemap = {
  517. 'Nome evento': 'event_title',
  518. 'ID evento': 'event_id',
  519. 'N. codice a barre': 'ebqrcode',
  520. 'Cognome acquirente': 'surname',
  521. 'Nome acquirente': 'name',
  522. 'E-mail acquirente': 'email',
  523. 'Cognome': 'surname',
  524. 'Nome': 'name',
  525. 'E-mail': 'email',
  526. 'Indirizzo e-mail': 'email',
  527. 'Tipologia biglietto': 'ticket_kind',
  528. 'Data partecipazione': 'attending_datetime',
  529. 'Data check-in': 'checkin_datetime',
  530. 'Ordine n.': 'order_nr',
  531. 'ID ordine': 'order_nr',
  532. 'Titolo professionale': 'job_title',
  533. 'Azienda': 'company',
  534. 'Prefisso': 'name_title',
  535. 'Prefisso (Sig., Sig.ra, ecc.)': 'name_title',
  536. 'Order #': 'order_nr',
  537. 'Prefix': 'name_title',
  538. 'First Name': 'name',
  539. 'Last Name': 'surname',
  540. 'Suffix': 'name_suffix',
  541. 'Email': 'email',
  542. 'Attendee #': 'attendee_nr',
  543. 'Barcode #': 'ebqrcode',
  544. 'Company': 'company',
  545. }
  546. # Only these information are stored in the person collection.
  547. keepPersonData = ('name', 'surname', 'email', 'name_title', 'name_suffix',
  548. 'company', 'job_title')
  549. @gen.coroutine
  550. @authenticated
  551. def post(self, **kwargs):
  552. # import a CSV list of persons
  553. event_handler = EventsHandler(self.application, self.request)
  554. event_handler.db = self.db
  555. targetEvent = None
  556. try:
  557. targetEvent = self.get_body_argument('targetEvent')
  558. except:
  559. pass
  560. reply = dict(total=0, valid=0, merged=0, new_in_event=0)
  561. for fieldname, contents in self.request.files.iteritems():
  562. for content in contents:
  563. filename = content['filename']
  564. parseStats, persons = utils.csvParse(content['body'], remap=self.csvRemap)
  565. reply['total'] += parseStats['total']
  566. reply['valid'] += parseStats['valid']
  567. for person in persons:
  568. person_data = dict([(k, person[k]) for k in self.keepPersonData
  569. if k in person])
  570. merged, stored_person = self.db.update('persons',
  571. [('email', 'name', 'surname')],
  572. person_data)
  573. if merged:
  574. reply['merged'] += 1
  575. if targetEvent and stored_person:
  576. event_id = targetEvent
  577. person_id = stored_person['_id']
  578. registered_data = {
  579. 'person_id': person_id,
  580. 'attended': False,
  581. 'from_file': filename}
  582. person.update(registered_data)
  583. if not self.db.query('events',
  584. {'_id': event_id, 'persons.person_id': person_id}):
  585. event_handler.handle_post_persons(event_id, person_id, person)
  586. reply['new_in_event'] += 1
  587. self.write(reply)
  588. class SettingsHandler(BaseHandler):
  589. """Handle requests for Settings."""
  590. @gen.coroutine
  591. @authenticated
  592. def get(self, **kwds):
  593. query = self.arguments_tobool()
  594. settings = self.db.query('settings', query)
  595. self.write({'settings': settings})
  596. class InfoHandler(BaseHandler):
  597. """Handle requests for Info."""
  598. @gen.coroutine
  599. @authenticated
  600. def get(self, **kwds):
  601. info = {}
  602. user_info = self.current_user_info
  603. if user_info:
  604. info['user'] = user_info
  605. self.write({'info': info})
  606. class WebSocketEventUpdatesHandler(tornado.websocket.WebSocketHandler):
  607. """Manage websockets."""
  608. def _clean_url(self, url):
  609. return re_slashes.sub('/', url)
  610. def open(self, event_id, *args, **kwds):
  611. logging.debug('WebSocketEventUpdatesHandler.on_open event_id:%s' % event_id)
  612. _ws_clients.setdefault(self._clean_url(self.request.uri), set()).add(self)
  613. logging.debug('WebSocketEventUpdatesHandler.on_open %s clients connected' % len(_ws_clients))
  614. def on_message(self, message):
  615. logging.debug('WebSocketEventUpdatesHandler.on_message')
  616. count = 0
  617. for client in _ws_clients.get(self._clean_url(self.request.uri), []):
  618. if client == self:
  619. continue
  620. client.write_message(message)
  621. count += 1
  622. logging.debug('WebSocketEventUpdatesHandler.on_message sent message to %d clients' % count)
  623. def on_close(self):
  624. logging.debug('WebSocketEventUpdatesHandler.on_close')
  625. try:
  626. if self in _ws_clients.get(self._clean_url(self.request.uri), []):
  627. _ws_clients[self._clean_url(self.request.uri)].remove(self)
  628. except Exception as e:
  629. logging.warn('WebSocketEventUpdatesHandler.on_close error closing websocket: %s', str(e))
  630. class LoginHandler(RootHandler):
  631. """Handle user authentication requests."""
  632. re_split_salt = re.compile(r'\$(?P<salt>.+)\$(?P<hash>.+)')
  633. @gen.coroutine
  634. def get(self, **kwds):
  635. # show the login page
  636. if self.is_api():
  637. self.set_status(401)
  638. self.write({'error': True,
  639. 'message': 'authentication required'})
  640. else:
  641. with open(self.angular_app_path + "/login.html", 'r') as fd:
  642. self.write(fd.read())
  643. def _authorize(self, username, password):
  644. """Return True is this username/password is valid."""
  645. res = self.db.query('users', {'username': username})
  646. if not res:
  647. return False
  648. user = res[0]
  649. db_password = user.get('password') or ''
  650. if not db_password:
  651. return False
  652. match = self.re_split_salt.match(db_password)
  653. if not match:
  654. return False
  655. salt = match.group('salt')
  656. if utils.hash_password(password, salt=salt) == db_password:
  657. return True
  658. return False
  659. @gen.coroutine
  660. def post(self):
  661. # authenticate a user
  662. username = self.get_body_argument('username')
  663. password = self.get_body_argument('password')
  664. if self._authorize(username, password):
  665. logging.info('successful login for user %s' % username)
  666. self.set_secure_cookie("user", username)
  667. if self.is_api():
  668. self.write({'error': False, 'message': 'successful login'})
  669. else:
  670. self.redirect('/')
  671. return
  672. logging.info('login failed for user %s' % username)
  673. if self.is_api():
  674. self.set_status(401)
  675. self.write({'error': True, 'message': 'wrong username and password'})
  676. else:
  677. self.redirect('/login?failed=1')
  678. class LogoutHandler(RootHandler):
  679. """Handle user logout requests."""
  680. @gen.coroutine
  681. def get(self, **kwds):
  682. # log the user out
  683. logging.info('logout')
  684. self.logout()
  685. if self.is_api():
  686. self.redirect('/v%s/login' % API_VERSION)
  687. else:
  688. self.redirect('/login')
  689. def run():
  690. """Run the Tornado web application."""
  691. # command line arguments; can also be written in a configuration file,
  692. # specified with the --config argument.
  693. define("port", default=5242, help="run on the given port", type=int)
  694. define("address", default='', help="bind the server at the given address", type=str)
  695. define("data_dir", default=os.path.join(os.path.dirname(__file__), "data"),
  696. help="specify the directory used to store the data")
  697. define("ssl_cert", default=os.path.join(os.path.dirname(__file__), 'ssl', 'eventman_cert.pem'),
  698. help="specify the SSL certificate to use for secure connections")
  699. define("ssl_key", default=os.path.join(os.path.dirname(__file__), 'ssl', 'eventman_key.pem'),
  700. help="specify the SSL private key to use for secure connections")
  701. define("mongo_url", default=None,
  702. help="URL to MongoDB server", type=str)
  703. define("db_name", default='eventman',
  704. help="Name of the MongoDB database to use", type=str)
  705. define("authentication", default=False, help="if set to true, authentication is required")
  706. define("debug", default=False, help="run in debug mode")
  707. define("config", help="read configuration file",
  708. callback=lambda path: tornado.options.parse_config_file(path, final=False))
  709. tornado.options.parse_command_line()
  710. logger = logging.getLogger()
  711. logger.setLevel(logging.INFO)
  712. if options.debug:
  713. logger.setLevel(logging.DEBUG)
  714. # database backend connector
  715. db_connector = backend.EventManDB(url=options.mongo_url, dbName=options.db_name)
  716. init_params = dict(db=db_connector, data_dir=options.data_dir, listen_port=options.port,
  717. authentication=options.authentication, logger=logger)
  718. # If not present, we store a user 'admin' with password 'eventman' into the database.
  719. if not db_connector.query('users', {'username': 'admin'}):
  720. db_connector.add('users',
  721. {'username': 'admin', 'password': utils.hash_password('eventman'),
  722. 'permissions': ['admin|all']})
  723. # If present, use the cookie_secret stored into the database.
  724. cookie_secret = db_connector.query('settings', {'setting': 'server_cookie_secret'})
  725. if cookie_secret:
  726. cookie_secret = cookie_secret[0]['cookie_secret']
  727. else:
  728. # the salt guarantees its uniqueness
  729. cookie_secret = utils.hash_password('__COOKIE_SECRET__')
  730. db_connector.add('settings',
  731. {'setting': 'server_cookie_secret', 'cookie_secret': cookie_secret})
  732. _ws_handler = (r"/ws/+event/+(?P<event_id>[\w\d_-]+)/+updates/?", WebSocketEventUpdatesHandler)
  733. _persons_path = r"/persons/?(?P<id_>[\w\d_-]+)?/?(?P<resource>[\w\d_-]+)?/?(?P<resource_id>[\w\d_-]+)?"
  734. _events_path = r"/events/?(?P<id_>[\w\d_-]+)?/?(?P<resource>[\w\d_-]+)?/?(?P<resource_id>[\w\d_-]+)?"
  735. application = tornado.web.Application([
  736. (_persons_path, PersonsHandler, init_params),
  737. (r'/v%s%s' % (API_VERSION, _persons_path), PersonsHandler, init_params),
  738. (_events_path, EventsHandler, init_params),
  739. (r'/v%s%s' % (API_VERSION, _events_path), EventsHandler, init_params),
  740. (r"/(?:index.html)?", RootHandler, init_params),
  741. (r"/ebcsvpersons", EbCSVImportPersonsHandler, init_params),
  742. (r"/settings", SettingsHandler, init_params),
  743. (r"/info", InfoHandler, init_params),
  744. _ws_handler,
  745. (r'/login', LoginHandler, init_params),
  746. (r'/v%s/login' % API_VERSION, LoginHandler, init_params),
  747. (r'/logout', LogoutHandler),
  748. (r'/v%s/logout' % API_VERSION, LogoutHandler),
  749. (r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
  750. ],
  751. template_path=os.path.join(os.path.dirname(__file__), "templates"),
  752. static_path=os.path.join(os.path.dirname(__file__), "static"),
  753. cookie_secret='__COOKIE_SECRET__',
  754. login_url='/login',
  755. debug=options.debug)
  756. ssl_options = {}
  757. if os.path.isfile(options.ssl_key) and os.path.isfile(options.ssl_cert):
  758. ssl_options = dict(certfile=options.ssl_cert, keyfile=options.ssl_key)
  759. http_server = tornado.httpserver.HTTPServer(application, ssl_options=ssl_options or None)
  760. logger.info('Start serving on %s://%s:%d', 'https' if ssl_options else 'http',
  761. options.address if options.address else '127.0.0.1',
  762. options.port)
  763. http_server.listen(options.port, options.address)
  764. # Also listen on options.port+1 for our local ws connection.
  765. ws_application = tornado.web.Application([_ws_handler], debug=options.debug)
  766. ws_http_server = tornado.httpserver.HTTPServer(ws_application)
  767. ws_http_server.listen(options.port+1, address='127.0.0.1')
  768. logger.debug('Starting WebSocket on ws://127.0.0.1:%d', options.port+1)
  769. tornado.ioloop.IOLoop.instance().start()
  770. if __name__ == '__main__':
  771. run()