Пример #1
0
        /// <summary>
        /// Return a list of resources which this user is allowed to install from DBL.
        /// If we cannot contact DBL, return an empty list.
        /// </summary>
        /// <param name="userSecret">The user secret.</param>
        /// <param name="paratextOptions">The paratext options.</param>
        /// <param name="restClientFactory">The rest client factory.</param>
        /// <param name="fileSystemService">The file system service.</param>
        /// <param name="jwtTokenHelper">The JWT token helper.</param>
        /// <param name="baseUrl">The base URL.</param>
        /// <returns>The Installable Resources.</returns>
        /// <exception cref="ArgumentNullException">restClientFactory
        /// or
        /// userSecret</exception>
        /// <remarks>Tests on this method can be found in ParatextServiceTests.cs calling GetResources().</remarks>
        public static IEnumerable <SFInstallableDblResource> GetInstallableDblResources(UserSecret userSecret,
                                                                                        ParatextOptions paratextOptions, ISFRestClientFactory restClientFactory,
                                                                                        IFileSystemService fileSystemService, IJwtTokenHelper jwtTokenHelper, IExceptionHandler exceptionHandler,
                                                                                        string baseUrl = null)
        {
            // Parameter check (just like the constructor)
            if (restClientFactory == null)
            {
                throw new ArgumentNullException(nameof(restClientFactory));
            }
            else if (userSecret == null)
            {
                throw new ArgumentNullException(nameof(userSecret));
            }

            ISFRestClient client =
                restClientFactory.Create(string.Empty, ApplicationProduct.DefaultVersion, userSecret);

            baseUrl = string.IsNullOrWhiteSpace(baseUrl) ? InternetAccess.ParatextDBLServer : baseUrl;
            string response = null;

            try
            {
                response = client.Get(BuildDblResourceEntriesUrl(baseUrl));
            }
            catch (WebException e)
            {
                // If we get a temporary 401 Unauthorized response, return an empty list.
                string errorExplanation = "GetInstallableDblResources failed when attempting to inquire about"
                                          + $" resources and is ignoring error {e}";
                var report = new Exception(errorExplanation);
                // Report to bugsnag, but don't throw.
                exceptionHandler.ReportException(report);
                return(Enumerable.Empty <SFInstallableDblResource>());
            }
            IEnumerable <SFInstallableDblResource> resources = ConvertJsonResponseToInstallableDblResources(baseUrl,
                                                                                                            response, restClientFactory, fileSystemService, jwtTokenHelper, DateTime.Now, userSecret,
                                                                                                            paratextOptions, new ParatextProjectDeleter(), new ParatextMigrationOperations(),
                                                                                                            new ParatextZippedResourcePasswordProvider(paratextOptions));

            return(resources);
        }
Пример #2
0
        /// <summary>
        /// Checks the resource permission.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="userSecret">The user secret.</param>
        /// <param name="paratextOptions">The paratext options.</param>
        /// <param name="restClientFactory">The rest client factory.</param>
        /// <param name="fileSystemService">The file system service.</param>
        /// <param name="jwtTokenHelper">The JWT token helper.</param>
        /// <param name="baseUrl">The base URL.</param>
        /// <returns>
        ///   <c>true</c> if the user has permission to access the resource; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">id
        /// or
        /// userSecret
        /// or
        /// restClientFactory</exception>
        public static bool CheckResourcePermission(string id, UserSecret userSecret,
                                                   ParatextOptions paratextOptions, ISFRestClientFactory restClientFactory,
                                                   IFileSystemService fileSystemService, IJwtTokenHelper jwtTokenHelper,
                                                   IExceptionHandler exceptionHandler, string baseUrl = null)
        {
            // Parameter check
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }
            else if (userSecret == null)
            {
                throw new ArgumentNullException(nameof(userSecret));
            }
            else if (restClientFactory == null)
            {
                throw new ArgumentNullException(nameof(restClientFactory));
            }

            ISFRestClient client = restClientFactory.Create(string.Empty, ApplicationProduct.DefaultVersion, userSecret);

            baseUrl = string.IsNullOrWhiteSpace(baseUrl) ? InternetAccess.ParatextDBLServer : baseUrl;
            try
            {
                _ = client.Head(BuildDblResourceEntriesUrl(baseUrl, id));
                return(true);
            }
            catch (Exception ex)
            {
                // Normally we would catch the specific WebException,
                // but something in ParatextData is interfering with it.
                if (ex.InnerException?.Message.StartsWith("401: ", StringComparison.OrdinalIgnoreCase) ?? false)
                {
                    // A 401 error means unauthorized (probably a bad token)
                    return(false);
                }
                else if (ex.InnerException?.Message.StartsWith("403: ", StringComparison.OrdinalIgnoreCase) ?? false)
                {
                    // A 403 error means no access.
                    return(false);
                }
                else if (ex.InnerException?.Message.StartsWith("404: ", StringComparison.OrdinalIgnoreCase) ?? false)
                {
                    // A 404 error means that the resource is not on the server
                    return(false);
                }
                else if (ex.InnerException?.Message.StartsWith("405: ", StringComparison.OrdinalIgnoreCase) ?? false)
                {
                    // A 405 means that HEAD request does not work on the server, so we will use the resource list
                    // This is slower (although faster than a GET request on the resource), but more reliable
                    IEnumerable <SFInstallableDblResource> resources =
                        GetInstallableDblResources(
                            userSecret,
                            paratextOptions,
                            restClientFactory,
                            fileSystemService,
                            jwtTokenHelper,
                            exceptionHandler,
                            baseUrl);
                    return(resources.Any(r => r.DBLEntryUid == id));
                }
                else if (ex.Source == "NSubstitute")
                {
                    // This occurs during unit tests to test whether there is permission or not
                    return(false);
                }
                else
                {
                    // An unknown error
                    throw;
                }
            }
        }