private DelegationDTO CreateEmptyDelegationModel(int userRoleId) { var sourceUserRole = _adminUserRoleService.GetById(userRoleId); var model = new DelegationDTO { SourceUserRole = sourceUserRole, DestinationUserRole = new UserRoleDTO { Role = new RoleDto { Id = sourceUserRole.RoleId }, StartDate = sourceUserRole.StartDate, EndDate = sourceUserRole.EndDate } }; return(model); }
public void Save(DelegationDTO delegationDTO, List <int> groupsIds) { var delegation = Mapper.Map <Delegation>(delegationDTO); if (delegationDTO.IsNew) { _unitOfWork.DelegationRepository.Add(delegation); _unitOfWork.UserRoleRepository.Add(delegation.DestinationUserRole); foreach (var groupId in groupsIds) { _unitOfWork.UserRoleGroupRepository.Add(new UserRoleGroup { UserRole = delegation.DestinationUserRole, GroupId = groupId }); } } else { var destinationUserRole = _unitOfWork.DelegationRepository.AsQueryable() .Include(del => del.DestinationUserRole) .Include(del => del.DestinationUserRole.UserRoleGroups) .First(del => del.Id == delegation.Id) .DestinationUserRole; foreach (var destinationUserRoleGroup in destinationUserRole.UserRoleGroups.ToList()) { _unitOfWork.UserRoleGroupRepository.Delete(destinationUserRoleGroup); } destinationUserRole.StartDate = delegation.DestinationUserRole.StartDate; destinationUserRole.EndDate = delegation.DestinationUserRole.EndDate; _unitOfWork.UserRoleRepository.Update(destinationUserRole); foreach (var groupId in groupsIds) { _unitOfWork.UserRoleGroupRepository.Add(new UserRoleGroup { UserRole = destinationUserRole, GroupId = groupId }); } } _unitOfWork.Save(); }
public IHttpActionResult SaveDelegation([FromUri] int userRoleId, [FromBody] MyDelegationsModel myDelegations) { var sourceUserRole = _adminUserRoleService.GetById(userRoleId); var delegation = new DelegationDTO { Id = myDelegations.DelegationId, SourceUserRole = sourceUserRole, DestinationUserRole = new UserRoleDTO { RoleId = sourceUserRole.RoleId, UserId = myDelegations.UserId, StartDate = myDelegations.StartDate, EndDate = myDelegations.EndDate } }; var newGroups = _groupService.GetByIds(myDelegations.GroupCheckedIds).Select(x => x.Description); IList <string> oldGroupsFixed = new List <string>(); if (myDelegations.DelegationId != 0) { var existingDelegation = _delegationService.Get(myDelegations.DelegationId); var oldGroups = _authorizationService.GetAllowedGroups(existingDelegation.DestinationUserRole.Id); oldGroupsFixed = _groupService.GetByIds(oldGroups).Select(x => x.Description).ToList(); } _delegationService.Save(delegation, myDelegations.GroupCheckedIds); var user = _userService.GetUserById(myDelegations.UserId); var role = _roleService.GetRoleByUserRoleId(userRoleId); _auditService.Log(AuditEntryDTOBuilder.Build( myDelegations.DelegationId == 0 ? AuditAction.DelegationCreated : AuditAction.DelegationEdited, _connectionInfo.UserId, null, myDelegations.DelegationId == 0 ? null : new[] { _connectionInfo.UserName, role.Name, user.Username, myDelegations.StartDate?.ToShortDateString() ?? "", myDelegations.EndDate?.ToShortDateString() ?? "", string.Join <string>(",", oldGroupsFixed) }, new[] { _connectionInfo.UserName, role.Name, user.Username, myDelegations.StartDate?.ToShortDateString() ?? "", myDelegations.EndDate?.ToShortDateString() ?? "", string.Join <string>(",", newGroups) }, _auditHelper.GetIdentity())); return(Ok(delegation)); }