/// <summary> /// Get Paratext resources that a user has access to. /// </summary> /// <param name="userSecret">The user secret.</param> /// <param name="includeInstallableResource">If set to <c>true</c> include the installable resource.</param> /// <returns> /// The available resources. /// </returns> private async Task <IReadOnlyList <ParatextResource> > GetResourcesInternalAsync(string userId, bool includeInstallableResource) { IEnumerable <SFInstallableDblResource> resources; using (ParatextAccessLock accessLock = await GetParatextAccessLock(userId)) { resources = SFInstallableDblResource.GetInstallableDblResources( accessLock.UserSecret, this._paratextOptions.Value, this._restClientFactory, this._fileSystemService, this._jwtTokenHelper, _exceptionHandler, this._dblServerUri); } IReadOnlyDictionary <string, int> resourceRevisions = SFInstallableDblResource.GetInstalledResourceRevisions(); return(resources.OrderBy(r => r.FullName).Select(r => new ParatextResource { AvailableRevision = r.DBLRevision, InstallableResource = includeInstallableResource ? r : null, InstalledRevision = resourceRevisions .ContainsKey(r.DBLEntryUid.Id) ? resourceRevisions[r.DBLEntryUid.Id] : 0, IsConnectable = false, IsConnected = false, IsInstalled = resourceRevisions.ContainsKey(r.DBLEntryUid.Id), LanguageTag = r.LanguageID.Code, Name = r.FullName, ParatextId = r.DBLEntryUid.Id, ProjectId = null, ShortName = r.Name, }).ToArray()); }
/// <summary> /// Creates the DBL URL with username query. /// </summary> /// <param name="resource">The resource.</param> /// <returns> /// The URL. /// </returns> private static string CreateDblUrlWithUsernameQuery(SFInstallableDblResource resource) { var uriBuilder = new UriBuilder(resource.DblSourceUrl); var query = HttpUtils.ParseQueryString(uriBuilder.Query); uriBuilder.Query = query.ToString(); return(uriBuilder.ToString()); }
/// <summary> /// Gets the permission a user has to access a resource. /// </summary> /// <param name="userSecret">The user secret.</param> /// <param name="paratextId">The paratext resource identifier.</param> /// <param name="userId">The user identifier.</param> /// <returns> /// A dictionary of permissions where the key is the user ID and the value is the permission /// </returns> /// <remarks> /// See <see cref="TextInfoPermission" /> for permission values. /// </remarks> public async Task <string> GetResourcePermissionAsync(UserSecret userSecret, string paratextId, string userId) { // See if the source is a resource if (paratextId.Length == SFInstallableDblResource.ResourceIdentifierLength) { // The resource id is a 41 character project id truncated to 16 characters UserSecret thisUserSecret; if (userId == userSecret.Id) { thisUserSecret = userSecret; } else { // Get the user secret Attempt <UserSecret> userSecretAttempt = await this._userSecretRepository.TryGetAsync(userId); if (!userSecretAttempt.TryResult(out thisUserSecret)) { thisUserSecret = null; } } bool canRead = false; if (thisUserSecret != null) { if (!(thisUserSecret.ParatextTokens?.ValidateLifetime() ?? false)) { await this.RefreshAccessTokenAsync(thisUserSecret); } canRead = SFInstallableDblResource.CheckResourcePermission( paratextId, thisUserSecret, this._paratextOptions.Value, this._restClientFactory, this._fileSystemService, this._jwtTokenHelper, _exceptionHandler, this._dblServerUri); } return(canRead ? TextInfoPermission.Read : TextInfoPermission.None); } else { // Default to no permissions for projects used as sources return(TextInfoPermission.None); } }
/// <summary> /// Gets the permission a user has to access a resource. /// </summary> /// <param name="paratextId">The paratext resource identifier.</param> /// <param name="userId">The user identifier.</param> /// <returns> /// A dictionary of permissions where the key is the user ID and the value is the permission /// </returns> /// <remarks> /// See <see cref="TextInfoPermission" /> for permission values. /// </remarks> public async Task <string> GetResourcePermissionAsync(string paratextId, string userId) { // See if the source is a resource if (paratextId.Length != SFInstallableDblResource.ResourceIdentifierLength) { // Default to no permissions for projects used as sources return(TextInfoPermission.None); } using (ParatextAccessLock accessLock = await GetParatextAccessLock(userId)) { bool canRead = SFInstallableDblResource.CheckResourcePermission( paratextId, accessLock.UserSecret, _paratextOptions.Value, _restClientFactory, _fileSystemService, _jwtTokenHelper, _exceptionHandler, _dblServerUri); return(canRead ? TextInfoPermission.Read : TextInfoPermission.None); } }
/// <summary> /// Converts the JSON response to a list of Installable DBL Resources. /// </summary> /// <param name="baseUri">The base URI.</param> /// <param name="response">The response.</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="createdTimestamp">The created timestamp.</param> /// <param name="userSecret">The user secret.</param> /// <param name="paratextOptions">The paratext options.</param> /// <param name="projectDeleter">The project deleter.</param> /// <param name="migrationOperations">The migration operations.</param> /// <param name="passwordProvider">The password provider.</param> /// <returns> /// The Installable Resources. /// </returns> private static IEnumerable <SFInstallableDblResource> ConvertJsonResponseToInstallableDblResources( string baseUri, string response, ISFRestClientFactory restClientFactory, IFileSystemService fileSystemService, IJwtTokenHelper jwtTokenHelper, DateTime createdTimestamp, UserSecret userSecret, ParatextOptions paratextOptions, IProjectDeleter projectDeleter, IMigrationOperations migrationOperations, IZippedResourcePasswordProvider passwordProvider) { if (!string.IsNullOrWhiteSpace(response)) { JObject jsonResources; try { jsonResources = JObject.Parse(response); } catch (JsonReaderException) { // Ignore the exception and just return empty result // This is probably caused by partial result from poor connection to DBL yield break; } foreach (JToken jsonResource in jsonResources["resources"] as JArray ?? new JArray()) { var name = (string)jsonResource["name"]; var nameCommon = (string)jsonResource["nameCommon"]; var fullname = (string)jsonResource["fullname"]; if (string.IsNullOrWhiteSpace(fullname)) { fullname = nameCommon; } var languageName = (string)jsonResource["languageName"]; var id = (string)jsonResource["id"]; var revision = (string)jsonResource["revision"]; var permissionsChecksum = (string)jsonResource["permissions-checksum"]; var manifestChecksum = (string)jsonResource["p8z-manifest-checksum"]; var languageIdLdml = (string)jsonResource["languageLDMLId"]; var languageIdCode = (string)jsonResource["languageCode"]; LanguageId languageId = migrationOperations.DetermineBestLangIdToUseForResource(languageIdLdml, languageIdCode); if (string.IsNullOrEmpty(languageId.Id)) { languageId = LanguageIdHelper.FromCommonLanguageName(languageName); } else { languageId = LanguageId.FromEthnologueCode(languageId.Id); } string url = BuildDblResourceEntriesUrl(baseUri, id); var resource = new SFInstallableDblResource(userSecret, paratextOptions, restClientFactory, fileSystemService, jwtTokenHelper, projectDeleter, migrationOperations, passwordProvider) { DisplayName = name, Name = name, FullName = fullname, LanguageID = languageId, DblSourceUrl = url, DBLEntryUid = id, DBLRevision = int.Parse(revision), PermissionsChecksum = permissionsChecksum, ManifestChecksum = manifestChecksum, CreatedTimestamp = createdTimestamp, }; resource.LanguageName = MacroLanguageHelper.GetMacroLanguage(resource.LanguageID) ?? languageName; yield return(resource); } } }