Implement Add/Remove User to/from Group
This commit is contained in:
@@ -93,5 +93,31 @@ namespace VMware.vSphere.SsoAdminClient.Tests
|
||||
Assert.Greater(actual.Length, 1);
|
||||
Assert.AreEqual("localos", actual[0].Domain);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddRemoveUserFromGroup() {
|
||||
// Arrange
|
||||
var ssoAdminClient = new SsoAdminClient(_vc, _user, _password, new AcceptAllX509CertificateValidator());
|
||||
|
||||
var expectedUserName = "test-user5";
|
||||
var expectedPassword = "te$tPa$sW0rd";
|
||||
var newUser = ssoAdminClient.CreateLocalUser(
|
||||
expectedUserName,
|
||||
expectedPassword);
|
||||
|
||||
var group = ssoAdminClient.GetGroups("administrators", newUser.Domain).FirstOrDefault<Group>();
|
||||
|
||||
// Act
|
||||
var addActual = ssoAdminClient.AddPersonUserToGroup(newUser, group);
|
||||
var removeActual = ssoAdminClient.RemovePersonUserFromGroup(newUser, group);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(addActual);
|
||||
Assert.IsTrue(removeActual);
|
||||
|
||||
// Cleanup
|
||||
ssoAdminClient.DeleteLocalUser(
|
||||
newUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,6 +262,46 @@ namespace VMware.vSphere.SsoAdminClient
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool AddPersonUserToGroup(PersonUser user, DataTypes.Group group) {
|
||||
// Create Authorization Invocation Context
|
||||
var authorizedInvocationContext =
|
||||
CreateAuthorizedInvocationContext();
|
||||
|
||||
// Invoke SSO Admin AddUserToLocalGroupAsync operation
|
||||
return authorizedInvocationContext.
|
||||
InvokeOperation(() =>
|
||||
_ssoAdminBindingClient.AddUserToLocalGroupAsync(
|
||||
new ManagedObjectReference {
|
||||
type = "SsoAdminPrincipalManagementService",
|
||||
Value = "principalManagementService"
|
||||
},
|
||||
new SsoPrincipalId {
|
||||
name = user.Name,
|
||||
domain = user.Domain
|
||||
},
|
||||
group.Name)).Result;
|
||||
}
|
||||
|
||||
public bool RemovePersonUserFromGroup(PersonUser user, DataTypes.Group group) {
|
||||
// Create Authorization Invocation Context
|
||||
var authorizedInvocationContext =
|
||||
CreateAuthorizedInvocationContext();
|
||||
|
||||
// Invoke SSO Admin RemoveFromLocalGroupAsync operation
|
||||
return authorizedInvocationContext.
|
||||
InvokeOperation(() =>
|
||||
_ssoAdminBindingClient.RemoveFromLocalGroupAsync(
|
||||
new ManagedObjectReference {
|
||||
type = "SsoAdminPrincipalManagementService",
|
||||
Value = "principalManagementService"
|
||||
},
|
||||
new SsoPrincipalId {
|
||||
name = user.Name,
|
||||
domain = user.Domain
|
||||
},
|
||||
group.Name)).Result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,6 +262,80 @@ Describe "PersonUser Tests" {
|
||||
}
|
||||
}
|
||||
|
||||
Context "Set-PersonUser Add/Remove Group" {
|
||||
It 'Adds person user to group' {
|
||||
# Arrange
|
||||
$userName = "TestAddGroupPersonUserName"
|
||||
$userPassword = '$tr0NG_TestPa$$w0rd'
|
||||
$connection = Connect-SsoAdminServer `
|
||||
-Server $VcAddress `
|
||||
-User $User `
|
||||
-Password $Password `
|
||||
-SkipCertificateCheck
|
||||
|
||||
$personUserToUpdate = New-PersonUser `
|
||||
-UserName $userName `
|
||||
-Password $userPassword `
|
||||
-Server $connection
|
||||
|
||||
$script:usersToCleanup += $personUserToUpdate
|
||||
|
||||
$groupUserToBeAddedTo = Get-Group `
|
||||
-Name 'Administrators' `
|
||||
-Domain $personUserToUpdate.Domain `
|
||||
-Server $connection
|
||||
|
||||
# Act
|
||||
$actual = Set-PersonUser `
|
||||
-User $personUserToUpdate `
|
||||
-Group $groupUserToBeAddedTo `
|
||||
-Add `
|
||||
-Server $connection
|
||||
|
||||
# Assert
|
||||
$actual | Should Not Be $null
|
||||
}
|
||||
|
||||
It 'Removes person user from group' {
|
||||
# Arrange
|
||||
$userName = "TestRemoveGroupPersonUserName"
|
||||
$userPassword = '$tr0NG_TestPa$$w0rd'
|
||||
$connection = Connect-SsoAdminServer `
|
||||
-Server $VcAddress `
|
||||
-User $User `
|
||||
-Password $Password `
|
||||
-SkipCertificateCheck
|
||||
|
||||
$personUserToUpdate = New-PersonUser `
|
||||
-UserName $userName `
|
||||
-Password $userPassword `
|
||||
-Server $connection
|
||||
|
||||
$script:usersToCleanup += $personUserToUpdate
|
||||
|
||||
$groupToBeUsed = Get-Group `
|
||||
-Name 'Administrators' `
|
||||
-Domain $personUserToUpdate.Domain `
|
||||
-Server $connection
|
||||
|
||||
Set-PersonUser `
|
||||
-User $personUserToUpdate `
|
||||
-Group $groupToBeUsed `
|
||||
-Add `
|
||||
-Server $connection | Out-Null
|
||||
|
||||
# Act
|
||||
$actual = Set-PersonUser `
|
||||
-User $personUserToUpdate `
|
||||
-Group $groupToBeUsed `
|
||||
-Remove `
|
||||
-Server $connection
|
||||
|
||||
# Assert
|
||||
$actual | Should Not Be $null
|
||||
}
|
||||
}
|
||||
|
||||
Context "Remove-PersonUser" {
|
||||
It 'Removes person user' {
|
||||
# Arrange
|
||||
|
||||
Reference in New Issue
Block a user