Ejemplo n.º 1
0
        public async Task SetRightsToProject(UpdateUserRightDTO update, int userId)
        {
            var project = await _context.Projects.FirstOrDefaultAsync(p => p.Id == update.ProjectId);

            if (project.AuthorId != userId)
            {
                _logger.LogWarning(LoggingEvents.HaveException, $"NonAuthorRightsChange");
                throw new NonAuthorRightsChange();
            }
            if (project.AuthorId == update.UserId)
            {
                _logger.LogWarning(LoggingEvents.HaveException, $"RightsChangeForProjectAuthorException");
                throw new RightsChangeForProjectAuthorException();
            }

            var projectMember = await _context.ProjectMembers
                                .FirstOrDefaultAsync(pm => pm.UserId == update.UserId && pm.ProjectId == update.ProjectId);

            if (projectMember == null)
            {
                await _context.AddAsync(new ProjectMember()
                {
                    ProjectId  = update.ProjectId,
                    UserId     = update.UserId,
                    UserAccess = update.Access
                }
                                        );
            }
            else
            {
                projectMember.UserAccess = update.Access;
                _context.Update(projectMember);
            }
            await _context.SaveChangesAsync();

            var opportunity = string.Empty;

            switch (update.Access)
            {
            case UserAccess.CanRead:
                opportunity = "can read";
                break;

            case UserAccess.CanWrite:
                opportunity = "can write";
                break;

            case UserAccess.CanBuild:
                opportunity = "can build";
                break;

            case UserAccess.CanRun:
                opportunity = "can run";
                break;

            default:
                opportunity = string.Empty;
                break;
            }

            var notification = new NotificationDTO()
            {
                Type      = NotificationType.AssinedToProject,
                ProjectId = update.ProjectId,
                DateTime  = DateTime.Now,
                Message   = $"Now you can {opportunity} project \"{project.Name}\".",
                Status    = NotificationStatus.Message
            };

            using (var scope = _serviceScopeFactory.CreateScope())
            {
                var notificationService = scope.ServiceProvider.GetService <INotificationService>();
                await notificationService.SendNotificationToUserById(update.UserId, notification);
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SetRights([FromBody] UpdateUserRightDTO update)
        {
            await _rightsService.SetRightsToProject(update, this.GetUserIdFromToken());

            return(Ok());
        }