//public UserDevice GetUserDeviceByLoginAndDeviceID(Guid LoginGUID, string DeviceGUID) //{ // using (var dataContext = new WorkersInMotionDB()) // { // return (from p in dataContext.UserDevices // where p.LoginGUID == LoginGUID && p.DeviceID == DeviceGUID // select p).FirstOrDefault(); // } //} public List<UserDevice> GetUserDevice(UserDevice pUserDevice) { //using (var dataContext = new WorkersInMotionDB()) //{ // return (from p in dataContext.UserDevices // where p.LoginGUID == pUserDevice.LoginGUID && p.DeviceID == pUserDevice.DeviceID // select p).ToList(); //} SqlParameter[] Param = new SqlParameter[2]; Param[0] = new SqlParameter("@pLoginGUID", SqlDbType.UniqueIdentifier); Param[0].Value = pUserDevice.LoginGUID; Param[1] = new SqlParameter("@pDeviceID", SqlDbType.NVarChar, -1); Param[1].Value = (object)pUserDevice.DeviceID ?? DBNull.Value; return context.Database.SqlQuery<UserDevice>("select * from UserDevices where LoginGUID=@pLoginGUID and DeviceID=@pDeviceID", Param).ToList(); }
public LoginResponse Login(LoginRequest pLoginRequest) { LoginResponse loginResponse = new LoginResponse(); IUserRepository _IUserRepository; _IUserRepository = new UserRepository(new WorkersInMotionDB()); string UpdatedSessionID = string.Empty; Logger.Debug("Inside UserLogin"); LoginResponse lResponse = new LoginResponse(); try { MasterLogin masterlogin = new MasterLogin(); var aspuser = _IUserRepository.UserLogin(pLoginRequest.UserName, _IUserRepository.EncodeTo64(pLoginRequest.Password)); if (aspuser != null) { Logger.Debug("Inside Role"); AspNetRole asprole = _IUserRepository.GetRole(aspuser.Role_Id); switch (asprole.UserType) { case "WIM_A": case "ENT_A": case "ENT_OM": case "ENT_U_RM": case "ENT_U_TM": lResponse.Role = 1; break; case "ENT_U": lResponse.Role = 2; break; case "IND_C": lResponse.Role = 3; break; default: break; } UserDevice userDevice = new UserDevice(); List<MasterLogin> masterlogins = new List<MasterLogin>(); MasterLogin lMasterLogin = new MasterLogin(); lMasterLogin.UserGUID = aspuser.UserGUID; lMasterLogin.LoginType = (short)pLoginRequest.LoginType; masterlogins = _IUserRepository.GetMasterLogin(lMasterLogin); if (masterlogins != null && masterlogins.Count > 0) { #region masterlogins record available masterlogin = masterlogins[0]; // Alok need to be fixed // Update the Master Login masterlogin.ExpiryTime = DateTime.UtcNow.AddYears(10); Logger.Debug("Updating MasterLogin Record"); UpdatedSessionID = _IUserRepository.UpdateMasterLogin(masterlogin); if (!string.IsNullOrEmpty(UpdatedSessionID)) { #region UpdatedSessionID is not null Logger.Debug("Updated Session ID: " + UpdatedSessionID); lResponse.SessionID = UpdatedSessionID; lResponse.UserGUID = aspuser.UserGUID.ToString(); Logger.Debug("Inside MasterLogin"); userDevice.LoginGUID = masterlogin.LoginGUID; userDevice.DeviceID = pLoginRequest.DeviceInfo.deviceid; List<UserDevice> lUserDevices = _IUserRepository.GetUserDevice(userDevice); if (lUserDevices != null && lUserDevices.Count > 0) { // Delete the user device record userDevice = lUserDevices[0]; // Need to modify Alok int deviceresult = _IUserRepository.DeleteUserDevices(userDevice.UserDevicesGUID); //int deviceresult = _IUserRepository.Save(); if (deviceresult <= 0) { lResponse = null; return lResponse; } } // Insert the User Device info if (CreateUserDevice(masterlogin, pLoginRequest) > 0) { if (!string.IsNullOrEmpty(lResponse.SessionID) && !string.IsNullOrEmpty(lResponse.UserGUID)) { DownloadUsers lDownloadUsers = DownloadUsers(lResponse.SessionID, new Guid(lResponse.UserGUID)); if (lDownloadUsers != null && lDownloadUsers.UserRecords.Count > 0) lResponse.UserRecord = lDownloadUsers.UserRecords[0]; else lResponse.UserRecord = null; } Logger.Debug("UserDevice record created for updated Session ID: " + UpdatedSessionID); } #endregion } else { #region UpdatedSessionID is NULL Logger.Error("Unable to generate Session ID"); lResponse = null; return lResponse; #endregion } #endregion } else { #region masterlogins record not available Logger.Debug("Creating MasterLogin Record"); lMasterLogin.ExpiryTime = DateTime.UtcNow.AddYears(10); if (CreateMasterLogin(lMasterLogin) > 0) { Logger.Debug("New Session ID: " + lMasterLogin.SessionID); lResponse.SessionID = lMasterLogin.SessionID; lResponse.UserGUID = lMasterLogin.UserGUID.ToString(); Logger.Debug("Inside UserDevice create"); if (CreateUserDevice(masterlogin, pLoginRequest) > 0) { if (!string.IsNullOrEmpty(lResponse.SessionID) && !string.IsNullOrEmpty(lResponse.UserGUID)) { DownloadUsers lDownloadUsers = DownloadUsers(lResponse.SessionID, new Guid(lResponse.UserGUID)); if (lDownloadUsers != null && lDownloadUsers.UserRecords.Count > 0) lResponse.UserRecord = lDownloadUsers.UserRecords[0]; else lResponse.UserRecord = null; } Logger.Debug("UserDevice record created for new Session ID: " + lMasterLogin.SessionID); } else { Logger.Error("Unable to craete UserDevice record for new Session ID: " + lMasterLogin.SessionID); lResponse = null; } } else { Logger.Error("Unable to craete MasterLogin record"); lResponse = null; } #endregion } } else { Logger.Error("Unable to find user record in AspUser"); lResponse = null; } return lResponse; } catch (Exception ex) { Logger.Error(ex.Message); lResponse = null; //lResponse.SessionID = new WorkersInMotionDB().Database.Connection.ConnectionString + " Exception: " + ex.Message; return lResponse; } }
public int InsertUserDevice(UserDevice UserDevice) { //context.UserDevices.Add(UserDevice); SqlParameter[] Param = new SqlParameter[15]; Param[0] = new SqlParameter("@pUserDevicesGUID", SqlDbType.UniqueIdentifier); Param[0].Value = UserDevice.UserDevicesGUID; Param[1] = new SqlParameter("@pUserGUID", SqlDbType.UniqueIdentifier); Param[1].Value = (object)UserDevice.UserGUID ?? DBNull.Value; Param[2] = new SqlParameter("@pLoginGUID", SqlDbType.UniqueIdentifier); Param[2].Value = (object)UserDevice.LoginGUID ?? DBNull.Value; Param[3] = new SqlParameter("@pDeviceID", SqlDbType.NVarChar, -1); Param[3].Value = (object)UserDevice.DeviceID ?? DBNull.Value; Param[4] = new SqlParameter("@pIPAddress", SqlDbType.NVarChar, -1); Param[4].Value = (object)UserDevice.IPAddress ?? DBNull.Value; Param[5] = new SqlParameter("@pDeviceInfo", SqlDbType.NVarChar, -1); Param[5].Value = (object)UserDevice.DeviceInfo ?? DBNull.Value; Param[6] = new SqlParameter("@pDeviceType", SqlDbType.SmallInt); Param[6].Value = (object)UserDevice.DeviceType ?? DBNull.Value; Param[7] = new SqlParameter("@pPUSHID", SqlDbType.NVarChar, -1); Param[7].Value = (object)UserDevice.PUSHID ?? DBNull.Value; Param[8] = new SqlParameter("@pPhone", SqlDbType.NVarChar, -1); Param[8].Value = (object)UserDevice.Phone ?? DBNull.Value; Param[9] = new SqlParameter("@pIsActive", SqlDbType.Bit); Param[9].Value = (object)UserDevice.IsActive ?? DBNull.Value; Param[10] = new SqlParameter("@pTimeZone", SqlDbType.Float); Param[10].Value = (object)UserDevice.TimeZone ?? DBNull.Value; Param[11] = new SqlParameter("@pCreateDate", SqlDbType.DateTime); Param[11].Value = (object)UserDevice.CreateDate ?? DBNull.Value; Param[12] = new SqlParameter("@pCreateBy", SqlDbType.UniqueIdentifier); Param[12].Value = (object)UserDevice.CreateBy ?? DBNull.Value; Param[13] = new SqlParameter("@pLastModifiedDate", SqlDbType.DateTime); Param[13].Value = (object)UserDevice.LastModifiedDate ?? DBNull.Value; Param[14] = new SqlParameter("@pLastModifiedBy", SqlDbType.UniqueIdentifier); Param[14].Value = (object)UserDevice.LastModifiedBy ?? DBNull.Value; return context.Database.ExecuteSqlCommand("insert into UserDevices(UserDevicesGUID,UserGUID,LoginGUID,DeviceID,IPAddress,DeviceInfo,DeviceType,PUSHID,Phone," + "IsActive,TimeZone,CreateDate,CreateBy,LastModifiedDate,LastModifiedBy)" + "values(@pUserDevicesGUID,@pUserGUID,@pLoginGUID,@pDeviceID,@pIPAddress,@pDeviceInfo,@pDeviceType,@pPUSHID,@pPhone," + "@pIsActive,@pTimeZone,@pCreateDate,@pCreateBy,@pLastModifiedDate,@pLastModifiedBy)", Param); }
private int CreateUserDevice(MasterLogin pMasterLogin, LoginRequest pLoginRequest) { int lResult; IUserRepository _IUserRepository = new UserRepository(new WorkersInMotionDB()); UserDevice lUserDevice = new UserDevice(); lUserDevice.UserDevicesGUID = Guid.NewGuid(); lUserDevice.LoginGUID = pMasterLogin.LoginGUID; lUserDevice.UserGUID = pMasterLogin.UserGUID; lUserDevice.IPAddress = pLoginRequest.DeviceInfo.deviceipaddress; lUserDevice.DeviceID = pLoginRequest.DeviceInfo.deviceid; lUserDevice.DeviceInfo = new JavaScriptSerializer().Serialize(pLoginRequest.DeviceInfo); lUserDevice.DeviceType = pLoginRequest.DeviceInfo.devicetype; lUserDevice.PUSHID = pLoginRequest.PushID; lUserDevice.Phone = pMasterLogin.Phone; lUserDevice.IsActive = true; lUserDevice.TimeZone = pLoginRequest.DeviceInfo.TimeZone; lUserDevice.CreateDate = DateTime.UtcNow; lUserDevice.CreateBy = pMasterLogin.UserGUID; lUserDevice.LastModifiedDate = DateTime.UtcNow; lUserDevice.LastModifiedBy = pMasterLogin.UserGUID; // _IUserRepository.InsertUserDevice(lUserDevice); if (_IUserRepository.InsertUserDevice(lUserDevice) > 0) { lResult = 1; } else { lResult = 0; } return lResult; }