def insert_user(self, login, full_name, email, avatar, role): try: user = Model.DbUser(login, full_name, email, avatar, role) self.session.add(user) self.session.commit() return user except IntegrityError: return None
def test_model_invitations(self): # # Create objects. # group = Model.DbGroup("InvGroup") role = Model.DbRole("student") user = Model.DbUser("InvUser", "Name of Invitation User", "*****@*****.**", None, role) invitation = Model.DbInvitation(group, 5, True) invitation_accept = Model.DbAcceptedInvitation(invitation, user, True)
def _create_user(session, login, role, full_name, email, password = '******', invalid_password = None, other_auths = None): user = model.DbUser(login, full_name, email, None, role) session.add(user) weblab_db = session.query(model.DbAuth).filter_by(name = "WebLab DB").one() if not invalid_password: session.add(model.DbUserAuth(user, weblab_db, password2sha(password, 'aaaa'))) else: session.add(model.DbUserAuth(user, weblab_db, invalid_password)) for (auth_type, value) in (other_auths or ()): session.add(model.DbUserAuth(user, auth_type, value)) return user
def add_user(sessionmaker, login, password, user_name, mail, randomstuff = None, role = 'student'): sessionmaker._model_changes = {} session = sessionmaker() role = session.query(model.DbRole).filter_by(name=role).one() weblab_db = session.query(model.DbAuth).filter_by(name = "WebLab DB").one() user = model.DbUser(login, user_name, mail, None, role) session.add(user) user_auth = model.DbUserAuth(user, weblab_db, password2sha(password, randomstuff)) session.add(user_auth) session.commit() session.close()
def create_external_user(self, external_user, external_id, system, group_names): session = self.Session() try: try: auth_type = session.query( Model.DbAuthType).filter_by(name=system).one() auth = auth_type.auths[0] except (NoResultFound, KeyError): raise DbErrors.DbUserNotFoundError( "System '%s' not found in database" % system) groups = [] for group_name in group_names: try: group = session.query( Model.DbGroup).filter_by(name=group_name).one() except NoResultFound: raise DbErrors.DbUserNotFoundError( "Group '%s' not found in database" % group_name) groups.append(group) try: role = session.query(Model.DbRole).filter_by( name=external_user.role.name).one() user = Model.DbUser(external_user.login, external_user.full_name, external_user.email, role=role) user_auth = Model.DbUserAuth(user, auth, configuration=external_id) for group in groups: group.users.append(user) session.add(user) session.add(user_auth) session.commit() except Exception as e: log.log(AuthDatabaseGateway, log.level.Warning, "Couldn't create user: %s" % e) log.log_exc(AuthDatabaseGateway, log.level.Info) raise DbErrors.DatabaseError( "Couldn't create user! Contact administrator") finally: session.close()
def test_model(self): # # Create objects # auth_type = Model.DbAuthType("DB") permission_type = permissions.EXPERIMENT_ALLOWED permission_type_p1 = permissions.EXPERIMENT_PERMANENT_ID permission_type_p2 = permissions.EXPERIMENT_CATEGORY_ID permission_type_p3 = permissions.TIME_ALLOWED auth = Model.DbAuth(auth_type, "WebLab DB", 1) role = Model.DbRole("administrator") user = Model.DbUser("admin1", "Name of administrator 1", "*****@*****.**", None, role) group = Model.DbGroup("5A") user_permission = Model.DbUserPermission( user, permission_type, "student2::weblab-pld", datetime.datetime.utcnow(), "Permission for student2 to use WebLab-PLD") user_permission_p1 = Model.DbUserPermissionParameter( user_permission, permission_type_p1, "ud-pld") user_permission_p2 = Model.DbUserPermissionParameter( user_permission, permission_type_p2, "PLD experiments") user_permission_p3 = Model.DbUserPermissionParameter( user_permission, permission_type_p3, "100") group_permission = Model.DbGroupPermission( group, permission_type, "5A::weblab-dummy", datetime.datetime.utcnow(), "Permission for group 5A to use WebLab-Dummy") group_permission_p1 = Model.DbGroupPermissionParameter( group_permission, permission_type_p1, "ud-dummy") group_permission_p2 = Model.DbGroupPermissionParameter( group_permission, permission_type_p2, "Dummy experiments") group_permission_p3 = Model.DbGroupPermissionParameter( group_permission, permission_type_p3, "300") role_permission = Model.DbRolePermission( role, permission_type, "administrator::weblab-dummy", datetime.datetime.utcnow(), "Permission for administrator to use WebLab-Dummy") role_permission_p1 = Model.DbRolePermissionParameter( role_permission, permission_type_p1, "ud-dummy") role_permission_p2 = Model.DbRolePermissionParameter( role_permission, permission_type_p2, "Dummy experiments") role_permission_p3 = Model.DbRolePermissionParameter( role_permission, permission_type_p3, "300") # # Method __repr__() # variables = locals() variables.pop("self") for k in variables.keys(): repr(eval(k)) # # Method DbAuth.get_config_value() # auth.configuration = "param1=value1;param2=value2" self.assertEquals("value1", auth.get_config_value("param1")) self.assertEquals("value2", auth.get_config_value("param2")) # # Method Db(User|Group|Role)Permission.get_permission_type() # self.assertEquals(permission_type, user_permission.get_permission_type()) self.assertEquals(permission_type, group_permission.get_permission_type()) self.assertEquals(permission_type, role_permission.get_permission_type()) # # Method Db(User|Group|Role)Permission.get_parameter() # self.assertEquals( user_permission_p1, user_permission.get_parameter("experiment_permanent_id")) self.assertEquals( user_permission_p2, user_permission.get_parameter("experiment_category_id")) self.assertEquals(user_permission_p3, user_permission.get_parameter("time_allowed")) self.assertEquals( group_permission_p1, group_permission.get_parameter("experiment_permanent_id")) self.assertEquals( group_permission_p2, group_permission.get_parameter("experiment_category_id")) self.assertEquals(group_permission_p3, group_permission.get_parameter("time_allowed")) self.assertEquals( role_permission_p1, role_permission.get_parameter("experiment_permanent_id")) self.assertEquals( role_permission_p2, role_permission.get_parameter("experiment_category_id")) self.assertEquals(role_permission_p3, role_permission.get_parameter("time_allowed")) # # Method Db(User|Group|Role)PermissionParameter.get_name() # self.assertEquals("experiment_permanent_id", user_permission_p1.get_name()) self.assertEquals("experiment_category_id", user_permission_p2.get_name()) self.assertEquals("time_allowed", user_permission_p3.get_name()) self.assertEquals("experiment_permanent_id", group_permission_p1.get_name()) self.assertEquals("experiment_category_id", group_permission_p2.get_name()) self.assertEquals("time_allowed", group_permission_p3.get_name()) self.assertEquals("experiment_permanent_id", role_permission_p1.get_name()) self.assertEquals("experiment_category_id", role_permission_p2.get_name()) self.assertEquals("time_allowed", role_permission_p3.get_name()) # # Method Db(User|Group|Role)PermissionParameter.get_datatype() # self.assertEquals("string", user_permission_p1.get_datatype()) self.assertEquals("string", user_permission_p2.get_datatype()) self.assertEquals("float", user_permission_p3.get_datatype()) self.assertEquals("string", group_permission_p1.get_datatype()) self.assertEquals("string", group_permission_p2.get_datatype()) self.assertEquals("float", group_permission_p3.get_datatype()) self.assertEquals("string", role_permission_p1.get_datatype()) self.assertEquals("string", role_permission_p2.get_datatype()) self.assertEquals("float", role_permission_p3.get_datatype())
def test_model(self): # # Create objects # auth_type = Model.DbAuthType("DB") permission_type = Model.DbPermissionType( 'experiment_allowed', 'This type has a parameter which is the permanent ID (not a INT) of an Experiment. Users which have this permission will have access to the experiment defined in this parameter', user_applicable=True, group_applicable=True, role_applicable=True, ee_applicable=True) permission_type_p1 = Model.DbPermissionTypeParameter( permission_type, 'experiment_permanent_id', 'string', 'the unique name of the experiment') permission_type_p2 = Model.DbPermissionTypeParameter( permission_type, 'experiment_category_id', 'string', 'the unique name of the category of experiment') permission_type_p3 = Model.DbPermissionTypeParameter( permission_type, 'time_allowed', 'float', 'Time allowed (in seconds)') auth = Model.DbAuth(auth_type, "WebLab DB", 1) role = Model.DbRole("administrator") user = Model.DbUser("admin1", "Name of administrator 1", "*****@*****.**", None, role) ee = Model.DbExternalEntity("ee1", "Country of ee1", "Description of ee1", "*****@*****.**", "password") group = Model.DbGroup("5A") user_permission = Model.DbUserPermission( user, permission_type.group_applicable, "student2::weblab-pld", datetime.datetime.utcnow(), "Permission for student2 to use WebLab-PLD") user_permission_p1 = Model.DbUserPermissionParameter( user_permission, permission_type_p1, "ud-pld") user_permission_p2 = Model.DbUserPermissionParameter( user_permission, permission_type_p2, "PLD experiments") user_permission_p3 = Model.DbUserPermissionParameter( user_permission, permission_type_p3, "100") group_permission = Model.DbGroupPermission( group, permission_type.group_applicable, "5A::weblab-dummy", datetime.datetime.utcnow(), "Permission for group 5A to use WebLab-Dummy") group_permission_p1 = Model.DbGroupPermissionParameter( group_permission, permission_type_p1, "ud-dummy") group_permission_p2 = Model.DbGroupPermissionParameter( group_permission, permission_type_p2, "Dummy experiments") group_permission_p3 = Model.DbGroupPermissionParameter( group_permission, permission_type_p3, "300") role_permission = Model.DbRolePermission( role, permission_type.role_applicable, "administrator::weblab-dummy", datetime.datetime.utcnow(), "Permission for administrator to use WebLab-Dummy") role_permission_p1 = Model.DbRolePermissionParameter( role_permission, permission_type_p1, "ud-dummy") role_permission_p2 = Model.DbRolePermissionParameter( role_permission, permission_type_p2, "Dummy experiments") role_permission_p3 = Model.DbRolePermissionParameter( role_permission, permission_type_p3, "300") ee_permission = Model.DbExternalEntityPermission( ee, permission_type.ee_applicable, "ee1::weblab-dummy", datetime.datetime.utcnow(), "Permission for ee1 to use WebLab-Dummy") ee_permission_p1 = Model.DbExternalEntityPermissionParameter( ee_permission, permission_type_p1, "ud-dummy") ee_permission_p2 = Model.DbExternalEntityPermissionParameter( ee_permission, permission_type_p2, "Dummy experiments") ee_permission_p3 = Model.DbExternalEntityPermissionParameter( ee_permission, permission_type_p3, "300") # # Method __repr__() # variables = locals() variables.pop("self") for k in variables.keys(): repr(eval(k)) # # Method DbAuth.get_config_value() # auth.configuration = "param1=value1;param2=value2" self.assertEquals("value1", auth.get_config_value("param1")) self.assertEquals("value2", auth.get_config_value("param2")) # # Method DbPermissionType.get_parameter() # self.assertEquals( permission_type_p1, permission_type.get_parameter("experiment_permanent_id")) self.assertEquals( permission_type_p2, permission_type.get_parameter("experiment_category_id")) self.assertEquals(permission_type_p3, permission_type.get_parameter("time_allowed")) # # Method Db(User|Group|Role|ExternalEntity)Permission.get_permission_type() # self.assertEquals(permission_type, user_permission.get_permission_type()) self.assertEquals(permission_type, group_permission.get_permission_type()) self.assertEquals(permission_type, role_permission.get_permission_type()) self.assertEquals(permission_type, ee_permission.get_permission_type()) # # Method Db(User|Group|Role|ExternalEntity)Permission.get_parameter() # self.assertEquals( user_permission_p1, user_permission.get_parameter("experiment_permanent_id")) self.assertEquals( user_permission_p2, user_permission.get_parameter("experiment_category_id")) self.assertEquals(user_permission_p3, user_permission.get_parameter("time_allowed")) self.assertEquals( group_permission_p1, group_permission.get_parameter("experiment_permanent_id")) self.assertEquals( group_permission_p2, group_permission.get_parameter("experiment_category_id")) self.assertEquals(group_permission_p3, group_permission.get_parameter("time_allowed")) self.assertEquals( role_permission_p1, role_permission.get_parameter("experiment_permanent_id")) self.assertEquals( role_permission_p2, role_permission.get_parameter("experiment_category_id")) self.assertEquals(role_permission_p3, role_permission.get_parameter("time_allowed")) self.assertEquals( ee_permission_p1, ee_permission.get_parameter("experiment_permanent_id")) self.assertEquals( ee_permission_p2, ee_permission.get_parameter("experiment_category_id")) self.assertEquals(ee_permission_p3, ee_permission.get_parameter("time_allowed")) # # Method Db(User|Group|Role|ExternalEntity)PermissionParameter.get_name() # self.assertEquals("experiment_permanent_id", user_permission_p1.get_name()) self.assertEquals("experiment_category_id", user_permission_p2.get_name()) self.assertEquals("time_allowed", user_permission_p3.get_name()) self.assertEquals("experiment_permanent_id", group_permission_p1.get_name()) self.assertEquals("experiment_category_id", group_permission_p2.get_name()) self.assertEquals("time_allowed", group_permission_p3.get_name()) self.assertEquals("experiment_permanent_id", role_permission_p1.get_name()) self.assertEquals("experiment_category_id", role_permission_p2.get_name()) self.assertEquals("time_allowed", role_permission_p3.get_name()) self.assertEquals("experiment_permanent_id", ee_permission_p1.get_name()) self.assertEquals("experiment_category_id", ee_permission_p2.get_name()) self.assertEquals("time_allowed", ee_permission_p3.get_name()) # # Method Db(User|Group|Role|ExternalEntity)PermissionParameter.get_datatype() # self.assertEquals("string", user_permission_p1.get_datatype()) self.assertEquals("string", user_permission_p2.get_datatype()) self.assertEquals("float", user_permission_p3.get_datatype()) self.assertEquals("string", group_permission_p1.get_datatype()) self.assertEquals("string", group_permission_p2.get_datatype()) self.assertEquals("float", group_permission_p3.get_datatype()) self.assertEquals("string", role_permission_p1.get_datatype()) self.assertEquals("string", role_permission_p2.get_datatype()) self.assertEquals("float", role_permission_p3.get_datatype()) self.assertEquals("string", ee_permission_p1.get_datatype()) self.assertEquals("string", ee_permission_p2.get_datatype()) self.assertEquals("float", ee_permission_p3.get_datatype())