/// <summary>
 /// Adds a list of principals to the given collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.RemoteApp.IPrincipalOperations.
 /// </param>
 /// <param name='collectionName'>
 /// Required. The RemoteApp collection name.
 /// </param>
 /// <param name='securityPrincipalList'>
 /// Required. A list of RemoteApp principals to add.
 /// </param>
 /// <returns>
 /// The response for the collection user operation.
 /// </returns>
 public static SecurityPrincipalOperationsResult Add(this IPrincipalOperations operations, string collectionName, SecurityPrincipalList securityPrincipalList)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IPrincipalOperations)s).AddAsync(collectionName, securityPrincipalList);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
Esempio n. 2
0
        public static int SetUpRemoteAppUserToRemoveFromApp(Mock<IRemoteAppManagementClient> clientMock, string collectionName, string appAlias, PrincipalProviderType userIdType, string[] userNames)
        {
            SecurityPrincipalOperationsResult response = new SecurityPrincipalOperationsResult()
            {
                RequestId = "122-13342",
                TrackingId = "1348570-182754",
                StatusCode = System.Net.HttpStatusCode.Accepted,
                Errors = null
            };
            mockSecurityPrincipalResult = new List<SecurityPrincipalOperationsResult>()
            {
                new SecurityPrincipalOperationsResult()
                {
                    RequestId = response.RequestId,
                    TrackingId = response.TrackingId,
                    StatusCode = response.StatusCode,
                    Errors = response.Errors
                },
            };

            SecurityPrincipalList spRemove = new SecurityPrincipalList();

            foreach (string userName in userNames)
            {
                SecurityPrincipal mockUser = new SecurityPrincipal()
                {
                    Name = userName,
                    SecurityPrincipalType = PrincipalType.User,
                    UserIdType = userIdType,
                };
                spRemove.SecurityPrincipals.Add(mockUser);
            }

            ISetup<IRemoteAppManagementClient, Task<SecurityPrincipalOperationsResult>> setup = clientMock.Setup(c => c.Principals.DeleteFromAppAsync(collectionName, appAlias, It.IsAny<SecurityPrincipalList>(), It.IsAny<CancellationToken>()));
            setup.Returns(Task.Factory.StartNew(() => response));

            mockUsers = spRemove.SecurityPrincipals;

            return mockUsers.Count;
        }
        protected SecurityPrincipalList BuildUserList(string[] Users, PrincipalProviderType userIdType)
        {
            SecurityPrincipalList userList = new SecurityPrincipalList();
            List<SecurityPrincipal> spList = new List<SecurityPrincipal>();

            foreach (string user in Users)
            {
                SecurityPrincipal principal = new SecurityPrincipal()
                {
                    AadObjectId = null,
                    Description = null,
                    Name = user,
                    SecurityPrincipalType = PrincipalType.User,
                    UserIdType = userIdType
                };

                spList.Add(principal);
            }

            userList.SecurityPrincipals = spList;

            return userList;
        }
 /// <summary>
 /// Adds a list of principals to the given collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.RemoteApp.IPrincipalOperations.
 /// </param>
 /// <param name='collectionName'>
 /// Required. The RemoteApp collection name.
 /// </param>
 /// <param name='securityPrincipalList'>
 /// Required. A list of RemoteApp principals to add.
 /// </param>
 /// <returns>
 /// The response for the collection user operation.
 /// </returns>
 public static Task<SecurityPrincipalOperationsResult> AddAsync(this IPrincipalOperations operations, string collectionName, SecurityPrincipalList securityPrincipalList)
 {
     return operations.AddAsync(collectionName, securityPrincipalList, CancellationToken.None);
 }
 /// <summary>
 /// Deletes a list of principals from the given app.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.RemoteApp.IPrincipalOperations.
 /// </param>
 /// <param name='collectionName'>
 /// Required. The RemoteApp collection name.
 /// </param>
 /// <param name='appAlias'>
 /// Required. Application alias.
 /// </param>
 /// <param name='securityPrincipalList'>
 /// Required. A list of RemoteApp principals to delete from the
 /// published app.
 /// </param>
 /// <returns>
 /// The response for the collection user operation.
 /// </returns>
 public static Task<SecurityPrincipalOperationsResult> DeleteFromAppAsync(this IPrincipalOperations operations, string collectionName, string appAlias, SecurityPrincipalList securityPrincipalList)
 {
     return operations.DeleteFromAppAsync(collectionName, appAlias, securityPrincipalList, CancellationToken.None);
 }
        public void CanNotAddInvalidPrincipalToCollection()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                RemoteAppManagementClient client = GetRemoteAppManagementClient();

                string collectionName = "asquick";

                SecurityPrincipal user = new SecurityPrincipal("johndoe_test");
                user.SecurityPrincipalType = PrincipalType.User;
                user.UserIdType = PrincipalProviderType.MicrosoftAccount;

                SecurityPrincipalList principals = new SecurityPrincipalList();
                principals.SecurityPrincipals.Add(user);

                SecurityPrincipalOperationsResult result = client.Principals.Add(collectionName, principals);

                Assert.NotNull(result);
                Assert.NotNull(result.Errors);
                Assert.NotEmpty(result.Errors);
            }
        }
        public void CanAddAndRemovePrincipalToApplication()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string collectionName = "testd165";
                string appAlias = "085b40de-e013-4750-a0f4-082aa0d80adc";

                RemoteAppManagementClient client = GetRemoteAppManagementClient();
                client.RdfeNamespace = "rdsm54westus";

                // verifying the added principals
                SecurityPrincipalInfoListResult principalList = client.Principals.ListForApp(collectionName, appAlias);

                int numberOfUsersBeforeAdd = principalList.SecurityPrincipalInfoList.Count;

                SecurityPrincipalList principals = new SecurityPrincipalList();

                // adding the principals to the collection

                SecurityPrincipal user = new SecurityPrincipal("*****@*****.**");
                user.SecurityPrincipalType = PrincipalType.User;
                user.UserIdType = PrincipalProviderType.OrgId;

                principals.SecurityPrincipals.Add(user);

                SecurityPrincipalOperationsResult result = client.Principals.AddToApp(collectionName, appAlias, principals);

                Assert.NotNull(result);
                Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode == System.Net.HttpStatusCode.Accepted, "Failed to add security principal. Status code: " + result.StatusCode + ".");
                Assert.NotNull(result.Errors);
                Assert.Empty(result.Errors);

                // verifying the added principals
                principalList = client.Principals.ListForApp(collectionName, appAlias);

                Assert.NotNull(principalList);

                // verify that all the requested users are added
                Assert.True(principalList.SecurityPrincipalInfoList.Count == (numberOfUsersBeforeAdd + principals.SecurityPrincipals.Count), "Add users did not add the requested users to the collection.");

                List<SecurityPrincipal> matchedPrincipals = new List<SecurityPrincipal>();

                foreach (var principal in principalList.SecurityPrincipalInfoList)
                {
                    foreach (SecurityPrincipal p in principals.SecurityPrincipals)
                    {
                        if (String.Equals(principal.SecurityPrincipal.Name, p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            matchedPrincipals.Add(principal.SecurityPrincipal);
                        }
                    }
                }

                Assert.True(matchedPrincipals.Count() == 1);
                Assert.Equal(matchedPrincipals.First().Name.ToLowerInvariant(), user.Name.ToLowerInvariant());

                // now remove the added security principals here
                result = client.Principals.DeleteFromApp(collectionName, appAlias, principals);

                Assert.NotNull(result);
                Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode == System.Net.HttpStatusCode.Accepted, "Failed to remove security principal. Status code: " + result.StatusCode + ".");

                // verifying the deletion of the principals
                principalList = client.Principals.ListForApp(collectionName, appAlias);

                Assert.NotNull(principalList);

                // verify that all the requested users are added
                Assert.Equal(principalList.SecurityPrincipalInfoList.Count, numberOfUsersBeforeAdd);

                matchedPrincipals.Clear();
                Assert.Empty(matchedPrincipals);

                foreach (var principal in principalList.SecurityPrincipalInfoList)
                {
                    foreach (SecurityPrincipal p in principals.SecurityPrincipals)
                    {
                        if (String.Equals(principal.SecurityPrincipal.Name, p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            matchedPrincipals.Add(principal.SecurityPrincipal);
                        }
                    }
                }

                Assert.Empty(matchedPrincipals);
            }
        }
        public void CanAddAndRemovePrincipalToCollection()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string collectionName = "simple";

                RemoteAppManagementClient client = GetRemoteAppManagementClient();

                // verifying the added principals
                SecurityPrincipalInfoListResult principalList = client.Principals.List(collectionName);

                int numberOfUsersBeforeAdd = principalList.SecurityPrincipalInfoList.Count;

                SecurityPrincipalList principals = new SecurityPrincipalList();

                // adding the principals to the collection

                SecurityPrincipal user = new SecurityPrincipal("*****@*****.**");
                user.SecurityPrincipalType = PrincipalType.User;
                user.UserIdType = PrincipalProviderType.MicrosoftAccount;

                principals.SecurityPrincipals.Add(user);

                SecurityPrincipalOperationsResult result = client.Principals.Add(collectionName, principals);

                Assert.NotNull(result);
                Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode == System.Net.HttpStatusCode.Accepted, "Failed to add security principal. Status code: " + result.StatusCode + ".");
                Assert.NotNull(result.Errors);
                Assert.Empty(result.Errors);

                // verifying the added principals
                principalList = client.Principals.List(collectionName);

                Assert.NotNull(principalList);

                // verify that all the requested users are added
                Assert.True(principalList.SecurityPrincipalInfoList.Count == (numberOfUsersBeforeAdd + principals.SecurityPrincipals.Count), "Add users did not add the requested users to the collection.");

                List<SecurityPrincipal> matchedPrincipals = new List<SecurityPrincipal>();

                foreach (var principal in principalList.SecurityPrincipalInfoList)
                {
                    foreach (SecurityPrincipal p in principals.SecurityPrincipals)
                    {
                        if (String.Equals(principal.SecurityPrincipal.Name, p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            matchedPrincipals.Add(principal.SecurityPrincipal);
                        }
                    }
                }

                Assert.True(matchedPrincipals.Count() == 1);
                Assert.Equal(matchedPrincipals.First().Name.ToLowerInvariant(), user.Name.ToLowerInvariant());

                // now remove the added security principals here
                result = client.Principals.Delete(collectionName, principals);

                Assert.NotNull(result);
                Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode == System.Net.HttpStatusCode.Accepted, "Failed to remove security principal. Status code: " + result.StatusCode + ".");

                // verifying the deletion of the principals
                principalList = client.Principals.List(collectionName);

                Assert.NotNull(principalList);

                // verify that all the requested users are added
                Assert.Equal(principalList.SecurityPrincipalInfoList.Count, numberOfUsersBeforeAdd);

                matchedPrincipals.Clear();
                Assert.Empty(matchedPrincipals);

                foreach (var principal in principalList.SecurityPrincipalInfoList)
                {
                    foreach (SecurityPrincipal p in principals.SecurityPrincipals)
                    {
                        if (String.Equals(principal.SecurityPrincipal.Name, p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            matchedPrincipals.Add(principal.SecurityPrincipal);
                        }
                    }
                }

                Assert.Empty(matchedPrincipals);
            }
        }