use of org.structr.core.entity.Principal in project structr by structr.
the class StructrSessionDataStore method doStore.
@Override
public void doStore(final String id, final SessionData data, final long lastSaveTime) throws Exception {
assertInitialized();
try (final Tx tx = StructrApp.getInstance().tx()) {
final Principal user = AuthHelper.getPrincipalForSessionId(id);
// store sessions only for authenticated users
if (user != null) {
user.setSessionData(Base64.encode(SerializationUtils.serialize(data)));
}
tx.success();
} catch (FrameworkException ex) {
logger.info("Unable to store session data for session id " + id + ".", ex);
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class StructrSessionDataStore method load.
@Override
public SessionData load(final String id) throws Exception {
assertInitialized();
SessionData sessionData = null;
try (final Tx tx = StructrApp.getInstance().tx()) {
final Principal user = AuthHelper.getPrincipalForSessionId(id);
// store sessions only for authenticated users
if (user != null) {
final String sessionDataString = user.getSessionData();
if (sessionDataString != null) {
sessionData = SerializationUtils.deserialize(Base64.decode(sessionDataString));
}
}
tx.success();
} catch (FrameworkException ex) {
logger.info("Unable to load session data for session id " + id + ".", ex);
}
return sessionData;
}
use of org.structr.core.entity.Principal in project structr by structr.
the class AdvancedSearchTest method testSearchWithOwnerAndEnum.
@Test
public void testSearchWithOwnerAndEnum() {
final Class testUserType = createTestUserType();
try {
final List<Principal> users = createTestNodes(testUserType, 3);
final List<TestThree> testThrees = new LinkedList<>();
final Random random = new Random();
String uuid = null;
int count = 0;
try (final Tx tx = app.tx()) {
for (final Principal user : users) {
// create 20 entities for every user
for (int i = 0; i < 20; i++) {
testThrees.add(app.create(TestThree.class, new NodeAttribute(AbstractNode.name, "test" + count++), new NodeAttribute(AbstractNode.owner, user), new NodeAttribute(TestThree.enumProperty, TestEnum.values()[random.nextInt(TestEnum.values().length)])));
}
}
uuid = users.get(0).getUuid();
tx.success();
}
// test with core API
try (final Tx tx = app.tx()) {
for (final Principal user : users) {
for (final TestThree test : app.nodeQuery(TestThree.class).and(AbstractNode.owner, user).and(TestThree.enumProperty, TestEnum.Status1).getAsList()) {
assertEquals("Invalid enum query result", TestEnum.Status1, test.getProperty(TestThree.enumProperty));
}
}
tx.success();
}
// test via REST
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).when().get(concat("/test_threes?sort=createdDate&owner=" + uuid + "&enumProperty=" + TestEnum.Status1));
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class PagingAndSortingTest method testRelationshipResourcePagingOnCollectionResource.
@Test
public void testRelationshipResourcePagingOnCollectionResource() {
final Class testUserType = createTestUserType();
final PropertyKey<String> passwordKey = StructrApp.key(testUserType, "password");
Principal tester = null;
try (final Tx tx = app.tx()) {
tester = app.create(testUserType, new Name("tester"), new NodeAttribute<>(passwordKey, "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final App app = StructrApp.getInstance(SecurityContext.getInstance(tester, AccessMode.Backend));
try (final Tx tx = app.tx()) {
app.create(TestOne.class, "TestOne1");
app.create(TestOne.class, "TestOne2");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).expect().statusCode(200).body("result_count", equalTo(4)).body("page_size", equalTo(2)).body("page_count", equalTo(2)).body("result", hasSize(2)).body("result[0].type", equalTo("PrincipalOwnsNode")).body("result[1].type", equalTo("Security")).when().get("/TestOne/in?pageSize=2");
}
use of org.structr.core.entity.Principal in project structr by structr.
the class PagingAndSortingTest method testRelationshipResourcePagingOnEntityResource.
@Test
public void testRelationshipResourcePagingOnEntityResource() {
final Class testUserType = createTestUserType();
final PropertyKey<String> passwordKey = StructrApp.key(testUserType, "password");
Principal tester = null;
try (final Tx tx = app.tx()) {
tester = app.create(testUserType, new Name("tester"), new NodeAttribute<>(passwordKey, "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final App app = StructrApp.getInstance(SecurityContext.getInstance(tester, AccessMode.Backend));
try (final Tx tx = app.tx()) {
app.create(TestOne.class, "TestOne1");
app.create(TestOne.class, "TestOne2");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).expect().statusCode(200).body("result_count", equalTo(4)).body("page_size", equalTo(2)).body("page_count", equalTo(2)).body("result", hasSize(2)).body("result[0].type", equalTo("PrincipalOwnsNode")).body("result[1].type", equalTo("Security")).when().get("/TestUser/" + tester.getUuid() + "/out?pageSize=2");
}
Aggregations