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 ConsentStatusModel(SecurityPrincipalInfo securityPrincipalInfo) { Name = securityPrincipalInfo.SecurityPrincipal.Name; UserIdType = securityPrincipalInfo.SecurityPrincipal.UserIdType; ConsentStatus = securityPrincipalInfo.Status; }
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(); } } }