예제 #1
0
        public IActionResult ChangeUser([FromBody] User user)
        {
            if (user == null)
            {
                return(BadRequest("Invalid user: object was null"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid user object sent from client");
                return(BadRequest("Invalid user object sent from client"));
            }

            if (!user.ValidateChangeUser())
            {
                return(BadRequest("Some required values were not filled!"));
            }

            try {
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                dbUser.Map(user);
                _repository.User.Update(dbUser);
                _repository.Save();

                return(Accepted());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside ChangeUser action: {e.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
예제 #2
0
        private bool CheckSubGroupAuthorized(long groupId)
        {
            var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var dbUser   = _repository.User.GetUserByEmail(userMail);

            return(_repository.UserToSubgroup.IsMember(dbUser.Id, groupId));
        }
예제 #3
0
        public IActionResult GetNewToken()
        {
            string token = Request.Headers["Authorization"];
            string email = AuthControllerExtensions.JwtNameExtractor(token);

            return(Ok(new { Token = AuthControllerExtensions.GenerateToken(email), Leasetime = 30 }));
        }
        public IActionResult GetAllStudents()
        {
            var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var dbUser   = _repository.User.GetUserByEmail(userMail);

            var allStudents = _repository.User.FindByCondition(u => u.University_Id.Equals(dbUser.University_Id));

            return(Ok(allStudents));
        }
        public IActionResult RemoveSubgroupMember(long id)
        {
            var userToSubgroup = new UserToSubgroup
            {
                SubgroupId = id
            };

            if (userToSubgroup == null)
            {
                _logger.LogError($"Invalid client request: object was null");
                return(BadRequest("Invalid client request: object was null"));
            }

            var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var dbUser   = _repository.User.GetUserByEmail(userMail);

            try
            {
                var subgroup           = _repository.Subgroup.GetSubgroupById(userToSubgroup.SubgroupId);
                var allSubgroupMembers = _repository.UserToSubgroup.GetMembersForSubgroup(subgroup).ToList();
                var isMember           = false;

                foreach (var mem in allSubgroupMembers)
                {
                    if (mem.Id.Equals(dbUser.Id))
                    {
                        isMember = true;
                        break;
                    }
                }

                if (isMember)
                {
                    var allUserToSubgroups = _repository.UserToSubgroup.GetMembershipsForSubgroup(subgroup).ToList();

                    foreach (var ms in allUserToSubgroups)
                    {
                        if (ms.UserId.Equals(dbUser.Id))
                        {
                            _repository.UserToSubgroup.DeleteMembership(ms);
                            _repository.Save();

                            return(Accepted());
                        }
                    }
                }

                return(BadRequest("User is not member of this subgroup"));
            }

            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside RemoveSubgroupMember: {e.Message}");
                return(StatusCode(500, $"Something went wrong while deleting subgroup member"));
            }
        }
예제 #6
0
        public IActionResult DeleteGroup(long id)
        {
            var group = _repository.Group.GetGroupById(id);

            if (group == null)
            {
                _logger.LogError("Group to delete was not found!");
                return(BadRequest("Invalid client request, group was not found!"));
            }

            var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var dbUser   = _repository.User.GetUserByEmail(userMail);

            if (!(group.AdminUserId == dbUser.Id))
            {
                _logger.LogError("Only admin can delte his groups!");
                return(BadRequest("Only group admin can delete this group!"));
            }

            try
            {
                var listOfMembers = _repository.UserToGroup.GetMembershipsForGroup(group).ToList();
                var subgroups     = _repository.Subgroup.GetSubgroupsForGroup(group.Id).ToList();

                foreach (var subgroup in subgroups)
                {
                    var memberships = _repository.UserToSubgroup.GetMembershipsForSubgroup(subgroup).ToList();

                    foreach (var mem in memberships)
                    {
                        _repository.UserToSubgroup.DeleteMembership(mem);
                        _repository.Save();
                    }

                    _repository.Subgroup.DeleteGroup(subgroup);
                    _repository.Save();
                }

                foreach (var mem in listOfMembers)
                {
                    _repository.UserToGroup.DeleteMembership(mem);
                }

                _repository.Save();
                _repository.Group.DeleteGroup(group);
                _repository.Save();

                return(Accepted());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside DeleteGroup: {e.Message}");
                return(StatusCode(500, "Something went wrong during deleting the group"));
            }
        }
예제 #7
0
        public IActionResult CreateDiscussionEntry([FromBody] DiscussionEntry entry)
        {
            if (entry == null)
            {
                _logger.LogError("Invalid object: entry was null");
                return(BadRequest("Invalid client request"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid DiscussionEntry object sent from client.");
                return(BadRequest("Invalid DiscussionEntry object sent from client."));
            }

            try
            {
                if (entry.NormalGroup.HasValue && !CheckGroupAuthorized(entry.NormalGroup.Value))
                {
                    return(Unauthorized());
                }
                else if (entry.Subgroup.HasValue && !CheckSubGroupAuthorized(entry.Subgroup.Value))
                {
                    return(Unauthorized());
                }


                entry.TimeStamp = DateTime.Now;
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);
                entry.UserId = dbUser.Id;

                _repository.DiscussionEntry.PostDiscussion(entry);
                _repository.Save();

                if (entry.Subgroup != null)
                {
                    SimpleLongPolling.Publish($"subgroup{entry.Subgroup.Value}", entry.Id);
                    _pushSender.SendSubGroupPush(entry.Subgroup.Value, entry.Id, entry.Text, dbUser.Id);
                }
                else if (entry.NormalGroup != null)
                {
                    SimpleLongPolling.Publish($"group{entry.NormalGroup.Value}", entry.Id);
                    _pushSender.SendGroupPush(entry.NormalGroup.Value, entry.Id, entry.Text, dbUser.Id);
                }

                return(Ok(_repository.DiscussionEntry.GetDiscussionEntryById(entry.Id)));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside CreateDiscussionEntry: {e.Message}");
                return(StatusCode(500, "Something went wrong during creating discussion entry"));
            }
        }
        public IActionResult CreateSubgroup([FromBody] Subgroup subgroup)
        {
            if (subgroup == null)
            {
                _logger.LogError("Invalid object: subgroup was null");
                return(BadRequest("Invalid client request"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid subgroup object sent from client.");
                return(BadRequest("Invalid subgroup object sent from client."));
            }

            if (!subgroup.ValidateCreateSubgroup())
            {
                return(BadRequest("Sent object was not valid!"));
            }

            try
            {
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                var mainGroup = _repository.Group.GetGroupById(subgroup.Main_group);

                if (mainGroup.AdminUserId.Equals(dbUser.Id))
                {
                    _repository.Subgroup.Create(subgroup);
                    _repository.Save();

                    var adminMemberShip = new UserToSubgroup
                    {
                        UserId     = dbUser.Id,
                        SubgroupId = subgroup.Id
                    };

                    _repository.UserToSubgroup.AddMembership(adminMemberShip);
                    _repository.Save();

                    return(Ok(_repository.Subgroup.GetSubgroupById(subgroup.Id)));
                }

                return(BadRequest("Only admin can add a subgroup to a group!"));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside CreateSubgroup: {e.Message}");
                return(StatusCode(500, "Something went wrong during creating subgroup"));
            }
        }
예제 #9
0
        public IActionResult GetUser()
        {
            string token = Request.Headers["Authorization"];
            string email = AuthControllerExtensions.JwtNameExtractor(token);

            var user = _repository.User.GetUserByEmail(email);

            if (user == null)
            {
                _logger.LogError($"User with email {user.Email} was not found");
                return(BadRequest($"User with email {user.Email} was not found"));
            }

            return(Ok(user));
        }
예제 #10
0
        public IActionResult GetAllGroupsForUser()
        {
            var userMail     = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var loggedInUser = _repository.User.GetUserByEmail(userMail);

            try
            {
                var myGroups    = _repository.UserToGroup.GetGroupsForUser(loggedInUser).ToList();
                var mySubgroups = _repository.UserToSubgroup.GetSubgroupsForUser(loggedInUser).ToList();

                foreach (var subgroup in mySubgroups)
                {
                    foreach (var group in myGroups)
                    {
                        if (subgroup.Main_group.Equals(group.Id))
                        {
                            if (group.Subgroups == null)
                            {
                                group.Subgroups = new List <Subgroup>();
                            }
                            group.Subgroups.Add(subgroup);
                            break;
                        }
                    }
                }

                foreach (var group in myGroups)
                {
                    if (group.AdminUserId.Equals(loggedInUser.Id))
                    {
                        group.IsAdmin = true;
                    }
                    else
                    {
                        group.IsAdmin = false;
                    }
                }

                return(Ok(myGroups));
            }

            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside GetAllGroupsForUser: {e.Message}");
                return(StatusCode(500, "Something went wrong during getting all groups for logged in user"));
            }
        }
예제 #11
0
        public IActionResult CreateGroup([FromBody] Group group)
        {
            if (group == null)
            {
                return(BadRequest("Invalid client request: group object was null"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid user: model state not valid");
                return(BadRequest("Invalid user: model state not valid"));
            }

            if (!group.ValidateCreateGroup())
            {
                return(BadRequest("Sent object was not valid!"));
            }

            try
            {
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                group.AdminUserId = dbUser.Id;

                _repository.Group.CreateGroup(group);
                _repository.Save();

                var adminMembership = new UserToGroup()
                {
                    GroupId = group.Id,
                    UserId  = dbUser.Id
                };

                _repository.UserToGroup.AddMembership(adminMembership);
                _repository.Save();

                return(Ok(_repository.Group.GetGroupById(group.Id)));
            }

            catch (Exception e) {
                _logger.LogError($"Something went wrong inside CreateGroup: {e.Message}");
                return(BadRequest("Something went wrong while creating group, the group was not saved!"));
            }
        }
예제 #12
0
        public IActionResult UpdateSubgroup([FromBody] Subgroup subgroup)
        {
            if (subgroup == null)
            {
                _logger.LogError("Invalid request: Subgroup object was null");
                return(BadRequest("Invalid request: Subgroup object was null"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid user object sent from client");
                return(BadRequest("Invalid user object sent from client"));
            }

            if (!subgroup.ValidateUpdateSubgroup())
            {
                return(BadRequest("Sent object was not valid!"));
            }

            try
            {
                var dbSubgroup = _repository.Subgroup.GetSubgroupById(subgroup.Id);
                var mainGroup  = _repository.Group.GetGroupById(subgroup.Main_group);

                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                if (dbUser.Id.Equals(mainGroup.AdminUserId))
                {
                    dbSubgroup.Map(subgroup);
                    _repository.Subgroup.Update(dbSubgroup);
                    _repository.Save();
                    return(Accepted());
                }

                _logger.LogError("Only admin can change subgroup");
                return(BadRequest("Only admin can change subgroup"));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside UpdateSubgroup: {e.Message}");
                return(StatusCode(500, "Something went wrong during update subgroup"));
            }
        }
예제 #13
0
        public IActionResult UpdateGroup([FromBody] Group group)
        {
            if (group == null)
            {
                return(BadRequest("Invalid client request: Group object was null"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid group object sent from client.");
                return(BadRequest("Invalid group object sent from client."));
            }

            if (!group.ValidateChangeGroup())
            {
                return(BadRequest("Sent object was not valid!"));
            }

            try
            {
                var dbGroup = _repository.Group.GetGroupById(group.Id);

                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                if (dbUser.Id == dbGroup.AdminUserId)
                {
                    dbGroup.Map(group);
                    _repository.Group.Update(dbGroup);
                    _repository.Save();
                    return(Accepted());
                }

                _logger.LogError("Only admin can change group");
                return(BadRequest("Only admin can change group"));
            }

            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside UpdateGroup: {e.Message}");
                return(StatusCode(500, "Internal Server Error while updating the group"));
            }
        }
예제 #14
0
        public IActionResult ChangePassword([FromBody] User user)
        {
            string token = Request.Headers["Authorization"];
            string email = AuthControllerExtensions.JwtNameExtractor(token);

            var dbUser = _repository.User.GetUserByEmail(email);

            if (dbUser == null)
            {
                return(BadRequest("Invalid client request, User was not found"));
            }

            if ((user.Password == null) || user.Password.Equals(""))
            {
                return(BadRequest("No password was sent!"));
            }

            if (!AuthControllerExtensions.ValidatePassword(user.Password))
            {
                return(BadRequest("The password must have one number, one uppercased char and at least 8 chars!"));
            }

            dbUser.Password = user.Password;

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid user object sent from client.");
                return(BadRequest("Invalid model object"));
            }

            try
            {
                _repository.User.ChangePassword(dbUser);
                _repository.Save();

                return(Accepted());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside ChangePassword action: {e.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
예제 #15
0
        public IActionResult DeleteSubgroup(long id)
        {
            var subgroup = _repository.Subgroup.GetSubgroupById(id);

            if (subgroup == null)
            {
                _logger.LogError("Invalid object: subgroup was null");
                return(BadRequest("Subgroup with this id is not existing!"));
            }

            try
            {
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                var mainGroup = _repository.Group.GetGroupById(subgroup.Main_group);

                if (mainGroup.AdminUserId.Equals(dbUser.Id))
                {
                    var listOfMembers = _repository.UserToSubgroup.GetMembershipsForSubgroup(subgroup);

                    foreach (var mem in listOfMembers)
                    {
                        _repository.UserToSubgroup.DeleteMembership(mem);
                    }

                    _repository.Save();
                    _repository.Subgroup.Delete(subgroup);
                    _repository.Save();

                    return(Accepted());
                }

                return(BadRequest("Only admin of main group can delete subgroup"));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside DelteSubgroup {e.Message}");
                return(StatusCode(500, $"Something went wrong during deleting the subgroup"));
            }
        }
예제 #16
0
        public IActionResult GetAdminGroups()
        {
            var userMail     = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var loggedInUser = _repository.User.GetUserByEmail(userMail);

            try
            {
                var adminGroups = _repository.Group.FindByCondition(gr => (gr.AdminUserId == loggedInUser.Id)).ToList();

                if (adminGroups.Count >= 1)
                {
                    return(Ok(adminGroups));
                }

                return(Accepted());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside GetAdminGroups: {e.Message}");
                return(StatusCode(500, "Something went wrong during getting all admin groups"));
            }
        }
예제 #17
0
        public IActionResult GetPublicGroups()
        {
            var userMail     = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var loggedInUser = _repository.User.GetUserByEmail(userMail);

            try
            {
                var allPublicGroups = _repository.Group.GetPublicGroups().ToList();
                var groupsOfUser    = _repository.UserToGroup.GetGroupsForUser(loggedInUser).ToList();
                var resultGroups    = new List <Group>();

                foreach (var availableGroup in allPublicGroups)
                {
                    var isExisting = false;

                    foreach (var currentUserGroup in groupsOfUser)
                    {
                        if (currentUserGroup.Id == availableGroup.Id)
                        {
                            isExisting = true;
                            break;
                        }
                    }
                    if (!isExisting)
                    {
                        resultGroups.Add(availableGroup);
                    }
                }

                return(Ok(resultGroups));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside GetPublicGroups: {e.Message}");
                return(StatusCode(500, "Something went wrong during getting public groups"));
            }
        }
예제 #18
0
        public IActionResult RemoveMember(long userId, long groupId)
        {
            var group        = _repository.Group.GetGroupById(groupId);
            var userMail     = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
            var loggedInUser = _repository.User.GetUserByEmail(userMail);

            if (group == null)
            {
                _logger.LogError("Group with was not found!");
                return(BadRequest("Group with was not found!"));
            }

            var userToRemove = _repository.User.FindByCondition(us => us.Id == userId).FirstOrDefault();

            if (userToRemove == null)
            {
                _logger.LogError("User with was not found!");
                return(BadRequest("User with was not found!"));
            }

            // Admin cant be deleted from its group
            if (userToRemove.Id == group.AdminUserId)
            {
                _logger.LogError($"User with id {userToRemove.Id} is admin and cant be removed!");
                return(BadRequest($"User with id {userToRemove.Id} is admin and cant be removed!"));
            }

            try
            {
                // Delete all subgroups if existing
                if ((group.AdminUserId == loggedInUser.Id) || (loggedInUser.Id == userToRemove.Id))
                {
                    var listSubgroups = _repository.Subgroup.GetSubgroupsForGroup(groupId).ToList();

                    foreach (var sgr in listSubgroups)
                    {
                        var memberships = _repository.UserToSubgroup.GetMembershipsForSubgroup(sgr).ToList();
                        foreach (var mem in memberships)
                        {
                            if (mem.UserId.Equals(userToRemove.Id))
                            {
                                _repository.UserToSubgroup.DeleteMembership(mem);
                                _repository.Save();
                            }
                        }
                    }

                    var  removeUserToGroup = _repository.UserToGroup.GetMembershipsForUser(userToRemove);
                    bool deleted           = false;

                    foreach (var mem in removeUserToGroup)
                    {
                        if (mem.GroupId == groupId)
                        {
                            _repository.UserToGroup.DeleteMembership(mem);
                            deleted = true;
                            break;
                        }
                    }

                    if (deleted)
                    {
                        _repository.Save();
                        return(Accepted());
                    }

                    _logger.LogError($"User is no member of this group!");
                    return(BadRequest("User is no member of this group!"));
                }

                _logger.LogError("Only admins or members themself can delete their membership!");
                return(BadRequest("Only admins or members themself can delete their membership!"));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside RemoveMember: {e.Message}");
                return(StatusCode(500, "Something went wrong during removing the membership"));
            }
        }
예제 #19
0
        public IActionResult AddMember([FromBody] UserToGroup userToGroupParam)
        {
            var group = _repository.Group.GetGroupById(userToGroupParam.GroupId);

            if (group == null)
            {
                _logger.LogError("Group was not found!");
                return(BadRequest("Invalid client request: Group was not found"));
            }

            var userToAdd = _repository.User.FindByCondition(us => us.Id == userToGroupParam.UserId).FirstOrDefault();

            if (userToAdd == null)
            {
                _logger.LogError("User with this id was not found!");
                return(BadRequest("User with this id was not found!"));
            }

            var currentGroupMemberships = _repository.UserToGroup.GetMembershipsForGroup(group).ToList();

            foreach (var mem in currentGroupMemberships)
            {
                if (mem.UserId == userToAdd.Id)
                {
                    _logger.LogError("User is already member of this group!");
                    return(BadRequest("User is already member of this group!"));
                }
            }

            try
            {
                if (group.IsPublic.Equals("true"))
                {
                    var userToGroupPublic = new UserToGroup()
                    {
                        UserId  = userToAdd.Id,
                        GroupId = group.Id
                    };
                    _repository.UserToGroup.AddMembership(userToGroupPublic);
                    _repository.Save();
                    return(Accepted());
                }

                var userMail     = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var loggedInUser = _repository.User.GetUserByEmail(userMail);

                if (loggedInUser.Id != group.AdminUserId)
                {
                    _logger.LogError("Logged in user is no admin therefore he cant add members");
                    return(BadRequest("Logged in user is no admin therefore he cant add members"));
                }

                var userToGroup = new UserToGroup()
                {
                    UserId  = userToAdd.Id,
                    GroupId = group.Id
                };

                _repository.UserToGroup.AddMembership(userToGroup);
                _repository.Save();
                return(Accepted());
            }

            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside AddMember: {e.Message}");
                return(StatusCode(500, "Something went wrong while adding a new member"));
            }
        }
예제 #20
0
        public IActionResult AddMembers([FromBody] UserToSubgroup userToSubgroup)
        {
            if (userToSubgroup == null)
            {
                _logger.LogError($"Invalid client request: object was null");
                return(BadRequest("Invalid client request: object was null"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError($"Invalid client request: object was invalid");
                return(BadRequest("Invalid client request: object was invalid"));
            }

            try
            {
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);

                var subgroup = _repository.Subgroup.GetSubgroupById(userToSubgroup.SubgroupId);

                if (subgroup == null)
                {
                    _logger.LogError("The subgroup was not found!");
                    return(BadRequest("The subgroup was not found!"));
                }

                var mainGroup           = _repository.Group.GetGroupById(subgroup.Main_group);
                var allMainGroupMembers = _repository.UserToGroup.GetMembersForGroup(mainGroup);

                var userIsMemberOfGroup = false;

                foreach (var user in allMainGroupMembers)
                {
                    if (user.Id.Equals(dbUser.Id))
                    {
                        userIsMemberOfGroup = true;
                        break;
                    }
                }

                if (!userIsMemberOfGroup)
                {
                    return(BadRequest("User is not member of group so user cant be member of subgroup"));
                }

                var allSubgroupMembers = _repository.UserToSubgroup.GetMembersForSubgroup(subgroup);

                var userIsMemberOfSubgroup = false;

                foreach (var user in allSubgroupMembers)
                {
                    if (user.Id.Equals(dbUser.Id))
                    {
                        userIsMemberOfSubgroup = true;
                        break;
                    }
                }

                if (userIsMemberOfSubgroup)
                {
                    return(BadRequest("User is already member of subgroup"));
                }

                UserToSubgroup _userToSubgroup = new UserToSubgroup()
                {
                    SubgroupId = subgroup.Id,
                    UserId     = dbUser.Id
                };

                _repository.UserToSubgroup.Create(_userToSubgroup);
                _repository.Save();
                return(Accepted());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside AddMembers {e.Message}");
                return(StatusCode(500, $"Something went wrong while saving new subgroup member"));
            }
        }
예제 #21
0
        public IActionResult DeleteUser()
        {
            string token = Request.Headers["Authorization"];
            string email = AuthControllerExtensions.JwtNameExtractor(token);

            var dbUser = _repository.User.GetUserByEmail(email);

            try
            {
                //Delete all memberships of user in subgroups
                var removeUserToSubgroup = _repository.UserToSubgroup.GetMembershipsForUser(dbUser).ToList();

                foreach (var mem in removeUserToSubgroup)
                {
                    _repository.UserToSubgroup.DeleteMembership(mem);
                }

                // Delete all memberships of user in groups
                var removeUserToGroup = _repository.UserToGroup.GetMembershipsForUser(dbUser).ToList();

                foreach (var mem in removeUserToGroup)
                {
                    _repository.UserToGroup.DeleteMembership(mem);
                }

                _repository.Save();

                // Delete all groups/subgroups of user
                var groupsFromUser = _repository.Group.FindByCondition(gr => gr.AdminUserId == dbUser.Id).ToList();

                foreach (var group in groupsFromUser)
                {
                    var subgroups = _repository.Subgroup.GetSubgroupsForGroup(group.Id).ToList();

                    foreach (var subgroup in subgroups)
                    {
                        var memberships = _repository.UserToSubgroup.GetMembershipsForSubgroup(subgroup).ToList();

                        foreach (var mem in memberships)
                        {
                            _repository.UserToSubgroup.DeleteMembership(mem);
                            _repository.Save();
                        }

                        _repository.Subgroup.DeleteGroup(subgroup);
                        _repository.Save();
                    }
                }

                var membershipsInGroupsFromUser = new List <UserToGroup>();

                foreach (var group in groupsFromUser)
                {
                    var tempMemberships = _repository.UserToGroup.GetMembershipsForGroup(group).ToList();
                    foreach (var mem in tempMemberships)
                    {
                        membershipsInGroupsFromUser.Add(mem);
                    }
                }

                foreach (var mem in membershipsInGroupsFromUser)
                {
                    _repository.UserToGroup.DeleteMembership(mem);
                }
                _repository.Save();

                foreach (var group in groupsFromUser)
                {
                    _repository.Group.DeleteGroup(group);
                }
                _repository.Save();

                _repository.User.DeleteUser(dbUser);
                _repository.Save();

                return(Accepted());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside DeleteUser action: {e.InnerException}");
                return(StatusCode(500, "Internal server error"));
            }
        }