diff --git a/Step.Database/Controllers/FunctionAccessController.cs b/Step.Database/Controllers/FunctionsAccessController.cs similarity index 93% rename from Step.Database/Controllers/FunctionAccessController.cs rename to Step.Database/Controllers/FunctionsAccessController.cs index 59cfa29e..05446713 100644 --- a/Step.Database/Controllers/FunctionAccessController.cs +++ b/Step.Database/Controllers/FunctionsAccessController.cs @@ -6,11 +6,11 @@ using Step.Model.DTOModels; namespace Step.Database.Controllers { - public class FunctionAccessController : IDisposable + public class FunctionsAccessController : IDisposable { private DatabaseContext dbCtx; - public FunctionAccessController() + public FunctionsAccessController() { // Initialize database context dbCtx = new DatabaseContext(); diff --git a/Step.Database/Controllers/MachineController.cs b/Step.Database/Controllers/MachinesController.cs similarity index 93% rename from Step.Database/Controllers/MachineController.cs rename to Step.Database/Controllers/MachinesController.cs index 6898a9a3..a787c2fe 100644 --- a/Step.Database/Controllers/MachineController.cs +++ b/Step.Database/Controllers/MachinesController.cs @@ -5,11 +5,11 @@ using static Step.Config.ServerConfig; namespace Step.Database.Controllers { - public class MachineController : IDisposable + public class MachinesController : IDisposable { private DatabaseContext dbCtx; - public MachineController() + public MachinesController() { // Initialize database context dbCtx = new DatabaseContext(); diff --git a/Step.Database/Controllers/MachinesUsersController.cs b/Step.Database/Controllers/MachinesUsersController.cs index 2be52201..6d0adad3 100644 --- a/Step.Database/Controllers/MachinesUsersController.cs +++ b/Step.Database/Controllers/MachinesUsersController.cs @@ -24,7 +24,7 @@ namespace Step.Database.Controllers dbCtx.Dispose(); } - public MachinesUsersModel FindByUserId(int machineId, int userId) + public MachineUserModel FindByUserId(int machineId, int userId) { return dbCtx .MachinesUsers @@ -34,9 +34,9 @@ namespace Step.Database.Controllers } - public MachinesUsersModel Create(int machineId, int userId, int roleId) + public MachineUserModel Create(int machineId, int userId, int roleId) { - MachinesUsersModel machine = new MachinesUsersModel() { MachineId = machineId, UserId = userId, RoleId = roleId }; + MachineUserModel machine = new MachineUserModel() { MachineId = machineId, UserId = userId, RoleId = roleId }; dbCtx.MachinesUsers.Add( machine ); diff --git a/Step.Database/Controllers/SessionsController.cs b/Step.Database/Controllers/SessionsController.cs new file mode 100644 index 00000000..1adfd1d3 --- /dev/null +++ b/Step.Database/Controllers/SessionsController.cs @@ -0,0 +1,73 @@ +using Step.Model.DatabaseModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Step.Database.Controllers +{ + public class SessionsController : IDisposable + { + private DatabaseContext dbCtx; + + public SessionsController() + { + // Initialize database context + dbCtx = new DatabaseContext(); + } + + public void Dispose() + { + // Clear database context + dbCtx.Dispose(); + } + + public SessionModel FindSessionByToken(string token) + { + return dbCtx + .Sessions + .Where(x => x.Token == token) // Find session by token + .SingleOrDefault(); + } + + public void DeleteUserSessions(int machineUserId) + { + dbCtx + .Sessions + .RemoveRange( // Delete rows + dbCtx + .Sessions + .Where(x => x.MachineUserId == machineUserId) // Find all the session with matching machineUserId + ); + // Commit changes + dbCtx.SaveChanges(); + } + + public void DeleteSessionsByUserAndMachineId(int machineId, int userId) + { + MachineUserModel machineUser = null; + using (MachinesUsersController machinesUsersController = new MachinesUsersController()) + { + // Find machine_user id + machineUser = machinesUsersController.FindByUserId(machineId, userId); + } + + DeleteUserSessions(machineUser.MachineUserId); + } + + public void Create(int machineUserId, string token) + { + // Create session model + SessionModel session = new SessionModel() + { + MachineUserId = machineUserId, + Token = token + }; + // Add to database + dbCtx.Sessions.Add(session); + // Commit changes + dbCtx.SaveChanges(); + } + } +} diff --git a/Step.Database/Controllers/UsersController.cs b/Step.Database/Controllers/UsersController.cs index c1c7d25b..d64e9045 100644 --- a/Step.Database/Controllers/UsersController.cs +++ b/Step.Database/Controllers/UsersController.cs @@ -117,12 +117,12 @@ namespace Step.Database.Controllers dbCtx.SaveChanges(); } - using (MachinesUsersController machineUserController = new MachinesUsersController()) + using (MachinesUsersController machinesUsersController = new MachinesUsersController()) { - MachinesUsersModel machineUser = machineUserController.FindByUserId(machineId, user.UserId); + MachineUserModel machineUser = machinesUsersController.FindByUserId(machineId, user.UserId); if (machineUser == null) - machineUserController.Create(machineId, user.UserId, 1); + machinesUsersController.Create(machineId, user.UserId, 1); } } diff --git a/Step.Database/DatabaseContext.cs b/Step.Database/DatabaseContext.cs index e10e45e0..0c625798 100644 --- a/Step.Database/DatabaseContext.cs +++ b/Step.Database/DatabaseContext.cs @@ -17,9 +17,11 @@ namespace Step.Database { public DbSet Users { get; set; } public DbSet Machines { get; set; } - public DbSet MachinesUsers { get; set; } + public DbSet MachinesUsers { get; set; } public DbSet Roles { get; set; } public DbSet FunctionsAccess { get; set; } + public DbSet Sessions { get; set; } + public DatabaseContext() : base("mySQLDatabaseConnection") @@ -99,12 +101,12 @@ namespace Step.Database } } // Set machine info into static server config - using (MachineController machineController = new MachineController()) + using (MachinesController machinesController = new MachinesController()) { - MachineConfig = machineController.GetMachineByUniqueId(uniqueId); + MachineConfig = machinesController.GetMachineByUniqueId(uniqueId); if (MachineConfig == null) { - MachineConfig = machineController.Create(uniqueId); + MachineConfig = machinesController.Create(uniqueId); using (UsersController usersController = new UsersController()) { usersController.CreateCmsDefaultUserIfNotExists(MachineConfig.MachineId); diff --git a/Step.Database/Migrations/201801161604529_InitCreate.Designer.cs b/Step.Database/Migrations/201801251430146_InitCreate.Designer.cs similarity index 93% rename from Step.Database/Migrations/201801161604529_InitCreate.Designer.cs rename to Step.Database/Migrations/201801251430146_InitCreate.Designer.cs index d94b4cc4..d8c3d8e0 100644 --- a/Step.Database/Migrations/201801161604529_InitCreate.Designer.cs +++ b/Step.Database/Migrations/201801251430146_InitCreate.Designer.cs @@ -13,7 +13,7 @@ namespace Step.Database.Migrations string IMigrationMetadata.Id { - get { return "201801161604529_InitCreate"; } + get { return "201801251430146_InitCreate"; } } string IMigrationMetadata.Source diff --git a/Step.Database/Migrations/201801161604529_InitCreate.cs b/Step.Database/Migrations/201801251430146_InitCreate.cs similarity index 60% rename from Step.Database/Migrations/201801161604529_InitCreate.cs rename to Step.Database/Migrations/201801251430146_InitCreate.cs index 20883b37..20a02292 100644 --- a/Step.Database/Migrations/201801161604529_InitCreate.cs +++ b/Step.Database/Migrations/201801251430146_InitCreate.cs @@ -8,7 +8,7 @@ namespace Step.Database.Migrations public override void Up() { CreateTable( - "dbo.functions_access", + "dbo.function_access", c => new { id = c.Int(nullable: false, identity: true), @@ -31,23 +31,22 @@ namespace Step.Database.Migrations .PrimaryKey(t => t.id); CreateTable( - "dbo.machines_users", + "dbo.machine_user", c => new { + id = c.Int(nullable: false, identity: true), machine_id = c.Int(nullable: false), user_id = c.Int(nullable: false), role_id = c.Int(nullable: false), }) - .PrimaryKey(t => new { t.machine_id, t.user_id, t.role_id }) + .PrimaryKey(t => t.id) .ForeignKey("dbo.machine", t => t.machine_id, cascadeDelete: true) - .ForeignKey("dbo.roles", t => t.role_id, cascadeDelete: true) - .ForeignKey("dbo.users", t => t.user_id, cascadeDelete: true) - .Index(t => t.machine_id) - .Index(t => t.user_id) - .Index(t => t.role_id); + .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"); CreateTable( - "dbo.roles", + "dbo.role", c => new { id = c.Int(nullable: false, identity: true), @@ -57,7 +56,7 @@ namespace Step.Database.Migrations .PrimaryKey(t => t.id); CreateTable( - "dbo.users", + "dbo.user", c => new { id = c.Int(nullable: false, identity: true), @@ -71,21 +70,34 @@ namespace Step.Database.Migrations }) .PrimaryKey(t => t.id); + CreateTable( + "dbo.session", + c => new + { + id = c.Int(nullable: false, identity: true), + token = c.String(unicode: false), + machine_user_id = c.Int(nullable: false), + }) + .PrimaryKey(t => t.id) + .ForeignKey("dbo.machine_user", t => t.machine_user_id, cascadeDelete: true) + .Index(t => t.machine_user_id); + } public override void Down() { - DropForeignKey("dbo.machines_users", "user_id", "dbo.users"); - DropForeignKey("dbo.machines_users", "role_id", "dbo.roles"); - DropForeignKey("dbo.machines_users", "machine_id", "dbo.machine"); - DropIndex("dbo.machines_users", new[] { "role_id" }); - DropIndex("dbo.machines_users", new[] { "user_id" }); - DropIndex("dbo.machines_users", new[] { "machine_id" }); - DropTable("dbo.users"); - DropTable("dbo.roles"); - DropTable("dbo.machines_users"); + DropForeignKey("dbo.session", "machine_user_id", "dbo.machine_user"); + DropForeignKey("dbo.machine_user", "user_id", "dbo.user"); + 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", "unique_machine_user"); + DropTable("dbo.session"); + DropTable("dbo.user"); + DropTable("dbo.role"); + DropTable("dbo.machine_user"); DropTable("dbo.machine"); - DropTable("dbo.functions_access"); + DropTable("dbo.function_access"); } } } diff --git a/Step.Database/Migrations/201801161604529_InitCreate.resx b/Step.Database/Migrations/201801251430146_InitCreate.resx similarity index 63% rename from Step.Database/Migrations/201801161604529_InitCreate.resx rename to Step.Database/Migrations/201801251430146_InitCreate.resx index 21384366..6ee3e62a 100644 --- a/Step.Database/Migrations/201801161604529_InitCreate.resx +++ b/Step.Database/Migrations/201801251430146_InitCreate.resx @@ -118,7 +118,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - H4sIAAAAAAAEAO1c227cNhB9L9B/EPRYOJadIEBr7CZw1nZhNL7A66R9W9ASdy2UohSRcrwo+mV96Cf1F0rqSomUREpr7zoIAgQ2RZ4ZDg/JGc4k//3z7+T9Y4CsBxgTP8RT+3D/wLYgdkPPx6upndDlq5/t9+9+/GFy6gWP1uei3xvej43EZGrfUxodOQ5x72EAyH7gu3FIwiXdd8PAAV7ovD44+MU5PHQgg7AZlmVNbhJM/QCmv7BfZyF2YUQTgC5CDyKSt7Mv8xTVugQBJBFw4dSeUxjtnwAK7gCBtnWMfMCUmEO0tC2AcUgBZSo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G6JaKJadLAMfZIPEXbBs553E+1hcZdcUwu3ZTVZywudjgtnTWl2flc/t727kJKiSCGLiitQqLz1dNdt/YqPVR0GpQpZryNu5WhovDs6zKZaSwXe1h33TNVPnEmGNTTibfg949CyX1zfPSpyVS9JXuOQJObCYGTVlokcWIXSLs8/IB25xMuGJOWgnTCVXlw9glbT9+qELevCRw2nI1Voe2G/FfG3/NaZRlfur4nSMJ/4mxqC/W1RDF0hrlZ1rvA6LON8w6KySvOVPiTAo0H3PCb+GriEPmYhMPvDCl8ASmmVi+AOelf4JiVRSuiQYXCHhL/UwNKErv4zhbto8+wmyhTeUwyBmunTIcAb/Db1kVfZfam4GGqBYPlHcXvG5pKwW7TNU4X0IWzG6jagwn1V2nQLgwhRsOQGL8EDHGIbZfB7uAHuU6kdaAfpnwjR7bN3PtjEIEgKjLo9/ZVy2AseX/8Lw1R8c1RXAAA= dbo diff --git a/Step.Database/Migrations/Configuration.cs b/Step.Database/Migrations/Configuration.cs index 3f406166..4626c635 100644 --- a/Step.Database/Migrations/Configuration.cs +++ b/Step.Database/Migrations/Configuration.cs @@ -15,8 +15,6 @@ namespace Step.Database.Migrations AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; ContextKey = "Step.Database.DatabaseContext"; - - } protected override void Seed(DatabaseContext context) @@ -31,7 +29,8 @@ namespace Step.Database.Migrations 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 = 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 } ); context.SaveChanges(); diff --git a/Step.Database/Step.Database.csproj b/Step.Database/Step.Database.csproj index ace2c400..0dfc3f71 100644 --- a/Step.Database/Step.Database.csproj +++ b/Step.Database/Step.Database.csproj @@ -67,14 +67,15 @@ - - + + + - - - 201801161604529_InitCreate.cs + + + 201801251430146_InitCreate.cs @@ -110,8 +111,8 @@ - - 201801161604529_InitCreate.cs + + 201801251430146_InitCreate.cs diff --git a/Step.Model/DatabaseModels/FunctionAccessModel.cs b/Step.Model/DatabaseModels/FunctionAccessModel.cs index 623cc016..77659c81 100644 --- a/Step.Model/DatabaseModels/FunctionAccessModel.cs +++ b/Step.Model/DatabaseModels/FunctionAccessModel.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace Step.Model.DatabaseModels { - [Table("functions_access")] + [Table("function_access")] public class FunctionAccessModel { diff --git a/Step.Model/DatabaseModels/MachinesUsersModel.cs b/Step.Model/DatabaseModels/MachineUserModel.cs similarity index 55% rename from Step.Model/DatabaseModels/MachinesUsersModel.cs rename to Step.Model/DatabaseModels/MachineUserModel.cs index e22d4189..a422e9c8 100644 --- a/Step.Model/DatabaseModels/MachinesUsersModel.cs +++ b/Step.Model/DatabaseModels/MachineUserModel.cs @@ -8,21 +8,27 @@ using System.Threading.Tasks; namespace Step.Model.DatabaseModels { - [Table("machines_users")] - public class MachinesUsersModel + [Table("machine_user")] + public class MachineUserModel { + [Key] + [Column("id")] + public int MachineUserId { get; set; } - [Key, Column(name:"machine_id", Order = 1)] + [Index("unique_machine_user", IsClustered = true, IsUnique = true, Order = 1)] + [Column(name: "machine_id")] public int MachineId { get; set; } [ForeignKey("MachineId")] public MachineModel Machine { get; set; } - [Key, Column(name: "user_id", Order = 2)] + [Index("unique_machine_user", IsClustered = true, IsUnique = true, Order = 2)] + [Column(name: "user_id")] public int UserId { get; set; } [ForeignKey("UserId")] public UserModel User { get; set; } - [Key, Column(name: "role_id", Order = 3)] + [Index("unique_machine_user", IsClustered = true, IsUnique = true, Order = 3)] + [Column(name: "role_id")] public int RoleId { get; set; } [ForeignKey("RoleId")] public RoleModel Role { get; set; } diff --git a/Step.Model/DatabaseModels/RoleModel.cs b/Step.Model/DatabaseModels/RoleModel.cs index adebbf85..225e49f6 100644 --- a/Step.Model/DatabaseModels/RoleModel.cs +++ b/Step.Model/DatabaseModels/RoleModel.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Step.Model.DatabaseModels { - [Table("roles")] + [Table("role")] public class RoleModel { [Key] diff --git a/Step.Model/DatabaseModels/SessionModel.cs b/Step.Model/DatabaseModels/SessionModel.cs new file mode 100644 index 00000000..22878c6c --- /dev/null +++ b/Step.Model/DatabaseModels/SessionModel.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Step.Model.DatabaseModels +{ + [Table("session")] + public class SessionModel + { + [Key] + [Column("id")] + public int SessionId { get; set; } + [Column("token")] + public string Token { get; set; } + [Column("machine_user_id")] + public int MachineUserId { get; set; } + + [ForeignKey("MachineUserId")] + public MachineUserModel MachineUser { get; set; } + } +} diff --git a/Step.Model/DatabaseModels/UserModel.cs b/Step.Model/DatabaseModels/UserModel.cs index 40fc65f0..573ea01c 100644 --- a/Step.Model/DatabaseModels/UserModel.cs +++ b/Step.Model/DatabaseModels/UserModel.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace Step.Model.DatabaseModels { - [Table("users")] + [Table("user")] public class UserModel { [Key] diff --git a/Step.Model/Step.Model.csproj b/Step.Model/Step.Model.csproj index 900fb179..9a588d5d 100644 --- a/Step.Model/Step.Model.csproj +++ b/Step.Model/Step.Model.csproj @@ -70,7 +70,8 @@ DtsGenerator RoleModel.cs.d.ts - + + diff --git a/Step/Attributes/SignalRAuthorizeAttribute.cs b/Step/Attributes/SignalRAuthorizeAttribute.cs index 053bb814..a9d344cc 100644 --- a/Step/Attributes/SignalRAuthorizeAttribute.cs +++ b/Step/Attributes/SignalRAuthorizeAttribute.cs @@ -41,7 +41,7 @@ namespace Step.Attributes private bool CheckAuthorization(string machineUniqueId, int userLevel, string functionName) { - using (FunctionAccessController acController = new FunctionAccessController()) + using (FunctionsAccessController acController = new FunctionsAccessController()) { // Check if the machine is the same where the user logged in if (machineUniqueId != MachineConfig.UniqueId) diff --git a/Step/Attributes/WebApiAuthorizeAttribute.cs b/Step/Attributes/WebApiAuthorizeAttribute.cs index 33ddc45c..221d3b69 100644 --- a/Step/Attributes/WebApiAuthorizeAttribute.cs +++ b/Step/Attributes/WebApiAuthorizeAttribute.cs @@ -30,20 +30,30 @@ namespace Step // User data not found -> not authorized if (userRoleLevel == null || machineUniqueId == null) return false; - + // Get token from headers + string token = actionContext.Request.Headers.Authorization.ToString(); + token = token.Split(' ')[1]; + // check authorization - if (!CheckAuthorization(machineUniqueId.Value.ToString(), Convert.ToInt32(userRoleLevel.Value), FunctionAccess)) + if (!CheckAuthorization(Convert.ToInt32(machineUniqueId.Value), Convert.ToInt32(userRoleLevel.Value), FunctionAccess, token)) return false; return true; } - private bool CheckAuthorization(string machineUniqueId, int userLevel, string functionName) + private bool CheckAuthorization(int machineUniqueId, int userLevel, string functionName, string token) { - using (FunctionAccessController acController = new FunctionAccessController()) + using (FunctionsAccessController acController = new FunctionsAccessController()) { + using (SessionsController sessionsController = new SessionsController()) + { + SessionModel session = sessionsController.FindSessionByToken(token); + if (session == null) + return false; + } + // Check if the machine is the same where the user logged in - if (machineUniqueId != MachineConfig.UniqueId) + if (machineUniqueId != MachineConfig.MachineId) return false; // Read from db category levels diff --git a/Step/Controllers/WebApi/AuthorizationController.cs b/Step/Controllers/WebApi/AuthorizationController.cs index 1bca0cc0..a8da7d55 100644 --- a/Step/Controllers/WebApi/AuthorizationController.cs +++ b/Step/Controllers/WebApi/AuthorizationController.cs @@ -17,13 +17,13 @@ namespace Step.Controllers.WebApi [WebApiAuthorize(FunctionAccess = "functionAccess", Action = ACTIONS.READ)] public IHttpActionResult GetFunctionsConfig() { - using (FunctionAccessController functionController = new FunctionAccessController()) + using (FunctionsAccessController acController = new FunctionsAccessController()) { var identity = User.Identity as ClaimsIdentity; var userRoleLevel = identity.Claims.Where(c => c.Type == ROLE_LEVEL_KEY).SingleOrDefault(); - List functionsList = functionController.GetFunctionAccess(Convert.ToInt32(userRoleLevel.Value)); + List functionsList = acController.GetFunctionAccess(Convert.ToInt32(userRoleLevel.Value)); if (functionsList == null) return NotFound(); diff --git a/Step/Controllers/WebApi/UserController.cs b/Step/Controllers/WebApi/UserController.cs index e90f621b..de91b626 100644 --- a/Step/Controllers/WebApi/UserController.cs +++ b/Step/Controllers/WebApi/UserController.cs @@ -14,6 +14,29 @@ namespace Step.Controllers.WebApi [RoutePrefix("api/user")] public class UserController : ApiController { + [Route("logout"), HttpPost] + [WebApiAuthorize(FunctionAccess = "logout", Action = ACTIONS.WRITE)] + public IHttpActionResult Logout() + { + var identity = User.Identity as ClaimsIdentity; + // Find user id from the bearer token + var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).SingleOrDefault(); + if (userId == null) + return Unauthorized(); + // Find machine id from the bearer token + var machineId = identity.Claims.Where(c => c.Type == MACHINE_ID_KEY).SingleOrDefault(); + if (machineId == null) + return Unauthorized(); + + using (SessionsController sessionsController = new SessionsController()) + { + // Delete all the user session on the machine + sessionsController.DeleteSessionsByUserAndMachineId(Convert.ToInt32(machineId.Value), Convert.ToInt32(userId.Value)); + } + + return Ok(); + } + [Route("register"), HttpPost] public IHttpActionResult CreateUser(UserModel model) { @@ -27,14 +50,14 @@ namespace Step.Controllers.WebApi public IHttpActionResult UserInfo() { var identity = User.Identity as ClaimsIdentity; - + // Find user id from the bearer token var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).SingleOrDefault(); if (userId == null) return Unauthorized(); - using (UsersController userController = new UsersController()) + using (UsersController usersController = new UsersController()) { - return Ok(userController.GetUserInfo(Convert.ToInt32(userId.Value))); + return Ok(usersController.GetUserInfo(Convert.ToInt32(userId.Value))); } } @@ -58,14 +81,14 @@ namespace Step.Controllers.WebApi if (newLanguage == null || !IsValidLanguage(newLanguage)) return BadRequest(); - + // Find if language is Available in the server directory if (!LanguageIsAvailable(newLanguage)) return NotFound(); - - - using (UsersController userController = new UsersController()) + + using (UsersController usersController = new UsersController()) { - userController.ChangeUserLanguage(Convert.ToInt32(userId.Value), CultureInfo.CreateSpecificCulture(newLanguage)); + // Update database with new language + usersController.ChangeUserLanguage(Convert.ToInt32(userId.Value), CultureInfo.CreateSpecificCulture(newLanguage)); return Ok(); } diff --git a/Step/Listeners/Database/DatabaseTest.cs b/Step/Listeners/Database/DatabaseTest.cs index c83b2167..fcc94ec1 100644 --- a/Step/Listeners/Database/DatabaseTest.cs +++ b/Step/Listeners/Database/DatabaseTest.cs @@ -16,7 +16,7 @@ namespace Step.Listeners.Database { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - using (UsersController user = new UsersController()) + using (UsersController usersController = new UsersController()) { //user.Create("test", "nuova", "nome", "last", 1, "italian"); Console.WriteLine("DB scritto " + taskName + " Thread:" + Thread.CurrentThread.ManagedThreadId + " " + stopwatch.ElapsedMilliseconds); diff --git a/Step/Provider/ApplicationOAuthProvider.cs b/Step/Provider/ApplicationOAuthProvider.cs index fd8abcb4..7453e2e4 100644 --- a/Step/Provider/ApplicationOAuthProvider.cs +++ b/Step/Provider/ApplicationOAuthProvider.cs @@ -6,12 +6,13 @@ using Step.Model.DatabaseModels; using System.Security.Claims; using static Step.Utils.Constants; using static Step.Config.ServerConfig; +using System.Linq; namespace Step.Provider { public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider { - public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) + public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { // Validate client context.Validated(); @@ -41,7 +42,7 @@ namespace Step.Provider using (MachinesUsersController machinesUsersController = new MachinesUsersController()) { // Check if user can access to the machine - MachinesUsersModel machineUser = machinesUsersController.FindByUserId(MachineConfig.MachineId, user.UserId); + MachineUserModel machineUser = machinesUsersController.FindByUserId(MachineConfig.MachineId, user.UserId); if (machineUser == null) { @@ -50,13 +51,8 @@ namespace Step.Provider return; } - // Find machine unique id - using (MachineController machineController = new MachineController()) - { - MachineModel machineModel = machineController.GetMachineById(machineUser.MachineId); - // Add machine id - identity.AddClaim(new Claim(MACHINE_ID_KEY, machineModel.UniqueId)); - } + // 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())); @@ -75,8 +71,33 @@ namespace Step.Provider context.SetError("invalid_grant", ex.Message); return; } - + } } + + 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(); + + using(MachinesUsersController machinesUsersController = new MachinesUsersController()) + { + // Find machineUser Id from database by machineId and userId + MachineUserModel machineUser = machinesUsersController.FindByUserId(Convert.ToInt32(machineId.Value), Convert.ToInt32(userId.Value)); + + using (SessionsController sessionsController = new SessionsController()) + { + // Create new user session + sessionsController.Create(machineUser.MachineUserId, context.AccessToken); + } + } + + return base.TokenEndpointResponse(context); + } } }