public async Task <Module?> FindByIdAsync(ClaimsPrincipal?principal, int id, ModuleOwnershipRef ownerRef)
    {
        ownerRef            = principal.UpdateFrom(ownerRef);
        using var dbContext = Factory.CreateDbContext();
        if (principal is not null)
        {
            if (ownerRef.IsGroup)
            {
                var isAdministrator = await IsGroupOrDataAdministrator(dbContext, principal, ownerRef);

                if (isAdministrator)
                {
                    return(await dbContext.Modules.AsNoTracking()
                           .Where(m => m.Id == id && m.ModuleOwnerships.Any(mo => mo.GroupId == ownerRef.GroupId))
                           .Include(m => m.ModuleOwnerships).ThenInclude(mo => mo.Group)
                           .Include(m => m.ModuleExits).ThenInclude(me => me.GableType)
                           .SingleOrDefaultAsync());
                }
            }
            else
            {
                return(await dbContext.Modules.AsNoTracking()
                       .Where(m => m.Id == id && m.ModuleOwnerships.Any(mo => mo.PersonId == ownerRef.PersonId))
                       .Include(m => m.ModuleOwnerships).ThenInclude(mo => mo.Person)
                       .Include(m => m.ModuleExits).ThenInclude(me => me.GableType)
                       .SingleOrDefaultAsync());
            }
        }
        return(null);
    }
    public void IsPersonInGroup()
    {
        var target = ModuleOwnershipRef.PersonInGroup(10, 11);

        Assert.IsTrue(target.IsAny);
        Assert.IsFalse(target.IsPerson);
        Assert.IsFalse(target.IsGroup);
        Assert.IsTrue(target.IsPersonInGroup);
        Assert.IsFalse(target.IsNone);
        Assert.AreEqual(10, target.PersonId);
        Assert.AreEqual(11, target.GroupId);
    }
    public void IsGroupWithClaimsPrincipal()
    {
        var principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(AppClaimTypes.PersonId, "11") }));
        var target    = ModuleOwnershipRef.Group(10);

        target = principal.UpdateFrom(target);
        Assert.IsTrue(target.IsAny);
        Assert.IsFalse(target.IsPerson);
        Assert.IsTrue(target.IsGroup);
        Assert.IsFalse(target.IsPersonInGroup);
        Assert.IsFalse(target.IsNone);
        Assert.AreEqual(0, target.PersonId);
        Assert.AreEqual(10, target.GroupId);
    }
     static ModuleOwnership CreateModuleOwnership(ModuleOwnershipRef ownerRef)
     {
         if (ownerRef.IsGroup)
         {
             return new ModuleOwnership {
                        GroupId = ownerRef.GroupId, OwnedShare = 1
             }
         }
         ;
         return(new ModuleOwnership {
             PersonId = ownerRef.PersonId, OwnedShare = 1
         });
     }
 }
    public async Task <(int Count, string Message, Person?Entity)> SaveAsync(ClaimsPrincipal?principal, Person entity, bool isGroupAdministrator = false)
    {
        var ownerRef = ModuleOwnershipRef.Person(entity.Id);

        if (principal.MaySave(ownerRef, isGroupAdministrator))
        {
            using var dbContext = Factory.CreateDbContext();
            dbContext.People.Attach(entity);
            dbContext.Entry(entity).State = entity.Id.GetState();
            var count = await dbContext.SaveChangesAsync();

            return(count.SaveResult(entity));
        }
        return(principal.SaveNotAuthorised <Person>());
    }
 public async Task <bool> HasAnyNonStationAsync(ClaimsPrincipal?principal, ModuleOwnershipRef ownerRef)
 {
     if (principal is not null)
     {
         ownerRef            = principal.UpdateFrom(ownerRef);
         using var dbContext = Factory.CreateDbContext();
         if (ownerRef.IsPerson || ownerRef.IsPersonInGroup)
         {
             return(await dbContext.Modules
                    .Where(m => m.ModuleOwnerships.Any(mo => mo.PersonId == ownerRef.PersonId))
                    .AnyAsync(m => !m.StationId.HasValue));
         }
         else if (ownerRef.IsGroup)
         {
             return(await dbContext.Modules
                    .Where(m => m.ModuleOwnerships.Any(mo => mo.GroupId == ownerRef.GroupId))
                    .AnyAsync(m => !m.StationId.HasValue));
         }
     }
     return(false);
 }
    public async Task <IEnumerable <Module> > GetAllAsync(ClaimsPrincipal?principal, ModuleOwnershipRef ownershipRef)
    {
        if (principal.IsAuthenticated())
        {
            ownershipRef = principal.UpdateFrom(ownershipRef);

            using var dbContext = Factory.CreateDbContext();
            var isMemberInGroupsInSameDomain = GroupService.IsMemberInGroupsInSameDomain(dbContext, principal, ownershipRef);

            var modules = await dbContext.Modules.AsNoTracking()
                          .Where(m => m.ModuleOwnerships.Any(mo => ownershipRef.IsGroup && mo.GroupId == ownershipRef.GroupId || mo.PersonId == ownershipRef.PersonId))
                          .Include(m => m.ModuleOwnerships).ThenInclude(mo => mo.Person)
                          .Include(m => m.ModuleOwnerships).ThenInclude(mo => mo.Group)
                          .Include(m => m.Scale)
                          .Include(m => m.Standard)
                          .ToListAsync();


            if (ownershipRef.IsGroup)
            {
                return(modules
                       .Where(m => m.ObjectVisibilityId >= principal.MinimumObjectVisibility(ownershipRef, isMemberInGroupsInSameDomain) && m.ModuleOwnerships.Any(mo => principal.IsMemberOfGroupSpecificGroupDomainOrNone(mo.Group.GroupDomainId))));
            }
            else if (ownershipRef.IsPersonInGroup)
            {
                return(modules
                       .Where(m => m.ObjectVisibilityId >= principal.MinimumObjectVisibility(ownershipRef, isMemberInGroupsInSameDomain)));
            }
            else if (ownershipRef.IsPerson)
            {
                return(modules
                       .Where(m => m.ObjectVisibilityId >= principal.MinimumObjectVisibility(ownershipRef, isMemberInGroupsInSameDomain)));
            }
        }
        return(Array.Empty <Module>());
    }
    public async Task <IEnumerable <Module> > GetAllGroupOwnedForDataAdministrator(ClaimsPrincipal?principal, ModuleOwnershipRef ownershipRef)
    {
        if (principal.IsAuthenticated())
        {
            ownershipRef        = principal.UpdateFrom(ownershipRef);
            using var dbContext = Factory.CreateDbContext();
            var administeredGroupsIds = await dbContext.Groups.AsNoTracking()
                                        .Where(g => g.GroupMembers.Any(gm => gm.PersonId == ownershipRef.PersonId && gm.IsDataAdministrator))
                                        .Select(g => g.Id).ToListAsync();

            if (administeredGroupsIds.Any())
            {
                return(await dbContext.Modules.AsNoTracking()
                       .Where(m => m.ModuleOwnerships.Any(mo => mo.GroupId.HasValue && administeredGroupsIds.Contains(mo.GroupId.Value)))
                       .Include(m => m.ModuleOwnerships).ThenInclude(mo => mo.Person)
                       .Include(m => m.ModuleOwnerships).ThenInclude(mo => mo.Group)
                       .Include(m => m.Scale)
                       .Include(m => m.Standard)
                       .ToListAsync());
            }
        }
        return(Array.Empty <Module>());
    }
