use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class RestControllerTest method loginPasswordReset.
@Test
public void loginPasswordReset() throws Exception {
String username = "henk";
String password = "123henk";
Authentication authentication = mock(Authentication.class);
when(authentication.isAuthenticated()).thenReturn(true);
when(authentication.getName()).thenReturn(username);
when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class))).thenReturn(authentication);
User user = mock(User.class);
when(user.isChangePassword()).thenReturn(true);
when(dataService.findOne(UserMetaData.USER, new QueryImpl<User>().eq(UserMetaData.USERNAME, username), User.class)).thenReturn(user);
mockMvc.perform(post(BASE_URI + "/login").content(format("{username: '%s', password: '%s'}", username, password)).contentType(APPLICATION_JSON)).andExpect(status().isUnauthorized());
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class QueryStringParser method parseQueryString.
public Query<Entity> parseQueryString(Map<String, String[]> parameterMap) {
QueryImpl<Entity> q = new QueryImpl<>();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
String paramName = entry.getKey();
String[] paramValues = entry.getValue();
if ((paramValues != null) && (paramValues.length > 0) && (paramValues[0] != null)) {
if (paramName.equalsIgnoreCase("num")) {
q.pageSize(DataConverter.toInt(paramValues[0]));
} else if (paramName.equalsIgnoreCase("start")) {
q.offset(DataConverter.toInt(paramValues[0]));
} else if (paramName.equalsIgnoreCase("q")) {
Query<Entity> query = molgenisRSQL.createQuery(paramValues[0], entityType);
for (QueryRule rule : query.getRules()) {
q.addRule(rule);
}
}
}
}
return q;
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class RestController method login.
/**
* Login to the api.
* <p>
* Returns a json object with a token on correct login else throws an AuthenticationException. Clients can use this
* token when calling the api.
* <p>
* Example:
* <p>
* Request: {username:admin,password:xxx}
* <p>
* Response: {token: b4fd94dc-eae6-4d9a-a1b7-dd4525f2f75d}
*/
@PostMapping(value = "/login", produces = APPLICATION_JSON_VALUE)
@ResponseBody
public LoginResponse login(@Valid @RequestBody LoginRequest login, HttpServletRequest request) {
if (login == null) {
throw new HttpMessageNotReadableException("Missing login");
}
if (isUser2fa()) {
throw new BadCredentialsException("Login using /api/v1/login is disabled, two factor authentication is enabled");
}
return runAsSystem(() -> {
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword());
authToken.setDetails(new WebAuthenticationDetails(request));
// Authenticate the login
Authentication authentication = authenticationManager.authenticate(authToken);
if (!authentication.isAuthenticated()) {
throw new BadCredentialsException("Unknown username or password");
}
User user = dataService.findOne(USER, new QueryImpl<User>().eq(UserMetaData.USERNAME, authentication.getName()), User.class);
if (user.isChangePassword()) {
throw new BadCredentialsException("Unable to log in because a password reset is required. Sign in to the website to reset your password.");
}
// User authenticated, log the user in
SecurityContextHolder.getContext().setAuthentication(authentication);
// Generate a new token for the user
String token = tokenService.generateAndStoreToken(authentication.getName(), "REST API login");
return new LoginResponse(token, user.getUsername(), user.getFirstName(), user.getLastName());
});
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class QueryValidatorTest method validateInvalidProvider.
@DataProvider(name = "validateInvalidProvider")
public static Iterator<Object[]> validateInvalidProvider() {
List<Object[]> queries = new ArrayList<>(6);
EnumSet.of(BOOL, DECIMAL, INT, LONG, DATE, DATE_TIME, ENUM).forEach(attrType -> queries.add(new Object[] { new QueryImpl().eq("attr", "invalid"), createEntityType(attrType) }));
EnumSet.of(BOOL, DECIMAL, INT, LONG, DATE, DATE_TIME, ENUM, XREF, MREF, CATEGORICAL, CATEGORICAL_MREF).forEach(attrType -> queries.add(new Object[] { new QueryImpl().eq("attr", new Object()), createEntityType(attrType) }));
queries.add(new Object[] { new QueryImpl().eq("unknownAttr", "str"), createEntityType(STRING) });
queries.add(new Object[] { new QueryImpl().eq("attr", "str"), createEntityType(COMPOUND) });
return queries.iterator();
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class RepositoryValidationDecorator method initReferenceValidation.
private void initReferenceValidation(ValidationResource validationResource) {
// get reference attrs
List<Attribute> refAttrs;
if (!getCapabilities().contains(VALIDATE_REFERENCE_CONSTRAINT)) {
// get reference attrs
refAttrs = stream(getEntityType().getAtomicAttributes().spliterator(), false).filter(attr -> isReferenceType(attr) && attr.getExpression() == null).collect(toList());
} else {
// validate cross-repository collection reference constraints. the decorated repository takes care of
// validating other reference constraints
String backend = dataService.getMeta().getBackend(getEntityType()).getName();
refAttrs = stream(getEntityType().getAtomicAttributes().spliterator(), false).filter(attr -> isReferenceType(attr) && attr.getExpression() == null && isDifferentBackend(backend, attr)).collect(toList());
}
// get referenced entity ids
if (!refAttrs.isEmpty()) {
Map<String, HugeSet<Object>> refEntitiesIds = new HashMap<>();
refAttrs.forEach(refAttr -> {
EntityType refEntityType = refAttr.getRefEntity();
String refEntityName = refEntityType.getId();
HugeSet<Object> refEntityIds = refEntitiesIds.get(refEntityName);
if (refEntityIds == null) {
refEntityIds = new HugeSet<>();
refEntitiesIds.put(refEntityName, refEntityIds);
Query<Entity> q = new QueryImpl<>().fetch(new Fetch().field(refEntityType.getIdAttribute().getName()));
for (Iterator<Entity> it = dataService.findAll(refEntityName, q).iterator(); it.hasNext(); ) {
refEntityIds.add(it.next().getIdValue());
}
}
});
validationResource.setRefEntitiesIds(refEntitiesIds);
}
validationResource.setSelfReferencing(refAttrs.stream().anyMatch(refAttr -> refAttr.getRefEntity().getId().equals(getEntityType().getId())));
validationResource.setRefAttrs(refAttrs);
}
Aggregations