From 8cb178986e891a1e7ae5dfe416244bb031fe6def Mon Sep 17 00:00:00 2001 From: Nate Rickard Date: Fri, 4 May 2018 15:21:02 -0400 Subject: [PATCH] added DefaultPermissionProvider --- .../DefaultPermissionProvider.kt | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Android/azuremobile.android/src/main/java/com/azure/mobile/azuremobileandroid/DefaultPermissionProvider.kt diff --git a/Android/azuremobile.android/src/main/java/com/azure/mobile/azuremobileandroid/DefaultPermissionProvider.kt b/Android/azuremobile.android/src/main/java/com/azure/mobile/azuremobileandroid/DefaultPermissionProvider.kt new file mode 100644 index 0000000..2b86d86 --- /dev/null +++ b/Android/azuremobile.android/src/main/java/com/azure/mobile/azuremobileandroid/DefaultPermissionProvider.kt @@ -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) -> 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) -> 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) -> 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) -> 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) -> 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) -> Unit) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } +} \ No newline at end of file