def tower(tower_id, decorator=None): try: towers.garbage_collection(tower_id) tower = towers[tower_id] except KeyError: log('Bad tower_id') abort(404) # Generate a jwt token to pass user_id # This allows us to pass user_id securely through the client html, # which in turn allows backup servers to get the current user without cross-domain cookies user_token = '' if current_user.is_anonymous \ else jwt.encode({'id': current_user.get_id()}, app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8') # Pass in both the tower and the user_name return render_template('ringing_room.html', tower = tower, user_name = '' if current_user.is_anonymous else current_user.username, user_email = '' if current_user.is_anonymous else current_user.email, server_ip=get_server_ip(tower_id), user_token = user_token, host_permissions = current_user.check_permissions(tower_id,'host')\ if current_user.is_authenticated else False, listen_link = False)
def tower(tower_id, decorator=None): try: towers.garbage_collection(tower_id) tower = towers[tower_id] except KeyError: log('Bad tower_id') abort(404) # Make sure the Bearer token for the current user is not expired and pass it to the client html # This is how the client will be automatically logged in w/o cross-domain cookies user_token = '' if current_user.is_anonymous\ else current_user.get_token() # Pass in both the tower and the user_name return render_template('ringing_room.html', tower = tower, user_id = 0 if current_user.is_anonymous else current_user.id, user_name = '' if current_user.is_anonymous else current_user.username, user_email = '' if current_user.is_anonymous else current_user.email, user_badge = '' if current_user.is_anonymous else current_user.badge, user_settings = Config.DEFAULT_SETTINGS if current_user.is_anonymous else current_user.get_settings_with_defaults(), server_ip=get_server_ip(tower_id), user_token = user_token, host_permissions = current_user.check_permissions(tower_id,'host')\ if current_user.is_authenticated else False, listen_link = False)
def remove_hosts(tower_id): if not current_user.check_permissions(tower_id, 'creator'): return error_response(403) data = request.get_json() or {} tower = TowerDB.query.get_or_404(tower_id) users = [User.query.filter_by(email=u).first() for u in data['hosts']] for u in users: if u and u.id != current_user.id: u.remove_host(tower) response = jsonify(tower.to_dict()) response.status_code = 200 return response
def change_tower_settings(tower_id): if not current_user.check_permissions(tower_id, 'creator'): return error_response(403) data = request.get_json() or {} tower = TowerDB.query.get_or_404(tower_id) new_name = data.get('tower_name') new_permit_host = data.get('permit_host_mode') if new_name and new_name != tower.tower_name: tower.tower_name = new_name if new_permit_host and new_permit_host != tower.permit_host_mode: tower.permit_host_mode = new_permit_host db.session.commit() response = jsonify(tower.to_dict()) response.status_code = 200 return response
def delete_tower(tower_id): tower_db = TowerDB.query.get_or_404(tower_id) if not current_user.check_permissions(tower_id, 'creator'): return error_response(403) # First, delete all relations rels = UserTowerRelation.query.filter_by(tower_id=tower_id).all() for r in rels: db.session.delete(r) # Next, delete the tower_db object db.session.delete(tower_db) db.session.commit() # Finally, delete the in-memory tower (if it exists) try: del towers[tower_id] except KeyError: pass # Respond payload = {'deleted_tower_id': tower_id} response = jsonify(payload) response.status_code = 202 return response
def get_tower_settings(tower_id): tower = TowerDB.query.get_or_404(tower_id) if not current_user.check_permissions(tower_id, 'creator'): return error_response(403) return jsonify(tower.to_dict())