public override bool Update(OrganizationAlias org) { if (org != null && !org.Identity.IsNullOrEmpty() && !org.AliasSchemeIdentity.IsNullOrEmpty() && CanUpdate(org)) { if (OrganizationUtils.IsDirty(org)) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.UpdateAlias; cmd.Parameters.AddWithValue("sid", org.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("id", org.Identity.Identity); cmd.Parameters.AddWithValue("ssid", org.AliasSchemeIdentity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", org.AliasSchemeIdentity.Identity); cmd.Parameters.AddWithValue("name", org.Name); cmd.Parameters.AddWithValue("origName", OrganizationUtils.OriginalName(org)); Db.ExecuteNonQuery(cmd); return(true); } catch { } } else { return(true); //nothing to change } } return(false); }
static OrganizationAlias MakeOrg(OrganizationAliasProviderBase prov, OrganizationAliasScheme scheme, Organization org, string orgName) { OrganizationAlias 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 <OrganizationAlias> orgs = prov.Get(scheme, orgName); if (orgs != null) { foreach (OrganizationAlias o in orgs) { if (o.OrganizationEquals(org)) { sch = o; break; } } } } return(sch); }
public static JObject ToJson(OrganizationAlias org) { if (org != null) { JObject o = new JObject(); o.Add(JsonUtils.Id, JsonUtils.ToJson(org.Identity)); o.Add(JsonUtils.SchemeId, JsonUtils.ToJson(org.AliasSchemeIdentity)); o.Add(JsonUtils.Name, org.Name); return(o); } return(null); }
public override IEnumerable <OrganizationAlias> Get(OrganizationAliasScheme scheme, string name, StringComparison comparisonOption) { if (!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.SelectAlias + where; cmd.Parameters.AddWithValue("ssid", scheme.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", scheme.Identity.Identity); cmd.Parameters.AddWithValue("name", name); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); List <OrganizationAlias> schemes = new List <OrganizationAlias>(); OrganizationAlias o = null; if (rdr != null) { try { while (rdr.Read()) { o = OrganizationAliasBuilder.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 <OrganizationAlias> Get(CompoundIdentity id, OrganizationAliasScheme scheme) { if (!id.IsNullOrEmpty() && scheme != null && !scheme.Identity.IsNullOrEmpty() && this.CanGet()) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectAlias + Db.SelectAliasById; 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 <OrganizationAlias> schemes = new List <OrganizationAlias>(); OrganizationAlias o = null; if (rdr != null) { try { while (rdr.Read()) { o = OrganizationAliasBuilder.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(OrganizationAlias alias) { if (alias != null && this.CanDelete()) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.DeleteAlias + "\"SystemId\"=:sid AND \"Id\"=:id AND \"SchemeSystemId\"=:ssid AND \"SchemeId\"=:scid AND \"Name\"=:n"; cmd.Parameters.AddWithValue("sid", alias.Identity.DataStoreIdentity); cmd.Parameters.AddWithValue("id", alias.Identity.Identity); cmd.Parameters.AddWithValue("ssid", alias.AliasSchemeIdentity.DataStoreIdentity); cmd.Parameters.AddWithValue("scid", alias.AliasSchemeIdentity.Identity); cmd.Parameters.AddWithValue("n", alias.Name); Db.ExecuteNonQuery(cmd); return(true); } catch { } } return(false); }
public static void AddAliasList(Organization org, ICollection<AliasOrganizationRel> aliases) { foreach (var rel in aliases) { var c = rel; var @alias = new OrganizationAlias() { Id = c.AliasId, Name = c.Alias.AliasName, PrimaryStatus = (PrimaryStatus)c.PrimaryStatusId, DateCreated = c.DateCreated, DateUpdated = c.DateModified }; org.OrganizationAliases.Add(@alias); org.LogEntries.Add(new OrganizationLogEntry() { Note = $"Added Organization {org.Name} alias {@alias.Name}" }); } }
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) { GetByOrgAndScheme(JsonUtils.ToId(token["id"]), JsonUtils.ToId(token["schemeid"]), user, context, cancel); return; } else if (token["id"] != null && token["name"] != null) { GetByOrgAndName(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) { CompoundIdentity cid = JsonUtils.ToId(token["id"]); GetByOrg(cid, 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); OrganizationProviderBase org_provider = OrganizationManager.Instance.GetOrganizationProvider(user); OrganizationAliasSchemeProviderBase scheme_provider = OrganizationManager.Instance.GetOrganizationAliasSchemeProvider(user); OrganizationAliasProviderBase alias_provider = OrganizationManager.Instance.GetOrganizationAliasProvider(user); if (org_provider != null && scheme_provider != null && alias_provider != null && token != null) { //get inputs OrganizationAliasScheme scheme = scheme_provider.Get(JsonUtils.ToId(token["schemeid"])); Organization org = org_provider.Get(JsonUtils.ToId(token["id"])); string name = token["name"].ToString(); if (scheme != null && org != null && !string.IsNullOrEmpty(name)) { //create object OrganizationAlias alias = alias_provider.Create(scheme, org, name); if (alias != null) { JObject jalias = Jsonifier.ToJson(alias); RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jalias.ToString())); 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); OrganizationAliasSchemeProviderBase scheme_provider = OrganizationManager.Instance.GetOrganizationAliasSchemeProvider(user); OrganizationAliasProviderBase alias_provider = OrganizationManager.Instance.GetOrganizationAliasProvider(user); if (scheme_provider != null && alias_provider != null && token != null) { //required fields CompoundIdentity orgid = JsonUtils.ToId(token["id"]); OrganizationAliasScheme scheme = scheme_provider.Get(JsonUtils.ToId(token["schemeid"])); string old_name = token["name"].ToString(); string new_name = token["newname"].ToString(); if (orgid != null && scheme != null && !string.IsNullOrEmpty(old_name) && !string.IsNullOrEmpty(new_name)) { //get entity and modify its properties IEnumerable <OrganizationAlias> aliases = alias_provider.Get(orgid, scheme); if (aliases != null) { //match alias by name (an org could have multiple aliases in the same scheme, but they must be unique) OrganizationAlias alias = null; foreach (OrganizationAlias 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 org_id = null; OrganizationAliasScheme scheme = null; string name = null; try { JToken token = JsonUtils.GetDataPayload(context.Request); OrganizationProviderBase org_provider = OrganizationManager.Instance.GetOrganizationProvider(user); OrganizationAliasSchemeProviderBase scheme_provider = OrganizationManager.Instance.GetOrganizationAliasSchemeProvider(user); OrganizationAliasProviderBase provider = OrganizationManager.Instance.GetOrganizationAliasProvider(user); if (provider != null && scheme_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); } } //orgid if (token.SelectToken("id") != null) { if (token["id"] == null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } else { org_id = JsonUtils.ToId(token["id"]); Organization org = org_provider.Get(org_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_id != null && org_id != null && name != null) //delete specific alias { //don't have a provider method that returns specific alias, so get by org, scheme and match alias by name (most of the time a single alias will be returned) IEnumerable <OrganizationAlias> aliases = provider.Get(org_id, scheme); if (aliases != null) { foreach (OrganizationAlias alias in aliases) { if (alias.Name == name) { result = provider.Delete(alias); break; //aliases should be unique for a given org in a given scheme } } } } else if (scheme_id != null && org_id != null) //delete * for given org in a given scheme { result = provider.Delete(org_id, scheme_id); } else if (scheme_id != null) //delete * for a given scheme { result = provider.Delete(scheme); } else if (org_id != null) //delete * for a given org { result = provider.Delete(org_id); } 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; }
public override bool CanUpdate(OrganizationAlias alias) { return(this.CanUpdate()); //TODO -- add fine grained security }
public override bool CanDelete(OrganizationAlias scheme) { return(this.CanDelete()); //TODO -- add fine grained security }