Ejemplo n.º 1
0
        public async Task <IActionResult> RemoveUsersFromGroups(UsersGroupsIdsDTO model, [FromServices] IAuditLogService auditLogger)
        {
            List <ApplicationUser> users = new List <ApplicationUser>();

            foreach (var userId in model.UserIds)
            {
                var userInDb = await db.Users.FindAsync(userId);

                if (userInDb == null)
                {
                    return(BadRequest());
                }
                users.Add(userInDb);
            }
            List <Group> groups = new List <Group>();

            foreach (var groupId in model.GroupIds)
            {
                var groupInDb = await db.Groups.FindAsync(groupId);

                if (groupInDb == null)
                {
                    return(BadRequest());
                }
                groups.Add(groupInDb);
            }

            try
            {
                db.AuditDisabled = true;    // doing custom logging
                foreach (var user in users) // remove each user from each group
                {
                    foreach (var group in groups)
                    {
                        await auditLogger.CreateAsync($"Removed user: {user.UserName} from group: {group.Name}", "Removing users from groups", "User"); // log for each change

                        var userGroup = await db.UserGroups.FirstOrDefaultAsync(ug => ug.UserId == user.Id && ug.GroupId == group.GroupId);

                        if (userGroup != null)
                        {
                            db.UserGroups.Remove(userGroup);
                        }
                    }
                }

                await db.SaveChangesAsync();

                await auditLogger.SaveAsync(); // save all logs after successful save

                return(Ok());
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddUsersToGroups(UsersGroupsIdsDTO model, [FromServices] IAuditLogService auditLogger)
        {
            List <ApplicationUser> users = new List <ApplicationUser>();

            foreach (var userId in model.UserIds)
            {
                var userInDb = await db.Users.FindAsync(userId);

                if (userInDb == null)
                {
                    return(BadRequest());
                }
                users.Add(userInDb);
            }
            List <Group> groups = new List <Group>();

            foreach (var groupId in model.GroupIds)
            {
                var groupInDb = await db.Groups.FindAsync(groupId);

                if (groupInDb == null)
                {
                    return(BadRequest());
                }
                groups.Add(groupInDb);
            }

            try
            {
                db.AuditDisabled = true;
                foreach (var user in users) // add each user to each group
                {
                    foreach (var group in groups)
                    {
                        if (!await db.UserGroups.AnyAsync(ug => ug.UserId == user.Id && ug.GroupId == group.GroupId))                               // if not already in group
                        {
                            await auditLogger.CreateAsync($"Added user: {user.UserName} to group: {group.Name}", "Adding users to groups", "User"); // log for each change

                            //await auditLogger.CreateAsync($"Added user: {user.UserName} to group: {group.Name}", "Adding users to groups", "User"); // log for each change
                            await db.UserGroups.AddAsync(new UserGroup { UserId = user.Id, GroupId = group.GroupId });
                        }
                    }
                }
                await db.SaveChangesAsync();

                await auditLogger.SaveAsync(); // save all logs after successful save

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }