/// <summary> /// Default constructor /// </summary> /// <param name="user"></param> public HotspotUserViewModel(HotspotUser user) { UserModel = user; if (user.Email != null) { Validity = Regex.IsMatch(user.Email.Trim(), @"^(\d+)\@") ? int.Parse(Regex.Match(user.Email.Trim(), @"^(\d+)\@").Groups[1].Value) : -1; Price = Regex.IsMatch(user.Email.Trim(), @"^\w+?\@(\d+)?\.\w+$") ? Regex.Match(user.Email.Trim(), @"^\w+?\@(\d+)?\.\w+$").Groups[1].Value : "free"; } }
public void AddSingleUserWillNotFail() { var user = new HotspotUser() { Name = "TEST " + DateTime.Now.ToString(), LimitUptime = "1:00:00", Password = "******", }; Connection.Save(user); }
public Task <bool> RemoveUserAsync(HotspotUser user) { return(Task.Run(() => { try { _connection.Delete(user); return true; } catch { return false; } })); }
public Task <bool> UpdateUserAsync(HotspotUser user) { return(Task.Run(() => { try { _connection.Save(user); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } })); }
public void AddUserWithProfileWillNotFail() { string profileName = "TEST " + DateTime.Now.ToString(); var profile = new HotspotUserProfile() { Name = profileName, }; Connection.Save(profile); var user = new HotspotUser() { Name = "User for " + profileName, Profile = profileName, LimitUptime = "1:00:00", }; Connection.Save(user); }
public void UpdateUserWillNotFail() { //Create user var user = new HotspotUser() { Name = "TEST " + DateTime.Now.ToString(), LimitUptime = "1:00:00", Password = "******", }; Connection.Save(user); //Update user.Disabled = true; Connection.Save(user); //Cleanup Connection.Delete(user); }
/// <summary> /// Subscribe to the child events /// </summary> private void SubcribeEvents() { // Subscribe to add event #region Add One Hotspot user _eventAggregator.GetEvent <AddHotspotUserEvent>().Subscribe(async userVm => { var isUpdate = userVm.IsUpdate; // TODO: // Optimize this code var user = userVm.User; if (isUpdate && Users.Any(u => u.Username == user.Name && u.Username != userVm.OriginalName)) { _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs("Can't use this username because it is already used!")); return; } if (!isUpdate && Users.Any(u => u.Username == user.Name)) { _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs("This user is already exists!")); return; } /* Setting the EMAIL */ // update the current email for the validity if the email is already exists var validity = !string.IsNullOrEmpty(userVm.LimitsViewModel.Validity) ? userVm.LimitsViewModel.Validity : "unlimited"; var price = !string.IsNullOrEmpty(userVm.LimitsViewModel.Price) ? userVm.LimitsViewModel.Price.TrimStart('0') : "free"; var status = isUpdate && !string.IsNullOrEmpty(user.Email) && Regex.IsMatch(user.Email.Trim(), @"^\w+?@\w+?\.\w+$") ? user.Email.Split('.')[1] : "np"; // Setting the new email var newEmail = $"{validity}@{price}.{status}"; user.Email = newEmail; // add the user and get the result var done = isUpdate ? await _hotspotClient.UpdateUserAsync(user) : await _hotspotClient.AddUserAsync(user); // Calculate the notifcation message string message; if (done && isUpdate) { message = "User updated successfully!"; } else if (done) { message = "User added successfully!"; } else if (isUpdate) { message = "Failed in updating user!"; } else { message = "Failed in adding user!"; } // Show notification about the proccess _eventAggregator.GetEvent <NotificationEvent>() .Publish(new NotificationEventArgs(message, done ? "DISMISS" : "RETRY", async() => { if (done) { return; } if (isUpdate) { await _hotspotClient.UpdateUserAsync(user); } else { await _hotspotClient.AddUserAsync(user); } }, new SideNotificationViewModel("Hotspot Users", message) ) ); // return if failed if (!done) { return; } // Update the edited user if (user.Id != null) { var usr = Users.SingleOrDefault(u => u.UserModel.Id == user.Id); if (usr == null) { return; } usr.Validity = validity != "unlimited" && Regex.IsMatch(validity, @"^\d+$") ? int.Parse(validity) : -1; usr.SetModel(user); } // Add the created user to th users list else { Users.Add(new HotspotUserViewModel(user)); } }); #endregion #region Add Multiple Hotspot Users _eventAggregator.GetEvent <AddMultipleHotspotUsersEvent>().Subscribe(async usersVm => { var countObj = await _dialogService.ShowInputAsync("Add Hotspot Users", "Enter the count of the users you want to add"); if (countObj == null) { return; } int count; if (!int.TryParse(countObj.ToString(), out count)) { _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs("Invalid input!")); return; } var dialog = await _dialogService.ShowProgressAsync("Adding users", $"Adding user - of {count}"); dialog.SetCancelable(true); int[] mul = { 1024, 1048576, 1073741824 }; long limitBytesOut = 0, limitBytesIn = 0; if (usersVm.ProfileAndLimitsViewModel.HasBandwidthLimits) { limitBytesOut = long.Parse(usersVm.ProfileAndLimitsViewModel.DownloadAmount ?? "0") * mul[usersVm.ProfileAndLimitsViewModel.DownloadUnitType]; limitBytesIn = long.Parse(usersVm.ProfileAndLimitsViewModel.UploadAmount ?? "0") * mul[usersVm.ProfileAndLimitsViewModel.UploadUnitType]; } var limitUptime = "00:00:00"; if (usersVm.ProfileAndLimitsViewModel.HasTimeLimits) { limitUptime = (new TimeSpan(int.Parse(usersVm.ProfileAndLimitsViewModel.TimeLimitDays ?? "0"), int.Parse(usersVm.ProfileAndLimitsViewModel.TimeLimitHours ?? "0"), int.Parse(usersVm.ProfileAndLimitsViewModel.TimeLimitMinutes ?? "0"), 0)).ToString().Replace(".", "d "); } var price = !string.IsNullOrEmpty(usersVm.ProfileAndLimitsViewModel.Price?.Trim()) ? usersVm.ProfileAndLimitsViewModel.Price : "free"; var validity = !string.IsNullOrEmpty(usersVm.ProfileAndLimitsViewModel.Validity?.Trim()) ? usersVm.ProfileAndLimitsViewModel.Validity : "unlimited"; var email = $"{validity}@{price}.np"; var pass = 0; for (int i = 0, j = 0; i < count;) { if (dialog.IsCanceled) { break; } string name, password; if (usersVm.UsernameAndPasswordViewModel.IsUsernameRandomlyGenerated) { var nameKind = usersVm.UsernameAndPasswordViewModel.IsUsernameLettersOnly ? RandomStringKind.CharactersOnly : usersVm.UsernameAndPasswordViewModel.IsUsernameNumbersOnly ? RandomStringKind.NumbersOnly : RandomStringKind.Mixed; name = StringUtils.GetRandomString(nameKind, int.Parse(usersVm.UsernameAndPasswordViewModel.UsernameLength)); } else { name = $"{usersVm.UsernameAndPasswordViewModel.UsernameStartCharacters}{int.Parse(usersVm.UsernameAndPasswordViewModel.UsernameStartNumber) + j++}"; } if (Users.Any(u => u.Username == name)) { continue; } dialog.SetMessage($"Adding user ( {i} : {name} ) of {count}"); dialog.SetProgress((double)i / count); if (usersVm.UsernameAndPasswordViewModel.IsPasswordTheSameAsUsername) { password = name; } else { var passKind = usersVm.UsernameAndPasswordViewModel.IsPasswordLettersOnly ? RandomStringKind.CharactersOnly : usersVm.UsernameAndPasswordViewModel.IsPasswordNumbersOnly ? RandomStringKind.NumbersOnly : RandomStringKind.Mixed; password = StringUtils.GetRandomString(passKind, name.Length); } var user = new HotspotUser { Name = name, Password = password, Profile = "default", LimitBytesOut = limitBytesOut, LimitBytesIn = limitBytesIn, LimitUptime = limitUptime, Email = email }; if (await _hotspotClient.AddUserAsync(user)) { Users.Add(new HotspotUserViewModel(user)); pass++; } i++; } await dialog.CloseAsync(); _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs($"{pass} Users added successfully!", null, new SideNotificationViewModel("Hotspot Users", $"{pass} Users added successfully!"))); RaisePropertyChanged(nameof(UsersCount)); }); #endregion }
public ActionResult Login(Login model, string returnUrl) { string msg = "Incorrect username or password."; if (this.ModelState.IsValid) { try { using (DatabaseContext context = new DatabaseContext()) { HotspotUser user = context.HotspotUser.Where(u => u.username.Equals(model.Username) && u.password.Equals(model.Password) && u.status).FirstOrDefault(); if (user != null && user.Hotspot != null) { UserOnline userData = new UserOnline(); userData.Username = user.username; userData.FullName = user.Hotspot.name; userData.Location = user.Hotspot.type; userData.CustomerCode = user.Hotspot.code; userData.HotspotId = user.Hotspot.id; userData.Role = Shared.RoleName.StandardUser; FormsAuthenticationTicket myticket = new FormsAuthenticationTicket(1, user.username, DateTime.Now, DateTime.Now.AddDays(1), true, new JavaScriptSerializer().Serialize(userData)); string encTicket = FormsAuthentication.Encrypt(myticket); Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); if (this.Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } else { return(Redirect("Customer/List")); } } else { Operator admin = context.Operator.Where(u => u.username.Equals(model.Username) && u.password.Equals(model.Password)).FirstOrDefault(); if (admin != null) { UserOnline userData = new UserOnline(); userData.Username = admin.username; userData.Role = Shared.RoleName.Admintstrator; FormsAuthenticationTicket myticket = new FormsAuthenticationTicket(1, admin.username, DateTime.Now, DateTime.Now.AddDays(1), true, new JavaScriptSerializer().Serialize(userData)); string encTicket = FormsAuthentication.Encrypt(myticket); Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); if (this.Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } else { return(Redirect("Admin/Report")); } } } } } catch (Exception ex) { msg = ex.Message + (ex.InnerException != null ? ex.InnerException.ToString() : string.Empty); } } this.ModelState.AddModelError("", msg); return(View(model)); }
public Task <bool> AddUserAsync(HotspotUser user) => UpdateUserAsync(user);