public bool CheckTrusteeWithIdIsInRole(Guid trusteeId, BudgetRole role)
 {
     using (var context = CreateContext())
     {
         using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
         {
             return context.SecurityDescriptorRecords.Count(
                 p =>
                 p.RoleId == role.Id && p.SecurityTrustee.Enabled &&
                 (p.TrusteeId == trusteeId ||
                  (p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled &&
                   p.SecurityTrustee.SecurityGroups1.Count(t => t.SecurityTrustee.Enabled && t.TrusteeId == trusteeId) > 0))) > 0;
         }
     }
 }
        public bool CheckThatSomebodyHasRoleInFilial(BudgetRole role, Guid filialId, Guid budgetId)
        {
            using (var context = CreateContext())
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
                {
                    return context.SecurityDescriptorRecords.Count(
                        p =>
                        p.RoleId == role.Id &&
                        ((!p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled  && p.SecurityTrustee.Employees.Count(e=>!e.IsDeleted && e.CFO.FilialId.HasValue && e.CFO.FilialId == filialId && e.BudgetId == budgetId) > 0) ||
                         (p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled &&
                          p.SecurityTrustee.SecurityGroups1.Count(t => t.SecurityTrustee.Enabled && t.SecurityTrustee.Employees.Count(e => !e.IsDeleted && e.CFO.FilialId.HasValue && e.CFO.FilialId == filialId && e.BudgetId == budgetId) > 0) >
                          0))) > 0;
                }

            }
        }
        public bool CheckThatSomebodyHasRole (BudgetRole role)
        {
            using (var context = CreateContext())
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
                {
                    return context.SecurityDescriptorRecords.Count(
                        p =>
                        p.RoleId == role.Id &&
                        ((!p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled  && !p.SecurityTrustee.Employees.First().IsDeleted) ||
                         (p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled &&
                          p.SecurityTrustee.SecurityGroups1.Count(t => t.SecurityTrustee.Enabled && !t.SecurityTrustee.Employees.First().IsDeleted) >
                          0))) > 0;
                }

            }
        }
 public bool CheckThatSomebodyHasRole(List<Guid> trusteeIds, BudgetRole role)
 {
     using (var context = CreateContext())
     {
         using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
         {
             return context.SecurityDescriptorRecords.Count(
                 p =>
                 p.RoleId == role.Id &&
                 ((trusteeIds.Contains(p.TrusteeId) && p.SecurityTrustee.Enabled) ||
                  (p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled && p.SecurityTrustee.SecurityGroups1.Count(t => t.SecurityTrustee.Enabled &&  trusteeIds.Contains(t.TrusteeId)) > 0))) > 0;
         }
     }
 }
        public IEnumerable<Employee> GetAllEmployeesInRole(IEnumerable<Guid> trusteeIds, BudgetRole role, Guid budgetId, bool addDeputies)
        {
            var emplRecord = new Dictionary<Guid, Employee>();
            using (var context = CreateContext())
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
                {
                    var allEmployee = context.SecurityDescriptorRecords.Where(
                        p =>
                        p.RoleId == role.Id &&
                        ((trusteeIds.Contains(p.TrusteeId) && p.SecurityTrustee.Enabled) ||
                         (p.SecurityTrustee.IsContainer && p.SecurityTrustee.Enabled &&
                          p.SecurityTrustee.SecurityGroups1.Count(t => t.SecurityTrustee.Enabled && trusteeIds.Contains(t.TrusteeId)) > 0))).Select(
                              sdr => sdr.SecurityTrustee.Employees.Where(empl => empl.SecurityTrusteeId != null /*&&  empl.EMail != null && empl.EMail != string.Empty && empl.IsSendWorkflowNotification*/));

                    foreach (var employees in allEmployee)
                    {
                        foreach (var employee in employees)
                        {
                            if (!emplRecord.ContainsKey(employee.Id))
                                emplRecord.Add(employee.Id, new Employee() { Email = employee.EMail, Id = employee.Id, IdentityId = employee.SecurityTrusteeId.Value, IsSendNotification = employee.IsSendWorkflowNotification });
                        }
                    }
                }
            }

            if (!addDeputies)
                return emplRecord.Values.Where(e=>e.IsSendNotification && !string.IsNullOrEmpty(e.Email));
            else
                return EmployeeService.AddDeputies(emplRecord.Values, budgetId);
        }
        public IEnumerable<Guid> GetAllSecurityTrusteeInRoleInStructDivision(Guid structDivisionId, BudgetRole role)
        {
            using (var context = CreateContext())
            {
               var ids =
                        context.Employees.Where(
                            e => e.SecurityTrusteeId.HasValue && 
                            !e.IsDeleted && e.StructDivisionId == structDivisionId && e.SecurityTrustee.Enabled &&
                            e.SecurityTrustee.vSecurityTrusteeRoles.Count(sr => sr.RoleId == role.Id) > 0).Select(e => e.SecurityTrusteeId.Value).ToList();

                    return ids;
            }
        }
 public bool IsInRole(Guid identityId, BudgetRole role)
 {
     return SecurityEntityService.CheckTrusteeWithIdIsInRole(identityId, role);
 }