public override void ExecuteCmdlet() { SecurityPrincipalInfoListResult response = null; bool found = false; showAllUsers = String.IsNullOrWhiteSpace(UserUpn); if (showAllUsers == false) { CreateWildcardPattern(UserUpn); } // You must pass in an empty string to this call. After that pass in the token returned by the previous call response = CallClient(() => Client.Principals.List(CollectionName), Client.Principals); if (response != null && response.SecurityPrincipalInfoList != null) { found = ProccessUsers(response); } if (!found && !showAllUsers) { WriteVerboseWithTimestamp(String.Format("User '{0}' is not assigned to Collection '{1}'.", UserUpn, CollectionName)); } }
private bool ProccessUsers(SecurityPrincipalInfoListResult response) { ConsentStatusModel model = null; bool found = false; if (ExactMatch) { SecurityPrincipalInfo userconsent = null; userconsent = response.SecurityPrincipalInfoList.FirstOrDefault(user => user.SecurityPrincipal.SecurityPrincipalType == PrincipalType.User && String.Equals(user.SecurityPrincipal.Name, UserUpn, StringComparison.OrdinalIgnoreCase)); if (userconsent == null) { WriteErrorWithTimestamp("User: "******" does not exist in collection " + CollectionName); found = false; } else { model = new ConsentStatusModel(userconsent); WriteObject(model); found = true; } } else { IEnumerable <SecurityPrincipalInfo> spList = null; if (showAllUsers) { spList = response.SecurityPrincipalInfoList.Where(user => user.SecurityPrincipal.SecurityPrincipalType == PrincipalType.User); } else { spList = response.SecurityPrincipalInfoList.Where(user => user.SecurityPrincipal.SecurityPrincipalType == PrincipalType.User && Wildcard.IsMatch(user.SecurityPrincipal.Name)); } if (spList != null && spList.Count() > 0) { List <SecurityPrincipalInfo> userConsents = new List <SecurityPrincipalInfo>(spList); IComparer <SecurityPrincipalInfo> comparer = new ServicePrincipalComparer(); userConsents.Sort(comparer); foreach (SecurityPrincipalInfo consent in userConsents) { model = new ConsentStatusModel(consent); WriteObject(model); } found = true; } } return(found); }
public void CanGetRemoteAppPrincipalListForApp() { using (var undoContext = UndoContext.Current) { undoContext.Start(); var client = GetRemoteAppManagementClient(); client.RdfeNamespace = "rdsm54westus"; string collectionName = "testd165"; string appAlias = "867aca6b-9b32-4db8-b5b2-2160ec986169"; SecurityPrincipalInfoListResult principalList = client.Principals.ListForApp(collectionName, appAlias); Assert.NotNull(principalList); Assert.True(principalList.SecurityPrincipalInfoList.Count > 0, "No user assigned to the collection with id: " + collectionName + "."); } }
public void CanGetRemoteAppPrincipalList() { using (var undoContext = UndoContext.Current) { undoContext.Start(); var client = GetRemoteAppManagementClient(); // get the list of active collections IEnumerable <Collection> collections = GetAllActiveCollections(client); foreach (Collection collection in collections) { SecurityPrincipalInfoListResult principalList = client.Principals.List(collection.Name); Assert.NotNull(principalList); Assert.True(principalList.SecurityPrincipalInfoList.Count > 0, "No user assigned to the collection with id: " + collection.Name + "."); } } }
public static int SetUpRemoteAppSecurityPrincipalsForApp(Mock <IRemoteAppManagementClient> clientMock, string collectionName, string appAlias, string userName) { SecurityPrincipalInfoListResult response = new SecurityPrincipalInfoListResult(); response.SecurityPrincipalInfoList = new List <SecurityPrincipalInfo>() { new SecurityPrincipalInfo() { SecurityPrincipal = new SecurityPrincipal() { Name = userName, SecurityPrincipalType = PrincipalType.User, UserIdType = PrincipalProviderType.OrgId, }, Status = ConsentStatus.Pending }, new SecurityPrincipalInfo() { SecurityPrincipal = new SecurityPrincipal() { Name = "user2", SecurityPrincipalType = PrincipalType.User, UserIdType = PrincipalProviderType.OrgId, }, Status = ConsentStatus.Pending }, }; mockUsersConsents = new List <ConsentStatusModel>(); foreach (SecurityPrincipalInfo consent in response.SecurityPrincipalInfoList) { mockUsersConsents.Add(new ConsentStatusModel(consent)); } ; ISetup <IRemoteAppManagementClient, Task <SecurityPrincipalInfoListResult> > setup = clientMock.Setup(c => c.Principals.ListForAppAsync(collectionName, appAlias, It.IsAny <CancellationToken>())); setup.Returns(Task.Factory.StartNew(() => response)); return(mockUsersConsents.Count); }
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); } }
public override void ExecuteCmdlet() { SecurityPrincipalInfoListResult response = null; ConsentStatusModel model = null; bool showAllUsers = String.IsNullOrWhiteSpace(UserUpn); bool found = false; if (showAllUsers == false) { CreateWildcardPattern(UserUpn); } response = CallClient(() => Client.Principals.List(CollectionName), Client.Principals); if (response != null && response.SecurityPrincipalInfoList != null) { if (ExactMatch) { SecurityPrincipalInfo userconsent = null; userconsent = response.SecurityPrincipalInfoList.FirstOrDefault(user => user.SecurityPrincipal.SecurityPrincipalType == PrincipalType.User && String.Equals(user.SecurityPrincipal.Name, UserUpn, StringComparison.OrdinalIgnoreCase)); if (userconsent == null) { WriteErrorWithTimestamp("User: "******" does not exist in collection " + CollectionName); found = false; } else { model = new ConsentStatusModel(userconsent); WriteObject(model); found = true; } } else { IEnumerable <SecurityPrincipalInfo> spList = null; if (showAllUsers) { spList = response.SecurityPrincipalInfoList.Where(user => user.SecurityPrincipal.SecurityPrincipalType == PrincipalType.User); } else { spList = response.SecurityPrincipalInfoList.Where(user => user.SecurityPrincipal.SecurityPrincipalType == PrincipalType.User && Wildcard.IsMatch(user.SecurityPrincipal.Name)); } if (spList != null && spList.Count() > 0) { List <SecurityPrincipalInfo> userConsents = new List <SecurityPrincipalInfo>(spList); IComparer <SecurityPrincipalInfo> comparer = new ServicePrincipalComparer(); userConsents.Sort(comparer); foreach (SecurityPrincipalInfo consent in spList) { model = new ConsentStatusModel(consent); WriteObject(model); } found = true; } } } if (!found && !showAllUsers) { WriteVerboseWithTimestamp(String.Format("User '{0}' is not assigned to Collection '{1}'.", UserUpn, CollectionName)); } }
/// <summary> /// Gets a list of all RemoteApp principals associated with the given /// collection. /// </summary> /// <param name='collectionName'> /// Required. The RemoteApp collection name. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The list of principals with consent status. /// </returns> public async Task <SecurityPrincipalInfoListResult> ListAsync(string collectionName, CancellationToken cancellationToken) { // Validate if (collectionName == null) { throw new ArgumentNullException("collectionName"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("collectionName", collectionName); TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/services/"; if (this.Client.RdfeNamespace != null) { url = url + Uri.EscapeDataString(this.Client.RdfeNamespace); } url = url + "/collections/"; url = url + Uri.EscapeDataString(collectionName); url = url + "/securityPrincipals"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2014-09-01"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/json; charset=utf-8"); httpRequest.Headers.Add("x-ms-version", "2014-08-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result SecurityPrincipalInfoListResult result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new SecurityPrincipalInfoListResult(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { JToken securityPrincipalInfoListArray = responseDoc; if (securityPrincipalInfoListArray != null && securityPrincipalInfoListArray.Type != JTokenType.Null) { foreach (JToken securityPrincipalInfoListValue in ((JArray)securityPrincipalInfoListArray)) { SecurityPrincipalInfo securityPrincipalInfoInstance = new SecurityPrincipalInfo(); result.SecurityPrincipalInfoList.Add(securityPrincipalInfoInstance); JToken userValue = securityPrincipalInfoListValue["User"]; if (userValue != null && userValue.Type != JTokenType.Null) { SecurityPrincipal userInstance = new SecurityPrincipal(); securityPrincipalInfoInstance.SecurityPrincipal = userInstance; JToken securityPrincipalTypeValue = userValue["SecurityPrincipalType"]; if (securityPrincipalTypeValue != null && securityPrincipalTypeValue.Type != JTokenType.Null) { PrincipalType securityPrincipalTypeInstance = ((PrincipalType)(((int)securityPrincipalTypeValue))); userInstance.SecurityPrincipalType = securityPrincipalTypeInstance; } JToken userIdTypeValue = userValue["UserIdType"]; if (userIdTypeValue != null && userIdTypeValue.Type != JTokenType.Null) { PrincipalProviderType userIdTypeInstance = ((PrincipalProviderType)(((int)userIdTypeValue))); userInstance.UserIdType = userIdTypeInstance; } JToken nameValue = userValue["Name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); userInstance.Name = nameInstance; } JToken aadObjectIdValue = userValue["AadObjectId"]; if (aadObjectIdValue != null && aadObjectIdValue.Type != JTokenType.Null) { string aadObjectIdInstance = ((string)aadObjectIdValue); userInstance.AadObjectId = aadObjectIdInstance; } JToken descriptionValue = userValue["Description"]; if (descriptionValue != null && descriptionValue.Type != JTokenType.Null) { string descriptionInstance = ((string)descriptionValue); userInstance.Description = descriptionInstance; } } JToken statusValue = securityPrincipalInfoListValue["Status"]; if (statusValue != null && statusValue.Type != JTokenType.Null) { ConsentStatus statusInstance = ((ConsentStatus)(((int)statusValue))); securityPrincipalInfoInstance.Status = statusInstance; } } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
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); } }