This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static async Task<Permission> GetPartitionPermission(string userId, DocumentClient client, string databaseId, string collectionId) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string permissionId = ""; | |
| bool isLimitedPartition = false; | |
| Permission partitionPermission = new Permission(); | |
| if (userId == publicUserId) | |
| { | |
| permissionId = $"{userId}-partition-limited-{collectionId}"; | |
| isLimitedPartition = true; | |
| } | |
| else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Uri permissionUri = UriFactory.CreatePermissionUri(databaseId, userId, permissionId); | |
| partitionPermission = await client.ReadPermissionAsync(permissionUri); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| catch (DocumentClientException ex) | |
| { | |
| if (ex.StatusCode == HttpStatusCode.NotFound) | |
| { | |
| await CreateUserIfNotExistAsync(userId, client, databaseId); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static async Task CreateUserIfNotExistAsync(string userId, DocumentClient client, string databaseId) | |
| { | |
| try | |
| { | |
| await client.ReadUserAsync(UriFactory.CreateUserUri(databaseId, userId)); | |
| } | |
| catch (DocumentClientException e) | |
| { | |
| if (e.StatusCode == HttpStatusCode.NotFound) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var newPermission = new Permission | |
| { | |
| PermissionMode = PermissionMode.Read, | |
| Id = permissionId, | |
| ResourceLink = collectionUri.ToString() | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (isLimitedPartition) | |
| newPermission.ResourcePartitionKey = new PartitionKey(false); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| partitionPermission = await client.CreatePermissionAsync(userUri, newPermission); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public async Task<string> GetPermissionToken(string accessToken) | |
| { | |
| var baseUri = new Uri(APIKeys.BrokerUrlBase); | |
| var client = new HttpClient { BaseAddress = baseUri }; | |
| var brokerUrl = new Uri(baseUri, APIKeys.BrokerUrlPath); | |
| var request = new HttpRequestMessage(HttpMethod.Get, brokerUrl); | |
| // Here check if there's a token or not |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var token = await functionService.GetPermissionToken(accessToken); | |
| docClient = new DocumentClient(new Uri(APIKeys.CosmosUrl), token); |