Exemplo n.º 9
0
    internal static bool IsMemberInGroupsInSameDomain(ModulesDbContext dbContext, ClaimsPrincipal?principal, ModuleOwnershipRef ownershipRef)
    {
        if (principal.IsAuthenticated() && ownershipRef.IsPerson)
        {
            if (principal.PersonId() == ownershipRef.PersonId)
            {
                return(false);
            }

            var memberships = dbContext.GroupMembers.AsNoTracking().Include(gm => gm.Group)
                              .Where(gm => gm.Group.GroupDomainId > 0 && (gm.PersonId == ownershipRef.PersonId || gm.PersonId == principal.PersonId()))
                              .AsEnumerable()
                              .GroupBy(gm => gm.Group.GroupDomainId)
                              .ToList();
            return(memberships.Any(m => m.Count(gm => gm.PersonId == ownershipRef.PersonId || gm.PersonId == principal.PersonId()) > 1));
        }
        return(false);
    }
Exemplo n.º 10
0
 public bool IsMemberInGroupsInSameDomain(ClaimsPrincipal?principal, ModuleOwnershipRef ownershipRef)
 {
     using var dbContext = Factory.CreateDbContext();
     return(IsMemberInGroupsInSameDomain(dbContext, principal, ownershipRef));
 }
 public async Task <IEnumerable <ListboxItem> > ModuleItems(ClaimsPrincipal?principal, ModuleOwnershipRef ownerRef, int?stationId)
 {
     ownerRef = principal.UpdateFrom(ownerRef);
     if (principal is not null)
     {
         using var dbContext = Factory.CreateDbContext();
         List <Module>?modules;
         if (stationId.HasValue)
         {
             modules = await dbContext.Modules.AsNoTracking()
                       .Where(m => (!m.StationId.HasValue || m.StationId.Value == stationId.Value) && m.ModuleOwnerships
                              .Any(mo => mo.PersonId == ownerRef.PersonId || mo.GroupId == ownerRef.GroupId))
                       .ToListAsync();
         }
         else
         {
             modules = await dbContext.Modules.AsNoTracking()
                       .Where(m => m.ModuleOwnerships
                              .Any(mo => mo.PersonId == ownerRef.PersonId || mo.GroupId == ownerRef.GroupId))
                       .ToListAsync();
         }
         return(modules.Select(m => new ListboxItem(m.Id, $"{m.FullName} {m.ConfigurationLabel}{m.PackageLabel} {m.FremoNumber}".Trim())).OrderBy(l => l.Description));
     }
     return(Array.Empty <ListboxItem>());
 }
    public async Task <(int Count, string Message, Module?Entity)> SaveAsync(ClaimsPrincipal?principal, Module entity, ModuleOwnershipRef ownerRef)
    {
        ownerRef            = principal.UpdateFrom(ownerRef);
        entity.Length       = entity.CalculateLength();
        using var dbContext = Factory.CreateDbContext();
        var isGroupAdministrator = await IsPrincipalGroupsDataAdministrator(dbContext, principal, ownerRef);

        if (await IsSameNameAlreadyExisting(dbContext, entity, ownerRef))
        {
            return(Strings.ModuleNameIsAlreadyTaken.SaveResult(entity));
        }
        if (principal.MaySave(ownerRef, isGroupAdministrator))
        {
            return(await AddOrUpdate(dbContext, entity, ownerRef));
        }
        else if (ownerRef.IsGroup)
        {
            bool isPrincipalGroupsDataAdministrator = await IsPrincipalGroupsDataAdministrator(dbContext, principal, ownerRef);

            return(isPrincipalGroupsDataAdministrator ? await AddOrUpdate(dbContext, entity, ownerRef) : principal.SaveNotAuthorised <Module>());
        }
        else if (ownerRef.IsPersonInGroup)
        {
            var isMember = await IsOwnerMemberOfGroup(ownerRef, dbContext);

            return(isGroupAdministrator && isMember ? await AddOrUpdate(dbContext, entity, ownerRef) : principal.SaveNotAuthorised <Module>());
        }
        return(principal.SaveNotAuthorised <Module>());
 public static string Href(this ModuleOwnershipRef ownershipRef, string objectName, int objectId, string ActionName) =>
 ownershipRef.IsPersonInGroup ?
 $"/{objectName}/{objectId}/{ActionName}/PersonOwned/{ownershipRef.PersonId}/InGroup/{ownershipRef.GroupId}" :
 ownershipRef.IsGroup ?
 $"/{objectName}/{objectId}/{ActionName}/GroupOwned/{ownershipRef.GroupId}" :
 $"/{objectName}/{objectId}/{ActionName}/PersonOwned/{ownershipRef.PersonId}";
 private static async Task <bool> IsGroupOrDataAdministrator(ModulesDbContext dbContext, ClaimsPrincipal?principal, ModuleOwnershipRef ownerRef)
 {
     if (principal.IsGlobalAdministrator())
     {
         return(true);
     }
     if (principal.IsCountryAdministrator())
     {
         if (await dbContext.Groups.AnyAsync(g => g.Id == ownerRef.GroupId && g.CountryId == principal.CountryId()))
         {
             return(true);
         }
     }
     return(await dbContext.GroupMembers.AsNoTracking()
            .AnyAsync(gm => gm.GroupId == ownerRef.GroupId && gm.PersonId == principal.PersonId() && (gm.IsDataAdministrator || gm.IsGroupAdministrator)));
 }
        static async Task <(int Count, string Message, Module?Entity)> AddNew(ModulesDbContext dbContext, Module entity, ModuleOwnershipRef ownerRef)
        {
            if (entity.ModuleOwnerships.Count == 0)
            {
                entity.ModuleOwnerships.Add(CreateModuleOwnership(ownerRef));
            }
            dbContext.Add(entity);
            var result = await dbContext.SaveChangesAsync();

            return(result.SaveResult(entity));
        static async Task <(int Count, string Message, Module?Entity)> AddOrUpdate(ModulesDbContext dbContext, Module entity, ModuleOwnershipRef ownerRef)
        {
            var existing = await dbContext.Modules
                           .Include(m => m.ModuleExits)
                           .FirstOrDefaultAsync(m => m.Id == entity.Id);

            return((existing is null) ?
                   await AddNew(dbContext, entity, ownerRef) :
                   await UpdateExisting(dbContext, entity, existing));
        }
        static async Task <bool> IsSameNameAlreadyExisting(ModulesDbContext dbContext, Module module, ModuleOwnershipRef ownerRef)
        {
            var existing = await dbContext.Modules.Where(m =>
                                                         m.Id != module.Id &&
                                                         m.ModuleOwnerships.Any(mo => mo.PersonId == ownerRef.PersonId || mo.GroupId == ownerRef.GroupId)).ToListAsync();

            if (existing is null)
            {
                return(false);
            }
            return(existing.Any(m =>
                                m.FullName.Equals(module.FullName, StringComparison.OrdinalIgnoreCase) &&
                                (m.ConfigurationLabel is null || m.ConfigurationLabel.Equals(module.ConfigurationLabel, StringComparison.OrdinalIgnoreCase))));
        }