Fix migration

Fix login with sessions
This commit is contained in:
Lucio Maranta
2018-01-29 12:43:29 +01:00
parent 44dfe1753b
commit 6071a96d11
11 changed files with 109 additions and 78 deletions
@@ -31,6 +31,17 @@ namespace Step.Database.Controllers
.Include("Role") // TODO Add machine and user info?
.Where(x => x.MachineId == machineId && x.UserId == userId)
.FirstOrDefault();
}
public MachineUserModel FindById(int id)
{
return dbCtx
.MachinesUsers
.Include("Role")
.Include("Machine")
.Include("User")
.Where(x => x.MachineUserId == id)
.FirstOrDefault();
}
+32 -19
View File
@@ -9,6 +9,8 @@ using System.IO;
using Step.Database.Controllers;
using static Step.Config.ServerConfig;
using Microsoft.Win32;
using System.Data.Entity.Migrations;
using System.Linq;
namespace Step.Database
{
@@ -35,33 +37,44 @@ namespace Step.Database
public static void TestDatabaseConnection()
{
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Configuration>());
using (DatabaseContext dbContext = new DatabaseContext())
try
{
try
System.Data.Entity.Database.SetInitializer<DatabaseContext>(null);
// Make sure database exists.
using (var db = new DatabaseContext())
{
FindOrCreateMachineUniqueId();
db.Database.Initialize(false);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
var migrator = new DbMigrator(new Configuration());
if (migrator.GetPendingMigrations().Any())
{
if (ex.Number == 0)
{
dbContext.Database.Initialize(true);
}
else if (ex.Number == 1042) // Can't find MySQLServer
{
Manage(ERROR_LEVEL.FATAL, ex);
}
else
{
Manage(ERROR_LEVEL.ERROR, ex);
}
// Run migrations and seed.
migrator.Update();
}
catch (Exception ex)
FindOrCreateMachineUniqueId();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
if (ex.Number == 0)
{
//dbContext.Database.Initialize(true);
}
else if (ex.Number == 1042) // Can't find MySQLServer
{
Manage(ERROR_LEVEL.FATAL, ex);
}
else
{
Manage(ERROR_LEVEL.ERROR, ex);
}
}
catch (Exception ex)
{
Manage(ERROR_LEVEL.FATAL, ex);
}
}
@@ -13,7 +13,7 @@ namespace Step.Database.Migrations
string IMigrationMetadata.Id
{
get { return "201801251430146_InitCreate"; }
get { return "201801291142206_InitCreate"; }
}
string IMigrationMetadata.Source
@@ -43,7 +43,8 @@ namespace Step.Database.Migrations
.ForeignKey("dbo.machine", t => t.machine_id, cascadeDelete: true)
.ForeignKey("dbo.role", t => t.role_id, cascadeDelete: true)
.ForeignKey("dbo.user", t => t.user_id, cascadeDelete: true)
.Index(t => new { t.machine_id, t.user_id, t.role_id }, unique: true, clustered: true, name: "unique_machine_user");
.Index(t => new { t.machine_id, t.user_id }, unique: true, clustered: true, name: "unique_machine_user")
.Index(t => t.role_id);
CreateTable(
"dbo.role",
@@ -91,6 +92,7 @@ namespace Step.Database.Migrations
DropForeignKey("dbo.machine_user", "role_id", "dbo.role");
DropForeignKey("dbo.machine_user", "machine_id", "dbo.machine");
DropIndex("dbo.session", new[] { "machine_user_id" });
DropIndex("dbo.machine_user", new[] { "role_id" });
DropIndex("dbo.machine_user", "unique_machine_user");
DropTable("dbo.session");
DropTable("dbo.user");
@@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Target" xml:space="preserve">
<value>H4sIAAAAAAAEAO1cy27cNhTdF+g/CFoWrmUnCNAaMwmciV0YjeMg46TdDWiJMxZKUapEOTaKflkX/aT+Qkk9SZGSSEnzqFEECGyKPLy8POS9pI78z19/z948Bsh6gHHih3hunx6f2BbEbuj5eDO3U7L+/gf7zetvv5ldeMGj9aWs95LVoy1xMrfvCYnOHCdx72EAkuPAd+MwCdfk2A0DB3ih8+Lk5Efn9NSBFMKmWJY1+5Ri4gcw+4X+ugixCyOSAnQdehAlRTl9ssxQrQ8ggEkEXDi3lwRGx+8AAXcggbZ1jnxAjVhCtLYtgHFIAKEmnn1O4JLEId4sI1oA0O1TBGm9NUCsVWb6WV1ddxQnL9gonLphCeWmCQkDQ8DTl4VbnGbzQc61K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d+tPY0ur8DmRmS+2Y2aV2xmYefXrqmhWWXfS2/WAT0CeB2Wn2C0Q/ylFf5AY8CHF8/B1feZs97cWEH/iHDl1YCKgqPEBwVCc5jFz7PBE+f9QHNU+tAVx//OgOeHF80M9Lp3zzCJAo/kEkWA6lXohK9mHVLz93f1IeecYFWXbJYcO2a0xikmtPrteFBygtpxTUO1c431Y7Op6/XAY1Gq9uTxAabh6Knejfj0sXpnN8T541Xo3epjK7lGTuiWiiQrQwdLyST4u2AXPet43tIfFXXJNLcaWFWItL2k6LpE15db5/fvc9u5CSooihSwqrkCh3NbTUrf1Kz5WdRiUymK9jriVo6Hb7ugwm2otZXdbd9wzVT9xJgPW0H234feMQ8t+cX33KMNVvSR5jUOSjQuDkZVYJhJfFUq75P6AtOESLxsyk4N2wlQacPUIWk3fqxO2rPUeNZyOVKHtJfxWBN3ym2QaXbm/EErDfOJvagj290IxdIW4WtW5wuuwjPMNi8oqzdf0kACPBt3zmPhr4BL6mIXA7I8lfAEopVUugjvoXeGblEQpoUOGwR0S/voCSxO6+s9U66LNs5soU21PMQRqpk+HAG/w29RHXmX3peJiqAWC5R/F7RmbS8Ju0TZPFdKHsBmr24AK91Vp0y0MIkTBkhu8BA9wiG2Uwe/hBrhPpR6gHaR/IkS3z975YBODICkw6vb0V8phL3h8/S9SzWqFKFcAAA==</value>
</data>
<data name="DefaultSchema" xml:space="preserve">
<value>dbo</value>
+10 -6
View File
@@ -7,6 +7,7 @@ namespace Step.Database.Migrations
using System.Data.Entity.Migrations;
using System.Globalization;
using System.Linq;
using static Step.Utils.Constants;
public sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
@@ -26,12 +27,15 @@ namespace Step.Database.Migrations
);
context.FunctionsAccess.AddOrUpdate(
new FunctionAccessModel() { FunctionAccessId = 1, Name = "test", Area = "production", Enabled = true, WriteLevelMin = 7, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 2, Name = "userData", Area = "production", Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 3, Name = "ncData", Area = "production", Enabled = true, WriteLevelMin = 100, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 4, Name = "functionAccess", Area = "production", Enabled = true, WriteLevelMin = 100, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 5, Name = "logout", Area = "production", Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1 }
);
// General Function
new FunctionAccessModel() { FunctionAccessId = 1, Name = "test", Area = AREAS.GENERAL_KEY, Enabled = true, WriteLevelMin = 7, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 2, Name = "functionAccess", Area = AREAS.GENERAL_KEY, Enabled = true, WriteLevelMin = 100, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 3, Name = "logout", Area = AREAS.GENERAL_KEY, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1 },
// Production function
new FunctionAccessModel() { FunctionAccessId = 4, Name = "userData", Area = AREAS.PRODUCTION_KEY, Enabled = true, WriteLevelMin = 1, ReadLevelMin = 1 },
new FunctionAccessModel() { FunctionAccessId = 5, Name = "ncData", Area = AREAS.PRODUCTION_KEY, Enabled = true, WriteLevelMin = 100, ReadLevelMin = 1 }
);
context.SaveChanges();
}
+5 -5
View File
@@ -73,9 +73,9 @@
<Compile Include="Controllers\UsersController.cs" />
<Compile Include="Controllers\MachinesUsersController.cs" />
<Compile Include="DatabaseContext.cs" />
<Compile Include="Migrations\201801251430146_InitCreate.cs" />
<Compile Include="Migrations\201801251430146_InitCreate.Designer.cs">
<DependentUpon>201801251430146_InitCreate.cs</DependentUpon>
<Compile Include="Migrations\201801291142206_InitCreate.cs" />
<Compile Include="Migrations\201801291142206_InitCreate.Designer.cs">
<DependentUpon>201801291142206_InitCreate.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -111,8 +111,8 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Migrations\201801251430146_InitCreate.resx">
<DependentUpon>201801251430146_InitCreate.cs</DependentUpon>
<EmbeddedResource Include="Migrations\201801291142206_InitCreate.resx">
<DependentUpon>201801291142206_InitCreate.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -27,7 +27,6 @@ namespace Step.Model.DatabaseModels
[ForeignKey("UserId")]
public UserModel User { get; set; }
[Index("unique_machine_user", IsClustered = true, IsUnique = true, Order = 3)]
[Column(name: "role_id")]
public int RoleId { get; set; }
[ForeignKey("RoleId")]
+1
View File
@@ -51,6 +51,7 @@ namespace Step.Utils
public const string MAINTENANCE_KEY = "maintenance";
public const string UTILITIES_KEY = "utilities";
public const string SCADA_KEY = "scada";
public const string GENERAL_KEY = "general";
}
// Filenames
+45 -37
View File
@@ -20,64 +20,72 @@ namespace Step
if (!base.IsAuthorized(actionContext))
return false;
// Get user level stored in the bearer token
// Get claims from token
ClaimsPrincipal principal = actionContext.RequestContext.Principal as ClaimsPrincipal;
var userRoleLevel = principal.Claims.Where(c => c.Type == ROLE_LEVEL_KEY).SingleOrDefault();
// Get user id stored in the bearer token
var userId = principal.Claims.Where(c => c.Type == USER_ID_KEY).SingleOrDefault();
// Get machine unique id stored in the bearer token
var machineUniqueId = principal.Claims.Where(c => c.Type == MACHINE_ID_KEY).SingleOrDefault();
var machineId = principal.Claims.Where(c => c.Type == MACHINE_ID_KEY).SingleOrDefault();
// User data not found -> not authorized
if (userRoleLevel == null || machineUniqueId == null)
//User data not found -> not authorized
if (userId == null || machineId == null)
return false;
// Get token from headers
string token = actionContext.Request.Headers.Authorization.ToString();
token = token.Split(' ')[1];
// check authorization
if (!CheckAuthorization(Convert.ToInt32(machineUniqueId.Value), Convert.ToInt32(userRoleLevel.Value), FunctionAccess, token))
if (!CheckAuthorization(Convert.ToInt32(machineId.Value), Convert.ToInt32(userId.Value), FunctionAccess, token))
return false;
return true;
}
private bool CheckAuthorization(int machineUniqueId, int userLevel, string functionName, string token)
{
using (FunctionsAccessController acController = new FunctionsAccessController())
{
using (SessionsController sessionsController = new SessionsController())
{
SessionModel session = sessionsController.FindSessionByToken(token);
if (session == null)
return false;
}
private bool CheckAuthorization(int machineId, int userId, string functionName, string token)
{
// Check if the machine is the same where the user logged in
if (machineId != MachineConfig.MachineId)
return false;
// Check if the machine is the same where the user logged in
if (machineUniqueId != MachineConfig.MachineId)
using (SessionsController sessionsController = new SessionsController())
{
// Find user session on this machine
SessionModel session = sessionsController.FindSessionByToken(token);
if (session == null)
return false;
// Read from db category levels
FunctionAccessModel functionAccess = acController.FindEnabledFunctionByName(functionName);
if (functionAccess != null && ServerConfigController.CheckAreaStatus(functionAccess.Area))
MachineUserModel machineUser = new MachineUserModel();
using (MachinesUsersController machineUsersController = new MachinesUsersController())
{
if (Action == ACTIONS.READ)
{ // Check read permissions
if (functionAccess.ReadLevelMin > userLevel)
return false; // Not authorized
// Find machineUser data and joined to user data, role data, machine data
machineUser = machineUsersController.FindById(session.MachineUserId);
}
using (FunctionsAccessController acController = new FunctionsAccessController())
{
// Read from db function levels
FunctionAccessModel functionAccess = acController.FindEnabledFunctionByName(functionName);
if (functionAccess != null && ServerConfigController.CheckAreaStatus(functionAccess.Area))
{
if (Action == ACTIONS.READ)
{ // Check read permissions
if (functionAccess.ReadLevelMin > machineUser.Role.Level)
return false; // Not authorized
}
else
{ // Check write permissions
if (functionAccess.WriteLevelMin > machineUser.Role.Level)
return false; // Not authorized
}
}
else
{ // Check write permissions
if (functionAccess.WriteLevelMin > userLevel)
return false; // Not authorized
}
}
else
{
return false;
}
return false;
// Authorized
return true;
// Authorized
return true;
}
}
}
}
@@ -53,9 +53,6 @@ namespace Step.Provider
// Add machine id
identity.AddClaim(new Claim(MACHINE_ID_KEY, machineUser.MachineId.ToString()));
// Add Role level key id
identity.AddClaim(new Claim(ROLE_LEVEL_KEY, machineUser.Role.Level.ToString()));
}
// Create Token with identity data
@@ -77,10 +74,6 @@ namespace Step.Provider
public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
{
//using (SessionsController sessionsController = new SessionsController())
//{
// sessionsController.Create()
//}
// Find userId and machineId from Claims
var userId = context.Identity.Claims.Where(c => c.Type == USER_ID_KEY).SingleOrDefault();
var machineId = context.Identity.Claims.Where(c => c.Type == MACHINE_ID_KEY).SingleOrDefault();