-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Added
CURRENT_USER
, 'USER and
SESSION_USER` system functions.
- Loading branch information
Showing
11 changed files
with
199 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
enterprise/users/src/main/java/io/crate/scalar/UsersScalarFunctionModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This file is part of a module with proprietary Enterprise Features. | ||
* | ||
* Licensed to Crate.io Inc. ("Crate.io") under one or more contributor | ||
* license agreements. See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
* | ||
* Unauthorized copying of this file, via any medium is strictly prohibited. | ||
* | ||
* To use this file, Crate.io must have given you permission to enable and | ||
* use such Enterprise Features and you must have a valid Enterprise or | ||
* Subscription Agreement with Crate.io. If you enable or use the Enterprise | ||
* Features, you represent and warrant that you have a valid Enterprise or | ||
* Subscription Agreement with Crate.io. Your use of the Enterprise Features | ||
* if governed by the terms and conditions of your Enterprise or Subscription | ||
* Agreement with Crate.io. | ||
*/ | ||
|
||
package io.crate.scalar; | ||
|
||
import io.crate.metadata.FunctionIdent; | ||
import io.crate.metadata.FunctionImplementation; | ||
import io.crate.scalar.systeminformation.UserFunction; | ||
import org.elasticsearch.common.inject.AbstractModule; | ||
import org.elasticsearch.common.inject.multibindings.MapBinder; | ||
|
||
public class UsersScalarFunctionModule extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
MapBinder<FunctionIdent, FunctionImplementation> functionBinder = | ||
MapBinder.newMapBinder(binder(), FunctionIdent.class, FunctionImplementation.class); | ||
UserFunction currentUserFunction = new UserFunction(UserFunction.CURRENT_USER_FUNCTION_NAME); | ||
functionBinder.addBinding(currentUserFunction.info().ident()).toInstance(currentUserFunction); | ||
UserFunction sessionUserFunction = new UserFunction(UserFunction.SESSION_USER_FUNCTION_NAME); | ||
functionBinder.addBinding(sessionUserFunction.info().ident()).toInstance(sessionUserFunction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
enterprise/users/src/test/java/io/crate/scalar/systeminformation/UserFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* This file is part of a module with proprietary Enterprise Features. | ||
* | ||
* Licensed to Crate.io Inc. ("Crate.io") under one or more contributor | ||
* license agreements. See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
* | ||
* Unauthorized copying of this file, via any medium is strictly prohibited. | ||
* | ||
* To use this file, Crate.io must have given you permission to enable and | ||
* use such Enterprise Features and you must have a valid Enterprise or | ||
* Subscription Agreement with Crate.io. If you enable or use the Enterprise | ||
* Features, you represent and warrant that you have a valid Enterprise or | ||
* Subscription Agreement with Crate.io. Your use of the Enterprise Features | ||
* if governed by the terms and conditions of your Enterprise or Subscription | ||
* Agreement with Crate.io. | ||
*/ | ||
|
||
package io.crate.scalar.systeminformation; | ||
|
||
import io.crate.action.sql.Option; | ||
import io.crate.action.sql.SessionContext; | ||
import io.crate.analyze.symbol.Symbol; | ||
import io.crate.analyze.symbol.format.SymbolPrinter; | ||
import io.crate.operation.scalar.AbstractScalarFunctionsTest; | ||
import io.crate.operation.user.User; | ||
import io.crate.scalar.UsersScalarFunctionModule; | ||
import io.crate.testing.SqlExpressions; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static io.crate.testing.SymbolMatchers.isLiteral; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class UserFunctionTest extends AbstractScalarFunctionsTest { | ||
|
||
private static final User TEST_USER = new User("testUser", Collections.emptySet(), Collections.emptySet()); | ||
|
||
private void setupFunctionsForTestUser() { | ||
SessionContext sessionContext = new SessionContext(0, Option.NONE, null, TEST_USER, | ||
s -> {}, t -> {}); | ||
sqlExpressions = new SqlExpressions(tableSources, null, null, sessionContext, | ||
new UsersScalarFunctionModule()); | ||
functions = sqlExpressions.functions(); | ||
} | ||
|
||
@Test | ||
public void testNormalizeCurrentUser() { | ||
setupFunctionsForTestUser(); | ||
assertNormalize("current_user", isLiteral("testUser"), false); | ||
} | ||
|
||
@Test | ||
public void testNormalizeSessionUser() { | ||
setupFunctionsForTestUser(); | ||
assertNormalize("session_user", isLiteral("testUser"), false); | ||
} | ||
|
||
@Test | ||
public void testNormalizeUser() { | ||
setupFunctionsForTestUser(); | ||
assertNormalize("user", isLiteral("testUser"), false); | ||
} | ||
|
||
@Test | ||
public void testCurrentUserForMissingUserReturnsNull() { | ||
setupFunctionsWithoutUser(); | ||
assertNormalize("current_user", isLiteral(null), false); | ||
} | ||
|
||
@Test | ||
public void testUserForMissingUserReturnsNull() { | ||
setupFunctionsWithoutUser(); | ||
assertNormalize("user", isLiteral(null), false); | ||
} | ||
|
||
@Test | ||
public void testSessionUserForMissingUserReturnsNull() { | ||
setupFunctionsWithoutUser(); | ||
assertNormalize("session_user", isLiteral(null), false); | ||
} | ||
|
||
@Test | ||
public void testFormatFunctionsWithoutBrackets() { | ||
setupFunctionsForTestUser(); | ||
SymbolPrinter printer = new SymbolPrinter(sqlExpressions.functions()); | ||
Symbol f = sqlExpressions.asSymbol("current_user"); | ||
assertThat(printer.printFullQualified(f), is("current_user")); | ||
|
||
f = sqlExpressions.asSymbol("session_user"); | ||
assertThat(printer.printFullQualified(f), is("session_user")); | ||
|
||
f = sqlExpressions.asSymbol("user"); | ||
assertThat(printer.printFullQualified(f), is("current_user")); | ||
} | ||
|
||
private void setupFunctionsWithoutUser() { | ||
SessionContext sessionContext = new SessionContext(0, Option.NONE, null, null, null, null); | ||
sqlExpressions = new SqlExpressions(tableSources, null, null, sessionContext, new UsersScalarFunctionModule()); | ||
functions = sqlExpressions.functions(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.