use of io.crate.user.metadata.UsersPrivilegesMetadata in project crate by crate.
the class TransportCreateUserAction method putUser.
/**
* Puts a user into the meta data and creates an empty privileges set.
*
* @return boolean true if the user already exists, otherwise false
*/
@VisibleForTesting
static boolean putUser(Metadata.Builder mdBuilder, String name, @Nullable SecureHash secureHash) {
UsersMetadata oldMetadata = (UsersMetadata) mdBuilder.getCustom(UsersMetadata.TYPE);
if (oldMetadata != null && oldMetadata.contains(name)) {
return true;
}
// create a new instance of the metadata, to guarantee the cluster changed action.
UsersMetadata newMetadata = UsersMetadata.newInstance(oldMetadata);
newMetadata.put(name, secureHash);
assert !newMetadata.equals(oldMetadata) : "must not be equal to guarantee the cluster change action";
mdBuilder.putCustom(UsersMetadata.TYPE, newMetadata);
// create empty privileges for this user
UsersPrivilegesMetadata privilegesMetadata = UsersPrivilegesMetadata.copyOf((UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE));
privilegesMetadata.createPrivileges(name, Collections.emptySet());
mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, privilegesMetadata);
return false;
}
use of io.crate.user.metadata.UsersPrivilegesMetadata in project crate by crate.
the class UserManagerDDLModifier method transferTablePrivileges.
private static boolean transferTablePrivileges(Metadata.Builder mdBuilder, RelationName sourceRelationName, RelationName targetRelationName) {
UsersPrivilegesMetadata oldMetadata = (UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE);
if (oldMetadata == null) {
return false;
}
// create a new instance of the metadata if privileges were changed, to guarantee the cluster changed action.
UsersPrivilegesMetadata newMetadata = UsersPrivilegesMetadata.maybeCopyAndReplaceTableIdents(oldMetadata, sourceRelationName.fqn(), targetRelationName.fqn());
if (newMetadata != null) {
mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, newMetadata);
return true;
}
return false;
}
use of io.crate.user.metadata.UsersPrivilegesMetadata in project crate by crate.
the class TransportDropUserAction method dropUser.
@VisibleForTesting
static boolean dropUser(Metadata.Builder mdBuilder, @Nullable UsersMetadata oldMetadata, String name) {
if (oldMetadata == null || oldMetadata.contains(name) == false) {
return false;
}
// create a new instance of the metadata, to guarantee the cluster changed action.
UsersMetadata newMetadata = UsersMetadata.newInstance(oldMetadata);
newMetadata.remove(name);
assert !newMetadata.equals(oldMetadata) : "must not be equal to guarantee the cluster change action";
mdBuilder.putCustom(UsersMetadata.TYPE, newMetadata);
// removes all privileges for this user
UsersPrivilegesMetadata privilegesMetadata = UsersPrivilegesMetadata.copyOf((UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE));
privilegesMetadata.dropPrivileges(name);
mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, privilegesMetadata);
return true;
}
use of io.crate.user.metadata.UsersPrivilegesMetadata in project crate by crate.
the class TransportPrivilegesActionTest method testApplyPrivilegesCreatesNewPrivilegesInstance.
@Test
public void testApplyPrivilegesCreatesNewPrivilegesInstance() {
// given
Metadata.Builder mdBuilder = Metadata.builder();
Map<String, Set<Privilege>> usersPrivileges = new HashMap<>();
usersPrivileges.put("Ford", new HashSet<>(PRIVILEGES));
UsersPrivilegesMetadata initialPrivilegesMetadata = new UsersPrivilegesMetadata(usersPrivileges);
mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, initialPrivilegesMetadata);
PrivilegesRequest denyPrivilegeRequest = new PrivilegesRequest(Collections.singletonList("Ford"), Collections.singletonList(DENY_DQL));
// when
TransportPrivilegesAction.applyPrivileges(mdBuilder, denyPrivilegeRequest);
// then
UsersPrivilegesMetadata newPrivilegesMetadata = (UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE);
assertNotSame(newPrivilegesMetadata, initialPrivilegesMetadata);
}
use of io.crate.user.metadata.UsersPrivilegesMetadata in project crate by crate.
the class TransportUserActionTest method testEmptyPrivilegesAreCreatedForNewUsers.
@Test
public void testEmptyPrivilegesAreCreatedForNewUsers() throws Exception {
Metadata.Builder mdBuilder = new Metadata.Builder();
TransportCreateUserAction.putUser(mdBuilder, "root", null);
UsersPrivilegesMetadata metadata = (UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE);
assertThat(metadata.getUserPrivileges("root"), is(Collections.emptySet()));
}
Aggregations