use of io.crate.user.User in project crate by crate.
the class SetSessionAuthorizationPlan method executeOrFail.
@Override
public void executeOrFail(DependencyCarrier executor, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) throws Exception {
var sessionContext = plannerContext.transactionContext().sessionContext();
String userName = setSessionAuthorization.user();
User user;
if (userName != null) {
user = userLookup.findUser(userName);
if (user == null) {
throw new IllegalArgumentException("User '" + userName + "' does not exist.");
}
} else {
user = sessionContext.authenticatedUser();
}
sessionContext.setSessionUser(user);
consumer.accept(InMemoryBatchIterator.empty(SENTINEL), null);
}
use of io.crate.user.User in project crate by crate.
the class CreateViewPlan method executeOrFail.
@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) {
User owner = createViewStmt.owner();
String formattedQuery = SqlFormatter.formatSql(createViewStmt.query(), makeExpressions(params));
ensureFormattedQueryCanStillBeAnalyzed(createViewStmt.name(), dependencies.nodeContext(), dependencies.schemas(), plannerContext.transactionContext(), formattedQuery, createViewStmt.replaceExisting());
CreateViewRequest request = new CreateViewRequest(createViewStmt.name(), formattedQuery, createViewStmt.replaceExisting(), owner == null ? null : owner.name());
dependencies.createViewAction().execute(request, new OneRowActionListener<>(consumer, resp -> {
if (resp.alreadyExistsFailure()) {
throw new RelationAlreadyExists(createViewStmt.name());
}
return new Row1(1L);
}));
}
use of io.crate.user.User in project crate by crate.
the class SysShardsTableInfo method getRouting.
/**
* Retrieves the routing for sys.shards
* <p>
* This routing contains ALL shards of ALL indices.
* Any shards that are not yet assigned to a node will have a NEGATIVE shard id (see {@link UnassignedShard}
*/
public static Routing getRouting(ClusterState clusterState, RoutingProvider routingProvider, SessionContext sessionContext) {
String[] concreteIndices = Arrays.stream(clusterState.metadata().getConcreteAllIndices()).filter(index -> !IndexParts.isDangling(index)).toArray(String[]::new);
User user = sessionContext != null ? sessionContext.sessionUser() : null;
if (user != null) {
List<String> accessibleTables = new ArrayList<>(concreteIndices.length);
for (String indexName : concreteIndices) {
String tableName = RelationName.fqnFromIndexName(indexName);
if (user.hasAnyPrivilege(Privilege.Clazz.TABLE, tableName)) {
accessibleTables.add(indexName);
}
}
concreteIndices = accessibleTables.toArray(new String[0]);
}
Map<String, Map<String, IntIndexedContainer>> locations = new TreeMap<>();
GroupShardsIterator<ShardIterator> groupShardsIterator = clusterState.getRoutingTable().allAssignedShardsGrouped(concreteIndices, true);
for (final ShardIterator shardIt : groupShardsIterator) {
final ShardRouting shardRouting = shardIt.nextOrNull();
processShardRouting(clusterState.getNodes().getLocalNodeId(), locations, shardRouting, shardIt.shardId());
}
return new Routing(locations);
}
use of io.crate.user.User in project crate by crate.
the class Schemas method getSimilarSchemas.
private List<String> getSimilarSchemas(User user, String schema) {
LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
ArrayList<Candidate> candidates = new ArrayList<>();
for (String availableSchema : schemas.keySet()) {
if (user.hasAnyPrivilege(Privilege.Clazz.SCHEMA, availableSchema)) {
float score = levenshteinDistance.getDistance(schema.toLowerCase(Locale.ENGLISH), availableSchema.toLowerCase(Locale.ENGLISH));
if (score > 0.7f) {
candidates.add(new Candidate(score, availableSchema));
}
}
}
candidates.sort(Comparator.comparing((Candidate x) -> x.score).reversed());
return candidates.stream().limit(5).map(x -> x.name).collect(Collectors.toList());
}
use of io.crate.user.User in project crate by crate.
the class SqlHttpHandler method ensureSession.
@VisibleForTesting
Session ensureSession(FullHttpRequest request) {
String defaultSchema = request.headers().get(REQUEST_HEADER_SCHEMA);
User authenticatedUser = userFromAuthHeader(request.headers().get(HttpHeaderNames.AUTHORIZATION));
Session session = this.session;
if (session == null) {
session = sqlOperations.createSession(defaultSchema, authenticatedUser);
} else if (session.sessionContext().authenticatedUser().equals(authenticatedUser) == false) {
session.close();
session = sqlOperations.createSession(defaultSchema, authenticatedUser);
}
this.session = session;
return session;
}
Aggregations