From d30e2c42a6cc0eba4918edc4b1c9c19344ce0937 Mon Sep 17 00:00:00 2001 From: Matthew Tamayo-Rios Date: Thu, 3 Mar 2022 17:17:22 -1000 Subject: [PATCH 1/2] Export user permissions. --- .../upgrades/ExportOrganizationMembers.kt | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt diff --git a/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt b/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt new file mode 100644 index 00000000..414d5017 --- /dev/null +++ b/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt @@ -0,0 +1,67 @@ +package com.openlattice.mechanic.upgrades + +import com.hazelcast.core.HazelcastInstance +import com.openlattice.hazelcast.HazelcastMap +import com.openlattice.mechanic.Toolbox +import com.openlattice.organizations.roles.HazelcastPrincipalService +import com.zaxxer.hikari.HikariDataSource +import org.slf4j.LoggerFactory + +/** + * + * @author Matthew Tamayo-Rios <matthew@openlattice.com> + */ +class ExportOrganizationMembers( + private val toolbox: Toolbox, + private val hds: HikariDataSource, + private val principalService: HazelcastPrincipalService, + hazelcast :HazelcastInstance +) : Upgrade { + companion object { + private const val USERS_EXPORT_TABLE_NAME = "users_export" + const val USERS_EXPORT_TABLE = """ + CREATE TABLE $USERS_EXPORT_TABLE_NAME ( + organization_id uuid, + principal_id text NOT NULL, + principal_email text, + principal_username text, + PRIMARY KEY( organization_id, principal_id ) ); + """ + const val INSERT_USER_SQL = """ + INSERT INTO $USERS_EXPORT_TABLE_NAME (organization_id, principal_id, principal_email, principal_username) VALUES (?,?,?,?) + """ + private val logger = LoggerFactory.getLogger(ExportOrganizationMembers::class.java) + } + + private val organizations = HazelcastMap.ORGANIZATIONS.getMap(hazelcast) + private val users = HazelcastMap.USERS.getMap(hazelcast) + + override fun upgrade(): Boolean { + val organizationsIds = organizations.keys.toMutableSet() + val members = principalService.getOrganizationMembers(organizationsIds) + + hds.connection.use { connection -> + connection.prepareStatement(INSERT_USER_SQL).use { ps -> + members.forEach { (orgId, orgMembers) -> + orgMembers.forEach { orgMember -> + val user = users[ orgMember.name ] + ps.setObject(1, orgId) + ps.setString(2, orgMember.name) + ps.setString(3, user?.email) + ps.setString(4, user?.username) + ps.addBatch() + } + } + val userCount = ps.executeBatch().sum() + logger.info("Exported $userCount users across ${organizationsIds.size} organizations.") + } + } + + return true + } + + override fun getSupportedVersion(): Long { + return Version.V2021_07_23.value + } + +} \ No newline at end of file From 9650a0f3b93ac7e458b2dfd385bf9d12ee008aa6 Mon Sep 17 00:00:00 2001 From: Matthew Tamayo-Rios Date: Thu, 3 Mar 2022 17:18:34 -1000 Subject: [PATCH 2/2] Add to upgrade pod. --- .../com/openlattice/mechanic/pods/MechanicUpgradePod.kt | 6 ++++++ .../mechanic/upgrades/ExportOrganizationMembers.kt | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/openlattice/mechanic/pods/MechanicUpgradePod.kt b/src/main/kotlin/com/openlattice/mechanic/pods/MechanicUpgradePod.kt index 3767ea48..9d03d914 100644 --- a/src/main/kotlin/com/openlattice/mechanic/pods/MechanicUpgradePod.kt +++ b/src/main/kotlin/com/openlattice/mechanic/pods/MechanicUpgradePod.kt @@ -72,6 +72,7 @@ import com.openlattice.linking.graph.PostgresLinkingQueryService import com.openlattice.mechanic.MechanicCli.Companion.UPGRADE import com.openlattice.mechanic.Toolbox import com.openlattice.mechanic.upgrades.DeleteOrgMetadataEntitySets +import com.openlattice.mechanic.upgrades.ExportOrganizationMembers import com.openlattice.mechanic.upgrades.V3StudyMigrationUpgrade import com.openlattice.organizations.roles.HazelcastPrincipalService import com.openlattice.organizations.roles.SecurePrincipalsManager @@ -379,6 +380,11 @@ class MechanicUpgradePod { ) } + @Bean + fun exportOrganizationMembers() : ExportOrganizationMembers { + return ExportOrganizationMembers(toolbox, hikariDataSource, principalService(), hazelcastInstance) + } + @PostConstruct fun post() { Principals.init(principalService(), hazelcastInstance) diff --git a/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt b/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt index 414d5017..6f087ca1 100644 --- a/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt +++ b/src/main/kotlin/com/openlattice/mechanic/upgrades/ExportOrganizationMembers.kt @@ -3,7 +3,7 @@ package com.openlattice.mechanic.upgrades import com.hazelcast.core.HazelcastInstance import com.openlattice.hazelcast.HazelcastMap import com.openlattice.mechanic.Toolbox -import com.openlattice.organizations.roles.HazelcastPrincipalService +import com.openlattice.organizations.roles.SecurePrincipalsManager import com.zaxxer.hikari.HikariDataSource import org.slf4j.LoggerFactory @@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory class ExportOrganizationMembers( private val toolbox: Toolbox, private val hds: HikariDataSource, - private val principalService: HazelcastPrincipalService, + private val principalService: SecurePrincipalsManager, hazelcast :HazelcastInstance ) : Upgrade { companion object {