diff --git a/Database/Database-Memberships.cs b/Database/Database-Memberships.cs index ba076bac..5999234c 100644 --- a/Database/Database-Memberships.cs +++ b/Database/Database-Memberships.cs @@ -273,7 +273,7 @@ namespace Swarmops.Database // Optimization: If more than 128 personIds, build a hash table, get _all_ memberships, and return // the matching ones logic wise. We've had _queries_ that exceed 64k text in length... - if (personIds.Length > 64) + if (personIds.Length > 128) { return GetMembershipsForPeopleOptimizedForLargeSets (personIds, gracePeriod); } diff --git a/Logic/Security/Accesses.cs b/Logic/Security/Accesses.cs index 630224a8..7d48fa97 100644 --- a/Logic/Security/Accesses.cs +++ b/Logic/Security/Accesses.cs @@ -66,6 +66,11 @@ namespace Swarmops.Logic.Security /// PersonalData, + /// + /// Access to the mere existence (photo, name) about people in an organization + /// + Participation, + /// /// Access to change organization's fundamental operating parameters /// diff --git a/Logic/Security/Authority.cs b/Logic/Security/Authority.cs index a0bc98d4..2c23b14c 100644 --- a/Logic/Security/Authority.cs +++ b/Logic/Security/Authority.cs @@ -234,28 +234,9 @@ namespace Swarmops.Logic.Security // Check if the person is currently acting at sysadmin level - // TODO: CHECK POSITION ASSIGNMENT - // for now, check if position assignment exists - - // This is not very efficient, but there shouldn't be many system-level positions or - // assignments, so it should scale reasonably even in an 1M organization - - PositionAssignments assignments = Positions.ForSystem().Assignments; - - foreach (PositionAssignment assignment in assignments) + if (HasSystemAccess (access.Type)) { - if (assignment.Active && assignment.PersonId == this._data.PersonId) - { - if ((assignment.Position.PositionType == PositionType.System_SysadminMain || - assignment.Position.PositionType == PositionType.System_SysadminReadWrite)) - return true; - - if (assignment.Position.PositionType == PositionType.System_SysadminAssistantReadOnly && - access.Type == AccessType.Read) - { - return true; // Read-only access - } - } + return true; } // If system-level access was requested and has not been granted at this point, deny it @@ -280,23 +261,217 @@ namespace Swarmops.Logic.Security } - - - - public bool CanSeePerson (Person person) + public bool HasSystemAccess (AccessType accessType = AccessType.Write) { - People initialList = People.FromSingle (person); + // TODO: CHECK POSITION ASSIGNMENT + // for now, check if position assignment exists - throw new NotImplementedException(); + // This is not very efficient, but there shouldn't be many system-level positions or + // assignments, so it should scale reasonably even in an 1M organization - /*People filteredList = Authorization.FilterPeopleToMatchAuthority (initialList, this); + PositionAssignments assignments = Positions.ForSystem().Assignments; - if (filteredList.Count == 0) + foreach (PositionAssignment assignment in assignments) + { + if (assignment.Active && assignment.PersonId == this._data.PersonId) + { + if ((assignment.Position.PositionType == PositionType.System_SysadminMain || + assignment.Position.PositionType == PositionType.System_SysadminReadWrite)) + return true; + + if (assignment.Position.PositionType == PositionType.System_SysadminAssistantReadOnly && + accessType == AccessType.Read) + { + return true; // Read-only access + } + } + } + + return false; + } + + + public People FilterPeople(People rawList, AccessAspect aspect = AccessAspect.Participation) + { + if (aspect != AccessAspect.Participation && aspect != AccessAspect.PersonalData) + { + throw new ArgumentException(@"AccessAspect needs to reflect visibility of people data", "aspect"); + } + + // Three cases: + + // 1) the current Position has system-level access. + // 2) the current Position has org-level, but not system-level, access. + // 3) the current Position has org-and-geo-level access. + + Dictionary orgLookup = new Dictionary(); + Dictionary geoLookup = new Dictionary(); + + People result = new People(); + + // Org lookup will always be needed. Geo lookup may be needed for case 3. + + Organizations orgStructure = this.Organization.GetTree(); + int[] orgIds = orgStructure.Identities; + foreach (int orgId in orgIds) + { + orgLookup[orgId] = true; + } + orgLookup[Organization.Identity] = true; + + Dictionary> membershipLookup = null; + + if (HasSystemAccess(AccessType.Read) || HasAccess(new Access(Organization, aspect, AccessType.Read))) + { + // cases 1 and 2: systemwide access, return everybody at or under the current Organization, + // or org-wide read access (at least) to participant/personal data at current Organization + + // Optimization: Get all memberships in advance, without instantiating logic objects + membershipLookup = Memberships.GetMembershipsForPeople (rawList.Identities, 0); + + foreach (Person person in rawList) + { + // For each person, we must test the list of active memberships to see if one of + // them is visible to this Authority - if it's a membership in an org at or below the + // Authority object's organization + + List list = membershipLookup[person.Identity]; + + foreach (BasicMembership basicMembership in list) + { + if (orgLookup.ContainsKey (basicMembership.OrganizationId)) + { + // hit - this person has an active membership that makes them visible to this Authority + result.Add (person); + } + } + } + + return result; + } + + // Case 3: Same as above but also check for Geography (in an AND pattern). + + if (this.Position == null) + { + // No access at all. That was an easy case! + + return new People(); // return empty list + } + + if (this.Position.Geography == null) + { + // Org-level position, but one that doesn't have access to personal data, apparently. + + return new People(); // empty list again + } + + if (!HasAccess (new Access (this.Organization, Position.Geography, aspect, AccessType.Read))) + { + // No people access for active position. Also a reasonably easy case. + + return new People(); // also return empty list + } + + Geographies geoStructure = this.Position.Geography.GetTree(); + int[] geoIds = geoStructure.Identities; + foreach (int geoId in geoIds) + { + geoLookup[geoId] = true; + } + geoLookup[Position.GeographyId] = true; + + // Optimization: Get all memberships in advance, without instantiating logic objects + Dictionary> personLookup = + Memberships.GetMembershipsForPeople(rawList.Identities, 0); + + foreach (Person person in rawList) + { + // For each person, we must test the list of active memberships to see if one of + // them is visible to this Authority - if it's a membership in an org at or below the + // Authority object's organization - and also test the person's Geography against + // the list (lookup) of visible Geographies. We do Geographies first, because that test is + // much cheaper. + + if (geoLookup[person.GeographyId]) + { + // Geography hit. Test Membership / Organization. + + List list = personLookup[person.Identity]; + + foreach (BasicMembership basicMembership in list) + { + if (orgLookup.ContainsKey (basicMembership.OrganizationId)) + { + // Organization hit - this person has an active membership that makes them visible to this Authority + + result.Add (person); + } + } + } + } + + return result; + + + } + + + public bool CanSeePerson (Person person, AccessAspect aspect = AccessAspect.Participation) + { + if (aspect != AccessAspect.Participation && aspect != AccessAspect.PersonalData) + { + throw new ArgumentException(@"AccessAspect needs to reflect visibility of people data", "aspect"); + } + + // Three cases: + + // 1) the current Position has system-level access. + // 2) the current Position has org-level, but not system-level, access. + // 3) the current Position has org-and-geo-level access. + + if (HasSystemAccess (AccessType.Read)) // case 1 + { + // Still filter to the current Organization, even though we have systemwide access + + if (person.MemberOfWithInherited(Organization)) + { + return true; + } + } + + // Is this Person a Participant of an org or sub-org where the current Authority + // has organizationwide access? Case 2. + + if ( + HasAccess (new Access (Organization, aspect, AccessType.Read))) + { + if (person.MemberOfWithInherited (Organization)) + { + return true; + } + } + + // Finally, determine by geography AND organization. + + if (Position == null || Position.Geography == null) { return false; } - return true;*/ + if ( + HasAccess (new Access (Organization, Position.Geography, aspect, AccessType.Read))) + { + if (person.MemberOfWithInherited(Organization)) + { + if (person.GeographyId == Position.GeographyId || person.Geography.Inherits (Position.Geography)) + { + return true; + } + } + } + + return false; } diff --git a/Site5/Automation/Json-SearchPeoplePattern.aspx.cs b/Site5/Automation/Json-SearchPeoplePattern.aspx.cs index b2f30ee0..56ffeddb 100644 --- a/Site5/Automation/Json-SearchPeoplePattern.aspx.cs +++ b/Site5/Automation/Json-SearchPeoplePattern.aspx.cs @@ -15,8 +15,7 @@ namespace Swarmops.Frontend.Automation People matches = People.FromNamePattern (pattern); - throw new NotImplementedException(); // Needs security check here - // matches = Authorization.FilterPeopleToMatchAuthority (matches, CurrentUser.GetAuthority()); + matches = CurrentAuthority.FilterPeople (matches); if (matches == null) { diff --git a/Site5/Automation/SwarmFunctions.aspx.cs b/Site5/Automation/SwarmFunctions.aspx.cs index df6937d1..97cf7e35 100644 --- a/Site5/Automation/SwarmFunctions.aspx.cs +++ b/Site5/Automation/SwarmFunctions.aspx.cs @@ -131,24 +131,19 @@ namespace Swarmops.Frontend.Automation { AuthenticationData authData = GetAuthenticationDataAndCulture(); - People matches = People.FromSingle(Person.FromIdentity(personId)); - - // CHange to new functions - throw new NotImplementedException(); - // matches = Authorization.FilterPeopleToMatchAuthority(matches, authData.CurrentUser.GetAuthority()); // TODO: Change to Access - - if (matches.Count != 1) + Person person = Person.FromIdentity (personId); + if (!authData.Authority.CanSeePerson (person)) { - throw new ArgumentException(); // no match, for whatever reason + throw new ArgumentException(); // can't see, for whatever reason } return new AvatarData { PersonId = personId, Success = true, - Canonical = matches[0].Canonical, - Avatar16Url = matches[0].GetSecureAvatarLink (16), - Avatar24Url = matches[0].GetSecureAvatarLink (24) + Canonical = person.Canonical, + Avatar16Url = person.GetSecureAvatarLink (16), + Avatar24Url = person.GetSecureAvatarLink (24) }; } } diff --git a/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs b/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs index d439bf8c..813e114f 100644 --- a/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs +++ b/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs @@ -28,7 +28,7 @@ namespace Swarmops.Frontend.Pages.Swarm People matches = People.FromOrganizationAndGeographyWithPattern (CurrentOrganization, geography, pattern); - // matches = Authorization.FilterPeopleToMatchAuthority(matches, CurrentUser.GetAuthority()); + matches = CurrentAuthority.FilterPeople (matches); if (matches == null) {