public async Task <IActionResult> OnPostAsync() { var manageruserName = await _applicationDbContext.ManagerUsers.FirstOrDefaultAsync(mu => mu.ManagerId == AddManagerUser.ManagerUserManagerId && mu.Id == AddManagerUser.ManagerUserUserId); if (manageruserName == null) { return(NotFound($"Not found manager user with id {AddManagerUser.ManagerUserUserId} in manager {AddManagerUser.ManagerUserManagerId}")); } UserManagerUser userManagerUser = new UserManagerUser(); userManagerUser.ManagerUserManagerId = AddManagerUser.ManagerUserManagerId; userManagerUser.ManagerUserName = manageruserName.Name; userManagerUser.UserId = UserView.Id; _applicationDbContext.UserManagerUsers.Add(userManagerUser); await _applicationDbContext.SaveChangesAsync(); return(RedirectToPage("./Edit", new { id = UserView.Id })); }
public UserManagerUserViewModel(UserManagerUser user, UserManagerProfile profile, UserManagerProfileLimitation limitation = null) { UserModel = user; ProfileModel = profile; ProfileLimitationModel = limitation ?? new UserManagerProfileLimitation(); }
/// <summary> /// Subsribe to related events /// </summary> private void SubscribeEvents() { #region Add User Event _eventAggregator.GetEvent <AddUserManagerUserEvent>().Subscribe(async userVm => { if (userVm == null) { return; } var user = new UserManagerUser { Customer = userVm.BasicViewModel.Customers[userVm.BasicViewModel.SelectedCustomerIndex], Name = userVm.BasicViewModel.Username, Password = userVm.BasicViewModel.Password, Comment = userVm.BasicViewModel.Comment, SharedUsers = !string.IsNullOrEmpty(userVm.BasicViewModel.SharedUsers) ? userVm.BasicViewModel.SharedUsers : "unlimited" }; var done = await _userManagerClient.AddUserAsync(user); var msg = done ? "User Add Successfully!" : "Fail Adding User!"; _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs(msg, null, new SideNotificationViewModel("UserManager Users", msg))); if (done && userVm.ProfileViewModel.ActivateUserNow && !string.IsNullOrEmpty(userVm.ProfileViewModel.SelectedProfile?.Name)) { done = await _userManagerClient.ActivateUser(user, userVm.ProfileViewModel.SelectedProfile.Name); msg = done ? "User Activated Successfully!" : "Fail Activating User!"; _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs(msg, null, new SideNotificationViewModel("UserManager Users", msg))); } // Adds to the users list Users.Add(new UserManagerUserViewModel(user, userVm.ProfileViewModel.ActivateUserNow ? userVm.ProfileViewModel?.SelectedProfile?.ProfileModel : null, userVm.ProfileViewModel?.SelectedProfile?.LimitationModel)); }); #endregion #region Add Multiple Users Event _eventAggregator.GetEvent <AddMultipleUserManagerUsersEvent>().Subscribe(async usersVm => { // Fetches the users count from the user var countObj = await _dialogService.ShowInputAsync("Add UserManager 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; } // Shows a progress dialog var dialog = await _dialogService.ShowProgressAsync("Adding users", $"Adding user - of {count}"); dialog.SetCancelable(true); // Gets the customer name var customer = usersVm.ProfileAndOwnerViewModel.Customers[usersVm.ProfileAndOwnerViewModel.SelectedCustomerIndex]; // Created an empty list to contain the created users var created = new List <UserManagerUserViewModel>(); // Gets if the user wants to activate the created users now var activateNow = usersVm.ProfileAndOwnerViewModel.ActivateUserNow; // Starts adding for (int i = 0, j = 0; i < count;) { // Breaks if the user has canceled the operation if (dialog.IsCanceled) { break; } // Generates the username and the password 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++}"; } // Reloops if the generated username is exits if (Users.Any(u => u.Username == name)) { continue; } // Genrates the password 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, int.Parse(usersVm.UsernameAndPasswordViewModel.PasswordLength)); } // Updates the progress dialog message dialog.SetMessage($"Adding user ( {i + 1} : {name} ) of {count}"); dialog.SetProgress((double)i / count); // Inits new user var user = new UserManagerUser { Name = name, Password = password, Customer = "admin", SharedUsers = !string.IsNullOrEmpty(usersVm.ProfileAndOwnerViewModel.SharedUsers.Trim()) ? usersVm.ProfileAndOwnerViewModel.SharedUsers : "unlimited" }; // Adds it to the created list if successed on adding it to the server if (await _userManagerClient.AddUserAsync(user)) { created.Add(new UserManagerUserViewModel(user, null)); } // Increases the counter if every thing is ok i++; } // Activates the created users ( if user has choosed to ) if (usersVm.ProfileAndOwnerViewModel.ActivateUserNow) { // Gets the selected profile by the user var profile = usersVm.ProfileAndOwnerViewModel.SelectedProfile; // Skips if the user is null or its name is not valid if (string.IsNullOrEmpty(profile?.Name.Trim())) { goto skip; } // Updates the progress dialog message dialog.SetTitle("Activating Users"); // Starts the activating proccess for (var i = 0; i < created.Count; i++) { dialog.SetMessage($"Activating User \"{created[i].Username}\"..."); dialog.SetProgress((double)i / created.Count); if (await _userManagerClient.ActivateUser(created[i].UserModel, profile.Name)) { created[i].ProfileModel = profile.ProfileModel; } } } skip: // Closes the progress dialog await dialog.CloseAsync(); // Adds created users to the view Users.AddRange(created); // Shows a summury notification _eventAggregator.GetEvent <NotificationEvent>().Publish(new NotificationEventArgs($"Added {created.Count} Users Successfully!", null, new SideNotificationViewModel("UserManager Users", $"Added {created.Count} Users Successfully!"))); RaisePropertyChanged(nameof(UsersCount)); }); #endregion }