def get(self): notifications = Notifications.query.all() schema = NotificationSchema(many=True) result = schema.dump(notifications) if result.errors: return {"success": False, "errors": result.errors}, 400 return {"success": True, "data": result.data}
def get(self): notifications = Notifications.query.all() schema = NotificationSchema(many=True) result = schema.dump(notifications) if result.errors: return {'success': False, 'errors': result.errors}, 400 return {'success': True, 'data': result.data}
def get(self, notification_id): notif = Notifications.query.filter_by( id=notification_id).first_or_404() schema = NotificationSchema() response = schema.dump(notif) if response.errors: return {"success": False, "errors": response.errors}, 400 return {"success": True, "data": response.data}
def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Notifications, query=q, field=field) notifications = (Notifications.query.filter_by(**query_args).filter( *filters).all()) schema = NotificationSchema(many=True) result = schema.dump(notifications) if result.errors: return {"success": False, "errors": result.errors}, 400 return {"success": True, "data": result.data}
def post(self): req = request.get_json() schema = NotificationSchema() result = schema.load(req) if result.errors: return {'success': False, 'errors': result.errors}, 400 db.session.add(result.data) db.session.commit() response = schema.dump(result.data) socketio.emit('notification', response.data, broadcast=True) return {'success': True, 'data': response.data}
def post(self): req = request.get_json() schema = NotificationSchema() result = schema.load(req) if result.errors: return {"success": False, "errors": result.errors}, 400 db.session.add(result.data) db.session.commit() response = schema.dump(result.data) current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data}
def send_notification(data, type, sound=True): data["sound"] = sound data["type"] = type schema = NotificationSchema() result = schema.load(data) if result.errors: return {"success": False, "errors": result.errors}, 400 db.session.add(result.data) db.session.commit() response = schema.dump(result.data) response.data["type"] = type response.data["sound"] = sound print(response.data) current_app.events_manager.publish(data=response.data, type="notification") print("Success")
def post(self): req = request.get_json() schema = NotificationSchema() result = schema.load(req) if result.errors: return {"success": False, "errors": result.errors}, 400 db.session.add(result.data) db.session.commit() response = schema.dump(result.data) users = Users.query.all() for user in users: email.sendmail(user.email, response.data['content']) current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data}
def post(self): req = request.get_json() schema = NotificationSchema() result = schema.load(req) if result.errors: return {"success": False, "errors": result.errors}, 400 db.session.add(result.data) db.session.commit() response = schema.dump(result.data) # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data}
def post(self): req = request.get_json() schema = NotificationSchema() result = schema.load(req) if result.errors: return {"success": False, "errors": result.errors}, 400 response = schema.dump(result.data) print(response) # Custom LR bar = '================\n' if ";" in response.data['content']: split_data = response.data['content'].split(";") for i in range(len(split_data)): if "=" in split_data[i].lower(): print( '\n{}WARNING! Usering Equal Sign instead of Colon!\n'. format(bar)) split_data[i].replace('=', ':') if "id" in split_data[i].lower(): response.data['id'] = split_data[i].replace('id:', '') elif "user" in split_data[i].lower(): response.data['user'] = split_data[i].replace('user:'******'') elif "ip" in split_data[i].lower(): response.data['ip'] = split_data[i].replace('ip:', '') elif "password" in split_data[i].lower(): response.data['password'] = split_data[i].replace( 'password:'******'') # If ip is supplied, password does too run the add VMs with existing if 'ip' in response.data: new_assoc = Association(user_id=response.data['id'], \ name=response.data['user'], ip=response.data['ip'], \ password=response.data['password']) else: new_assoc = Association(user_id=response.data['id'], name=response.data['user']) db.session.add(new_assoc) db.session.commit() return {"success": True} elif "#delete_association" in response.data['content'] or \ "#da" in response.data['content']: if "=" in response.data['content']: response.data['content'] = response.data['content'].replace( '=', ':') target_id = response.data['content'] target_id = target_id.replace('#da:', '') print('Target: {} - '.format(target_id, type(target_id))) db.session.execute( 'DELETE from association WHERE id={}'.format(target_id)) db.session.commit() # publish mods response.data['content'] += f'{target_id} Deleted\n' db.session.add(response.data) db.session.commit() # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#assign_association" in response.data['content'] or \ "#aa" in response.data['content']: if "=" in response.data['content']: response.data['content'] = response.data['content'].replace( '=', ':') target_id = response.data['content'].replace('#aa:', '') current_users = db.session.query(Users.id, Users.name, Users.email).all() can_continue = True for user in current_users: if target_id == user[2]: print(f'({user[0]}), {user[1]}: {user[2]}') #response.data['content'] += f'{user[0]},{user[1]}' can_continue = True else: can_continue = False # Get Current List of Assoc assoc_table = '' for current_ids in db.session.query(Association).all(): print(current_ids.user_id) assoc_table += f'{current_ids.user_id}, ' # Sift through current registered users if not in assoc_table add current_assoc = db.session.query(Association).all() if str(user[0]) not in assoc_table: # Check for available VMs for assoc in current_assoc: if assoc.user_name == None and assoc.ip != None: response.data[ 'content'] += f' > {assoc.ip} : {assoc.password} \n' #response.data['content'] += f'{user[1]},{user[0]} > {assoc.ip} : {assoc.password} \n' db.session.query(Association).filter_by( id=assoc.id).update({ 'user_name': user[1], 'user_id': user[0], 'email': user[2] }) db.session.commit() break else: print('Already has a VM!') user_inf = db.session.query(Association).filter_by( id=str(user[0])) for inf in user_inf: print(f'user_inf: {inf.ip} {inf.password}') response.data[ 'content'] += f' > (Already has VM)\nhttps://{inf.ip}.logwars.logrhythm.com\nUsername: {inf.user_name}\nPassword: {inf.password} \n' db.session.add(result.data) db.session.commit() # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#show_associations" in response.data['content'] or \ "#sa" in response.data['content']: print("\nCurrent Associations\n{}".format(bar)) print(f'response.data["content"] = {type(response.data)}') assoc_val = response.data['content'] for assoc in db.session.query(Association).all(): assoc_val = f'\nID: {assoc.id}, Name: {assoc.name}, Email: {assoc.email}, IP: {assoc.ip}, Password: {assoc.password}, UserId: {assoc.user_id}, UserName: {assoc.user_name}' response.data['content'] += f'{assoc_val}\n' # assoc_val = "empty" print(assoc_val) #return {"success": True} db.session.add(result.data) db.session.commit() # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#show_users" in response.data['content'] or \ "#su" in response.data['content']: current_users = db.session.query(Users.id, Users.name).all() print('\nRegistered Users:\n{}'.format(bar)) for user in current_users: print("({}) {}".format(user[0], user[1])) response.data['content'] += f'{user[0]},{user[1]}' # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#showassociationcolumns" in response.data['content']: result = db.session.execute('SHOW COLUMNS FROM association;') for r in result: print('Column: {}'.format(r)) elif "#ca" in response.data['content'] or \ "#checkAssociation" in response.data['content']: print(' - Checking for new Registered Users') current_users = db.session.query(Users.id, Users.name).all() # Get Current List of Assoc assoc_table = '' for current_ids in db.session.query(Association).all(): print(current_ids.user_id) assoc_table += f'{current_ids.user_id}, ' # Sift through current registered users if not in assoc_table add current_assoc = db.session.query(Association).all() for user in current_users: if str(user[0]) not in assoc_table: # Check for available VMs for assoc in current_assoc: if assoc.user_name == None and assoc.ip != None: response.data[ 'content'] += f'{user[1]},{user[0]} \n' db.session.query(Association).filter_by( id=assoc.id).update({ 'user_name': user[1], 'user_id': user[0] }) db.session.commit() break db.session.add(result.data) db.session.commit() # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound return {"success": True, "data": response.data} elif "#vm:" in response.data['content']: print(' - Uploading new VMs') existing_get_vms = False vm_list = response.data['content'].replace('#vm:', '').split("&") for current_vm in vm_list: print('Current VM: {}'.format(current_vm)) # TODO : Time constraints this is ugly and needs recoding but works # Check for existing Users and assign first 20 users per VM vms_allowed = 11 v_reset = vms_allowed print(f'allow : {vms_allowed}\nreset: {v_reset}') vm_used = 1 current_name = str(vm_used) found_users = False current_assoc_table = db.session.query(Association).all() for assoc in current_assoc_table: found_users = True if vm_used >= vms_allowed: print( 'Used up 10 VMs for registered users. ({})'.format( str(vm_used))) # Grab user if none register, update index for VM to stay 20 Range if assoc.name is not None and assoc.ip is None: print('assoc before: {}'.format(str(vm_used).zfill(2))) user_id = 'User{}'.format(str(vm_used).zfill(2)) db.session.query(Association).filter_by(id=assoc.id).\ update({'ip':current_vm, 'password':'******'.format(user_id.capitalize().replace('User', 'U')), 'name':user_id}) #update({'ip':vm_list[vm], 'password':gen_pass}) db.session.commit() vm_used += 1 print('assoc After: {}'.format(str(vm_used).zfill(2))) # If Used for registered users make available print('VMs Currently Used: {}'.format(str(vm_used).zfill(2))) if found_users: vms_allowed = vms_allowed - vm_used current_name = str(vms_allowed - 1).zfill(2) print( 'After Assoc VMs Currently Used: {}\nAllowed: {}\ncurrent_name:{}' .format( str(vm_used).zfill(2), vms_allowed, current_name)) else: current_name = str(vms_allowed).zfill(2) for no_user in range(vms_allowed): if vm_used < vms_allowed: user_id = 'User{}'.format(str(vm_used).zfill(2)) db.session.add( Association(ip=current_vm, password='******'.format( user_id.capitalize().replace( 'User', 'U')), name=user_id)) db.session.commit() vm_used += 1 current_name = int(current_name) + 1 # reset values for next VM vms_allowed = v_reset vm_used = 1 current_name = str(vm_used) db.session.add(result.data) db.session.commit() # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#pa" in response.data['content']: print( ' - Updating Association Table > MariaDB Syntax will not work in Debug' ) db.session.execute('DROP TABLE IF EXISTS association;') db.session.commit() new_table_qry = 'CREATE TABLE association (' \ 'id INTEGER NOT NULL AUTO_INCREMENT,user_id INTEGER,name VARCHAR(128),user_name VARCHAR(128),email VARCHAR(128),' \ 'ip VARCHAR(128),password VARCHAR(128),PRIMARY KEY (id),UNIQUE (id),UNIQUE (email))' db.session.execute(new_table_qry) db.session.commit() # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#tp" in response.data['content']: print('Generated Password: {}'.format(self.gen_password())) # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} elif "#testCreateUser:"******"type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound #current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data} else: db.session.add(result.data) db.session.commit() response = schema.dump(result.data) # Grab additional settings notif_type = req.get("type", "alert") notif_sound = req.get("sound", True) response.data["type"] = notif_type response.data["sound"] = notif_sound current_app.events_manager.publish(data=response.data, type="notification") return {"success": True, "data": response.data}