コード例 #1
0
        /// <summary>
        /// Gets the default Account or Group Store at the given <c>href</c>, if it exists.
        /// </summary>
        /// <param name="storeMappingHref">The AccountStoreMapping <c>href</c>.</param>
        /// <param name="internalDataStore">The <see cref="IInternalAsyncDataStore"/>.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The <see cref="IAccountStore">Account Store</see>, or <see langword="null"/>.</returns>
        public static async Task <IAccountStore> GetDefaultStoreAsync(
            string storeMappingHref,
            IInternalAsyncDataStore internalDataStore,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(storeMappingHref))
            {
                return(null);
            }

            var accountStoreMapping = await internalDataStore
                                      .GetResourceAsync <IAccountStoreMapping>(storeMappingHref, cancellationToken)
                                      .ConfigureAwait(false);

            return(accountStoreMapping == null
                ? null
                : await accountStoreMapping.GetAccountStoreAsync().ConfigureAwait(false));
        }
コード例 #2
0
        /// <summary>
        /// Adds an Account Store to this resource by <c>href</c> or name.
        /// </summary>
        /// <typeparam name="TMapping">The Account Store Mapping type.</typeparam>
        /// <param name="container">The Account Store container.</param>
        /// <param name="internalDataStore">The internal data store.</param>
        /// <param name="hrefOrName">The name or <c>href</c> of the Account Store to add.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The new <see cref="IAccountStoreMapping"/>.</returns>
        public static async Task <TMapping> AddAccountStoreAsync <TMapping>(
            IAccountStoreContainer <TMapping> container,
            IInternalAsyncDataStore internalDataStore,
            string hrefOrName,
            CancellationToken cancellationToken)
            where TMapping : class, IAccountStoreMapping <TMapping>
        {
            if (string.IsNullOrEmpty(hrefOrName))
            {
                throw new ArgumentNullException(nameof(hrefOrName));
            }

            IAccountStore accountStore = null;

            var  splitHrefOrName = hrefOrName.Split('/');
            bool looksLikeHref   = splitHrefOrName.Length > 4;

            if (looksLikeHref)
            {
                bool?isDirectoryType = null;
                if (splitHrefOrName.Length == container.Href.Split('/').Length)
                {
                    if (splitHrefOrName[4].Equals("directories", StringComparison.InvariantCultureIgnoreCase))
                    {
                        isDirectoryType = true;
                    }
                    else if (splitHrefOrName[4].Equals("groups", StringComparison.InvariantCultureIgnoreCase))
                    {
                        isDirectoryType = false;
                    }
                }

                if (isDirectoryType != null)
                {
                    try
                    {
                        if (isDirectoryType == true)
                        {
                            accountStore = await internalDataStore.GetResourceAsync <IDirectory>(hrefOrName, cancellationToken).ConfigureAwait(false);
                        }
                        else if (isDirectoryType == false)
                        {
                            accountStore = await internalDataStore.GetResourceAsync <IGroup>(hrefOrName, cancellationToken).ConfigureAwait(false);
                        }
                    }
                    catch (ResourceException)
                    {
                        // It looked like an href, but no resource was found.
                        // We'll try looking it up by name.
                    }
                }
            }

            if (accountStore == null)
            {
                // Try to find both a Directory and a Group with the given name
                var directory = await GetSingleTenantDirectoryAsync(container, x => x.Where(d => d.Name == hrefOrName), cancellationToken).ConfigureAwait(false);

                var group = await GetSingleTenantGroupAsync(container, x => x.Where(g => g.Name == hrefOrName), cancellationToken).ConfigureAwait(false);

                if (directory != null && group != null)
                {
                    throw new ArgumentException(
                              "There is both a Directory and a Group matching the provided name in the current tenant. " +
                              "Please provide the href of the intended Resource instead of its name in order to unambiguously identify it.");
                }

                accountStore = directory != null
                    ? directory as IAccountStore
                    : group as IAccountStore;
            }

            if (accountStore != null)
            {
                return((TMapping) await AddAccountStoreAsync(container, internalDataStore, accountStore, cancellationToken).ConfigureAwait(false));
            }

            // Could not find any resource matching the hrefOrName value
            return(null);
        }