def grant_experiment_on_group(sessionmaker, category_name, experiment_name, group_name, time_allowed): sessionmaker._model_changes = {} # flask-sqlalchemy bug bypass session = sessionmaker() group = session.query(model.DbGroup).filter_by(name = group_name).one() experiment_allowed = permissions.EXPERIMENT_ALLOWED experiment_allowed_p1 = permissions.EXPERIMENT_PERMANENT_ID experiment_allowed_p2 = permissions.EXPERIMENT_CATEGORY_ID experiment_allowed_p3 = permissions.TIME_ALLOWED group_permission = model.DbGroupPermission( group, experiment_allowed, "%s users::%s@%s" % (group_name, experiment_name, category_name), datetime.datetime.utcnow(), "Permission for group %s users to use %s@%s" % (group_name, experiment_name, category_name)) session.add(group_permission) group_permission_p1 = model.DbGroupPermissionParameter(group_permission, experiment_allowed_p1, experiment_name) session.add(group_permission_p1) group_permission_p2 = model.DbGroupPermissionParameter(group_permission, experiment_allowed_p2, category_name) session.add(group_permission_p2) group_permission_p3 = model.DbGroupPermissionParameter(group_permission, experiment_allowed_p3, str(time_allowed)) session.add(group_permission_p3) session.commit() session.close()
def _grant_permission_to_group(session, groups_data, experiments_data, group_name, exp_name, cat_name, time): if (exp_name, cat_name) not in experiments_data: raise Exception("Error: %s@%s not previously registered" % (exp_name, cat_name)) db_group = groups_data[group_name] gp_allowed = model.DbGroupPermission( db_group, permissions.EXPERIMENT_ALLOWED, "%s::%s" % (group_name, exp_name), datetime.datetime.utcnow(), "Permission for group %s to use %s" % (group_name, exp_name) ) session.add(gp_allowed) gp_allowed_p1 = model.DbGroupPermissionParameter(gp_allowed, permissions.EXPERIMENT_PERMANENT_ID, exp_name) session.add(gp_allowed_p1) gp_allowed_p2 = model.DbGroupPermissionParameter(gp_allowed, permissions.EXPERIMENT_CATEGORY_ID, cat_name) session.add(gp_allowed_p2) gp_allowed_p3 = model.DbGroupPermissionParameter(gp_allowed, permissions.TIME_ALLOWED, six.text_type(time)) session.add(gp_allowed_p3)
def grant_on_experiment_to_group(self, group, permission_type, permanent_id, date, comments, experiment, time_allowed, priority, initialization_in_accounting): try: group_permission = Model.DbGroupPermission( group, permission_type.group_applicable, permanent_id, date, comments) self.session.add(group_permission) group_permission_p1 = Model.DbGroupPermissionParameter( group_permission, permission_type.get_parameter("experiment_permanent_id"), experiment.name) self.session.add(group_permission_p1) group_permission_p2 = Model.DbGroupPermissionParameter( group_permission, permission_type.get_parameter("experiment_category_id"), experiment.category.name) self.session.add(group_permission_p2) group_permission_p3 = Model.DbGroupPermissionParameter( group_permission, permission_type.get_parameter("time_allowed"), time_allowed) self.session.add(group_permission_p3) group_permission_p4 = Model.DbGroupPermissionParameter( group_permission, permission_type.get_parameter("priority"), priority) self.session.add(group_permission_p4) group_permission_p5 = Model.DbGroupPermissionParameter( group_permission, permission_type.get_parameter("initialization_in_accounting"), initialization_in_accounting) self.session.add(group_permission_p5) self.session.commit() return group_permission except IntegrityError: return None
def grant_on_admin_panel_to_group(self, group, permission_type, permanent_id, date, comments): try: group_permission = Model.DbGroupPermission( group, permission_type.group_applicable, permanent_id, date, comments) self.session.add(group_permission) group_permission_p1 = Model.DbGroupPermissionParameter( group_permission, permission_type.get_parameter("full_privileges"), True) self.session.add(group_permission_p1) self.session.commit() return group_permission except IntegrityError: return None
def grant_admin_panel_on_group(sessionmaker, group_name): session = sessionmaker() permission_type = permissions.ADMIN_PANEL_ACCESS group = session.query(model.DbGroup).filter_by(name = group_name).one() group_permission = model.DbGroupPermission( group, permission_type, 'Administrators:admin-panel', datetime.datetime.now(), '' ) session.add(group_permission) group_permission_p1 = model.DbGroupPermissionParameter( group_permission, permissions.FULL_PRIVILEGES, True ) session.add(group_permission_p1) session.commit() 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())