use of com.infiniteautomation.mango.spring.service.RoleService in project ma-core-public by infiniteautomation.
the class AbstractRoleBasedTest method setupRoles.
protected void setupRoles() {
roleService = Common.getBean(RoleService.class);
// Add some roles
RoleVO temp = new RoleVO(Common.NEW_ID, "read-role", "Role to allow reading.");
roleService.insert(temp);
readRole = new Role(temp);
temp = new RoleVO(Common.NEW_ID, "edit-role", "Role to allow editing.");
roleService.insert(temp);
editRole = new Role(temp);
temp = new RoleVO(Common.NEW_ID, "set-role", "Role to allow setting.");
roleService.insert(temp);
setRole = new Role(temp);
temp = new RoleVO(Common.NEW_ID, "delete-role", "Role to allow deleting.");
roleService.insert(temp);
deleteRole = new Role(temp);
readUser = createUser("readUser", "readUser", "password", "readUser@example.com", readRole);
editUser = createUser("editUser", "editUser", "password", "editUser@example.com", editRole);
setUser = createUser("setUser", "setUser", "password", "setUser@example.com", setRole);
deleteUser = createUser("deleteUser", "deleteUser", "password", "deleteUser@example.com", deleteRole);
allUser = createUser("allUser", "allUser", "password", "allUser@example.com", readRole, editRole, setRole, deleteRole);
}
use of com.infiniteautomation.mango.spring.service.RoleService in project ma-core-public by infiniteautomation.
the class MangoTestBase method createRole.
/**
* Create a role with inherited roles (
*/
protected RoleVO createRole(String xid, String name, Role... inherited) {
RoleService service = Common.getBean(RoleService.class);
RoleVO role = new RoleVO(Common.NEW_ID, xid, name, new HashSet<>(Arrays.asList(inherited)));
return service.insert(role);
}
use of com.infiniteautomation.mango.spring.service.RoleService in project ma-core-public by infiniteautomation.
the class DataSourceDaoDeadlockDetection method detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesExplicit.
@Test
public void detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesExplicit() {
// This will create 2x threads for each operating as one of the desired problem scenarios
int numThreads = 5;
int numDataSources = 10;
AtomicInteger running = new AtomicInteger(numThreads * 2);
PermissionService permissionService = Common.getBean(PermissionService.class);
// Insert some roles
int roleCount = 0;
RoleService roleService = Common.getBean(RoleService.class);
List<RoleVO> roleVOs = new ArrayList<>();
Set<Role> roles = new HashSet<>();
for (int i = 0; i < roleCount; i++) {
RoleVO role = new RoleVO(Common.NEW_ID, Common.generateXid("ROLE_"), "Role " + i);
roleVOs.add(role);
roleService.insert(role);
roles.add(role.getRole());
}
DataSource dataSource = Common.getBean(DatabaseProxy.class).getDataSource();
JdbcConnectionPool pool = (JdbcConnectionPool) dataSource;
pool.setMaxConnections(numThreads * 2);
PlatformTransactionManager transactionManager = Common.getBean(DatabaseProxy.class).getTransactionManager();
AtomicInteger successes = new AtomicInteger();
AtomicInteger failures = new AtomicInteger();
MutableObject<Exception> failure = new MutableObject<>(null);
for (int i = 0; i < numThreads; i++) {
// #5 lock eventHandlerMappings and roleMappings and then try to lock dataSources
// Basically delete a data source
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < numDataSources; i++) {
// Insert an event handler
EventHandlerService eventHandlerService = Common.getBean(EventHandlerService.class);
ProcessEventHandlerVO eh = new ProcessEventHandlerVO();
eh.setDefinition(new ProcessEventHandlerDefinition());
eh.setName(Common.generateXid("Handler "));
eh.setActiveProcessCommand("ls");
eventHandlerService.insert(eh);
ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(dataSource);
// Get event handler
AbstractEventHandlerVO myEventHandler = eventHandlerService.get(eh.getXid());
// Create data source
MockDataSourceVO ds = new MockDataSourceVO();
ds.setName(Common.generateXid("Mock "));
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
dataSourceService.insert(ds);
// Insert a mapping
myEventHandler.setEventTypes(Collections.singletonList(new EventTypeMatcher(new DataSourceEventType(ds.getId(), ds.getPollAbortedExceptionEventId()))));
eventHandlerService.update(eh.getXid(), myEventHandler);
new TransactionTemplate(transactionManager).execute((status) -> {
// The order of these statements matters for deadlock, we must always lock groups of tables in the same order
ejt.update("DELETE FROM dataSources WHERE id=?", new Object[] { ds.getId() });
ejt.update("DELETE FROM eventHandlersMapping WHERE eventTypeName=? AND eventTypeRef1=?", new Object[] { EventTypeNames.DATA_SOURCE, ds.getId() });
return null;
});
successes.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
failure.setValue(e);
failures.getAndIncrement();
} finally {
running.decrementAndGet();
}
}
}.start();
// #8 lock dataSources and try to lock roleMappings
// Basically update a data source
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < numDataSources; i++) {
ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(dataSource);
// Insert an event handler
EventHandlerService eventHandlerService = Common.getBean(EventHandlerService.class);
ProcessEventHandlerVO eh = new ProcessEventHandlerVO();
eh.setDefinition(new ProcessEventHandlerDefinition());
eh.setName(Common.generateXid("Handler "));
eh.setActiveProcessCommand("ls");
eventHandlerService.insert(eh);
// Get event handler
AbstractEventHandlerVO myEventHandler = eventHandlerService.get(eh.getXid());
// Create data source
MockDataSourceVO ds = new MockDataSourceVO();
ds.setName(Common.generateXid("Mock "));
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
dataSourceService.insert(ds);
// Insert a mapping
myEventHandler.setEventTypes(Collections.singletonList(new EventTypeMatcher(new DataSourceEventType(ds.getId(), ds.getPollAbortedExceptionEventId()))));
eventHandlerService.update(eh.getXid(), myEventHandler);
new TransactionTemplate(transactionManager).execute((status) -> {
ejt.update("UPDATE dataSources SET xid=? WHERE id=?", new Object[] { ds.getXid() + "1", ds.getId() });
return null;
});
successes.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
failure.setValue(e);
failures.getAndIncrement();
} finally {
running.decrementAndGet();
}
}
}.start();
}
while (running.get() > 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
if (failures.get() > 0) {
fail("Ran " + successes.get() + " queries: " + failure.getValue().getMessage());
}
}
use of com.infiniteautomation.mango.spring.service.RoleService in project ma-modules-public by infiniteautomation.
the class ScriptEventHandlerModel method readInto.
@Override
public void readInto(ScriptEventHandlerVO vo) {
super.readInto(vo);
vo.setScript(this.script);
vo.setEngineName(this.engineName);
RoleService roleService = Common.getBean(RoleService.class);
Set<Role> roleXids = scriptRoles.stream().map(xid -> {
try {
return roleService.get(xid).getRole();
} catch (NotFoundException e) {
return null;
}
}).filter(r -> r != null).collect(Collectors.toSet());
vo.setScriptRoles(roleXids);
}
use of com.infiniteautomation.mango.spring.service.RoleService in project ma-core-public by infiniteautomation.
the class LazyFieldJsonTest method testLazyPermissionInObject.
@Test
public void testLazyPermissionInObject() {
RoleService roleService = Common.getBean(RoleService.class);
PermissionService permissionService = Common.getBean(PermissionService.class);
Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
LazyContainer container = new LazyContainer();
container.supplyPermission(() -> MangoPermission.builder().minterm(role1, role2).build());
try (StringWriter stringWriter = new StringWriter()) {
JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
JsonValue value = typeWriter.writeObject(container);
writer.setPrettyIndent(0);
writer.setPrettyOutput(true);
writer.writeObject(value);
String json = stringWriter.toString();
JsonTypeReader typeReader = new JsonTypeReader(json);
JsonValue read = typeReader.read();
JsonObject root = read.toJsonObject();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
LazyContainer readContainer = new LazyContainer();
context.getReader().readInto(readContainer, root);
assertEquals(container.getPermission(), readContainer.getPermission());
} catch (IOException | JsonException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations