static SiteAlias MakeOrg(SiteAliasProviderBase prov, SiteAliasScheme scheme, Site org, string orgName) { SiteAlias sch = null; if (!prov.Exists(scheme, orgName)) { sch = prov.Create(scheme, org, orgName); if (sch != null) { Console.WriteLine("Created Alias: For: " + org.Name + " Alias: " + sch.Name + " In: " + scheme.Name); } else { Console.WriteLine("Failed to create org alias"); } } else { IEnumerable <SiteAlias> orgs = prov.Get(scheme, orgName); if (orgs != null) { foreach (SiteAlias o in orgs) { if (o.SiteEquals(org)) { sch = o; Console.WriteLine("Fetched Alias: For: " + org.Name + " Alias: " + sch.Name + " In: " + scheme.Name); break; } } } } return(sch); }
static SiteAliasScheme MakeOrg(SiteAliasSchemeProviderBase prov, string orgName, CompoundIdentity oid) { SiteAliasScheme sch = null; if (!prov.Exists(oid, orgName)) { sch = prov.Create(oid, orgName); if (sch != null) { Console.WriteLine("Create Alias Scheme: For: " + oid + " Scheme Name: " + sch.Name); } else { Console.WriteLine("Failed to create org scheme"); } } else { IEnumerable <SiteAliasScheme> orgs = prov.GetByOwner(oid, orgName); if (orgs != null) { foreach (SiteAliasScheme o in orgs) { sch = o; Console.WriteLine("Fetched Alias Scheme: For: " + oid + " Scheme Name: " + sch.Name); break; } } } return(sch); }
public override bool Update(SiteAliasScheme scheme) { if (scheme != null && !scheme.Identity.IsNullOrEmpty() && CanUpdate(scheme)) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.UpdateAliasScheme; cmd.Parameters.AddWithValue("sid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("id", scheme.Identity.Identity); cmd.Parameters.AddWithValue("osid", scheme.OwningOrganizationIdentity.DataStoreIdentity); cmd.Parameters.AddWithValue("oid", scheme.OwningOrganizationIdentity.Identity); cmd.Parameters.AddWithValue("name", scheme.Name); if (string.IsNullOrEmpty(scheme.Description)) { cmd.Parameters.Add(NpgSqlCommandUtils.GetNullInParam("desc", NpgsqlTypes.NpgsqlDbType.Varchar)); } else { cmd.Parameters.AddWithValue("desc", scheme.Description); } cmd.Parameters.AddWithValue("start", DateTime.UtcNow); Db.ExecuteNonQuery(cmd); return(true); } catch { } } return(false); }
public override SiteAliasScheme Get(CompoundIdentity id) { if (!id.IsNullOrEmpty() && this.CanGet()) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectAliasScheme + Db.SelectAliasSchemeById + " AND" + Db.WhereCurrent; cmd.Parameters.AddWithValue("sid", id.DataStoreIdentity); cmd.Parameters.AddWithValue("id", id.Identity); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); SiteAliasScheme o = null; if (rdr != null) { try { rdr.Read(); o = SiteAliasSchemeBuilder.Instance.Build(rdr); if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(o); } return(null); }
public override SiteAlias Create(SiteAliasScheme scheme, Site org, string name) { if (scheme != null && !scheme.Identity.IsNullOrEmpty() && org != null && !org.Identity.IsNullOrEmpty() && !string.IsNullOrEmpty(name) && this.CanCreate()) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.InsertAlias; Guid id = Guid.NewGuid(); cmd.Parameters.AddWithValue("sid", org.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("id", org.Identity.Identity); cmd.Parameters.AddWithValue("ssid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", scheme.Identity.Identity); cmd.Parameters.AddWithValue("name", name); cmd.Parameters.AddWithValue("start", DateTime.UtcNow); Db.ExecuteNonQuery(cmd); return(new SiteAlias(org.Identity, scheme.Identity, name)); } catch { } } return(null); }
private static void GetBySiteAndScheme(CompoundIdentity site_id, CompoundIdentity scheme_id, UserSecurityContext user, HttpContext context, CancellationToken cancel) { try { SiteAliasProviderBase provider = SiteManager.Instance.GetSiteAliasProvider(user); SiteAliasScheme scheme = SiteManager.Instance.GetSiteAliasSchemeProvider(user).Get(scheme_id); if (provider != null && scheme != null && site_id != null) { IEnumerable <SiteAlias> aliases = provider.Get(site_id, scheme); JArray jaliases = Jsonifier.ToJson(aliases); if (jaliases != null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, jaliases.ToString()); } else { RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]"); } return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } }
public static JObject ToJson(SiteAliasScheme scheme) { if (scheme != null) { JObject o = new JObject(); o.Add(JsonUtils.Id, JsonUtils.ToJson(scheme.Identity)); o.Add(JsonUtils.OwnerId, JsonUtils.ToJson(scheme.OwningOrganizationIdentity)); o.Add(JsonUtils.Name, scheme.Name); o.Add(JsonUtils.Description, scheme.Description); return(o); } return(null); }
public override bool Exists(CompoundIdentity id, SiteAliasScheme scheme) { if (!id.IsNullOrEmpty() && scheme != null && !(scheme.Identity.IsNullOrEmpty()) && this.CanGet()) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.CountAlias + Db.SelectAliasById + " AND" + Db.WhereCurrent; cmd.Parameters.AddWithValue("sid", id.DataStoreIdentity); cmd.Parameters.AddWithValue("id", id.Identity); cmd.Parameters.AddWithValue("ssid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", scheme.Identity.Identity); return(Db.Exists(cmd)); } return(false); }
public override IEnumerable <SiteAliasScheme> GetByOwner(CompoundIdentity owningOrgId, string name, StringComparison comparisonOption) { if (!owningOrgId.IsNullOrEmpty() && !string.IsNullOrEmpty(name) && this.CanGet()) { string where = Db.SelectAliasSchemeByOwnerId; if (comparisonOption == StringComparison.CurrentCultureIgnoreCase || comparisonOption == StringComparison.OrdinalIgnoreCase) { where += " AND lower(\"Name\")=lower(:name)"; } else { where += " AND \"Name\"=:name"; } NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectAliasScheme + where + " AND" + Db.WhereCurrent; cmd.Parameters.AddWithValue("sid", owningOrgId.DataStoreIdentity); cmd.Parameters.AddWithValue("id", owningOrgId.Identity); cmd.Parameters.AddWithValue("name", name); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); List <SiteAliasScheme> schemes = new List <SiteAliasScheme>(); SiteAliasScheme o = null; if (rdr != null) { try { while (rdr.Read()) { o = SiteAliasSchemeBuilder.Instance.Build(rdr); if (o != null) { schemes.Add(o); } } if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(schemes); } return(null); }
public override IEnumerable <SiteAlias> Get(CompoundIdentity id, SiteAliasScheme scheme) { if (!id.IsNullOrEmpty() && scheme != null && !scheme.Identity.IsNullOrEmpty() && this.CanGet()) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectAlias + Db.SelectAliasById + " AND" + Db.WhereCurrent; cmd.Parameters.AddWithValue("sid", id.DataStoreIdentity); cmd.Parameters.AddWithValue("id", id.Identity); cmd.Parameters.AddWithValue("ssid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", scheme.Identity.Identity); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); List <SiteAlias> schemes = new List <SiteAlias>(); SiteAlias o = null; if (rdr != null) { try { while (rdr.Read()) { o = SiteAliasBuilder.Instance.Build(rdr); if (o != null) { schemes.Add(o); } } if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(schemes); } return(null); }
public override bool Delete(SiteAliasScheme scheme) { if (scheme != null && !scheme.Identity.IsNullOrEmpty() && this.CanDelete()) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.DeleteAlias + "\"SchemeSystemId\"=:ssid AND \"SchemeId\"=:scid AND" + Db.WhereCurrent; cmd.Parameters.AddWithValue("ssid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", scheme.Identity.Identity); cmd.Parameters.AddWithValue("end", DateTime.UtcNow); Db.ExecuteNonQuery(cmd); return(true); } catch { } } return(false); }
public override bool Exists(SiteAliasScheme scheme, string name, StringComparison comparisonOption) { if (scheme != null && !(scheme.Identity.IsNullOrEmpty()) && !string.IsNullOrEmpty(name) && this.CanGet()) { string where = Db.SelectAliasByScheme; if (comparisonOption == StringComparison.CurrentCultureIgnoreCase || comparisonOption == StringComparison.OrdinalIgnoreCase) { where += " AND lower(\"Name\")=lower(:name)"; } else { where += " AND \"Name\"=:name"; } NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.CountAlias + where + " AND" + Db.WhereCurrent; cmd.Parameters.AddWithValue("ssid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", scheme.Identity.Identity); cmd.Parameters.AddWithValue("name", name); return(Db.Exists(cmd)); } return(false); }
public override bool CanUpdate(SiteAliasScheme org) { return(this.CanUpdate()); //TODO -- add fine grained security }
// ---> /sites/schemes/all public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel) { if (context.Request.Method == "POST") { if (method.Equals("all", StringComparison.OrdinalIgnoreCase)) { Get(user, context, cancel); return; } else if (method.Equals("find", StringComparison.OrdinalIgnoreCase)) { try { JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { string name = null; CompoundIdentity org_id = null; if (token["name"] != null) { name = token["name"].ToString(); } if (token["orgid"] != null) { org_id = JsonUtils.ToId(token["orgid"]); } GetOrgAndName(org_id, name, user, context, cancel); return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("in", StringComparison.OrdinalIgnoreCase)) { try { HashSet <CompoundIdentity> ids = JsonUtils.ToIds(JsonUtils.GetDataPayload(context.Request)); if (ids != null) { GetIds(ids, user, context, cancel); return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("create", StringComparison.OrdinalIgnoreCase)) { CompoundIdentity orgid = null; string name = null; string desc = null; JToken token = null; SiteAliasSchemeProviderBase provider = null; try { //payload and provider token = JsonUtils.GetDataPayload(context.Request); provider = SiteManager.Instance.GetSiteAliasSchemeProvider(user); if (provider != null && token != null) { //required inputs name = token["name"].ToString(); orgid = JsonUtils.ToId(token["orgid"]); if (!string.IsNullOrEmpty(name) && orgid != null) { //optionals desc = token["desc"] != null ? token["desc"].ToString() : null; //create SiteAliasScheme scheme = scheme = provider.Create(orgid, name, desc); if (scheme != null) { JObject jscheme = Jsonifier.ToJson(scheme); if (jscheme != null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jscheme.ToString())); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("delete", StringComparison.OrdinalIgnoreCase)) { try { JToken t = JsonUtils.GetDataPayload(context.Request); CompoundIdentity cid = JsonUtils.ToId(t); SiteAliasSchemeProviderBase provider = SiteManager.Instance.GetSiteAliasSchemeProvider(user); if (provider != null && cid != null) { bool result = provider.Delete(cid); if (result == true) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("update", StringComparison.OrdinalIgnoreCase)) { CompoundIdentity cid = null; CompoundIdentity org_cid = null; string name = null; string desc = null; try { //provider and token JToken token = JsonUtils.GetDataPayload(context.Request); SiteAliasSchemeProviderBase provider = SiteManager.Instance.GetSiteAliasSchemeProvider(user); if (provider != null && token != null) { //GUID must be provided cid = JsonUtils.ToId(token["id"]); //fetch stored object bool dirty = false; SiteAliasScheme scheme = provider.Get(cid); if (scheme == null) { //## REQUIRED ## //name if (token.SelectToken("name") != null) { name = token["name"].ToString(); if (!string.IsNullOrEmpty(name)) { scheme.Name = name; dirty = true; } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //name is required and not nullable return; } } //owning org if (token.SelectToken("orgid") != null) { org_cid = JsonUtils.ToId(token["orgid"]); if (org_cid != null) { scheme.OwningOrganizationIdentity = org_cid; dirty = true; } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //owning org is required and not nullable return; } } //## OPTIONALS ## //description if (token.SelectToken("desc") != null) { desc = (token["desc"] != null) ? token["desc"].ToString() : null; scheme.Description = desc; dirty = true; } if (dirty) { //update bool result = provider.Update(scheme); if (result == true) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); return; } } else { //return ok - no values were modified RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); return; } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }
public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel) { if (context.Request.Method == "POST") { if (method.Equals("all", StringComparison.OrdinalIgnoreCase)) { Get(user, context, cancel); return; } else if (method.Equals("find", StringComparison.OrdinalIgnoreCase)) { try { JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token["id"] != null && token["schemeid"] != null) { GetBySiteAndScheme(JsonUtils.ToId(token["id"]), JsonUtils.ToId(token["schemeid"]), user, context, cancel); return; } else if (token["id"] != null && token["name"] != null) { GetBySiteAndName(JsonUtils.ToId(token["id"]), token["name"].ToString(), user, context, cancel); return; } else if (token["schemeid"] != null && token["name"] != null) { GetBySchemeAndName(JsonUtils.ToId(token["schemeid"]), token["name"].ToString(), user, context, cancel); return; } else if (token["schemeid"] != null) { GetByScheme(JsonUtils.ToId(token["schemeid"]), user, context, cancel); return; } else if (token["id"] != null) { GetBySite(JsonUtils.ToId(token["id"]), user, context, cancel); return; } else if (token["name"] != null) { GetByName(token["name"].ToString(), user, context, cancel); return; } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("create", StringComparison.OrdinalIgnoreCase)) { try { //token and providers JToken token = JsonUtils.GetDataPayload(context.Request); SiteAliasProviderBase alias_provider = SiteManager.Instance.GetSiteAliasProvider(user); SiteProviderBase site_provider = SiteManager.Instance.GetSiteProvider(user); SiteAliasSchemeProviderBase scheme_provider = SiteManager.Instance.GetSiteAliasSchemeProvider(user); if (alias_provider != null && scheme_provider != null && site_provider != null && token != null) { //required string name = token["name"].ToString(); SiteAliasScheme scheme = scheme_provider.Get(JsonUtils.ToId(token["schemeid"])); Site site = site_provider.Get(JsonUtils.ToId(token["id"])); if (site != null && scheme != null && !string.IsNullOrEmpty(name)) { //create object SiteAlias alias = alias_provider.Create(scheme, site, name); if (alias != null) { JObject jalias = Jsonifier.ToJson(alias); if (jalias != null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jalias.ToString())); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("update", StringComparison.OrdinalIgnoreCase)) { try { //token and providers JToken token = JsonUtils.GetDataPayload(context.Request); SiteAliasProviderBase alias_provider = SiteManager.Instance.GetSiteAliasProvider(user); SiteProviderBase site_provider = SiteManager.Instance.GetSiteProvider(user); SiteAliasSchemeProviderBase scheme_provider = SiteManager.Instance.GetSiteAliasSchemeProvider(user); if (alias_provider != null && scheme_provider != null && site_provider != null && token != null) { //required fields CompoundIdentity site_id = JsonUtils.ToId(token["id"]); Site site = site_provider.Get(site_id); CompoundIdentity scheme_id = JsonUtils.ToId(token["schemeid"]); SiteAliasScheme scheme = scheme_provider.Get(scheme_id); string old_name = token["name"].ToString(); string new_name = token["newname"].ToString(); if (site != null && scheme == null && !string.IsNullOrEmpty(old_name) && !string.IsNullOrEmpty(new_name)) { //retrieve by site, scheme IEnumerable <SiteAlias> aliases = alias_provider.Get(site_id, scheme); if (aliases == null) { //match alias by name (an org could have multiple aliases in the same scheme, but they must be unique) SiteAlias alias = null; foreach (SiteAlias a in aliases) { if (a.Name == old_name) { alias = a; } } alias.Name = new_name; bool result = alias_provider.Update(alias); if (result == true) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); return; } } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("delete", StringComparison.OrdinalIgnoreCase)) { CompoundIdentity scheme_id = null; CompoundIdentity site_id = null; SiteAliasScheme scheme = null; Site site = null; string name = null; try { //token and providers JToken token = JsonUtils.GetDataPayload(context.Request); SiteAliasProviderBase alias_provider = SiteManager.Instance.GetSiteAliasProvider(user); SiteProviderBase site_provider = SiteManager.Instance.GetSiteProvider(user); SiteAliasSchemeProviderBase scheme_provider = SiteManager.Instance.GetSiteAliasSchemeProvider(user); if (alias_provider != null && scheme_provider != null && site_provider != null && token != null) { //If a token is provided, it cannot be null //Checking values against intent avoids firing a degenerate delete override //schemeid if (token.SelectToken("schemeid") != null) { if (token["schemeid"] == null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } else { scheme_id = JsonUtils.ToId(token["schemeid"]); scheme = scheme_provider.Get(scheme_id); } } //id if (token.SelectToken("id") != null) { if (token["id"] == null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } else { site_id = JsonUtils.ToId(token["id"]); site = site_provider.Get(site_id); } } //name if (token.SelectToken("name") != null) { if (token["name"] == null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } else { name = token["name"].ToString(); } } //determine override bool result = false; if (scheme != null && site != null && name != null) //delete specific alias { //don't have a provider method that returns specific alias, so get by site, scheme and match alias by name (most of the time a single alias will be returned) IEnumerable <SiteAlias> aliases = alias_provider.Get(site_id, scheme); if (aliases != null) { foreach (SiteAlias alias in aliases) { if (alias.Name == name) { result = alias_provider.Delete(alias); break; //aliases should be unique for a given site in a given scheme } } } } else if (scheme != null && site_id != null) { result = alias_provider.Delete(site_id, scheme); //delete * for given site in a given scheme (could be multiple) } else if (scheme != null) { result = alias_provider.Delete(scheme); //delete * for a given scheme (across orgs) } else if (site_id != null) { result = alias_provider.Delete(site_id); //delete * for a given org (across schemes) } if (result == true) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); return; } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }