public OperationResult CreateFriendRequest(string requesterLogin, string requestedLogin) { var requester = citizenUserRepository.GetUserByLogin(requesterLogin); if (requester == null) { return(OperationResult.Failed($"User with Login = {requesterLogin} not found")); } var requested = citizenUserRepository.GetUserByLogin(requestedLogin); if (requested == null) { return(OperationResult.Failed($"User with Login = {requestedLogin} not found")); } if (friendshipRepository.FriendshipBetweenUsersExists(requesterLogin, requestedLogin)) { return(OperationResult.Failed("Friendship between users already exists")); } var friendRequest = new Friendship { RequestDate = DateTime.Now, AcceptanceDate = null, FriendshipStatus = FriendshipStatus.Pending, Requester = requester, Requested = requested }; friendshipRepository.Save(friendRequest); return(OperationResult.Success()); }
public IActionResult SignUp() { var user = cuRepo.GetUserByLogin(User.Identity.Name); var item = mapper.Map <UserVerificationViewModel>(user); if (pmRepo.IsUserPoliceman(user, out _)) { item.Verified = true; } return(View(item)); }
public void Validate(CitizenUser user) { if (user == null) { throw new ValidationException("Specified user does not exist."); } if (string.IsNullOrWhiteSpace(user.Login)) { var invalidUserName = $"Login '{user.Login}' is invalid, can only contain letters or digits."; validationMessages += Environment.NewLine + invalidUserName; } var owner = citizenUserRepository.GetUserByLogin(user.Login); if (owner != null && owner.Id != user.Id) { var duplicateUserName = $"Login {user.Login} is already taken."; validationMessages += Environment.NewLine + duplicateUserName; } ValidatePassword(user.Password); if (validationMessages != string.Empty) { throw new ValidationException(validationMessages); } }
public async Task <ActionResult <CertificateViewModel> > PostCertificate(CertificateViewModel certificateViewModel) { // Exclude property from binding. certificateViewModel.Id = 0; if (!ModelState.IsValid) { var errorMessages = ModelState.Values .SelectMany(modelStateEntry => modelStateEntry.Errors.Select(b => b.ErrorMessage)).ToList(); return(BadRequest(errorMessages)); } var citizenUser = citizenUserRepository.GetUserByLogin(certificateViewModel.OwnerLogin); if (citizenUser == null) { return(BadRequest(new List <string>() { $"Citizen with Login = {certificateViewModel.OwnerLogin} not found" })); } if (citizenUser.Certificates.Any(c => string.Equals(c.Name, certificateViewModel.Name, StringComparison.OrdinalIgnoreCase) && c.Status == CertificateStatus.Valid)) { return(BadRequest(new List <string>() { $"The citizen {certificateViewModel.OwnerLogin} already has a valid certificate." })); } var certificate = mapper.Map <Certificate>(certificateViewModel); certificate.Owner = citizenUser; await certificateRepository.SaveAsync(certificate); certificateViewModel = mapper.Map <CertificateViewModel>(certificate); return(CreatedAtAction("GetCertificate", new { id = certificateViewModel.Id }, certificateViewModel)); }
public IViewComponentResult Invoke() { decimal userBalance = 0; if (User.Identity.IsAuthenticated) { var userLogin = User.Identity.Name; var user = citizenUserRepository.GetUserByLogin(userLogin); userBalance = user.Balance; } return(View(userBalance)); }
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RestrictAccessToDeadUsersRequirement requirement) { var currentUserLogin = context.User.Identity?.Name; if (string.IsNullOrEmpty(currentUserLogin)) { context.Fail(); return(Task.CompletedTask); } var currentUser = citizenUserRepository.GetUserByLogin(currentUserLogin); if (currentUser.IsDead) { context.Fail(); } else { context.Succeed(requirement); } return(Task.CompletedTask); }
public CitizenUser FindByLogin(string login) { return(citizenUserRepository.GetUserByLogin(login)); }
private void AddUserTasksToBill() { var bill = citizenUserRepository.GetUserByLogin("Bill"); var billTasks = new List <UserTask>() { new UserTask { Name = "Develop task feature", Description = "Design an user interface and implement business logic", StartDate = DateTime.Now - TimeSpan.FromDays(1), Status = TaskStatus.Planned, Priority = TaskPriority.Medium, Owner = bill }, new UserTask { Name = "Add test data to database", Description = "Implement extension for database seeding. Ensure that admin account exists", StartDate = DateTime.Now + TimeSpan.FromHours(1), Status = TaskStatus.Planned, Priority = TaskPriority.Low, Owner = bill }, new UserTask { Name = "Migrate from Newtonsoft.Json to System.Text.Json", Description = "Remove all dependencies on Newtonsoft.Json and replace it with built-in Json", StartDate = DateTime.Now + TimeSpan.FromHours(2), Status = TaskStatus.Planned, Priority = TaskPriority.Medium, Owner = bill }, new UserTask { Name = "Fix bug with .Designer file", Description = "Last migration cause an error with DbContext file", StartDate = DateTime.Now + TimeSpan.FromHours(3), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill }, new UserTask { Name = "Rename CitizenUser properties", Description = "Marriage and HaveChildren should be renamed to IsMarried and HasChildren", StartDate = DateTime.Now + TimeSpan.FromHours(4), Status = TaskStatus.Planned, Priority = TaskPriority.Low, Owner = bill }, new UserTask { Name = "Refactor user service", Description = "Make UserService class more readable", StartDate = DateTime.Now + TimeSpan.FromHours(5), Status = TaskStatus.Planned, Priority = TaskPriority.Low, Owner = bill }, new UserTask { Name = "Create custom theme for site", Description = "Find good theme in Internet and add it to website", StartDate = DateTime.Now.Date + TimeSpan.FromDays(1), Status = TaskStatus.Planned, Priority = TaskPriority.Low, Owner = bill }, new UserTask { Name = "Merge with upstream", Description = "Pavel add new feature to the project. It should be pulled.", StartDate = DateTime.Now.Date + TimeSpan.FromHours(30), Status = TaskStatus.Planned, Priority = TaskPriority.Medium, Owner = bill }, new UserTask { Name = "Find the cause of the bug with database updating", Description = "After last migration updates are not working", StartDate = DateTime.Now.Date + TimeSpan.FromHours(31), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill }, new UserTask { Name = "Add unique constraint to login", Description = "Login should be unique for whole database", StartDate = DateTime.Now.Date + TimeSpan.FromHours(32), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill }, new UserTask { Name = "Implement policy-based authorization", Description = "Create 2 policies: one for admins and one for users", StartDate = DateTime.Now.Date + TimeSpan.FromHours(33), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill }, new UserTask { Name = "Remove unnecessary code from project", Description = "Delete code that is unreachable by program", StartDate = DateTime.Now + TimeSpan.FromDays(3), Status = TaskStatus.Planned, Priority = TaskPriority.Low, Owner = bill }, new UserTask { Name = "Write unit-tests", Description = "Cover the project with unit-tests", StartDate = DateTime.Now + TimeSpan.FromDays(3), Status = TaskStatus.Planned, Priority = TaskPriority.Medium, Owner = bill }, new UserTask { Name = "Add administration page", Description = "It should perform CRUD operations with all database tables", StartDate = DateTime.Now + TimeSpan.FromDays(4), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill }, new UserTask { Name = "Deploy website", Description = "Find free hosting and publish website to it", StartDate = DateTime.Now + TimeSpan.FromDays(5), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill }, new UserTask { Name = "Test deployed website", Description = "Search for bugs and make hot fixes", StartDate = DateTime.Now + TimeSpan.FromDays(10), Status = TaskStatus.Planned, Priority = TaskPriority.High, Owner = bill } }; var notExistBillTasks = billTasks.Where(task => !userTaskRepository.TaskWithNameExists(task.Name)); foreach (var billTask in notExistBillTasks) { userTaskRepository.Save(billTask); } }