PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.SQL.ACS/WindowsPhoneCloud.Web/Services/RegistrationService.vb

#
Visual Basic | 73 lines | 45 code | 13 blank | 15 comment | 0 complexity | eb66c92668049e5497f6f01cecc7dbf2 MD5 | raw file
  1. ' ----------------------------------------------------------------------------------
  2. ' Microsoft Developer & Platform Evangelism
  3. '
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '
  6. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ----------------------------------------------------------------------------------
  10. ' The example companies, organizations, products, domain names,
  11. ' e-mail addresses, logos, people, places, and events depicted
  12. ' herein are fictitious. No association with any real company,
  13. ' organization, product, domain name, email address, logo, person,
  14. ' places, or events is intended or should be inferred.
  15. ' ----------------------------------------------------------------------------------
  16. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
  17. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
  18. Imports Microsoft.IdentityModel.Claims
  19. Imports System.ServiceModel.Web
  20. Imports System.ServiceModel.Activation
  21. Imports System.ServiceModel
  22. Imports System.Net
  23. Namespace Services
  24. <ServiceBehavior(IncludeExceptionDetailInFaults:=False), AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
  25. Public Class RegistrationService
  26. Implements IRegistrationService
  27. Private ReadOnly userRepository As IUserRepository
  28. Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository
  29. Public Sub New()
  30. Me.New(Nothing, Nothing)
  31. End Sub
  32. <CLSCompliant(False)> _
  33. Public Sub New(ByVal userRepository As IUserRepository, ByVal userPrivilegesRepository As IUserPrivilegesRepository)
  34. Dim context As SqlDataContext = Nothing
  35. If (userPrivilegesRepository Is Nothing) OrElse (userRepository Is Nothing) Then
  36. context = New SqlDataContext()
  37. End If
  38. Me.userRepository = If(userRepository, context)
  39. Me.userPrivilegesRepository = If(userPrivilegesRepository, context)
  40. End Sub
  41. Public Function CreateUser(ByVal user As RegistrationUser) As String Implements IRegistrationService.CreateUser
  42. If (user Is Nothing) OrElse String.IsNullOrEmpty(user.Name) OrElse String.IsNullOrEmpty(user.EMail) Then
  43. Throw New WebFaultException(Of String)("Invalid user information.", HttpStatusCode.BadRequest)
  44. End If
  45. Dim identity = TryCast(HttpContext.Current.User.Identity, IClaimsIdentity)
  46. Dim nameIdentifier = identity.Claims.Single(Function(c) c.ClaimType = ClaimTypes.NameIdentifier).Value
  47. Me.userRepository.CreateUser(nameIdentifier, user.Name, user.EMail)
  48. Me.SetUserDefaultUserPrivileges(nameIdentifier)
  49. Return "Success"
  50. End Function
  51. Public Function CheckUserRegistration() As String Implements IRegistrationService.CheckUserRegistration
  52. Dim identity = TryCast(HttpContext.Current.User.Identity, IClaimsIdentity)
  53. Dim nameIdentifier = identity.Claims.Single(Function(c) c.ClaimType = ClaimTypes.NameIdentifier).Value
  54. Return (Me.userRepository.GetUser(nameIdentifier) IsNot Nothing).ToString()
  55. End Function
  56. Private Sub SetUserDefaultUserPrivileges(ByVal userId As String)
  57. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.SqlUsagePrivilege)
  58. End Sub
  59. End Class
  60. End Namespace