/WP7.1/Templates/VB/WPCloud.Mem/WindowsPhoneCloud.Web/Handlers/AzureTablesProxyHandler.vb
Visual Basic | 106 lines | 74 code | 13 blank | 19 comment | 0 complexity | 6b03dfdc3060ab8e1fcc5d836ba507b6 MD5 | raw file
- ' ----------------------------------------------------------------------------------
- ' Microsoft Developer & Platform Evangelism
- '
- ' Copyright (c) Microsoft Corporation. All rights reserved.
- '
- ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
- ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
- ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
- ' ----------------------------------------------------------------------------------
- ' The example companies, organizations, products, domain names,
- ' e-mail addresses, logos, people, places, and events depicted
- ' herein are fictitious. No association with any real company,
- ' organization, product, domain name, email address, logo, person,
- ' places, or events is intended or should be inferred.
- ' ----------------------------------------------------------------------------------
-
- Imports Microsoft.WindowsAzure
- Imports Microsoft.Samples.WindowsPhoneCloud.Web.Serializers
- Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
- Imports System.Net
- Imports System.Globalization
-
- Namespace Handlers
-
- Public Class AzureTablesProxyHandler
- Inherits StorageProxyHandler
- Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository
-
- #If ACS Then
- Public Sub New()
- Me.New(New UserTablesServiceContext(), CloudStorageAccount.FromConfigurationSetting("DataConnectionString"), New FormatSerializerFactory())
- End Sub
-
- <CLSCompliant(False)>
- Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal cloudStorageAccount As CloudStorageAccount, ByVal formatSerializerFactory As IFormatSerializerFactory)
- MyBase.New(cloudStorageAccount, formatSerializerFactory)
- Me.userPrivilegesRepository = userPrivilegesRepository
- End Sub
- #Else
- Public Sub New()
- Me.New(New UserTablesServiceContext(), CloudStorageAccount.FromConfigurationSetting("DataConnectionString"), New TableRequestValidator(), New FormatSerializerFactory())
- End Sub
-
- <CLSCompliant(False)>
- Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal cloudStorageAccount As CloudStorageAccount, ByVal requestValidator As IStorageRequestValidator, ByVal formatSerializerFactory As IFormatSerializerFactory)
- MyBase.New(cloudStorageAccount, requestValidator, formatSerializerFactory)
- Me.userPrivilegesRepository = userPrivilegesRepository
- End Sub
- #End If
-
- Protected Overrides ReadOnly Property AzureStorageUrl() As String
- Get
- Return Me.CloudStorageAccount.TableEndpoint.ToString().TrimEnd("/"c)
- End Get
- End Property
-
- Protected Overrides Sub SignRequest(ByVal webRequest As WebRequest)
- Dim httpWebRequest = TryCast(webRequest, HttpWebRequest)
- If httpWebRequest IsNot Nothing Then
- Me.CloudStorageAccount.Credentials.SignRequestLite(httpWebRequest)
- End If
- End Sub
-
- #If ACS Then
- Protected Overrides Sub PostProcessProxyRequest(ByVal context As HttpContext)
- Dim tableName = StorageRequestAnalyzer.GetRequestedTable(context.Request)
-
- If (context.Response.StatusCode = CInt(HttpStatusCode.Created)) AndAlso StorageRequestAnalyzer.IsCreatingTable(context.Request, tableName) Then
- ' A new table was created -> add permissions to the current user.
- Me.AddTablePermissions(StorageRequestAnalyzer.GetTableToCreate(context.Request), Me.UserId)
- ElseIf (context.Response.StatusCode = CInt(HttpStatusCode.NoContent)) AndAlso StorageRequestAnalyzer.IsDeletingTable(context.Request, tableName) Then
- ' A table was deleted -> remove all permissions to that table.
- Me.RemoveAllTablePermissions(tableName)
- End If
- End Sub
- #Else
- Protected Overrides Sub PostProcessProxyRequest(ByVal context As HttpContext)
- Dim tableName = StorageRequestAnalyzer.GetRequestedTable(context.Request)
-
- If (context.Response.StatusCode = CInt(HttpStatusCode.Created)) AndAlso StorageRequestAnalyzer.IsCreatingTable(context.Request, tableName) Then
- ' A new table was created -> add permissions to the current user.
- Me.AddTablePermissions(StorageRequestAnalyzer.GetTableToCreate(context.Request), Me.RequestValidator.GetUserId(context))
- ElseIf (context.Response.StatusCode = CInt(HttpStatusCode.NoContent)) AndAlso StorageRequestAnalyzer.IsDeletingTable(context.Request, tableName) Then
- ' A table was deleted -> remove all permissions to that table.
- Me.RemoveAllTablePermissions(tableName)
- End If
- End Sub
- #End If
- Private Sub AddTablePermissions(ByVal tableName As String, ByVal userName As String)
- If Not String.IsNullOrEmpty(tableName) Then
- Dim accessTablePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", tableName, PrivilegeConstants.TablePrivilegeSuffix)
- Me.userPrivilegesRepository.AddPrivilegeToUser(userName, accessTablePrivilege)
- End If
- End Sub
-
- Private Sub RemoveAllTablePermissions(ByVal tableName As String)
- If Not String.IsNullOrEmpty(tableName) Then
- Dim publicTablePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", tableName, PrivilegeConstants.PublicTablePrivilegeSuffix)
- Me.userPrivilegesRepository.DeletePublicPrivilege(publicTablePrivilege)
-
- Dim accessTablePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", tableName, PrivilegeConstants.TablePrivilegeSuffix)
- Me.userPrivilegesRepository.DeletePrivilege(accessTablePrivilege)
- End If
- End Sub
- End Class
- End Namespace