def __init__(self): try: School.create_table() UserType.create_table() Application.create_table() Users.create_table() ApplicationPermission.create_table() Device.create_table() OSContainer.create_table() SessionInfo.create_table() Student.create_table() Teacher.create_table() except peewee.InternalError as e: print(str(e))
def student_list(self, school_id): query = Users.select( Users.user_id, Users.user_name, Users.first_name, Users.last_name).where((Users.user_type == "student") & (Users.school_id == school_id)).dicts() response.body = json.dumps({'students': list(query)}) response.status = 200 return response
def get_userid_for_username(self, username): try: query = Users.select( Users.user_id).where(Users.user_name == username) if len(query) == 0: response.body = "Error: User does not exist" response.status = 400 return None return str(query[0].user_id) except Exception as e: print(e) response.body = "Error: User does not exist" response.status = 400 return None
def delete_user(self, user_id): try: user = Users.get(Users.user_id == user_id) user.delete_instance(recursive=True) response.body = json.dumps( {'user': '******' + user_id}) response.status = 200 except peewee.DoesNotExist as e: response.body = json.dumps({'error': 'User does not exist'}) response.status = 400 except Exception as e: response.body = json.dumps({'error': str(e)}) response.status = 500 return response
def user_info(self, username): try: join_condition = Users.school_id == School.school_id query = Users.select(Users, School.school_name).join( School, JOIN.INNER, on=join_condition).where(Users.user_name == username).dicts() response.body = json.dumps( {'user': list(query)}, default=response_format_helper.to_serializable) response.status = 200 except Exception as e: print(e) response.body = "Error: User does not exist" response.status = 400 return response
def validate_user(self, username, password): try: user = Users.select().where(Users.user_name == username) if not user: return False if bcrypt.checkpw(password.encode('utf-8'), user[0].hashed_password.encode('utf8')): return True else: return False except peewee.DoesNotExist as e: print(str(e)) return False except Exception as f: print(str(f)) return False
def user_scope(self, username): try: query = Users.select( Users.user_type).where(Users.user_name == username).dicts() if len(query) == 0: response.body = "Error: User does not exist" response.status = 400 return response response.body = json.dumps( {'user': list(query)}, default=response_format_helper.to_serializable) response.status = 200 except Exception as e: print(e) response.body = "Error: User does not exist" response.status = 400 return response
def create_new_user(self, username, password, query): try: new_user = Users() new_user.user_name = username new_user.hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) new_user.school_id = query.school_id new_user.first_name = query.first_name new_user.last_name = query.last_name new_user.user_type = query.user_type new_user.email = query.email new_user.profession = query.profession new_user.save() if query.user_type.lower() == "teacher": new_teacher = Teacher() new_teacher.user_id = new_user.user_id new_teacher.has_system_access = True new_teacher.save() response.body = json.dumps({ 'success': 'Successfully created teacher ' + new_user.user_name + ' with id ' + str(new_user.user_id), 'id': new_user.user_id }) response.status = 200 elif query.user_type.lower() == "student": new_student = Student() new_student.user_id = new_user.user_id new_student.has_system_access = False new_student.save() response.body = json.dumps({ 'success': 'Successfully created student ' + new_user.user_name + ' with id ' + str(new_user.user_id), 'id': new_user.user_id }) response.status = 200 else: response.body = json.dumps({'error': 'Unknown user type'}) response.status = 500 except Exception as e: response.body = json.dumps({'error': str(e)}) response.status = 500 return response
def setup_routes(self, user_id, client_ip, os_type, width, height): try: data = {} if user_id not in session_helper.session_info_map: source_port = session_helper.get_free_port() destination_port = "8080" container = self.get_free_container(os_type) if (container is None): response.body = json.dumps({'error': 'No free containers'}) response.status = 400 return response iptables_rules = self.build_iptable_rules_setup( client_ip, container.ip_address, source_port, destination_port) query = Users.select( Users.first_name, Users.last_name).where(Users.user_id == user_id) result = query[0] print("Name: " + result.first_name + result.last_name) session_helper.session_info_map[ user_id] = session_info.SessionInfo( client_ip, source_port, container.ip_address, destination_port, container.guacamole_stream_id, container.guacamole_view_only_id, os_type, result.first_name, result.last_name) process = subprocess.Popen(iptables_rules, stdout=subprocess.PIPE, shell=True) process.communicate()[0].strip() data['source_port'] = source_port data['guacamole_id'] = container.guacamole_stream_id data['os_type'] = os_type self.setup_user_in_container(user_id, container.ip_address, width, height) health_check_thread = threading.Thread( name='os_container_health_check', target=self.os_container_health_check, args=( container.ip_address, user_id, )) health_check_thread.start() else: if session_helper.session_info_map[user_id].os_type != os_type: container_helper.cleanup_user_session(user_id) self.delete_iptable_rules(user_id) return self.setup_routes(user_id, client_ip, os_type, width, height) else: data['source_port'] = session_helper.session_info_map[ user_id].source_port data['guacamole_id'] = session_helper.session_info_map[ user_id].guacamole_stream_id data['os_type'] = session_helper.session_info_map[ user_id].os_type response.body = json.dumps({'routes': data}) response.status = 200 except Exception as e: response.body = json.dumps({'error': str(e)}) response.status = 500 finally: return response