aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorOgnjen Cirkovic <ciraboxkg@gmail.com>2022-05-04 23:40:12 +0000
committerOgnjen Cirkovic <ciraboxkg@gmail.com>2022-05-04 23:40:12 +0000
commitc12f31b70fbce8542c2d4e23b5f003d7488bb03f (patch)
treecb64f2775335cdd856e81ec9e8ba0bed93fa0985 /backend
parentbdabccc6e8f4d35085a4defe61c579ea0002f798 (diff)
parent7d7c18239582f8dcc5337a573c02033c0f5c3194 (diff)
Merge branch 'Guest-Koristik-Ispravka-FrontEnd' into 'redesign'
Merge See merge request igrannonica/neuronstellar!28
Diffstat (limited to 'backend')
-rw-r--r--backend/api/api/Controllers/AuthController.cs32
-rw-r--r--backend/api/api/Controllers/DatasetController.cs12
-rw-r--r--backend/api/api/Controllers/ExperimentController.cs4
-rw-r--r--backend/api/api/Controllers/ModelController.cs12
-rw-r--r--backend/api/api/Controllers/PredictorController.cs10
-rw-r--r--backend/api/api/Interfaces/IAuthService.cs2
-rw-r--r--backend/api/api/Interfaces/IJwtToken.cs4
-rw-r--r--backend/api/api/Models/User.cs4
-rw-r--r--backend/api/api/Services/AuthService.cs16
-rw-r--r--backend/api/api/Services/JwtToken.cs25
-rw-r--r--backend/api/api/Services/TempRemovalService.cs69
11 files changed, 117 insertions, 73 deletions
diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs
index 901454e1..f70146ed 100644
--- a/backend/api/api/Controllers/AuthController.cs
+++ b/backend/api/api/Controllers/AuthController.cs
@@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Mvc;
using api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Net.Http.Headers;
+using System.Net.Http.Headers;
+using api.Models;
namespace api.Controllers
{
@@ -12,16 +14,20 @@ namespace api.Controllers
public class AuthController : ControllerBase
{
private IAuthService _auth;
- public AuthController(IAuthService auth)
+ private IJwtToken _jwtToken;
+ public AuthController(IAuthService auth, IJwtToken Token)
{
_auth = auth;
+ _jwtToken = Token;
}
[HttpPost("register")]
public async Task<ActionResult<string>> Register(RegisterRequest user)
{
-
- return Ok(_auth.Register(user));
+ string id=getUserId();
+ if (id == null)
+ return BadRequest();
+ return Ok(_auth.Register(user,id));
}
[HttpPost("login")]
@@ -45,7 +51,7 @@ namespace api.Controllers
}
[HttpPost("renewJwt")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public async Task<ActionResult<string>> RenewJwt() {
var authorization = Request.Headers[HeaderNames.Authorization];
@@ -57,6 +63,24 @@ namespace api.Controllers
}
+ public string getUserId()
+ {
+ string uploaderId;
+ var header = Request.Headers[HeaderNames.Authorization];
+ if (AuthenticationHeaderValue.TryParse(header, out var headerValue))
+ {
+ var scheme = headerValue.Scheme;
+ var parameter = headerValue.Parameter;
+ uploaderId = _jwtToken.TokenToId(parameter);
+ if (uploaderId == null)
+ return null;
+ }
+ else
+ return null;
+
+ return uploaderId;
+ }
+
}
diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs
index 1873d9ec..a6ebe8ac 100644
--- a/backend/api/api/Controllers/DatasetController.cs
+++ b/backend/api/api/Controllers/DatasetController.cs
@@ -67,7 +67,7 @@ namespace api.Controllers
//desc - opadajuce 0
//ako se posalje 0 kao latest onda ce da izlista sve u nekom poretku
[HttpGet("datesort/{ascdsc}/{latest}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Dataset>> SortDatasets(bool ascdsc, int latest)
{
string userId = getUserId();
@@ -98,7 +98,7 @@ namespace api.Controllers
//SEARCH za datasets (public ili private sa ovim imenom )
// GET api/<DatasetController>/search/{name}
[HttpGet("search/{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Dataset>> Search(string name)
{
return _datasetService.SearchDatasets(name);
@@ -108,7 +108,7 @@ namespace api.Controllers
// GET api/<DatasetController>/{name}
//get odredjeni dataset
[HttpGet("{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<Dataset> Get(string name)
{
string userId = getUserId();
@@ -138,7 +138,7 @@ namespace api.Controllers
var existingDataset = _datasetService.GetOneDatasetN(dataset.uploaderId, dataset.name);
if (existingDataset != null)
- return NotFound($"Dateset with name = {dataset.name} exisits");
+ return NotFound($"Dataset with this name already exists");
else
{
FileModel fileModel = _fileService.getFile(dataset.fileId);
@@ -152,7 +152,7 @@ namespace api.Controllers
// PUT api/<DatasetController>/{name}
[HttpPut("{id}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Put(string id, [FromBody] Dataset dataset)
{
string uploaderId = getUserId();
@@ -175,7 +175,7 @@ namespace api.Controllers
// DELETE api/<DatasetController>/name
[HttpDelete("{id}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Delete(string id)
{
string uploaderId = getUserId();
diff --git a/backend/api/api/Controllers/ExperimentController.cs b/backend/api/api/Controllers/ExperimentController.cs
index 6f1bbd42..08354615 100644
--- a/backend/api/api/Controllers/ExperimentController.cs
+++ b/backend/api/api/Controllers/ExperimentController.cs
@@ -92,7 +92,7 @@ namespace api.Controllers
// PUT api/<ExperimentController>/{name}
[HttpPut("{id}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Put(string id, [FromBody] Experiment experiment)
{
string uploaderId = getUserId();
@@ -114,7 +114,7 @@ namespace api.Controllers
// DELETE api/<ExperimentController>/name
[HttpDelete("{id}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Delete(string id)
{
string uploaderId = getUserId();
diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs
index 2916fa98..a0e51e1f 100644
--- a/backend/api/api/Controllers/ModelController.cs
+++ b/backend/api/api/Controllers/ModelController.cs
@@ -100,7 +100,7 @@ namespace api.Controllers
// GET: api/<ModelController>/mymodels
[HttpGet("mymodels")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Model>> Get()
{
string uploaderId = getUserId();
@@ -113,7 +113,7 @@ namespace api.Controllers
// GET: api/<ModelController>/mymodels
[HttpGet("mymodelsbytype/{problemtype}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Model>> GetMyModelsByType(string problemType)
{
string uploaderId = getUserId();
@@ -132,7 +132,7 @@ namespace api.Controllers
// vraca svoj model prema nekom imenu
// GET api/<ModelController>/{name}
[HttpGet("{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<Model> Get(string name)
{
string userId = getUserId();
@@ -156,7 +156,7 @@ namespace api.Controllers
//odraditi to i u Datasetove i Predictore
// GET: api/<ModelController>/getlatestmodels/{number}
[HttpGet("getlatestmodels/{latest}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Model>> GetLatestModels(int latest)
{
string userId = getUserId();
@@ -213,7 +213,7 @@ namespace api.Controllers
// PUT api/<ModelController>/{name}
[HttpPut("{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Put(string name, [FromBody] Model model)
{
string userId = getUserId();
@@ -233,7 +233,7 @@ namespace api.Controllers
// DELETE api/<ModelController>/name
[HttpDelete("{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Delete(string name)
{
string userId = getUserId();
diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs
index dd5aa5fd..3646187e 100644
--- a/backend/api/api/Controllers/PredictorController.cs
+++ b/backend/api/api/Controllers/PredictorController.cs
@@ -52,7 +52,7 @@ namespace api.Controllers
// GET: api/<PredictorController>/mypredictors
[HttpGet("mypredictors")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Predictor>> Get()
{
string userId = getUserId();
@@ -105,7 +105,7 @@ namespace api.Controllers
// GET api/<PredictorController>/{name}
[HttpGet("{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<Predictor> Get(string id)
{
string userId = getUserId();
@@ -128,7 +128,7 @@ namespace api.Controllers
//desc - opadajuce 0
//ako se posalje 0 kao latest onda ce da izlista sve u nekom poretku
[HttpGet("datesort/{ascdsc}/{latest}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult<List<Predictor>> SortPredictors(bool ascdsc, int latest)
{
string userId = getUserId();
@@ -193,7 +193,7 @@ namespace api.Controllers
// PUT api/<PredictorController>/{name}
[HttpPut("{name}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Put(string id, [FromBody] Predictor predictor)
{
string userId = getUserId();
@@ -214,7 +214,7 @@ namespace api.Controllers
// DELETE api/<PredictorController>/name
[HttpDelete("{id}")]
- [Authorize(Roles = "User")]
+ [Authorize(Roles = "User,Guest")]
public ActionResult Delete(string id)
{
string userId = getUserId();
diff --git a/backend/api/api/Interfaces/IAuthService.cs b/backend/api/api/Interfaces/IAuthService.cs
index 9a109208..570ce0a4 100644
--- a/backend/api/api/Interfaces/IAuthService.cs
+++ b/backend/api/api/Interfaces/IAuthService.cs
@@ -5,7 +5,7 @@ namespace api.Services
public interface IAuthService
{
string Login(AuthRequest user);
- string Register(RegisterRequest user);
+ string Register(RegisterRequest user, string id);
string RenewToken(string token);
public string GuestToken();
}
diff --git a/backend/api/api/Interfaces/IJwtToken.cs b/backend/api/api/Interfaces/IJwtToken.cs
index 2afb6683..5c54e4e3 100644
--- a/backend/api/api/Interfaces/IJwtToken.cs
+++ b/backend/api/api/Interfaces/IJwtToken.cs
@@ -4,8 +4,8 @@ namespace api.Models
{
public interface IJwtToken
{
- string GenGuestToken();
- string GenToken(AuthRequest user);
+ string GenGuestToken(string id);
+ string GenToken(User user);
string RenewToken(string existingToken);
string TokenToUsername(string token);
public string TokenToId(string token);
diff --git a/backend/api/api/Models/User.cs b/backend/api/api/Models/User.cs
index 1ae8e437..bea467fa 100644
--- a/backend/api/api/Models/User.cs
+++ b/backend/api/api/Models/User.cs
@@ -25,6 +25,8 @@ namespace api.Models
public string LastName { get; set; }
public string photoId { get; set; }
-
+ public bool isPermament { get; set; }
+ public DateTime dateCreated { get; set; }
+
}
}
diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs
index c7161dee..672511b3 100644
--- a/backend/api/api/Services/AuthService.cs
+++ b/backend/api/api/Services/AuthService.cs
@@ -25,10 +25,10 @@ namespace api.Services
return "Username doesn't exist";
if (!PasswordCrypt.checkPassword(user.Password, u.Password))
return "Wrong password";
- return _jwt.GenToken(user);
+ return _jwt.GenToken(u);
}
- public string Register(RegisterRequest user)
+ public string Register(RegisterRequest user,string id)
{
User u = new User();
u.Username = user.username;
@@ -37,12 +37,15 @@ namespace api.Services
u.FirstName = user.firstName;
u.LastName = user.lastName;
u.photoId = "1";
+ u.isPermament = true;
+ u._id = id;
+ u.dateCreated= DateTime.Now.ToUniversalTime();
if (_users.Find(user => user.Username == u.Username).FirstOrDefault() != null)
return "Username Already Exists";
if (_users.Find(user => user.Email == u.Email).FirstOrDefault() != null)
return "Email Already Exists";
- _users.InsertOne(u);
+ _users.ReplaceOne(x=>x._id==u._id,u);
return "User added";
}
@@ -60,7 +63,12 @@ namespace api.Services
public string GuestToken()
{
- return _jwt.GenGuestToken();
+ User u = new User();
+ u._id = "";
+ u.dateCreated = DateTime.Now.ToUniversalTime();
+ _users.InsertOne(u);
+ return _jwt.GenGuestToken(u._id);
+
}
diff --git a/backend/api/api/Services/JwtToken.cs b/backend/api/api/Services/JwtToken.cs
index 06b3a666..20b0bc73 100644
--- a/backend/api/api/Services/JwtToken.cs
+++ b/backend/api/api/Services/JwtToken.cs
@@ -19,16 +19,17 @@ namespace api.Models
}
- public string GenToken(AuthRequest user)
+ public string GenToken(User user)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value);
- var fullUser = _userService.GetUserByUsername(user.UserName);
+ string role=(user.isPermament)?"User":"Guest";
+ string name = (user.isPermament) ? user.Username : "";
var tokenDescriptor = new SecurityTokenDescriptor
{
- Subject = new ClaimsIdentity(new[] { new Claim("name", fullUser.Username),
- new Claim("role", "User"),
- new Claim("id",fullUser._id)}),
+ Subject = new ClaimsIdentity(new[] { new Claim("name", name),
+ new Claim("role", role),
+ new Claim("id",user._id)}),
Expires = DateTime.UtcNow.AddMinutes(20),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
@@ -39,13 +40,12 @@ namespace api.Models
public string RenewToken(string existingToken)
{
- var userName = TokenToUsername(existingToken);
- if (userName == null)
+ var id = TokenToId(existingToken);
+ if (id == null)
return null;
- var authUser = new AuthRequest();
- authUser.UserName = userName;
+ var user = _userService.GetUserById(id);
- return GenToken(authUser);
+ return GenToken(user);
}
@@ -100,15 +100,16 @@ namespace api.Models
}
- public string GenGuestToken()
+ public string GenGuestToken(string id)
{
+ var user=_userService.GetUserById(id);
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] { new Claim("name",""),
new Claim("role", "Guest"),
- new Claim("id","")}),
+ new Claim("id",user._id)}),
Expires = DateTime.UtcNow.AddMinutes(20),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
diff --git a/backend/api/api/Services/TempRemovalService.cs b/backend/api/api/Services/TempRemovalService.cs
index 302ca974..9e6b7f96 100644
--- a/backend/api/api/Services/TempRemovalService.cs
+++ b/backend/api/api/Services/TempRemovalService.cs
@@ -10,6 +10,8 @@ namespace api.Services
private readonly IMongoCollection<Model> _model;
private readonly IMongoCollection<Dataset> _dataset;
private readonly IMongoCollection<Experiment> _experiment;
+ private readonly IMongoCollection<User> _user;
+ private readonly IMongoCollection<Predictor> _predictor;
public TempRemovalService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient)
{
@@ -18,43 +20,42 @@ namespace api.Services
_model= database.GetCollection<Model>(settings.ModelCollectionName);
_dataset = database.GetCollection<Dataset>(settings.DatasetCollectionName);
_experiment= database.GetCollection<Experiment>(settings.ExperimentCollectionName);
+ _user = database.GetCollection<User>(settings.CollectionName);
+ _predictor = database.GetCollection<Predictor>(settings.PredictorCollectionName);
+
}
- public void DeleteTemps()
+ public void DeleteTemps()
{
- List<FileModel> files = _file.Find(file => file.uploaderId == "").ToList();
- foreach (var file in files)
+ List<User> tempUsers=_user.Find(u=>u.isPermament==false).ToList();
+ foreach (User user in tempUsers)
{
- if ((DateTime.Now.ToUniversalTime() - file.date).TotalDays >= 1)
+ if ((DateTime.Now.ToUniversalTime() - user.dateCreated).TotalDays < 1)
+ continue;
+ List<Predictor> tempPredictors=_predictor.Find(p=>p.uploaderId==user._id).ToList();
+ List<Model> tempModels=_model.Find(m=>m.uploaderId==user._id).ToList();
+ List<Experiment> tempExperiment = _experiment.Find(e => e.uploaderId == user._id).ToList();
+ List<Dataset> tempDatasets = _dataset.Find(d => d.uploaderId == user._id).ToList();
+ List<FileModel> tempFiles = _file.Find(f => f.uploaderId == user._id).ToList();
+
+
+ foreach (Predictor predictor in tempPredictors)
+ DeletePredictor(predictor._id);
+ foreach(Model model in tempModels)
+ DeleteModel(model._id);
+ foreach(Experiment experiment in tempExperiment)
+ DeleteExperiment(experiment._id);
+ foreach(Dataset dataset in tempDatasets)
+ DeleteDataset(dataset._id);
+ foreach(FileModel file in tempFiles)
{
DeleteFile(file._id);
- List<Dataset> datasets = _dataset.Find(dataset => dataset.fileId == file._id && dataset.uploaderId=="").ToList();
- foreach(var dataset in datasets)
- {
- DeleteDataset(dataset._id);
- List<Experiment> experiments = _experiment.Find(experiment=>experiment.datasetId== dataset._id && experiment.uploaderId=="").ToList();
- foreach(var experiment in experiments)
- {
- DeleteExperiment(experiment._id);
- foreach(var modelId in experiment.ModelIds)
- {
- var delModel=_model.Find(model=> modelId== model._id && model.uploaderId=="").FirstOrDefault();
- if(delModel!= null)
- DeleteModel(delModel._id);
- }
- }
- }
- if (File.Exists(file.path))
+ if(File.Exists(file.path))
File.Delete(file.path);
}
- }
- //Brisanje modela ukoliko gost koristi vec postojeci dataset
- List<Model> models1= _model.Find(model =>model.uploaderId == "").ToList();
- foreach(var model in models1)
- {
- if ((DateTime.Now.ToUniversalTime() - model.dateCreated.ToUniversalTime()).TotalDays >= 1)
- {
- DeleteModel(model._id);
- }
+ DeleteUser(user._id);
+
+
+
}
@@ -79,6 +80,14 @@ namespace api.Services
{
_experiment.DeleteOne(experiment => experiment._id == id);
}
+ public void DeletePredictor(string id)
+ {
+ _predictor.DeleteOne(predictor=> predictor._id == id);
+ }
+ public void DeleteUser(string id)
+ {
+ _user.DeleteOne(user=>user._id == id);
+ }
}