This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acdd47e
commit 8cb1789
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
...le.android/src/main/java/com/azure/mobile/azuremobileandroid/DefaultPermissionProvider.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.azure.mobile.azuremobileandroid | ||
|
||
import com.azure.data.model.DataError | ||
import com.azure.data.model.Permission | ||
import com.azure.data.model.PermissionMode | ||
import com.azure.data.service.PermissionProvider | ||
import com.azure.data.service.PermissionProviderConfiguration | ||
import com.azure.data.service.PermissionProviderError | ||
import com.azure.data.service.Response | ||
import com.google.gson.Gson | ||
import okhttp3.* | ||
import java.io.IOException | ||
|
||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
data class PermissionRequest(val databaseId: String, val collectionId: String, val documentId: String?, val tokenDuration: Int, val permissionMode: PermissionMode) | ||
|
||
class DefaultPermissionProvider(private val baseUrlBuilder: HttpUrl.Builder, override var configuration: PermissionProviderConfiguration? = PermissionProviderConfiguration.default, private val client: OkHttpClient = OkHttpClient.Builder().build()) : PermissionProvider { | ||
|
||
override fun getPermissionForCollection(collectionId: String, databaseId: String, permissionMode: PermissionMode, completion: (Response<Permission>) -> Unit) { | ||
|
||
try { | ||
|
||
val permissionRequest = PermissionRequest(databaseId, collectionId, null, configuration!!.defaultTokenDuration.toInt(), permissionMode) | ||
|
||
val url = baseUrlBuilder.addPathSegment("api/data/permission") | ||
.build() | ||
|
||
val gson = Gson() | ||
|
||
val request = Request.Builder() | ||
.url(url) | ||
.addHeader("Content-Type", "application/json") | ||
.post(RequestBody.create(MediaType.parse("application/json"), gson.toJson(permissionRequest))) | ||
.build() | ||
|
||
client.newCall(request) | ||
.enqueue(object : Callback { | ||
|
||
override fun onFailure(call: Call, ex: IOException) { | ||
|
||
return completion(Response(DataError(ex), request)) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun onResponse(call: Call, response: okhttp3.Response) { | ||
|
||
try { | ||
val body = response.body() | ||
?: return completion(Response(DataError(PermissionProviderError.GetPermissionFailed))) | ||
|
||
val json = body.string() | ||
val permission = gson.fromJson(json, Permission::class.java) | ||
|
||
completion(Response(permission)) | ||
|
||
} catch (ex: Exception) { | ||
|
||
completion(Response(DataError(ex), request, response)) | ||
} | ||
} | ||
}) | ||
|
||
} catch (ex: Exception) { | ||
completion(Response(DataError(PermissionProviderError.GetPermissionFailed))) | ||
} | ||
} | ||
|
||
override fun getPermissionForDocument(documentId: String, collectionId: String, databaseId: String, permissionMode: PermissionMode, completion: (Response<Permission>) -> Unit) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun getPermissionForAttachment(attachmentId: String, documentId: String, collectionId: String, databaseId: String, permissionMode: PermissionMode, completion: (Response<Permission>) -> Unit) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun getPermissionForStoredProcedure(storedProcedureId: String, collectionId: String, databaseId: String, permissionMode: PermissionMode, completion: (Response<Permission>) -> Unit) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun getPermissionForUserDefinedFunction(functionId: String, collectionId: String, databaseId: String, permissionMode: PermissionMode, completion: (Response<Permission>) -> Unit) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun getPermissionForTrigger(triggerId: String, collectionId: String, databaseId: String, permissionMode: PermissionMode, completion: (Response<Permission>) -> Unit) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
} |