Skip to content

Commit

Permalink
Wire up Daml Script user management to gRPC API (#12069)
Browse files Browse the repository at this point in the history
* Wire up Daml Script user management to gRPC API

part of #11997

The tests are disabled for now until #12063 is merged. I did test it
manually against that branch though and they pass happily.

changelog_begin
changelog_end

* fmt

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Dec 9, 2021
1 parent f3acd8d commit f59760e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,30 @@ class GrpcLedgerClient(val grpcClient: LedgerClient, val applicationId: Applicat
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[User] =
unsupportedOn("createUser")
grpcClient.userManagementClient.createUser(user, rights)

override def getUser(id: UserId)(implicit
ec: ExecutionContext,
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[Option[User]] =
unsupportedOn("getUser")
grpcClient.userManagementClient.getUser(id).map(Some(_)).recover {
case e: StatusRuntimeException if e.getStatus.getCode == Status.Code.NOT_FOUND => None
}

override def deleteUser(id: UserId)(implicit
ec: ExecutionContext,
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[Unit] =
unsupportedOn("deleteUser")
grpcClient.userManagementClient.deleteUser(id)

override def listUsers()(implicit
ec: ExecutionContext,
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[List[User]] =
unsupportedOn("listUsers")
grpcClient.userManagementClient.listUsers().map(_.toList)

override def grantUserRights(
id: UserId,
Expand All @@ -381,7 +383,7 @@ class GrpcLedgerClient(val grpcClient: LedgerClient, val applicationId: Applicat
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[List[UserRight]] =
unsupportedOn("grantUserRights")
grpcClient.userManagementClient.grantUserRights(id, rights).map(_.toList)

override def revokeUserRights(
id: UserId,
Expand All @@ -391,12 +393,12 @@ class GrpcLedgerClient(val grpcClient: LedgerClient, val applicationId: Applicat
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[List[UserRight]] =
unsupportedOn("revokeUserRights")
grpcClient.userManagementClient.revokeUserRights(id, rights).map(_.toList)

override def listUserRights(id: UserId)(implicit
ec: ExecutionContext,
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[List[UserRight]] =
unsupportedOn("listUserRights")
grpcClient.userManagementClient.listUserRights(id).map(_.toList)
}
2 changes: 2 additions & 0 deletions daml-script/test/daml-script-test-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ ScriptTest:testQueryContractId SUCCESS
ScriptTest:testQueryContractKey SUCCESS
ScriptTest:testSetTime SUCCESS
ScriptTest:testStack SUCCESS
ScriptTest:testUserManagement FAILURE (com.daml.lf.engine.script.ScriptF$FailedCmd: Command listUsers failed: UNIMPLEMENTED: Method not found: com.daml.ledger.api.v1.admin.UserManagementService/ListUsers
ScriptTest:testUserRightManagement FAILURE (com.daml.lf.engine.script.ScriptF$FailedCmd: Command createUser failed: UNIMPLEMENTED: Method not found: com.daml.ledger.api.v1.admin.UserManagementService/CreateUser
ScriptTest:traceOrder SUCCESS
ScriptTest:tree SUCCESS
ScriptTest:tupleKey SUCCESS
Expand Down
54 changes: 54 additions & 0 deletions daml-script/test/daml/ScriptTest.daml
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,57 @@ jsonMultiPartyPartySets (p1, p2) = do
r <- queryContractId p2 ownedByP1
r === None
pure ()

-- User created by Sandbox by default.
participantAdmin : User
participantAdmin = User "participant_admin" None

testUserManagement = do
users <- listUsers
users === [participantAdmin]
u1 <- createUser (User "u1" None) []
u1 === User "u1" None
u2 <- createUser (User "u2" None) []
u2 === User "u2" None
u <- getUser "u1"
u === Some u1
u <- getUser "u2"
u === Some u2
u <- getUser "nonexistent"
u === None
users <- listUsers
users === [User "u1" None, User "u2" None, participantAdmin]
deleteUser "u1"
users <- listUsers
users === [User "u2" None, participantAdmin]
deleteUser "u2"
users <- listUsers
users === [participantAdmin]
pure ()

testUserRightManagement = do
p1 <- allocateParty "p1"
p2 <- allocateParty "p2"
u1 <- createUser (User "u1" None) []
rights <- listUserRights "u1"
rights === []
newRights <- grantUserRights "u1" [ParticipantAdmin]
newRights === [ParticipantAdmin]
newRights <- grantUserRights "u1" [ParticipantAdmin]
newRights === []
rights <- listUserRights "u1"
rights === [ParticipantAdmin]
newRights <- grantUserRights "u1" [CanActAs p1, CanReadAs p2]
newRights === [CanActAs p1, CanReadAs p2]
rights <- listUserRights "u1"
rights === [ParticipantAdmin, CanActAs p1, CanReadAs p2]
revoked <- revokeUserRights "u1" [ParticipantAdmin]
revoked === [ParticipantAdmin]
revoked <- revokeUserRights "u1" [ParticipantAdmin]
revoked === []
rights <- listUserRights "u1"
rights === [CanActAs p1, CanReadAs p2]
revoked <- revokeUserRights "u1" [CanActAs p1, CanReadAs p2]
revoked === [CanActAs p1, CanReadAs p2]
rights <- listUserRights "u1"
rights === []
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,31 @@ abstract class AbstractFuncIT
)
}
}
// TODO https://github.com/digital-asset/daml/issues/11997
// Enable once #12063 is merged
"testUserManagement should succeed" ignore {
for {
clients <- participantClients()
r <-
run(
clients,
QualifiedName.assertFromString("ScriptTest:testUserManagement"),
dar = stableDar,
)
} yield r shouldBe SUnit
}
// TODO https://github.com/digital-asset/daml/issues/11997
// Enable once #12063 is merged
"testUserRightManagement should succeed" ignore {
for {
clients <- participantClients()
r <-
run(
clients,
QualifiedName.assertFromString("ScriptTest:testUserRightManagement"),
dar = stableDar,
)
} yield r shouldBe SUnit
}
}
}

0 comments on commit f59760e

Please sign in to comment.