public IHttpActionResult GetResourceByQuery( string connectionInfo, string query, [FromUri] string[] attributesToGet = null, int pageSize = 0, int index = 0, int cultureKey = 127, bool resolveID = false, bool deepResolve = false, [FromUri] string[] attributesToResolve = null, [FromUri] string[] attributesToSort = null) { try { Lazy <IOCGDSRepository> repo = RepositoryManager.GetRepository(repos, "MIMResource"); if (repo != null) { ConnectionInfo ci = ConnectionInfo.BuildConnectionInfo(connectionInfo); ResourceOption ro = new ResourceOption( ci, cultureKey, resolveID, deepResolve, attributesToResolve, attributesToSort); DSResourceSet rss = repo.Value.GetResourceByQuery( query, (attributesToGet == null || attributesToGet.Length == 0) ? new string[] { "DisplayName" } : attributesToGet, pageSize, index, ro); return(Ok(rss)); } else { return(NotFound()); } } catch (Exception exp) { return(InternalServerError(exp)); } }
public DSResourceSet GetResourceByQuery(string query, string[] attributes, int pageSize = 0, int index = 0, ResourceOption resourceOption = null) { ResourceOption option = resourceOption == null ? new ResourceOption() : resourceOption; ResourceManagementClient client = getClient(option.ConnectionInfo); client.RefreshSchema(); DSResourceSet retVal = new DSResourceSet(); List <SortingAttribute> sortingAttributes = getSortingAttributes(option.SortingAttributes); if (pageSize == 0) { SearchResultCollection src = sortingAttributes.Count == 0 ? client.GetResources(query, attributes) as SearchResultCollection : client.GetResources(query, attributes, sortingAttributes) as SearchResultCollection; if (src != null) { retVal.TotalCount = src.Count; foreach (ResourceObject resource in src) { retVal.Resources.Add(convertToDSResource(client, resource, attributes, false, option)); } } } else { SearchResultPager srp = sortingAttributes.Count == 0 ? client.GetResourcesPaged(query, pageSize, attributes) : client.GetResourcesPaged(query, pageSize, attributes, sortingAttributes); if (index >= 0) { srp.CurrentIndex = index; } srp.PageSize = pageSize; foreach (ResourceObject resource in srp.GetNextPage()) { retVal.Resources.Add(convertToDSResource(client, resource, attributes, false, option)); } retVal.TotalCount = srp.TotalCount; retVal.HasMoreItems = srp.HasMoreItems; } return(retVal); }
public IHttpActionResult GetCurrentUser([FromUri] string[] attributesToGet = null) { WindowsImpersonationContext wic = null; try { wic = ((WindowsIdentity)User.Identity).Impersonate(); Lazy <IOCGDSRepository> repo = RepositoryManager.GetRepository(repos, "MIMResource"); if (repo != null) { ResourceOption ro = new ResourceOption(); string userName = HttpContext.Current.User.Identity.Name; int pos = userName.IndexOf(@"\"); string accountName = userName.Substring(pos + 1); string query = string.Format(@"/Person[AccountName='{0}']", accountName); DSResourceSet rss = repo.Value.GetResourceByQuery(query, (attributesToGet == null || attributesToGet.Length == 0) ? new string[] { "DisplayName" } : attributesToGet); if (rss != null && rss.TotalCount == 1) { return(Ok(rss.Resources.First())); } else { return(InternalServerError(new Exception($"account not found: {accountName}"))); } } else { return(NotFound()); } } catch (Exception exp) { return(InternalServerError(exp)); } finally { if (wic != null) { wic.Undo(); } } }
public IHttpActionResult GetResourceByQuery( string query, [FromUri] string[] attributesToGet = null, int pageSize = 0, int index = 0, int cultureKey = 127, bool resolveID = false, bool deepResolve = false, [FromUri] string[] attributesToResolve = null, [FromUri] string[] attributesToSort = null) { WindowsImpersonationContext wic = null; try { wic = ((WindowsIdentity)User.Identity).Impersonate(); Lazy <IOCGDSRepository> repo = RepositoryManager.GetRepository(repos, "MIMResource"); if (repo != null) { ResourceOption ro = new ResourceOption( new ConnectionInfo(), cultureKey, resolveID, deepResolve, attributesToResolve, attributesToSort); DSResourceSet rss = repo.Value.GetResourceByQuery( query, (attributesToGet == null || attributesToGet.Length == 0) ? new string[] { "DisplayName" } : attributesToGet, pageSize, index, ro); return(Ok(rss)); } else { return(NotFound()); } } catch (Exception exp) { return(InternalServerError(exp)); } finally { if (wic != null) { wic.Undo(); } } }
public async Task <ActionResult <DSResourceSet> > GetResourceByQuery( [FromHeader, Required] string token, [FromQuery, Required] string query, [FromQuery] string attributes, [FromQuery] int pageSize = 0, [FromQuery] int index = 0, [FromQuery] bool resolveRef = false, [FromQuery] string orderBy = null) { try { DSResourceSet result = await Task.Run(() => { string[] attributeArray = string.IsNullOrEmpty(attributes) ? null : attributes.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray(); Dictionary <string, string> sortingAttributes = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(orderBy)) { foreach (string sortingAttribute in orderBy.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim())) { string[] sortingDef = sortingAttribute .Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray(); if (sortingDef.Length == 2) { sortingAttributes.Add(sortingDef[0], sortingDef[1]); } else { throw new ArgumentException("invalid sorting attributes"); } } } foreach (string key in sortingAttributes.Keys) { if (!attributeArray.Contains(key)) { throw new ArgumentException("loading attributes don't include sorting attributes"); } } return(this.repo.GetResourceByQuery(token, query, attributeArray, pageSize, index, resolveRef, sortingAttributes)); }); return(result); } catch (ArgumentException e) { return(this.BadRequest(e.Message)); } catch (InvalidOperationException e) { return(this.Conflict(e.Message)); } catch (Exception e) { return(this.BadRequest(e.Message)); } }
public DSResourceSet GetResourceByQuery(string token, string query, string[] attributes, int pageSize = 0, int index = 0, bool resolveRef = false, Dictionary <string, string> orderBy = null) { if (string.IsNullOrEmpty(query)) { throw new ArgumentException("xpath query must be specified"); } DSResourceSet result = new DSResourceSet(); List <SortingAttribute> sortingAttributes = new List <SortingAttribute>(); if (orderBy != null && orderBy.Count > 0) { foreach (KeyValuePair <string, string> kvp in orderBy) { sortingAttributes.Add(new SortingAttribute { AttributeName = kvp.Key, Ascending = new string[] { "ascending", "asc" }.Contains(kvp.Value, StringComparer.OrdinalIgnoreCase) ? true : false }); } } ResourceManagementClient client = Utiles.GetClient(repoCache, token); ResourceManagementClient rmClient = resolveRef ? client : null; if (pageSize == 0) { SearchResultCollection src; if (attributes == null || attributes.Length == 0) { src = client.GetResources(query) as SearchResultCollection; } else { src = sortingAttributes.Count == 0 ? client.GetResources(query, attributes) as SearchResultCollection : client.GetResources(query, attributes, sortingAttributes) as SearchResultCollection; } if (src != null) { result.TotalCount = src.Count; foreach (ResourceObject resource in src) { result.Results.Add(Utiles.BuildSimpleResource( resource, attributes == null || attributes.Length == 0? null : attributes.ToList(), rmClient)); } } } else { SearchResultPager srp; if (attributes == null || attributes.Length == 0) { srp = client.GetResourcesPaged(query, pageSize); } else { srp = sortingAttributes.Count == 0 ? client.GetResourcesPaged(query, pageSize, attributes) : client.GetResourcesPaged(query, pageSize, attributes, sortingAttributes); } if (index >= 0) { srp.CurrentIndex = index; } srp.PageSize = pageSize; foreach (ResourceObject resource in srp.GetNextPage()) { result.Results.Add(Utiles.BuildSimpleResource( resource, attributes == null || attributes.Length == 0? null : attributes.ToList(), rmClient)); } result.TotalCount = srp.TotalCount; result.HasMoreItems = srp.HasMoreItems; } return(result); }