Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedia committed Nov 27, 2017
1 parent 395f551 commit acd45c2
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 42 deletions.
Binary file modified Eyedia.Aarbac.Api/App_Data/rbac.mdf
Binary file not shown.
Binary file modified Eyedia.Aarbac.Api/App_Data/rbac_log.ldf
Binary file not shown.
11 changes: 7 additions & 4 deletions Eyedia.Aarbac.Api/Controllers/EngineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Modified By -
using System.Net.Http;
using System.Web.Http;
using Eyedia.Aarbac.Framework;
using System.Threading.Tasks;

namespace Eyedia.Aarbac.Api.Controllers
{
Expand Down Expand Up @@ -108,17 +109,19 @@ public RbacEngineWebResponse Post([FromBody]RbacEngineWebRequest request)

[HttpPut]
[Route("{id}")]
public void Put(int id, [FromBody]RbacEngineWeb rbacEngineWeb)
{
Rbac.Save(rbacEngineWeb);
public RbacEngineWeb Put(int id, [FromBody]RbacEngineWeb rbacEngineWeb)
{
return Rbac.Save(rbacEngineWeb);
}

[HttpDelete]
[Route("{id}")]
public void Delete(int id)
public IHttpActionResult Delete(int id)
{
Rbac.Delete(Rbac.GetRbac(id));
return Ok();
}

}
}

3 changes: 3 additions & 0 deletions Eyedia.Aarbac.Command/Eyedia.Aarbac.Command.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
</None>
<None Include="packages.config" />
<None Include="Samples\Books\tests.csv" />
<None Include="Samples\Books\tests_result.csv" />
<None Include="Samples\Books\tests_result.md" />
<None Include="Samples\Users.csv" />
</ItemGroup>
<ItemGroup>
Expand All @@ -112,6 +114,7 @@
<Content Include="Samples\Books\role_country_mgr.xml" />
<Content Include="Samples\Books\role_state_mgr.xml" />
<Content Include="Samples\Books\test.txt" />
<Content Include="Samples\Books\test_parsed_query.txt" />
<Content Include="Samples\Databases\Books.mdf" />
<Content Include="Samples\Databases\books_log.ldf">
<DependentUpon>Books.mdf</DependentUpon>
Expand Down
4 changes: 2 additions & 2 deletions Eyedia.Aarbac.Command/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ static void Main(string[] args)

//string query = File.ReadAllText(Path.Combine(@"..\..\..\Eyedia.Aarbac.Command\Samples", "Books", "Query.txt"));
//string sub = query.Substring(186, 21);
new BookStore().Setup();
new BookStore().TestOne(); ;
//new BookStore().Setup();
new BookStore().TestBatch();
//TestSamples();

