Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
added DefaultPermissionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
NateRickard committed May 4, 2018
1 parent acdd47e commit 8cb1789
Showing 1 changed file with 91 additions and 0 deletions.
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.
}
}

0 comments on commit 8cb1789

Please sign in to comment.