def __pre_conditions_user_role(self, user_id, role_id): user = self.session.query(User).get(user_id) if not user: raise TransactionDBException(("The user doesn't exist")) role = self.session.query(Role).get(role_id) if not role: raise TransactionDBException(("The role doesn't exist"))
def revoke_user_role(self, user_id: int, role_id: str): """Revoke a role from a user. Parameters: ----------- user_id: the numeric user ID returned by the database from a user which already exists in the Users table. role_id: the role identifier (string) which should exist in the roles table already. TransactionDBException will be thrown if the user-role assignment didn't exist in the first place. """ try: user_role = self.session.query(UserRole).filter_by(user_id=user_id)\ .filter_by(role_id=role_id)\ .first() if not user_role: raise TransactionDBException(("The role wasn't assigned" " to this user.")) self.session.delete(user_role) self.session.commit() finally: self.session.rollback()
def add_user_role(self, user_id: int, role_id: str): """ Assign a role to a user. Parameters: ----------- user_id: the numeric user ID returned by the database from a user which already exists in the Users table. role_id: the role identifier (string) which should exist in the roles table already. TransactionDBException will be thrown if the user-role assignment already exists. Other exceptions will be thrown by the database if the user doesn't exist or the role doesn't exist. """ try: user_role = self.session.query(UserRole).filter_by(user_id=user_id)\ .filter_by(role_id=role_id)\ .first() if user_role: raise TransactionDBException(("The role is already assigned" " to this user.")) self.__pre_conditions_user_role(user_id, role_id) user_role = UserRole() user_role.role_id = role_id user_role.user_id = user_id self.session.add(user_role) self.session.commit() finally: self.session.rollback()
def _get_transaction_or_raise_exception(self, id_: int): t = self.session.query(Transaction).get(id_) if t: return t else: raise TransactionDBException(""" transaction doesn't exist in DB (%s) """ % id)
def remove_user(self, user_id: int): """Remove a user from the database""" try: user = self.session.query(User).get(user_id) if not user: raise TransactionDBException("The user doesn't exist") self.session.delete(user) self.session.commit() finally: self.session.rollback()
def add_role(self, role_id: str, role_description: str, permissions: int): """For multi-tenant transaction DBs, where users have certain roles, add a new role in the database. TransactionDBException will be thrown if the role already exists.""" try: role = self.session.query(Role).filter_by(role_id=role_id).first() if role: raise TransactionDBException(("The role already exists")) role = Role() role.role_id = role_id role.description = role_description role.permissions = permissions self.session.add(role) self.session.commit() finally: self.session.rollback()
def add_study_metadata(self, study_id: str, origin: str, c_move_time: datetime, overwrite: bool=False): """Add metadata associated with a study sent to mdbrain. (mainly used by auto_pull systems). Throws TransactionDBException if metadata for this study was already added before.""" try: md = self.session.query(StudiesMetadata)\ .filter_by(study_id=study_id).first() if md and not overwrite: raise TransactionDBException(( "Study was already sent to mdbrain. ")) if not md: md = StudiesMetadata() md.study_id = study_id md.origin = origin md.c_move_time = c_move_time self.session.add(md) self.session.commit() finally: self.session.rollback()
def set_user_preferences(self, user_id: int, preferences: dict): """Change or set for the first time the preferences of a user in a multi-tenant environment""" try: user_prefs = self.session.query(UserPreferences).get(user_id) if not user_prefs: user_prefs = UserPreferences() user_prefs.user_id = user_id self.session.add(user_prefs) try: for key, value in preferences.items(): if key == 'user_id': continue setattr(user_prefs, key, value) except Exception: raise TransactionDBException("Invalid user preference key {}" .format(key)) self.session.commit() finally: self.session.rollback()
def add_user(self, name, password): """For multi-tenant transaction DBs, add a new user to it. The provided (clear) password will be hashed in the database. Returns the user ID set by the database upon successful insert. TransactionDBException will be thrown if a user with the same name already exists.""" try: user = self.session.query(User).filter_by(name=name).first() if user: raise TransactionDBException(("A user with the same " "name already exists")) user = User() user.name = name user.hashed_password = user.password_hash(password) self.session.add(user) self.session.commit() user = self.session.query(User).filter_by(name=name).first() return user.id finally: self.session.rollback()
def create_transaction( self, t: Transaction, user_id=None, product_id=None, analysis_type=None, qa_score=None, processing_state='waiting', task_state='queued') -> int: """will set the provided transaction object as queued, add it to the DB and return the transaction id. If the transaction has a last_message JSON with chosen T1/T2, it will index the sequence names as well. Parameters ---------- user_id: int product_id: int """ try: if task_state == 'failed': t.task_state = TaskState.failed else: t.task_state = TaskState.queued t.processing_state = processing_state if not t.creation_date: t.creation_date = datetime.datetime.utcnow() if product_id: t.product_id = product_id if analysis_type: t.analysis_type = analysis_type if qa_score: t.qa_score = qa_score self.session.add(t) # when we commit, we get the transaction ID self.session.commit() if user_id: user = self.session.query(User).get(user_id) if not user: raise TransactionDBException(("The provided user doesn't " "exist")) ut = UserTransaction() ut.user_id = user_id ut.transaction_id = t.transaction_id self.session.add(ut) self.session.commit() # set the transaction id in the task object if t.last_message: try: lm = json.loads(t.last_message) lm['t_id'] = t.transaction_id t.last_message = json.dumps(lm) except Exception: pass # index.set_index_institution(t) index.set_index_sequences(t) self.session.commit() return t.transaction_id except Exception: self.session.rollback() try: if(t.transaction_id): self.session.delete(t) except: pass raise