Skip to content
This repository was archived by the owner on Aug 4, 2022. It is now read-only.

Commit f7e2127

Browse files
Use 'ScaleUnitPipeline' user for the interAOS app (#154)
* Use 'ScaleUnitPipeline' user for the interAOS app Inserting the interAOS appId into SYSAADCLIENTTABLE will use the user 'ScaleUnitPipeline' if it exists, otherwise 'Admin' * Remove unnecessary field
1 parent 68ce214 commit f7e2127

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using ScaleUnitManagement.Utilities;
2+
3+
namespace ScaleUnitManagement.DatabaseManager
4+
{
5+
public class AxDbManager
6+
{
7+
public static bool UserExists(string userName)
8+
{
9+
string query = $"select count(name) from USERINFO where name = '{userName}'";
10+
11+
var sqlQueryExecutor = new SqlQueryExecutor();
12+
return sqlQueryExecutor.ExecuteBooleanQuery(query);
13+
}
14+
}
15+
}

src/ScaleUnitManagement/ScaleUnitFeatureManager/Hub/AddToolToHubSysAADClientTable.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@ public float Priority()
1919

2020
public Task Run()
2121
{
22-
const string UserName = "ScaleUnitManagement";
2322
string hubDb = Config.HubScaleUnit().AxDbName;
2423
var allowListing = new AADAppAllowListing();
2524

2625
try
2726
{
28-
string interAOSAppId = Config.InterAOSAppId();
27+
string interAOSUserName =
28+
AxDbManager.UserExists("ScaleUnitPipeline")
29+
? "ScaleUnitPipeline"
30+
: "Admin";
31+
2932
const string InterAOSAppName = "ScaleUnits";
30-
allowListing.UpdateAADAppClientTable(hubDb, UserName, InterAOSAppName, interAOSAppId);
33+
string interAOSAppId = Config.InterAOSAppId();
34+
allowListing.UpdateAADAppClientTable(hubDb, interAOSUserName, InterAOSAppName, interAOSAppId);
3135

36+
const string ScaleUnitUserName = "ScaleUnitManagement";
3237
const string ScaleUnitAppName = "Scale Unit Management Tool";
3338
ScaleUnitInstance scaleUnit = Config.FindScaleUnitWithId(ScaleUnitContext.GetScaleUnitId());
3439
string scaleUnitAppId = scaleUnit.AuthConfiguration.AppId;
35-
allowListing.UpdateAADAppClientTable(hubDb, UserName, ScaleUnitAppName, scaleUnitAppId);
40+
allowListing.UpdateAADAppClientTable(hubDb, ScaleUnitUserName, ScaleUnitAppName, scaleUnitAppId);
3641
}
3742
catch (Exception ex)
3843
{
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,59 @@
1+
using System;
12
using System.Data.SqlClient;
23

34
namespace ScaleUnitManagement.Utilities
45
{
56
public class SqlQueryExecutor
67
{
7-
private readonly ScaleUnitInstance scaleUnit;
8+
private readonly string dbName;
89

910
public SqlQueryExecutor()
1011
{
11-
scaleUnit = Config.FindScaleUnitWithId(ScaleUnitContext.GetScaleUnitId());
12+
ScaleUnitInstance scaleUnit = Config.FindScaleUnitWithId(ScaleUnitContext.GetScaleUnitId());
13+
dbName = scaleUnit.AxDbName;
14+
}
15+
16+
public SqlQueryExecutor(string dbName)
17+
{
18+
this.dbName = dbName;
19+
}
20+
21+
private string ConnectionString()
22+
{
23+
return $"Data Source=localhost;Initial Catalog={dbName};Integrated Security=True;Enlist=True;Application Name=ScaleUnitDevTool";
1224
}
1325

1426
public void Execute(string query)
1527
{
16-
string connectionString = $"Data Source=localhost;Initial Catalog={scaleUnit.AxDbName};Integrated Security=True;Enlist=True;Application Name=ScaleUnitDevTool";
17-
using (var conn = new SqlConnection(connectionString))
28+
using (var conn = new SqlConnection(ConnectionString()))
1829
using (var cmd = new SqlCommand(query, conn))
1930
{
2031
conn.Open();
2132
cmd.CommandTimeout = 65535;
2233
cmd.ExecuteNonQuery();
2334
}
2435
}
36+
37+
public bool ExecuteBooleanQuery(string query)
38+
{
39+
using (var conn = new SqlConnection(ConnectionString()))
40+
using (var cmd = new SqlCommand(query, conn))
41+
{
42+
conn.Open();
43+
cmd.CommandTimeout = 65535;
44+
SqlDataReader reader = cmd.ExecuteReader();
45+
try
46+
{
47+
reader.Read();
48+
int value = reader.GetInt32(0);
49+
return value != 0;
50+
}
51+
catch
52+
{
53+
Console.WriteLine("sql query did not return a boolean result");
54+
throw;
55+
}
56+
}
57+
}
2558
}
2659
}

0 commit comments

Comments
 (0)