use of org.jkiss.dbeaver.ext.postgresql.edit.PostgreCommandGrantPrivilege in project dbeaver by serge-rider.
the class PostgresRolePrivilegesEditor method updateCurrentPrivileges.
private void updateCurrentPrivileges(boolean grant, PostgrePrivilegeType privilegeType) {
if (ArrayUtils.isEmpty(currentObjects)) {
DBWorkbench.getPlatformUI().showError("Update privilege", "Can't update privilege - no current object");
return;
}
PostgrePrivilegeOwner databaseObject = getDatabaseObject();
for (int i = 0; i < currentObjects.length; i++) {
DBSObject currentObject = currentObjects[i];
PostgrePrivilege permission = currentPermissions[i];
if (permission == null) {
if (!grant) {
// No permission - nothing to revoke
continue;
}
if (isRoleEditor()) {
PostgrePrivilegeOwner permissionsOwner = (PostgrePrivilegeOwner) currentObject;
PostgrePrivilegeGrant.Kind kind;
String objectName;
if (permissionsOwner instanceof PostgreProcedure) {
if (((PostgreProcedure) permissionsOwner).getKind() == PostgreProcedureKind.p) {
kind = PostgrePrivilegeGrant.Kind.PROCEDURE;
} else {
kind = PostgrePrivilegeGrant.Kind.FUNCTION;
}
objectName = ((PostgreProcedure) permissionsOwner).getUniqueName();
} else {
if (permissionsOwner instanceof PostgreSchema) {
kind = PostgrePrivilegeGrant.Kind.SCHEMA;
} else if (permissionsOwner instanceof PostgreSequence) {
kind = PostgrePrivilegeGrant.Kind.SEQUENCE;
} else {
kind = PostgrePrivilegeGrant.Kind.TABLE;
}
objectName = permissionsOwner.getName();
}
permission = new PostgreRolePrivilege(databaseObject, kind, permissionsOwner.getSchema().getName(), objectName, Collections.emptyList());
} else {
String currentUser = databaseObject.getDataSource().getContainer().getActualConnectionConfiguration().getUserName();
PostgrePrivilegeGrant privGrant = new PostgrePrivilegeGrant(currentUser, currentObject.getName(), databaseObject.getDatabase().getName(), databaseObject.getSchema().getName(), databaseObject.getName(), privilegeType, false, false);
permission = new PostgreObjectPrivilege(databaseObject, currentObject.getName(), Collections.singletonList(privGrant));
}
// Add to map
currentPermissions[i] = permission;
permissionMap.put(permission.getName(), permission);
} else if (privilegeType != null) {
// Check for privilege was already granted for this object
boolean hasPriv = permission.getPermission(privilegeType) != PostgrePrivilege.NONE;
if (grant != hasPriv && !grant) {
permissionMap.remove(permission.getName());
}
}
// Add command
addChangeCommand(new PostgreCommandGrantPrivilege(databaseObject, grant, currentObject, permission, privilegeType == null ? null : new PostgrePrivilegeType[] { privilegeType }), new DBECommandReflector<PostgrePrivilegeOwner, PostgreCommandGrantPrivilege>() {
@Override
public void redoCommand(PostgreCommandGrantPrivilege cmd) {
// if (!privTable.isDisposed() && curCatalog == selectedCatalog && curTable == selectedTable) {
// privTable.checkPrivilege(privilege, isGrant);
// }
// updateLocalData(privilege, isGrant, curCatalog, curTable);
}
@Override
public void undoCommand(PostgreCommandGrantPrivilege cmd) {
// if (!privTable.isDisposed() && curCatalog == selectedCatalog && curTable == selectedTable) {
// privTable.checkPrivilege(privilege, !isGrant);
// }
// updateLocalData(privilege, !isGrant, curCatalog, curTable);
}
});
}
}
use of org.jkiss.dbeaver.ext.postgresql.edit.PostgreCommandGrantPrivilege in project dbeaver by dbeaver.
the class PostgresRolePrivilegesEditor method updateCurrentPrivileges.
private void updateCurrentPrivileges(boolean grant, PostgrePrivilegeType privilegeType) {
if (ArrayUtils.isEmpty(currentObjects)) {
DBWorkbench.getPlatformUI().showError("Update privilege", "Can't update privilege - no current object");
return;
}
PostgrePrivilegeOwner databaseObject = getDatabaseObject();
for (int i = 0; i < currentObjects.length; i++) {
DBSObject currentObject = currentObjects[i];
PostgrePrivilege permission = currentPermissions[i];
if (permission == null) {
if (!grant) {
// No permission - nothing to revoke
continue;
}
if (isRoleEditor()) {
PostgrePrivilegeOwner permissionsOwner = (PostgrePrivilegeOwner) currentObject;
PostgrePrivilegeGrant.Kind kind;
String objectName;
if (permissionsOwner instanceof PostgreProcedure) {
if (((PostgreProcedure) permissionsOwner).getKind() == PostgreProcedureKind.p) {
kind = PostgrePrivilegeGrant.Kind.PROCEDURE;
} else {
kind = PostgrePrivilegeGrant.Kind.FUNCTION;
}
objectName = ((PostgreProcedure) permissionsOwner).getUniqueName();
} else {
if (permissionsOwner instanceof PostgreSchema) {
kind = PostgrePrivilegeGrant.Kind.SCHEMA;
} else if (permissionsOwner instanceof PostgreSequence) {
kind = PostgrePrivilegeGrant.Kind.SEQUENCE;
} else {
kind = PostgrePrivilegeGrant.Kind.TABLE;
}
objectName = permissionsOwner.getName();
}
permission = new PostgreRolePrivilege(databaseObject, kind, permissionsOwner.getSchema().getName(), objectName, Collections.emptyList());
} else {
String currentUser = databaseObject.getDataSource().getContainer().getActualConnectionConfiguration().getUserName();
PostgrePrivilegeGrant privGrant = new PostgrePrivilegeGrant(currentUser, currentObject.getName(), databaseObject.getDatabase().getName(), databaseObject.getSchema().getName(), databaseObject.getName(), privilegeType, false, false);
permission = new PostgreObjectPrivilege(databaseObject, currentObject.getName(), Collections.singletonList(privGrant));
}
// Add to map
currentPermissions[i] = permission;
permissionMap.put(permission.getName(), permission);
} else if (privilegeType != null) {
// Check for privilege was already granted for this object
boolean hasPriv = permission.getPermission(privilegeType) != PostgrePrivilege.NONE;
if (grant != hasPriv && !grant) {
permissionMap.remove(permission.getName());
}
}
// Add command
addChangeCommand(new PostgreCommandGrantPrivilege(databaseObject, grant, currentObject, permission, privilegeType == null ? null : new PostgrePrivilegeType[] { privilegeType }), new DBECommandReflector<PostgrePrivilegeOwner, PostgreCommandGrantPrivilege>() {
@Override
public void redoCommand(PostgreCommandGrantPrivilege cmd) {
// if (!privTable.isDisposed() && curCatalog == selectedCatalog && curTable == selectedTable) {
// privTable.checkPrivilege(privilege, isGrant);
// }
// updateLocalData(privilege, isGrant, curCatalog, curTable);
}
@Override
public void undoCommand(PostgreCommandGrantPrivilege cmd) {
// if (!privTable.isDisposed() && curCatalog == selectedCatalog && curTable == selectedTable) {
// privTable.checkPrivilege(privilege, !isGrant);
// }
// updateLocalData(privilege, !isGrant, curCatalog, curTable);
}
});
}
}
use of org.jkiss.dbeaver.ext.postgresql.edit.PostgreCommandGrantPrivilege in project dbeaver by serge-rider.
the class PostgreUtils method getObjectGrantPermissionActions.
public static void getObjectGrantPermissionActions(DBRProgressMonitor monitor, PostgrePrivilegeOwner object, List<DBEPersistAction> actions, Map<String, Object> options) throws DBException {
if (object.isPersisted() && CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_PERMISSIONS)) {
DBCExecutionContext executionContext = DBUtils.getDefaultContext(object, true);
actions.add(new SQLDatabasePersistActionComment(object.getDataSource(), "Permissions"));
// Owner
PostgreRole owner = object.getOwner(monitor);
if (owner != null) {
String alterScript = object.generateChangeOwnerQuery(DBUtils.getQuotedIdentifier(owner));
if (!CommonUtils.isEmpty(alterScript)) {
actions.add(new SQLDatabasePersistAction("Owner change", alterScript));
}
}
// Permissions
Collection<PostgrePrivilege> permissions = object.getPrivileges(monitor, true);
if (!CommonUtils.isEmpty(permissions)) {
for (PostgrePrivilege permission : permissions) {
if (permission.hasAllPrivileges(object)) {
Collections.addAll(actions, new PostgreCommandGrantPrivilege(permission.getOwner(), true, object, permission, new PostgrePrivilegeType[] { PostgrePrivilegeType.ALL }).getPersistActions(monitor, executionContext, options));
} else {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, object, permission, permission.getPrivileges());
Collections.addAll(actions, grant.getPersistActions(monitor, executionContext, options));
}
}
}
}
}
use of org.jkiss.dbeaver.ext.postgresql.edit.PostgreCommandGrantPrivilege in project dbeaver by dbeaver.
the class PostgreUtils method getObjectGrantPermissionActions.
public static void getObjectGrantPermissionActions(DBRProgressMonitor monitor, PostgrePrivilegeOwner object, List<DBEPersistAction> actions, Map<String, Object> options) throws DBException {
if (object.isPersisted() && CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_PERMISSIONS)) {
DBCExecutionContext executionContext = DBUtils.getDefaultContext(object, true);
actions.add(new SQLDatabasePersistActionComment(object.getDataSource(), "Permissions"));
// Owner
PostgreRole owner = object.getOwner(monitor);
if (owner != null) {
String alterScript = object.generateChangeOwnerQuery(DBUtils.getQuotedIdentifier(owner));
if (!CommonUtils.isEmpty(alterScript)) {
actions.add(new SQLDatabasePersistAction("Owner change", alterScript));
}
}
// Permissions
Collection<PostgrePrivilege> permissions = object.getPrivileges(monitor, true);
if (!CommonUtils.isEmpty(permissions)) {
for (PostgrePrivilege permission : permissions) {
if (permission.hasAllPrivileges(object)) {
Collections.addAll(actions, new PostgreCommandGrantPrivilege(permission.getOwner(), true, object, permission, new PostgrePrivilegeType[] { PostgrePrivilegeType.ALL }).getPersistActions(monitor, executionContext, options));
} else {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, object, permission, permission.getPrivileges());
Collections.addAll(actions, grant.getPersistActions(monitor, executionContext, options));
}
}
}
}
}
Aggregations