faizinfy

UserService.vb

Dec 13th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.45 KB | None | 0 0
  1. Imports System.Data.Entity
  2. Imports Microsoft.AspNet.Identity
  3. Imports Microsoft.AspNet.Identity.EntityFramework
  4. Imports Microsoft.Owin.Security.DataProtection
  5. Imports Microsoft.Owin.Security
  6. Imports Microsoft.Owin.Host.SystemWeb
  7. Imports System.Data.Entity.Validation
  8. Imports EmployeePerformanceCore
  9. Imports System.Threading.Tasks
  10.  
  11. Public Class UserService
  12.     Implements IUserService
  13.  
  14.     Private idb As IDataContext
  15.     Private ius As IUserSession
  16.  
  17.     Private um As UserManager(Of User, Integer)
  18.  
  19.     Public Sub New(idb As IDataContext, ius As IUserSession)
  20.         Me.idb = idb
  21.         Me.ius = ius
  22.  
  23.         Dim aa As New UserStore(Of User, UserIdentityRole, Integer, UserLogin, UserRole, UserClaim)(idb)
  24.         Me.um = New UserManager(Of User, Integer)(aa)
  25.     End Sub
  26.  
  27.     Public Function FindUserByUsername(username As String) As Result Implements IUserService.FindUserByUsername
  28.         Dim r As New Result
  29.  
  30.         Dim user As User = um.FindByName(username)
  31.         If user Is Nothing Then
  32.             r.Errors.Add("User is not found.")
  33.         End If
  34.         Return r
  35.     End Function
  36.  
  37.     Public Function GetUser(id As Integer) As User Implements IUserService.GetUser
  38.  
  39.     End Function
  40.  
  41.     Public Function Login(username As String, password As String, Optional persistCookie As Boolean = False) As Result Implements IUserService.Login
  42.  
  43.         Dim r As New Result
  44.         Dim slogin As Boolean = False
  45.         Dim AuthenticationManager = HttpContext.Current.GetOwinContext.Authentication
  46.  
  47.         Dim user As User = um.Find(username, password)
  48.  
  49.         If user Is Nothing Then
  50.             r.Errors.Add("Please recheck your username and password")
  51.         End If
  52.  
  53.         Try
  54.             AuthenticationManager.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie)
  55.             Dim uidentity = um.CreateIdentity(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie)
  56.             AuthenticationManager.SignIn(New AuthenticationProperties() With {.IsPersistent = persistCookie}, uidentity)
  57.             slogin = True
  58.         Catch ex As Exception
  59.  
  60.         End Try
  61.  
  62.         If Not slogin Then
  63.             r.Errors.Add("The username of password provided is incorrect.")
  64.         End If
  65.  
  66.         user.DateTimeLastLoggedIn = Now
  67.         Dim rupdate As Result = idb.updatesave(user, ius.currentuserid, ius.currentcompanyid, "Login" & user.Id)
  68.  
  69.         For Each item In rupdate.Errors
  70.             r.Warnings.Add(item)
  71.         Next
  72.  
  73.         Return r
  74.     End Function
  75.  
  76.     Public Function Logout() As Result Implements IUserService.Logout
  77.  
  78.         Dim r As New Result
  79.  
  80.         Try
  81.             HttpContext.Current.Session.Clear()
  82.         Catch ex As Exception
  83.             r.Errors.Add(ex.Message)
  84.             Return r
  85.         End Try
  86.  
  87.         Dim AuthenticationManager = HttpContext.Current.GetOwinContext.Authentication
  88.         AuthenticationManager.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie)
  89.  
  90.         Return r
  91.     End Function
  92.  
  93.     Public Async Function RegisterCompanyAsync(companyname As String, username As String, staffname As String, password As String, confirmpassword As String) As Task(Of Result) Implements IUserService.RegisterCompanyAsync
  94.  
  95.         Dim r As New Result
  96.  
  97.         If password <> confirmpassword Then
  98.             r.Errors.Add("Your password and confirmation password does not match.")
  99.             Return r
  100.         End If
  101.  
  102.  
  103.         Dim cc As New Company
  104.         cc.name = companyname
  105.         cc.datetimecreated = DateTime.Now
  106.         cc.balancescorecardmethod = EnumBalanceScorecarCalculationMethod.ModifiableThresholdPercent
  107.  
  108.         Dim department As New Department
  109.         department.name = "Default Department"
  110.         department.description = "Default Department"
  111.         department.company = cc
  112.  
  113.         Dim designation As New Designation
  114.         designation.name = "Default Designation"
  115.         designation.description = "Default Designation"
  116.         designation.company = cc
  117.  
  118.         Dim jobgrade As New JobGrade
  119.         jobgrade.name = "Default JobGrade"
  120.         jobgrade.description = "Default JobGrade"
  121.         jobgrade.company = cc
  122.  
  123.         Dim jobclassification As New JobClassification
  124.         jobclassification.name = "Default JobClassification"
  125.         jobclassification.description = "Default JobClassification"
  126.         jobclassification.company = cc
  127.  
  128.         Dim staff As New Staff
  129.         staff.name = staffname
  130.         staff.email = username
  131.         staff.gender = 1
  132.         staff.race = 1
  133.         staff.religion = 1
  134.         staff.datejoined = Date.Now
  135.         staff.department = department
  136.         staff.designation = designation
  137.         staff.jobgrade = jobgrade
  138.         staff.jobclassification = jobclassification
  139.         staff.company = cc
  140.         staff.datetimecreated = DateTime.Now
  141.  
  142.  
  143.         Dim uu As New User
  144.         uu.UserName = username
  145.         uu.Displayname = staffname
  146.         uu.Email = uu.UserName
  147.         uu.company = cc
  148.         uu.staff = staff
  149.         uu.DateTimeCreated = DateTime.Now
  150.         uu.DateTimeLastLoggedIn = DateTime.Now
  151.         uu.isActive = UserStatus.Active
  152.  
  153.         Dim rI = Await um.CreateAsync(uu, password)
  154.  
  155.         If Not rI.Succeeded Then
  156.             If rI.Errors.FirstOrDefault.Contains("taken") Then
  157.                 r.Errors.Add("This username has been used. Please use another username")
  158.  
  159.             ElseIf rI.Errors.FirstOrDefault.Contains("Password must be at least 6 characters.") Then
  160.                 r.Errors.Add("")
  161.  
  162.             Else
  163.                 r.AddError(EnumErrorResult.Undefined)
  164.                 r.Errors.Add(rI.Errors.FirstOrDefault)
  165.             End If
  166.  
  167.             Return r
  168.         End If
  169.  
  170.         r.getsuccess.Clear()
  171.         r.AddSuccess(EnumSuccesResult.IPublicUserAccountService_RegisterCompanyAsync_Success)
  172.  
  173.         Return r
  174.     End Function
  175.  
  176.     Public Async Function ResetPasswordAsync(username As String, password As String) As Task(Of ChangePasswordResult) Implements IUserService.ResetPasswordAsync
  177.  
  178.         Dim user As User = idb.Users.Where(Function(a) a.UserName.Trim.ToLower = username.Trim.ToLower).FirstOrDefault
  179.         Return Await Me.ResetPassword(user, password)
  180.     End Function
  181.  
  182.     Public Async Function ResetPassword(user As User, password As String) As Threading.Tasks.Task(Of ChangePasswordResult)
  183.         Dim r As New ChangePasswordResult
  184.  
  185.         Dim rand As New Random
  186.         If password = "" Then
  187.             password = rand.Next(100000, 999999)
  188.         End If
  189.  
  190.         Dim r1 = Await um.RemovePasswordAsync(user.Id)
  191.  
  192.         If Not r1.Succeeded Then
  193.             r.Errors.Add("Failed to remove password")
  194.             Return r
  195.         End If
  196.  
  197.         ' add
  198.         Dim r2 = Await um.AddPasswordAsync(user.Id, password)
  199.  
  200.         If Not r2.Succeeded Then
  201.             r.Errors.Add("Failed to add password")
  202.             Return r
  203.         End If
  204.  
  205.         r.newpassword = password
  206.  
  207.         Return r
  208.     End Function
  209.  
  210.     Public Async Function RegisterUserAsync(username As String, staffname As String, staffid As Integer, password As String, confirmpassword As String) As Task(Of Result) Implements IUserService.RegisterUserAsync
  211.  
  212.         Dim r As New Result
  213.  
  214.         If password <> confirmpassword Then
  215.             r.Errors.Add("Your password and confirmation password does not match.")
  216.             Return r
  217.         End If
  218.  
  219.         Dim user As New User
  220.         user.UserName = username
  221.         user.Displayname = staffname
  222.         user.Email = user.UserName
  223.         user.companyid = ius.currentcompanyid
  224.         user.staffid = staffid
  225.         user.DateTimeCreated = DateTime.Now
  226.         user.DateTimeLastLoggedIn = DateTime.Now
  227.         user.isActive = UserStatus.Active
  228.  
  229.         Dim rI = Await um.CreateAsync(user, password)
  230.  
  231.         If Not rI.Succeeded Then
  232.             If rI.Errors.FirstOrDefault.Contains("taken") Then
  233.                 r.Errors.Add("This username has been used. Please use another username")
  234.  
  235.             ElseIf rI.Errors.FirstOrDefault.Contains("Password must be at least 6 characters.") Then
  236.                 r.Errors.Add("")
  237.  
  238.             Else
  239.                 r.AddError(EnumErrorResult.Undefined)
  240.                 r.Errors.Add(rI.Errors.FirstOrDefault)
  241.             End If
  242.  
  243.             Return r
  244.         End If
  245.  
  246.         r.getsuccess.Clear()
  247.         r.AddSuccess(EnumSuccesResult.IPublicUserAccountService_RegisterCompanyAsync_Success)
  248.  
  249.         Return r
  250.     End Function
  251.  
  252. End Class
Advertisement
Add Comment
Please, Sign In to add comment