Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.Entity
- Imports Microsoft.AspNet.Identity
- Imports Microsoft.AspNet.Identity.EntityFramework
- Imports Microsoft.Owin.Security.DataProtection
- Imports Microsoft.Owin.Security
- Imports Microsoft.Owin.Host.SystemWeb
- Imports System.Data.Entity.Validation
- Imports EmployeePerformanceCore
- Imports System.Threading.Tasks
- Public Class UserService
- Implements IUserService
- Private idb As IDataContext
- Private ius As IUserSession
- Private um As UserManager(Of User, Integer)
- Public Sub New(idb As IDataContext, ius As IUserSession)
- Me.idb = idb
- Me.ius = ius
- Dim aa As New UserStore(Of User, UserIdentityRole, Integer, UserLogin, UserRole, UserClaim)(idb)
- Me.um = New UserManager(Of User, Integer)(aa)
- End Sub
- Public Function FindUserByUsername(username As String) As Result Implements IUserService.FindUserByUsername
- Dim r As New Result
- Dim user As User = um.FindByName(username)
- If user Is Nothing Then
- r.Errors.Add("User is not found.")
- End If
- Return r
- End Function
- Public Function GetUser(id As Integer) As User Implements IUserService.GetUser
- End Function
- Public Function Login(username As String, password As String, Optional persistCookie As Boolean = False) As Result Implements IUserService.Login
- Dim r As New Result
- Dim slogin As Boolean = False
- Dim AuthenticationManager = HttpContext.Current.GetOwinContext.Authentication
- Dim user As User = um.Find(username, password)
- If user Is Nothing Then
- r.Errors.Add("Please recheck your username and password")
- End If
- Try
- AuthenticationManager.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie)
- Dim uidentity = um.CreateIdentity(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie)
- AuthenticationManager.SignIn(New AuthenticationProperties() With {.IsPersistent = persistCookie}, uidentity)
- slogin = True
- Catch ex As Exception
- End Try
- If Not slogin Then
- r.Errors.Add("The username of password provided is incorrect.")
- End If
- user.DateTimeLastLoggedIn = Now
- Dim rupdate As Result = idb.updatesave(user, ius.currentuserid, ius.currentcompanyid, "Login" & user.Id)
- For Each item In rupdate.Errors
- r.Warnings.Add(item)
- Next
- Return r
- End Function
- Public Function Logout() As Result Implements IUserService.Logout
- Dim r As New Result
- Try
- HttpContext.Current.Session.Clear()
- Catch ex As Exception
- r.Errors.Add(ex.Message)
- Return r
- End Try
- Dim AuthenticationManager = HttpContext.Current.GetOwinContext.Authentication
- AuthenticationManager.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie)
- Return r
- End Function
- 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
- Dim r As New Result
- If password <> confirmpassword Then
- r.Errors.Add("Your password and confirmation password does not match.")
- Return r
- End If
- Dim cc As New Company
- cc.name = companyname
- cc.datetimecreated = DateTime.Now
- cc.balancescorecardmethod = EnumBalanceScorecarCalculationMethod.ModifiableThresholdPercent
- Dim department As New Department
- department.name = "Default Department"
- department.description = "Default Department"
- department.company = cc
- Dim designation As New Designation
- designation.name = "Default Designation"
- designation.description = "Default Designation"
- designation.company = cc
- Dim jobgrade As New JobGrade
- jobgrade.name = "Default JobGrade"
- jobgrade.description = "Default JobGrade"
- jobgrade.company = cc
- Dim jobclassification As New JobClassification
- jobclassification.name = "Default JobClassification"
- jobclassification.description = "Default JobClassification"
- jobclassification.company = cc
- Dim staff As New Staff
- staff.name = staffname
- staff.email = username
- staff.gender = 1
- staff.race = 1
- staff.religion = 1
- staff.datejoined = Date.Now
- staff.department = department
- staff.designation = designation
- staff.jobgrade = jobgrade
- staff.jobclassification = jobclassification
- staff.company = cc
- staff.datetimecreated = DateTime.Now
- Dim uu As New User
- uu.UserName = username
- uu.Displayname = staffname
- uu.Email = uu.UserName
- uu.company = cc
- uu.staff = staff
- uu.DateTimeCreated = DateTime.Now
- uu.DateTimeLastLoggedIn = DateTime.Now
- uu.isActive = UserStatus.Active
- Dim rI = Await um.CreateAsync(uu, password)
- If Not rI.Succeeded Then
- If rI.Errors.FirstOrDefault.Contains("taken") Then
- r.Errors.Add("This username has been used. Please use another username")
- ElseIf rI.Errors.FirstOrDefault.Contains("Password must be at least 6 characters.") Then
- r.Errors.Add("")
- Else
- r.AddError(EnumErrorResult.Undefined)
- r.Errors.Add(rI.Errors.FirstOrDefault)
- End If
- Return r
- End If
- r.getsuccess.Clear()
- r.AddSuccess(EnumSuccesResult.IPublicUserAccountService_RegisterCompanyAsync_Success)
- Return r
- End Function
- Public Async Function ResetPasswordAsync(username As String, password As String) As Task(Of ChangePasswordResult) Implements IUserService.ResetPasswordAsync
- Dim user As User = idb.Users.Where(Function(a) a.UserName.Trim.ToLower = username.Trim.ToLower).FirstOrDefault
- Return Await Me.ResetPassword(user, password)
- End Function
- Public Async Function ResetPassword(user As User, password As String) As Threading.Tasks.Task(Of ChangePasswordResult)
- Dim r As New ChangePasswordResult
- Dim rand As New Random
- If password = "" Then
- password = rand.Next(100000, 999999)
- End If
- Dim r1 = Await um.RemovePasswordAsync(user.Id)
- If Not r1.Succeeded Then
- r.Errors.Add("Failed to remove password")
- Return r
- End If
- ' add
- Dim r2 = Await um.AddPasswordAsync(user.Id, password)
- If Not r2.Succeeded Then
- r.Errors.Add("Failed to add password")
- Return r
- End If
- r.newpassword = password
- Return r
- End Function
- 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
- Dim r As New Result
- If password <> confirmpassword Then
- r.Errors.Add("Your password and confirmation password does not match.")
- Return r
- End If
- Dim user As New User
- user.UserName = username
- user.Displayname = staffname
- user.Email = user.UserName
- user.companyid = ius.currentcompanyid
- user.staffid = staffid
- user.DateTimeCreated = DateTime.Now
- user.DateTimeLastLoggedIn = DateTime.Now
- user.isActive = UserStatus.Active
- Dim rI = Await um.CreateAsync(user, password)
- If Not rI.Succeeded Then
- If rI.Errors.FirstOrDefault.Contains("taken") Then
- r.Errors.Add("This username has been used. Please use another username")
- ElseIf rI.Errors.FirstOrDefault.Contains("Password must be at least 6 characters.") Then
- r.Errors.Add("")
- Else
- r.AddError(EnumErrorResult.Undefined)
- r.Errors.Add(rI.Errors.FirstOrDefault)
- End If
- Return r
- End If
- r.getsuccess.Clear()
- r.AddSuccess(EnumSuccesResult.IPublicUserAccountService_RegisterCompanyAsync_Success)
- Return r
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment