diff --git a/Database/Database-ConnectionsCommands.cs b/Database/Database-ConnectionsCommands.cs index 5decb80d..8badca64 100644 --- a/Database/Database-ConnectionsCommands.cs +++ b/Database/Database-ConnectionsCommands.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.Common; +using System.Security; using System.Text; using MySql.Data; @@ -227,5 +228,23 @@ namespace Swarmops.Database { return d.ToString("yyyyMMddHHmmss"); } + + + private string SqlSanitize(string input) + { + string[] forbiddenArray = {"\\", "--", ";", "/*", "*/", "select ", "drop ", "update ", "delete ", "insert ", "="}; + + string output = input.Replace("'", "''"); // the typical case + string inputLower = input.ToLowerInvariant(); + foreach (string forbidden in forbiddenArray) + { + if (inputLower.Contains(forbidden)) + { + throw new SecurityException("Attempt at SQL injection: parameter passed to SELECT was '" + input + "'"); + } + } + + return output; + } } } \ No newline at end of file diff --git a/Database/Database-People.cs b/Database/Database-People.cs index 912180bc..6a19bc84 100644 --- a/Database/Database-People.cs +++ b/Database/Database-People.cs @@ -64,7 +64,7 @@ namespace Swarmops.Database DbCommand command = GetDbCommand( - "SELECT " + personFieldSequence + " WHERE Name like '" + namePattern.Replace("'", "''") + "'", + "SELECT " + personFieldSequence + " WHERE Name like '" + GetSqlPatternFromNamePattern(namePattern).Replace("'", "''") + "'", connection); command.CommandTimeout = 120; @@ -80,6 +80,31 @@ namespace Swarmops.Database return result.ToArray(); } + private string GetSqlPatternFromNamePattern(string namePattern) + { + // Remove any injection codes + namePattern = SqlSanitize(namePattern).Replace("%", String.Empty).Trim(); + + while (namePattern.Contains(" ")) + { + namePattern = namePattern.Replace(" ", " "); + } + + if (namePattern.Length > 0) + { + namePattern = namePattern.Replace(" ", "% ") + "%"; + + if (!namePattern.Contains(" ")) + { + namePattern = "%" + namePattern; + } + + return namePattern; + } + + return string.Empty; // If no valid search string was supplied + } + public BasicPerson[] GetPeopleFromBirthdate (DateTime fromdate, DateTime todate) { // Is this really valid for MySQL? Yes .net connector makes @parameter substitutions @@ -405,7 +430,7 @@ namespace Swarmops.Database } - public BasicPerson[] GetPeopleInGeographies (int[] geographyIds) + public BasicPerson[] GetPeopleInGeographies(int[] geographyIds) { if (geographyIds.Length == 0) { @@ -435,7 +460,45 @@ namespace Swarmops.Database - public void SetPersonName (int personId, string name) + public BasicPerson[] GetPeopleInGeographiesWithPattern (int[] geographyIds, string pattern) + { + if (geographyIds.Length == 0) + { + return new BasicPerson[0]; + } + + pattern = pattern.Trim(); + + if (pattern.Length == 0) + { + return GetPeopleInGeographies(geographyIds); // no pattern => fall back to just geography + } + + string sqlPattern = GetSqlPatternFromNamePattern(pattern); + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + DbCommand command = + GetDbCommand("SELECT " + personFieldSequence + " WHERE GeographyId IN (" + JoinIds(geographyIds) + ") AND (Name like '" + sqlPattern + "' OR Email like '" + sqlPattern + "')", + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadPersonFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + + public void SetPersonName(int personId, string name) { this.SetPersonBasicStringValue(personId, "Name", name); } diff --git a/Logic/Swarm/People.cs b/Logic/Swarm/People.cs index f2dffd30..c32b8611 100644 --- a/Logic/Swarm/People.cs +++ b/Logic/Swarm/People.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Swarmops.Basic.Enums; +using Swarmops.Basic.Interfaces; using Swarmops.Basic.Types; using Swarmops.Database; using Swarmops.Logic.Security; @@ -68,27 +69,12 @@ namespace Swarmops.Logic.Swarm public static People FromNamePattern (string namePattern) { - // Remove any injection codes - namePattern = namePattern.Replace("%", String.Empty).Trim(); - - while (namePattern.Contains(" ")) + if (namePattern.Trim().Length < 3) { - namePattern = namePattern.Replace(" ", " "); + return new People(); } - if (namePattern.Length > 0) - { - namePattern = namePattern.Replace(" ", "% ") + "%"; - - if (!namePattern.Contains(" ")) - { - namePattern = "%" + namePattern; - } - - return People.FromArray(SwarmDb.GetDatabaseForReading().GetPeopleFromNamePattern(namePattern)); - } - - return null; // If no valid search string was supplied + return People.FromArray(SwarmDb.GetDatabaseForReading().GetPeopleFromNamePattern(namePattern)); } public static People FromBirthDatePattern (DateTime fromdate, DateTime todate) @@ -269,9 +255,34 @@ namespace Swarmops.Logic.Swarm return retlist; } - public static People FromOrganizationAndGeography (int organizationId, int geographyId) + + public static People FromOrganizationAndGeographyWithPattern(Organization organization, Geography geography, + string pattern) { - Geographies geoTree = Geography.FromIdentity(geographyId).GetTree(); + Geographies geoTree = geography.GetTree(); + + pattern = pattern.Trim(); + if (pattern.Length == 0) + { + return FromOrganizationAndGeography(organization, geography); + } + else if (pattern.Length < 3) + { + // too short pattern! Return empty set. + return new People(); + } + + // First, get list of people in the geography, then filter on memberships + + BasicPerson[] candidates = SwarmDb.GetDatabaseForReading().GetPeopleInGeographiesWithPattern(geoTree.Identities, pattern); + + return FilterByMembership(candidates, organization); + } + + + public static People FromOrganizationAndGeography(Organization organization, Geography geography) + { + Geographies geoTree = geography.GetTree(); // First, get list of people in the geography, then filter on memberships @@ -279,9 +290,16 @@ namespace Swarmops.Logic.Swarm // Filter on memberships + return FilterByMembership(people, organization); + } + + + private static People FilterByMembership(BasicPerson[] candidates, Organization organization) + { + // Get the organization tree - Organizations orgTree = Organization.FromIdentity(organizationId).GetTree(); + Organizations orgTree = organization.GetTree(); // Build a lookup table of this organization tree. For each person in 'people' // that has at least one membership in an organization in the lookup table, @@ -297,11 +315,11 @@ namespace Swarmops.Logic.Swarm // Get the list of all memberships Dictionary> memberships = - SwarmDb.GetDatabaseForReading().GetMembershipsForPeople(LogicServices.ObjectsToIdentifiers(people)); + SwarmDb.GetDatabaseForReading().GetMembershipsForPeople(LogicServices.ObjectsToIdentifiers(candidates)); var result = new People(); - foreach (BasicPerson basicPerson in people) + foreach (BasicPerson basicPerson in candidates) { bool memberOfOrganization = false; diff --git a/Site5/App_GlobalResources/Global.resx b/Site5/App_GlobalResources/Global.resx index 522c85eb..ab33ae20 100644 --- a/Site5/App_GlobalResources/Global.resx +++ b/Site5/App_GlobalResources/Global.resx @@ -392,7 +392,7 @@ Revenue - - - Select one - - + -- Select one -- Shown in dropdowns when selection required @@ -471,4 +471,8 @@ Non-profit Non-profit (activist, political parties) + + Geography + As in "location" - on any scale and anywhere from "World" to a named city + \ No newline at end of file diff --git a/Site5/App_GlobalResources/Pages.People.resx b/Site5/App_GlobalResources/Pages.People.resx index c00d516e..4ae33a5d 100644 --- a/Site5/App_GlobalResources/Pages.People.resx +++ b/Site5/App_GlobalResources/Pages.People.resx @@ -117,10 +117,20 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + Find people in the organization (write more here) - - Find, List People + + Matching People in {0} + The {0} parameter is the organization name. + + + Name Pattern + + + List and Find People + + + Over 1,000 matches. Please refine search. \ No newline at end of file diff --git a/Site5/Automation/Json-GeographiesTree.aspx.cs b/Site5/Automation/Json-GeographiesTree.aspx.cs index 2529ae15..395f117c 100644 --- a/Site5/Automation/Json-GeographiesTree.aspx.cs +++ b/Site5/Automation/Json-GeographiesTree.aspx.cs @@ -66,7 +66,7 @@ namespace Swarmops.Frontend.Automation int renderRootNodeId = rootGeography.ParentGeographyId; // This works as rootGeography will be the only present child in the collection; other children won't be there - accountsJson = RecurseTreeMap(treeMap, renderRootNodeId); + accountsJson = RecurseTreeMap(treeMap, renderRootNodeId, true); Cache.Insert(cacheKey, accountsJson, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero); // cache lasts for five minutes, no sliding expiration @@ -75,7 +75,7 @@ namespace Swarmops.Frontend.Automation Response.End(); } - private string RecurseTreeMap(Dictionary> treeMap, int node) + private string RecurseTreeMap(Dictionary> treeMap, int node, bool expanded) { List elements = new List(); @@ -86,7 +86,7 @@ namespace Swarmops.Frontend.Automation if (treeMap.ContainsKey(geography.Identity)) { - element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, geography.Identity); + element += ",\"state\":\"" + (expanded? "open":"closed") + "\",\"children\":" + RecurseTreeMap(treeMap, geography.Identity, false); } elements.Add("{" + element + "}"); diff --git a/Site5/Controls/v5/Base/ComboGeographies.ascx b/Site5/Controls/v5/Base/ComboGeographies.ascx index 51dfa51f..72e3111b 100644 --- a/Site5/Controls/v5/Base/ComboGeographies.ascx +++ b/Site5/Controls/v5/Base/ComboGeographies.ascx @@ -11,8 +11,8 @@ if (!String.IsNullOrEmpty(this.OnClientLoaded)) { Response.Write(this.OnClientLoaded + "(); "); - } - Response.Write("}"); + }%> + }<% if (!String.IsNullOrEmpty(this.OnClientSelect)) { Response.Write(", onSelect: function(account) { " + this.OnClientSelect + "(account.id); }"); diff --git a/Site5/MainMenu-v5.xml b/Site5/MainMenu-v5.xml index 8f92d19f..bef7096f 100644 --- a/Site5/MainMenu-v5.xml +++ b/Site5/MainMenu-v5.xml @@ -17,7 +17,7 @@ 2 - /Pages/v5/People/ListFindPeople.aspx + /Pages/v5/Swarm/ListFindPeople.aspx Link diff --git a/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx b/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx new file mode 100644 index 00000000..3936a7ce --- /dev/null +++ b/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-ListFindPeople.aspx.cs" Inherits="Swarmops.Frontend.Pages.Swarm.Json_ListFindPeople" %> diff --git a/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs b/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs new file mode 100644 index 00000000..0867e9e8 --- /dev/null +++ b/Site5/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using Swarmops.Logic.Financial; +using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Swarm; + +namespace Swarmops.Frontend.Pages.Swarm +{ + public partial class Json_ListFindPeople : DataV5Base + { + protected void Page_Load(object sender, EventArgs e) + { + Response.ContentType = "application/json"; + + string pattern = HttpUtility.UrlDecode(Request.QueryString["Pattern"]); + + string geographyString = Request.QueryString["GeographyId"]; + int geographyId = int.Parse(geographyString); + Geography geography = Geography.FromIdentity(geographyId); + + People matches = People.FromOrganizationAndGeographyWithPattern(this.CurrentOrganization, geography, pattern); + + // matches = Authorization.FilterPeopleToMatchAuthority(matches, CurrentUser.GetAuthority()); + + if (matches == null) + { + matches = new People(); + } + + if (matches.Count > 1000) + { + matches.RemoveRange(1000, matches.Count - 1000); + } + + List jsonPeople = new List(); + + foreach (Person person in matches) + { + string onePerson = '{' + String.Format("\"id\":\"{0}\",\"name\":\"{1}\",\"avatar16Url\":\"{2}\",\"geographyName\":\"{3}\"", + person.Identity, + JsonSanitize(person.Canonical), + person.GetSecureAvatarLink(16), + JsonSanitize(person.Geography.Name)) + '}'; + jsonPeople.Add(onePerson); + } + + string result = '[' + String.Join(",", jsonPeople.ToArray()) + ']'; + + Response.Output.WriteLine(result); + + Response.End(); + } + } +} \ No newline at end of file diff --git a/Site5/Pages/v5/People/ListFindPeople.aspx b/Site5/Pages/v5/Swarm/ListFindPeople.aspx similarity index 78% rename from Site5/Pages/v5/People/ListFindPeople.aspx rename to Site5/Pages/v5/Swarm/ListFindPeople.aspx index 43c92c11..cdc8c8da 100644 --- a/Site5/Pages/v5/People/ListFindPeople.aspx +++ b/Site5/Pages/v5/Swarm/ListFindPeople.aspx @@ -1,4 +1,4 @@ -<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="ListFindPeople.aspx.cs" Inherits="Swarmops.Frontend.Pages.People.ListFindPeople" %> +<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="ListFindPeople.aspx.cs" Inherits="Swarmops.Frontend.Pages.Swarm.ListFindPeople" %> <%@ Register src="~/Controls/v5/UI/ExternalScripts.ascx" tagname="ExternalScripts" tagprefix="Swarmops5" %> <%@ Register src="~/Controls/v5/Base/ComboGeographies.ascx" tagname="ComboGeographies" tagprefix="Swarmops5" %> @@ -25,7 +25,28 @@ ]); $(document).ready(function () { - $('#TablePayableCosts').datagrid( + + $('#<%=this.TextNamePattern.ClientID%>').on('fieldchange', function () { + var newNamePattern = $('#<%=this.TextNamePattern.ClientID%>').val(); + if (newNamePattern != lastNamePattern && newNamePattern.length >= 3) { + lastNamePattern = newNamePattern; + $('#TableSearchResults').datagrid({ url: 'Json-ListFindPeople.aspx?Pattern=' + escape(newNamePattern) + '&GeographyId=' + selectedGeographyId }); + } + }); + + $('#<%=this.TextNamePattern.ClientID%>').on('input', function () { + clearTimeout(this.delayer); + + var context = this; + this.delayer = setTimeout(function () { + $(context).trigger('fieldchange'); + }, 1000); + }); + + var count = 0; + + + $('#TableSearchResults').datagrid( { onLoadSuccess: function () { $(".LocalIconApproval").attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); @@ -117,6 +138,16 @@ ); }); + function onGeographyChange(newGeographyId) { + if (newGeographyId != selectedGeographyId) { + selectedGeographyId = newGeographyId; + $('#TableSearchResults').datagrid({ url: 'Json-ListFindPeople.aspx?Pattern=' + escape(lastNamePattern) + '&GeographyId=' + selectedGeographyId }); + } + } + + var lastNamePattern = ''; + var selectedGeographyId = 1; + -

-
-  
- +
+  
+
-