From 0010400ed39a13dcdad3890a0b71841fe1408964 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 30 Mar 2022 16:51:15 +0200 Subject: JwtToken servis pretvoren u dependency injection. U svim klasama dodato da se koristi jwtToken interface. Dodato da se u jwt tokena sada nalazi i id korisnika --- backend/api/api/Controllers/DatasetController.cs | 6 +++--- backend/api/api/Controllers/FileController.cs | 6 +++--- backend/api/api/Controllers/ModelController.cs | 6 +++--- backend/api/api/Controllers/PredictorController.cs | 6 +++--- backend/api/api/Controllers/UserController.cs | 6 +++--- backend/api/api/Models/IJwtToken.cs | 12 ++++++++++++ backend/api/api/Models/JwtToken.cs | 19 +++++++++++++------ backend/api/api/Program.cs | 2 ++ backend/api/api/Services/AuthService.cs | 6 +++--- backend/api/api/Services/IUserService.cs | 2 ++ backend/api/api/Services/UserService.cs | 8 ++++++++ 11 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 backend/api/api/Models/IJwtToken.cs (limited to 'backend/api') diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs index d9803744..8a622138 100644 --- a/backend/api/api/Controllers/DatasetController.cs +++ b/backend/api/api/Controllers/DatasetController.cs @@ -14,12 +14,12 @@ namespace api.Controllers public class DatasetController : ControllerBase { private readonly IDatasetService _datasetService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public DatasetController(IDatasetService datasetService, IConfiguration configuration) + public DatasetController(IDatasetService datasetService, IConfiguration configuration,IJwtToken Token) { _datasetService = datasetService; - jwtToken = new JwtToken(configuration); + jwtToken = Token; } // GET: api//mydatasets diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs index a6bab373..89b4e473 100644 --- a/backend/api/api/Controllers/FileController.cs +++ b/backend/api/api/Controllers/FileController.cs @@ -12,12 +12,12 @@ namespace api.Controllers { private string[] permittedExtensions = { ".csv" }; private readonly IConfiguration _configuration; - private JwtToken _token; + private IJwtToken _token; private IFileService _fileservice; - public FileController(IConfiguration configuration,IFileService fileService) + public FileController(IConfiguration configuration,IFileService fileService,IJwtToken token) { _configuration = configuration; - _token = new JwtToken(configuration); + _token = token; _fileservice = fileService; } diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index 355eb9f4..0be7894e 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -17,16 +17,16 @@ namespace api.Controllers private readonly IDatasetService _datasetService; private readonly IFileService _fileService; private readonly IModelService _modelService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public ModelController(IMlConnectionService mlService, IModelService modelService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration) + public ModelController(IMlConnectionService mlService, IModelService modelService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration,IJwtToken token) { _mlService = mlService; _modelService = modelService; _datasetService = datasetService; _fileService = fileService; - jwtToken = new JwtToken(configuration); + jwtToken = token; } [HttpPost("sendModel")] diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs index 63c5d2bf..8f2167c4 100644 --- a/backend/api/api/Controllers/PredictorController.cs +++ b/backend/api/api/Controllers/PredictorController.cs @@ -13,12 +13,12 @@ namespace api.Controllers public class PredictorController : Controller { private readonly IPredictorService _predictorService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public PredictorController(IPredictorService predictorService, IConfiguration configuration) + public PredictorController(IPredictorService predictorService, IConfiguration configuration, IJwtToken Token) { _predictorService = predictorService; - jwtToken = new JwtToken(configuration); + jwtToken = Token; } // GET: api//mypredictors diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs index 741382b8..782a02cf 100644 --- a/backend/api/api/Controllers/UserController.cs +++ b/backend/api/api/Controllers/UserController.cs @@ -15,12 +15,12 @@ namespace api.Controllers public class UserController : ControllerBase { private readonly IUserService userService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public UserController(IUserService userService, IConfiguration configuration) + public UserController(IUserService userService, IConfiguration configuration,IJwtToken token) { this.userService = userService; - jwtToken = new JwtToken(configuration); + jwtToken = token; } // GET: api/ diff --git a/backend/api/api/Models/IJwtToken.cs b/backend/api/api/Models/IJwtToken.cs new file mode 100644 index 00000000..da71f7ec --- /dev/null +++ b/backend/api/api/Models/IJwtToken.cs @@ -0,0 +1,12 @@ +using api.Models.Users; + +namespace api.Models +{ + public interface IJwtToken + { + string GenGuestToken(); + string GenToken(AuthRequest user); + string RenewToken(string existingToken); + string TokenToUsername(string token); + } +} \ No newline at end of file diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index f262fd23..29f4bafc 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -2,27 +2,33 @@ using System.Security.Claims; using System.Text; using api.Models.Users; +using api.Services; using Microsoft.IdentityModel.Tokens; namespace api.Models { - public class JwtToken + public class JwtToken : IJwtToken { private readonly IConfiguration _configuration; + private readonly IUserService _userService; - public JwtToken(IConfiguration configuration) + public JwtToken(IConfiguration configuration, IUserService userService) { _configuration = configuration; + _userService = userService; + } - + public string GenToken(AuthRequest user) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + var fullUser = _userService.GetUserByUsername(user.UserName); var tokenDescriptor = new SecurityTokenDescriptor { - Subject = new ClaimsIdentity(new[] { new Claim("name", user.UserName), - new Claim("role", "User")}), + Subject = new ClaimsIdentity(new[] { new Claim("name", fullUser.Username), + new Claim("role", "User"), + new Claim("id",fullUser._id)}), Expires = DateTime.UtcNow.AddMinutes(20), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; @@ -76,7 +82,8 @@ namespace api.Models var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim("name",""), - new Claim("role", "Guest")}), + new Claim("role", "Guest"), + new Claim("id","")}), Expires = DateTime.UtcNow.AddMinutes(20), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index 5913c2d3..2bb97e45 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -1,6 +1,7 @@ using System.Text; using api.Data; using api.Interfaces; +using api.Models; using api.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Options; @@ -32,6 +33,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); var mlwss = new MLWebSocketService(); diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index a646cc9d..c7161dee 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -8,13 +8,13 @@ namespace api.Services { public class AuthService : IAuthService { - private JwtToken _jwt; + private IJwtToken _jwt; private readonly IConfiguration _configuration; private readonly IMongoCollection _users; - public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient) + public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient,IJwtToken jwt) { _configuration = configuration; - _jwt = new JwtToken(_configuration); + _jwt = jwt; var database = mongoClient.GetDatabase(settings.DatabaseName); _users = database.GetCollection(settings.CollectionName); } diff --git a/backend/api/api/Services/IUserService.cs b/backend/api/api/Services/IUserService.cs index e4a23213..d34d410a 100644 --- a/backend/api/api/Services/IUserService.cs +++ b/backend/api/api/Services/IUserService.cs @@ -10,5 +10,7 @@ namespace api.Services User Create(User user); // kreira korisnika bool Update(string username, User user); //apdejtuje korisnika po idu void Delete(string username);//brise korisnika + public User GetUserByUsername(string username);//Uzima jednog korisnika po username-u + public User GetUserById(string id);//Uzima jednog korisnika po id-u } } diff --git a/backend/api/api/Services/UserService.cs b/backend/api/api/Services/UserService.cs index 607bb04b..39b3a8d3 100644 --- a/backend/api/api/Services/UserService.cs +++ b/backend/api/api/Services/UserService.cs @@ -33,6 +33,14 @@ namespace api.Services { return _users.Find(user => true).ToList(); } + public User GetUserByUsername(string username) + { + return _users.Find(user=>user.Username == username).FirstOrDefault(); + } + public User GetUserById(string id) + { + return _users.Find(user => user._id == id).FirstOrDefault(); + } public User GetUserUsername(string username) { return _users.Find(user => user.Username == username).FirstOrDefault(); -- cgit v1.2.3 From 686c049223a62f0a6926e08d8743ef9661200874 Mon Sep 17 00:00:00 2001 From: "DESKTOP-S0O2C44\\ROG" Date: Wed, 30 Mar 2022 17:49:50 +0200 Subject: Sitne izmene oko predict stranice.(Nije zavrsneno) #53 #66 --- backend/api/api/Controllers/ModelController.cs | 12 +++-- backend/api/api/Controllers/PredictorController.cs | 3 +- backend/api/api/Services/IModelService.cs | 1 + backend/api/api/Services/ModelService.cs | 5 ++ .../item-predictor/item-predictor.component.html | 4 +- .../item-predictor/item-predictor.component.ts | 4 ++ .../src/app/_pages/predict/predict.component.html | 61 +++++++++++++++++++++- .../src/app/_pages/predict/predict.component.ts | 6 ++- 8 files changed, 88 insertions(+), 8 deletions(-) (limited to 'backend/api') diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index 0be7894e..d8bc1515 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -129,8 +129,9 @@ namespace api.Controllers // POST api//add [HttpPost("add")] [Authorize(Roles = "User,Guest")] - public ActionResult Post([FromBody] Model model) + public ActionResult Post([FromBody] Model model)//, bool overwrite) { + bool overwrite = false; //username="" ako je GUEST model.inputNeurons = model.inputColumns.Length; if (_modelService.CheckHyperparameters(model.inputNeurons, model.hiddenLayerNeurons, model.hiddenLayers, model.outputNeurons) == false) @@ -138,11 +139,16 @@ namespace api.Controllers var existingModel = _modelService.GetOneModel(model.username, model.name); - if (existingModel != null) + if (existingModel != null && !overwrite) return NotFound($"Model with name = {model.name} exisits"); else { - _modelService.Create(model); + if (existingModel == null) + _modelService.Create(model); + else + { + _modelService.Replace(model); + } return CreatedAtAction(nameof(Get), new { id = model._id }, model); } diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs index 8f2167c4..98e2695e 100644 --- a/backend/api/api/Controllers/PredictorController.cs +++ b/backend/api/api/Controllers/PredictorController.cs @@ -74,8 +74,7 @@ namespace api.Controllers return _predictorService.SearchPredictors(name, username); } - //SEARCH za predictore (public ili private sa ovim imenom ) - // GET api//search/{name} + // GET api//{name} [HttpGet("{id}")] [Authorize(Roles = "User")] public ActionResult GetPredictor(string id) diff --git a/backend/api/api/Services/IModelService.cs b/backend/api/api/Services/IModelService.cs index 637d09a3..3e70a1c3 100644 --- a/backend/api/api/Services/IModelService.cs +++ b/backend/api/api/Services/IModelService.cs @@ -11,6 +11,7 @@ namespace api.Services List GetLatestModels(string username); //List GetPublicModels(); Model Create(Model model); + Model Replace(Model model); void Update(string username, string name, Model model); void Delete(string username, string name); bool CheckHyperparameters(int inputNeurons, int hiddenLayerNeurons, int hiddenLayers, int outputNeurons); diff --git a/backend/api/api/Services/ModelService.cs b/backend/api/api/Services/ModelService.cs index eae8c78b..c2b4e692 100644 --- a/backend/api/api/Services/ModelService.cs +++ b/backend/api/api/Services/ModelService.cs @@ -20,6 +20,11 @@ namespace api.Services _model.InsertOne(model); return model; } + public Model Replace(Model model) + { + _model.ReplaceOne(m => m._id == model._id, model); + return model; + } public void Delete(string username, string name) { diff --git a/frontend/src/app/_elements/item-predictor/item-predictor.component.html b/frontend/src/app/_elements/item-predictor/item-predictor.component.html index b4690154..7ae26fd3 100644 --- a/frontend/src/app/_elements/item-predictor/item-predictor.component.html +++ b/frontend/src/app/_elements/item-predictor/item-predictor.component.html @@ -19,6 +19,8 @@ \ No newline at end of file diff --git a/frontend/src/app/_elements/item-predictor/item-predictor.component.ts b/frontend/src/app/_elements/item-predictor/item-predictor.component.ts index cc782f45..d864480a 100644 --- a/frontend/src/app/_elements/item-predictor/item-predictor.component.ts +++ b/frontend/src/app/_elements/item-predictor/item-predictor.component.ts @@ -15,4 +15,8 @@ export class ItemPredictorComponent implements OnInit { ngOnInit(): void { } + openPredictor() { + console.log("iskoristi") + } + } diff --git a/frontend/src/app/_pages/predict/predict.component.html b/frontend/src/app/_pages/predict/predict.component.html index 74a83b71..67d047b7 100644 --- a/frontend/src/app/_pages/predict/predict.component.html +++ b/frontend/src/app/_pages/predict/predict.component.html @@ -1 +1,60 @@ -

predict works!

+ +
+
+
+ + + +
+ +
+ +

Izabrani prediktor:

+
+ + +
+
+ +
+ +
+
+
+ + + +
+ + +
+
+ + +
+
+    + +
+ + + +
+
\ No newline at end of file diff --git a/frontend/src/app/_pages/predict/predict.component.ts b/frontend/src/app/_pages/predict/predict.component.ts index 0e313c65..d5cb22bd 100644 --- a/frontend/src/app/_pages/predict/predict.component.ts +++ b/frontend/src/app/_pages/predict/predict.component.ts @@ -1,4 +1,5 @@ import { Component, OnInit } from '@angular/core'; +import Predictor from 'src/app/_data/Predictor'; @Component({ selector: 'app-predict', @@ -7,7 +8,10 @@ import { Component, OnInit } from '@angular/core'; }) export class PredictComponent implements OnInit { - constructor() { } + predictor:Predictor; + constructor() { + this.predictor = new Predictor(); + } ngOnInit(): void { } -- cgit v1.2.3