public PermissionResult Assign(List <int> permissions, int userId, UserSessionObject currentUser)
        {
            bool       flag            = true;
            var        wrongIds        = new List <int>();
            List <int> alreadyAssigned = RelationUserPermissionRepository
                                         .GetRelationUserPermissionFor(userId)
                                         .Select(x => x.PermissionId)
                                         .ToList();
            PermissionResult result = null;

            foreach (int permissionId in permissions)
            {
                if (!alreadyAssigned.Contains(permissionId))
                {
                    var newLink = new RelationUserPermissionData
                    {
                        PermissionId     = permissionId,
                        UserId           = userId,
                        RefUserCreatedBy = currentUser.UserId
                    };
                    RelationUserPermissionRepository.SaveData(newLink);
                }
                else
                {
                    flag = false;
                    wrongIds.Add(permissionId);
                }
            }

            if (!flag)
            {
                result = new PermissionResult(false, PermissionResult.OperationType.Assign, wrongIds);
            }
            else
            {
                result = new PermissionResult(true, PermissionResult.OperationType.Assign);
            }

            return(result);
        }
        private List <PermissionViewModel> Get(int userId, bool available)
        {
            var roleId = RoleService.GetRoleFor(userId).Id;

            List <int> rolesPermissionIds = RelationRolePermissionRepository.GetAllActiveData()
                                            .Where(x => x.RoleId == roleId)
                                            .Select(x => x.PermissionId)
                                            .ToList();

            List <int> userPermissionIds = RelationUserPermissionRepository.GetAllActiveData()
                                           .Where(x => x.UserId == userId)
                                           .Select(x => x.PermissionId)
                                           .ToList();

            var union = rolesPermissionIds.Union(userPermissionIds);

            var domainData = PermissionRepository.GetAllActiveData()
                             .Where(x => available ? !union.Contains(x.Id) : union.Contains(x.Id))
                             .ToList();

            return(ConvertDataToViewModels(domainData).OrderByDescending(x => x.Id).ToList());
        }
        public PermissionResult Remove(List <int> permissions, int userId, UserSessionObject currentUser)
        {
            bool       flag            = true;
            var        wrongIds        = new List <int>();
            var        result          = new PermissionResult();
            List <int> alreadyAssigned = RelationUserPermissionRepository
                                         .GetRelationUserPermissionFor(userId)
                                         .Select(x => x.PermissionId)
                                         .ToList();

            foreach (int permissionId in permissions)
            {
                if (!alreadyAssigned.Contains(permissionId))
                {
                    flag = false;
                    wrongIds.Add(permissionId);
                }
                else
                {
                    var data = RelationUserPermissionRepository.GetRelationUserPermissionFor(userId, permissionId);
                    RelationUserPermissionRepository.DeleteById(data.Id, currentUser.UserId);
                }
            }

            if (!flag)
            {
                result = new PermissionResult(false, PermissionResult.OperationType.Remove, wrongIds);
            }
            else
            {
                result = new PermissionResult(true, PermissionResult.OperationType.Remove);
            }


            return(result);
        }