public async Task <(bool IsDone, string Message)> SignUpAsync(RegistrationModel model) { try { var identityUser = await _userManager.FindByEmailAsync(model.Email); if (identityUser != null) { _logger.LogError($"Account service, SignUpAsync()", model.Email); return(IsDone : false, Message : "Such user already exist"); } ; var user = new User { UserName = model.Email, Email = model.Email }; await _userManager.CreateAsync(user, model.Password); var userSettings = new UserSettings { Id = user.Id, EmailNotifications = true, Theme = (int)Theme.LIGHT }; var userProfile = new UserProfile { Icon = model.Icon, Id = user.Id, Tag = $"{model.Email.Split('@').First()}#{model.Email.GetHashCode()}" }; await _context.UserProfiles.AddAsync(userProfile); await _context.UserSettings.AddAsync(userSettings); var result = await _context.SaveChangesAsync(); if (result < 0) { _logger.LogError($"Account service, SignUpAsync()", model.Email, result); return(IsDone : false, Message : "Can't create such user"); } return(await SendConfirmEmailAsync(model.Email)); } catch (Exception e) { _logger.LogError("AccountService: SignUpAsync()", e); return(IsDone : false, Message : "Can't create such user, please try again later"); } }
public async Task <(bool IsDone, string Message)> UpdateAsync(Guid userId, UpdatePoliciesModel model) { try { var user = await _context.Users.FindAsync(userId); var userAccess = await GetUserAccessAsync(user.Id, model.ProjectId); if (!userAccess[UserAction.CHANGE_SECURITY]) { return(IsDone : false, Message : "You don't have rights"); } foreach (var projectUser in model.Users) { var uId = _context.Users.SingleOrDefault(u => u.Email == projectUser.Email).Id; foreach (var action in projectUser.Actions) { var oldPolicy = _context.ProjectSecurityPolicies .SingleOrDefault(x => x.UserId == uId && x.Action == (int)action.Action && x.ProjectSettingsId == model.ProjectId); if (oldPolicy != null) { _context.Remove(oldPolicy); await _context.SaveChangesAsync(); } var policy = new ProjectSecurityPolicy { Action = (int)action.Action, IsAllowed = action.Allowed, UserId = uId, ProjectSettingsId = model.ProjectId }; await _context.ProjectSecurityPolicies.AddAsync(policy); } } await _context.SaveChangesAsync(); return(IsDone : true, Message : "Success"); } catch (Exception e) { _logger.LogError("SecurityService, UpdateAsync", e); } return(IsDone : false, Message : "Could not update security settings"); }
public async Task <(bool IsDone, string Message)> CreateBoardAsync(Guid userId, CreateBoardModel model) { try { var access = await _securityService.GetUserAccessAsync(userId, model.ProjectId); if (!access[UserAction.CREATE_BOARD]) { return(IsDone : false, Message : "Access denied"); } var boardsCount = _context.Projects .Where(x => x.Id == model.ProjectId) .Include(x => x.Boards) .SingleOrDefault().Boards.Count(); if (boardsCount > 5) { return(IsDone : false, Message : "Maximum 6 boards per project"); } var board = new Board { CreatorId = userId, ProjectId = model.ProjectId, Title = model.Title, TaskPrefix = model.Prefix?.ToUpper() ?? model.Title.ToUpper().Substring(0, model.Title.Length / 2), TaskIndex = 0 }; await _context.Boards.AddAsync(board); await _context.SaveChangesAsync(); return(IsDone : true, Message : "Done"); } catch (Exception e) { _logger.LogError("BoardService, CreateBoardAsync", e); } return(IsDone : false, Message : "Something went wrong, try again later"); }
public async Task <bool> CreateNotificationAsync(CreateNotificationModel model) { try { var user = await _userManager.FindByEmailAsync(model.RecipientEmail); var notification = new Notification { RecipientId = user.Id, Description = model.Description, DirectLink = model.DirectLink, Subject = model.Subject, IsDelivered = false }; _context.Notifications.Add(notification); var result = await _context.SaveChangesAsync(); if (result < 0) { return(false); } var notificationModel = new NotificationViewModel { Description = notification.Description, DirectLink = notification.DirectLink, Id = notification.Id, Subject = notification.Subject }; return(await Notify(user.Id, notificationModel)); } catch (Exception e) { _logger.LogWarning("NotificationService, CreateNotificationAsync:", e); } return(false); }
public async Task <(bool IsDone, string Message)> CreateProjectAsync(Guid userId, CreateProjectModel model) { try { var projectSettings = new ProjectSettings { AccessToChangeProject = (int)UserRole.ADMIN, AccessToChangeBoard = (int)UserRole.ADMIN, AccessToChangeTask = (int)UserRole.MEMBER }; var project = new Project { Title = model.Title, Description = model.Description, Settings = projectSettings }; var projectUser = new ProjectUser { Role = (int)UserRole.ADMIN, UserId = userId, Project = project }; await _context.Projects.AddAsync(project); await _context.ProjectUsers.AddAsync(projectUser); var result = await _context.SaveChangesAsync(); if (result > 0) { return(IsDone : true, Message : "Done"); } } catch (Exception e) { _logger.LogError("ProjectService, CreateProjectAsync", e); } return(IsDone : false, Message : "Something went wrong, please try again later"); }
public async Task <(bool IsDone, string Message)> CreateTaskAsync(Guid userId, CreateTaskModel model) { try { var access = await _securityService.GetUserAccessAsync(userId, model.ProjectId); if (!access[UserAction.CREATE_TASK]) { return(IsDone : false, Message : "Access denied"); } var column = await _context.Columns.FindAsync(model.ColumnId); var board = await _context.Boards.FindAsync(column.BoardId); var assigneeId = _context.Users.SingleOrDefault(x => x.Email == model.AssigneeEmail)?.Id; var attachments = model.Attachments == null ? null : await ToAttachments(model.Attachments); var task = new DataProvider.Entities.Task { Title = $"{board.TaskPrefix}-{board.TaskIndex} {model.Title}", ColumnId = model.ColumnId, CreatorId = userId, AssigneeId = assigneeId, Content = model.Content ?? "", Priority = model.Priority, Severity = model.Severity, Type = model.Type, Attachments = attachments }; await _context.Tasks.AddAsync(task); board.TaskIndex++; _context.Update(board); await _context.SaveChangesAsync(); if (assigneeId != null) { var notification = new CreateNotificationModel { Subject = "You were assigned to task", Description = $"You were assigned to {task.Title}", DirectLink = $"/project/{model.ProjectId}/board/{column.BoardId}", RecipientEmail = model.AssigneeEmail }; await _notificationService.CreateNotificationAsync(notification); } return(IsDone : true, Message : "Success"); } catch (Exception e) { _logger.LogError("TaskService: CreateTaskAsync", e); } return(IsDone : false, Message : "Something went wrong, try again later"); }