return;
Expand Down
88 changes: 80 additions & 8 deletions Eyedia.Aarbac.Command/Samples/Books/BookStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void InsertRoles(Rbac rbac)
public RbacSqlQueryEngine TestOne(string query = null)
{
RbacSqlQueryEngine engine = null;
using (Rbac rbac = new Rbac("essie"))
using (Rbac rbac = new Rbac("Lashawn"))
{
if (string.IsNullOrEmpty(query))
query = File.ReadAllText(Path.Combine(_rootDir, "Books", "test.txt"));
Expand All @@ -154,6 +154,9 @@ public RbacSqlQueryEngine TestOne(string query = null)
// table = engine.Table; //--> gives you data table if it is a select query

}
if (!string.IsNullOrEmpty(engine.AllErrors))
Console.WriteLine("Errors:{0}", engine.AllErrors);

if (engine.Table != null)
Console.WriteLine("The query was a select query and returned {0} records", engine.Table.Rows.Count);

Expand All @@ -165,23 +168,24 @@ public void TestBatch()
GenericParserAdapter genParser = new GenericParserAdapter(Path.Combine(_rootDir, "Books", "tests.csv"));
genParser.FirstRowHasHeader = true;
DataTable table = genParser.GetDataTable();
if(table.Columns["ParsedQueryStage1"] == null)
if (table.Columns["ParsedQueryStage1"] == null)
{
table.Columns.Add("ParsedQueryStage1");
table.Columns.Add("ParsedQuery");
table.Columns.Add("Records");
table.Columns.Add("Errors");
}

Rbac rbac = new Rbac("essie");
foreach (DataRow row in table.Rows)
{
RbacRole role = Rbac.GetRole(row[2].ToString());
{
Rbac rbac = new Rbac(row["User"].ToString());
RbacRole role = Rbac.GetRole(row["Role"].ToString());
SqlQueryParser parser = new SqlQueryParser(rbac);
try
{
parser.Parse(row[0].ToString());
parser.Parse(row["Query"].ToString());
}
catch(Exception ex)
catch (Exception ex)
{
row["Errors"] = ex.Message;
continue;
Expand All @@ -190,9 +194,77 @@ public void TestBatch()
engine.Execute();
row["ParsedQueryStage1"] = parser.ParsedQueryStage1;
row["ParsedQuery"] = parser.ParsedQuery;
row["Errors"] = parser.AllErrors + Environment.NewLine;
if (engine.IsErrored)
row["Records"] = "Errored";
else if ((parser.QueryType == RbacQueryTypes.Select) && (engine.Table == null))
row["Records"] = "Errored";
else if ((parser.QueryType == RbacQueryTypes.Select) && (engine.Table != null))
row["Records"] = engine.Table.Rows.Count + " record(s)";
else
row["Records"] = "Not Applicable";

if (!string.IsNullOrEmpty(parser.AllErrors))
row["Errors"] += parser.AllErrors + Environment.NewLine;

if (!string.IsNullOrEmpty(engine.AllErrors))
row["Errors"] += engine.AllErrors + Environment.NewLine;
}
table.ToCsv(Path.Combine(_rootDir, "Books", "tests_result.csv"));
ToCsvMarkdownFormat(table, Path.Combine(_rootDir, "Books", "tests_result.md"));
}

public void ToCsvMarkdownFormat(DataTable table, string fileName)
{
StringBuilder sb = new StringBuilder();

string rbac = "books";

int counter = 1;
foreach (DataRow row in table.Rows)
{
string oneRecord = counter + ". " + row["Comment"] + Environment.NewLine;
oneRecord += "```" + Environment.NewLine;
oneRecord += "Rbac:" + rbac + Environment.NewLine;
oneRecord += "User:" + row["User"] + Environment.NewLine;
oneRecord += "Role:" + row["Role"] + Environment.NewLine;
oneRecord += "Query:" + Environment.NewLine;
oneRecord += "```" + Environment.NewLine;


oneRecord += "```sql" + Environment.NewLine + FormatQuery(row["Query"]) + Environment.NewLine + "```" + Environment.NewLine;

oneRecord += "```" + Environment.NewLine;
oneRecord += "Parsed Query:" + Environment.NewLine;
oneRecord += "```" + Environment.NewLine;
oneRecord += "```sql" + Environment.NewLine + FormatQuery(row["ParsedQuery"]) + Environment.NewLine + "```" + Environment.NewLine;


if (string.IsNullOrEmpty(row["Records"].ToString()))
oneRecord += "```" + Environment.NewLine + "Record Count(s):" + Environment.NewLine + "```" + Environment.NewLine;
else
oneRecord += "```" + Environment.NewLine + "Record Count(s):" + row["Records"] + Environment.NewLine + "```" + Environment.NewLine;

if (string.IsNullOrEmpty(row["Errors"].ToString()))
oneRecord += "```" + Environment.NewLine + "Errors(s):" + Environment.NewLine + "```" + Environment.NewLine;
else
oneRecord += "```diff" + Environment.NewLine + "- " + row["Errors"] + Environment.NewLine + "```" + Environment.NewLine;


oneRecord += "***" + Environment.NewLine;

sb.AppendLine(oneRecord);
counter++;
}

File.WriteAllText(fileName, sb.ToString());
}
private string FormatQuery(object cell)
{
string fQuery = cell.ToString().Replace("where", Environment.NewLine + "where").Replace("inner", Environment.NewLine + "inner");
fQuery = fQuery.Replace("WHERE", Environment.NewLine + "WHERE");
fQuery = fQuery.Replace(" in ", Environment.NewLine + " in ");
fQuery = fQuery.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
return fQuery;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Eyedia.Aarbac.Command/Samples/Books/test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
select * from Author
select * from Author where ZipCodeId in (select zc.ZipCodeId from ZipCode zc where zc.CityId in (select c.CityId from City c where c.StateId in (select StateId from State where ShortName in ('NY', 'NC'))))
2 changes: 1 addition & 1 deletion Eyedia.Aarbac.Command/Samples/Books/test_parsed_query.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT Author.AuthorId , Author.Name , Author.ZipCodeId FROM Author inner join [ZipCode] [t1] on [t1].ZipCodeId = [Author].ZipCodeId inner join [City] [t2] on [t2].CityId = [t1].CityId inner join [State] [t3] on [t3].StateId = [t2].StateId inner join [Country] [t4] on [t4].CountryId = [t3].CountryId WHERE t4.Code in ('IN','US')
SELECT Author.AuthorId , Author.Name , Author.ZipCodeId FROM Author inner join [ZipCode] [t1] on [t1].ZipCodeId = [Author].ZipCodeId inner join [City] [t2] on [t2].CityId = [t1].CityId WHERE (ZipCodeId in (SELECT zc.ZipCodeId FROM ZipCode zc WHERE zc.CityId in (SELECT c.CityId FROM City c WHERE c.StateId in (SELECT StateId FROM State WHERE ShortName in ('NY' , 'NC' ) ) ) )) AND (t2.Name in ('New York','Charlotte'))
17 changes: 12 additions & 5 deletions Eyedia.Aarbac.Command/Samples/Books/tests.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
Instance,Query,User,Role
books,"select a.AuthorId, a.Name as [AuthorName], a.ZipCodeId, c.Name as City from Author a
Comment,Query,User,Role
Simple inner join,"select a.AuthorId, a.Name as [AuthorName], a.ZipCodeId, c.Name as City from Author a
inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId
inner join City c on c.CityId = zc.CityId
where c.Name = 'Charlotte'",Lashawn,role_city_mgr
books,select * from book,Lashawn,role_city_mgr
books,"select * from Author where Author.ZipCodeId in (select zc.ZipCodeId from ZipCode zc where zc.CityId in (select c.CityId from City c where c.StateId in (select StateId from State where ShortName in ('NY', 'NC'))))",Lashawn,role_city_mgr
books,"select zc.ZipCode, a.Name from ZipCode zc inner join Author a on a.ZipCodeId = zc.ZipCodeId where zc.ZipCodeId = 12",Lashawn,role_city_mgr
simple non scalar,select * from book,Lashawn,role_city_mgr
non scalar complex,"select * from Author where Author.ZipCodeId in (select zc.ZipCodeId from ZipCode zc where zc.CityId in (select c.CityId from City c where c.StateId in (select StateId from State where ShortName in ('NY', 'NC'))))",Lashawn,role_city_mgr
,"select zc.ZipCode, a.Name from ZipCode zc inner join Author a on a.ZipCodeId = zc.ZipCodeId where zc.ZipCodeId = 12",Lashawn,role_city_mgr
"aarbac recommends to use table or alias prefix, this query will parse good, but will throw error while executing ```Ambiguous column name 'ZipCodeId'```","select * from Author where ZipCodeId in (select zc.ZipCodeId from ZipCode zc where zc.CityId in (select c.CityId from City c where c.StateId in (select StateId from State where ShortName in ('NY', 'NC'))))",Lashawn,role_city_mgr
incorrect query,select,Lashawn,role_city_mgr
incorrect query,abc,Lashawn,role_city_mgr
incorrect query,select * ,Lashawn,role_city_mgr
incorrect query,select * from ,Lashawn,role_city_mgr
incorrect query,select * from book where,Lashawn,role_city_mgr
incorrect table,select * from abc,Lashawn,role_city_mgr
22 changes: 22 additions & 0 deletions Eyedia.Aarbac.Command/Samples/Books/tests_result.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Comment,Query,User,Role,ParsedQueryStage1,ParsedQuery,Records,Errors
"Simple inner join","select a.AuthorId, a.Name as [AuthorName], a.ZipCodeId, c.Name as City from Author a
inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId
inner join City c on c.CityId = zc.CityId
where c.Name = 'Charlotte'","Lashawn","role_city_mgr","SELECT a.AuthorId , a.Name as [AuthorName] , a.ZipCodeId , c.Name as City FROM Author a inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId inner join City c on c.CityId = zc.CityId WHERE c.Name in { CityNames } ","SELECT a.AuthorId , a.Name as [AuthorName] , a.ZipCodeId , c.Name as City FROM Author a inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId inner join City c on c.CityId = zc.CityId WHERE c.Name in ('New York','Charlotte') ","10 record(s)",""
"simple non scalar","select * from book","Lashawn","role_city_mgr","select book.BookId,book.Title,book.Subject,book.Price,book.Isbn13,book.Isbn10,book.PublisherId from book","select book.BookId,book.Title,book.Subject,book.Price,book.Isbn13,book.Isbn10,book.PublisherId from book","10 record(s)",""
"non scalar complex","select * from Author where Author.ZipCodeId in (select zc.ZipCodeId from ZipCode zc where zc.CityId in (select c.CityId from City c where c.StateId in (select StateId from State where ShortName in ('NY', 'NC'))))","Lashawn","role_city_mgr","SELECT Author.AuthorId , Author.Name , Author.SSN , Author.ZipCodeId FROM Author inner join [ZipCode] [t4] on [t4].ZipCodeId = [Author].ZipCodeId inner join [City] [t5] on [t5].CityId = [t4].CityId WHERE (Author.ZipCodeId in (SELECT zc.ZipCodeId FROM ZipCode zc WHERE zc.CityId in (SELECT c.CityId FROM City c WHERE c.StateId in (SELECT StateId FROM State WHERE ShortName in ('NY' , 'NC' ) ) ) )) AND (t5.Name in { CityNames }) ","SELECT Author.AuthorId , Author.Name , Author.ZipCodeId FROM Author inner join [ZipCode] [t4] on [t4].ZipCodeId = [Author].ZipCodeId inner join [City] [t5] on [t5].CityId = [t4].CityId WHERE (Author.ZipCodeId in (SELECT zc.ZipCodeId FROM ZipCode zc WHERE zc.CityId in (SELECT c.CityId FROM City c WHERE c.StateId in (SELECT StateId FROM State WHERE ShortName in ('NY' , 'NC' ) ) ) )) AND (t5.Name in ('New York','Charlotte')) ","10 record(s)",""
"","select zc.ZipCode, a.Name from ZipCode zc inner join Author a on a.ZipCodeId = zc.ZipCodeId where zc.ZipCodeId = 12","Lashawn","role_city_mgr","SELECT zc.ZipCode , a.Name FROM ZipCode zc inner join Author a on a.ZipCodeId = zc.ZipCodeId inner join [City] [t8] on [t8].CityId = [zc].CityId WHERE (zc.ZipCodeId = 12) AND (t8.Name in { CityNames }) ","SELECT zc.ZipCode , a.Name FROM ZipCode zc inner join Author a on a.ZipCodeId = zc.ZipCodeId inner join [City] [t8] on [t8].CityId = [zc].CityId WHERE (zc.ZipCodeId = 12) AND (t8.Name in ('New York','Charlotte')) ","0 record(s)",""
"aarbac recommends to use table or alias prefix, this query will parse good, but will throw error while executing ```Ambiguous column name 'ZipCodeId'```","select * from Author where ZipCodeId in (select zc.ZipCodeId from ZipCode zc where zc.CityId in (select c.CityId from City c where c.StateId in (select StateId from State where ShortName in ('NY', 'NC'))))","Lashawn","role_city_mgr","SELECT Author.AuthorId , Author.Name , Author.SSN , Author.ZipCodeId FROM Author inner join [ZipCode] [t11] on [t11].ZipCodeId = [Author].ZipCodeId inner join [City] [t12] on [t12].CityId = [t11].CityId WHERE (ZipCodeId in (SELECT zc.ZipCodeId FROM ZipCode zc WHERE zc.CityId in (SELECT c.CityId FROM City c WHERE c.StateId in (SELECT StateId FROM State WHERE ShortName in ('NY' , 'NC' ) ) ) )) AND (t12.Name in { CityNames }) ","SELECT Author.AuthorId , Author.Name , Author.ZipCodeId FROM Author inner join [ZipCode] [t11] on [t11].ZipCodeId = [Author].ZipCodeId inner join [City] [t12] on [t12].CityId = [t11].CityId WHERE (ZipCodeId in (SELECT zc.ZipCodeId FROM ZipCode zc WHERE zc.CityId in (SELECT c.CityId FROM City c WHERE c.StateId in (SELECT StateId FROM State WHERE ShortName in ('NY' , 'NC' ) ) ) )) AND (t12.Name in ('New York','Charlotte')) ","Errored","Ambiguous column name 'ZipCodeId'.
"
"incorrect query","select","Lashawn","role_city_mgr","select","select","Errored","Incorrect syntax near select.Error:Incorrect syntax near select. at line nr:1 column:1
Incorrect syntax near '10'.
"
"incorrect query","abc","Lashawn","role_city_mgr","","","","RBAC.Core - Invalid query type!"
"incorrect query","select * ","Lashawn","role_city_mgr","","","","Must specify table to select from."
"incorrect query","select * from ","Lashawn","role_city_mgr","select * from ","select * from ","Errored","Unexpected end of file occurred.Error:Unexpected end of file occurred. at line nr:1 column:15
Incorrect syntax near 'from'.
"
"incorrect query","select * from book where","Lashawn","role_city_mgr","select * from book where","select * from book where","Errored","Unexpected end of file occurred.Error:Unexpected end of file occurred. at line nr:1 column:25
Incorrect syntax near 'where'.
"
"incorrect table","select * from abc","Lashawn","role_city_mgr","","","","Invalid object name 'abc'."
Loading

0 comments on commit acd45c2

Please sign in to comment.