public ActionResult Login([FromBody] LoginRequest req) { var user = _context.User.Where(u => u.Email == req.Email).FirstOrDefault <User>(); if (user == null) { return(BadRequest(new { message = "Invalid Email or Password." })); } if (user.Active == 0) { return(Unauthorized(new { message = "Account Deactivated by Admin." })); } if (user.LockOut != null) { if (user.LockOut > DateTime.Now) { return(Unauthorized(new { message = "Locked." })); } else { user.LockOut = null; user.LoginAttempts = 0; } } if (!_service.VerifyHashedPassword(user.PasswordHash, req.Password)) { if (user.LoginAttempts == 4) { user.LockOut = DateTime.Now.AddMinutes(15); _context.SaveChanges(); return(Unauthorized(new { message = "Locked." })); } else { user.LoginAttempts++; _context.SaveChanges(); return(BadRequest(new { message = "Invalid Email or Password." })); } } user.LockOut = null; user.LoginAttempts = 0; var tokenString = _service.GenerateToken(user.Uid, user.Type); _context.SaveChanges(); return(Ok(new { uid = user.Uid, email = user.Email, firstName = user.FirstName, lastName = user.LastName, type = user.Type, token = tokenString })); }
public ActionResult StoreFile(CreateResearchFileRequest req) { string path = Path.Combine(_env.WebRootPath, "files"); IList <ResearchFile> researchFiles = new List <ResearchFile>(); Project project = _context.Project.Where(proj => proj.Uid == req.uid).First <Project>(); if (req.Files != null) { foreach (IFormFile file in req.Files) { string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); string filepath = Path.Combine(path, fileName); FileStream fileStream = new FileStream(filepath, FileMode.Create); file.CopyTo(fileStream); fileStream.Close(); AzureFileService fileService = new AzureFileService(this._appSettings); fileService.storeFile("files", fileName, filepath); ResearchFile researchFile = new ResearchFile(); researchFile.FileName = fileName; researchFile.ProjectId = project.Id; researchFiles.Add(researchFile); _context.ResearchFile.Add(researchFile); } } _context.SaveChanges(); return(Ok(researchFiles)); }
public ActionResult UploadPrototype([FromForm] CreatePrototypeRequest req) { Project proj = _context.Project.Where(p => p.Uid == req.ProjectUid).FirstOrDefault <Project>(); string path = Path.Combine(_env.WebRootPath, "files"); IList <string> filePaths = new List <string>(); if (req.Files != null) { foreach (IFormFile file in req.Files) { string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); string filepath = Path.Combine(path, fileName); FileStream fileStream = new FileStream(filepath, FileMode.Create); file.CopyTo(fileStream); fileStream.Close(); AzureFileService fileService = new AzureFileService(this._appSettings); fileService.storeFile("files", fileName, filepath); filePaths.Add(fileName); } } Project Project = _context.Project.Where(project => project.Uid == req.ProjectId).First <Project>(); Prototype prototype = new Prototype(); prototype.ProjectId = Project.Id; prototype.PrototypeName = req.PrototypeName; prototype.PrototypeDescription = req.PrototypeDescription; prototype.Uid = Guid.NewGuid().ToString(); prototype.PrototypePath = Newtonsoft.Json.JsonConvert.SerializeObject(filePaths); var result = _context.Prototype.Add(prototype); _context.SaveChanges(); return(Ok(prototype)); }
public ActionResult PostAnswers([FromBody] SurveyAnswerRequest req) { SurveyTaker taker = new SurveyTaker(); taker.Uid = Guid.NewGuid().ToString(); taker.Turk = req.Turk; taker.SurveyUid = req.SurveyUid; var answerList = req.AnswerList; _context.SurveyTaker.Add(taker); for (int i = 0; i < answerList.Length; i++) { if (answerList[i] != null) { SurveyAnswer answer = new SurveyAnswer(); answer.Uid = Guid.NewGuid().ToString(); answer.SurveyTakerUid = taker.Uid; answer.SurveyUid = req.SurveyUid; answer.Answer = answerList[i]; answer.DateCompleted = DateTime.Now; answer.Qid = i; _context.SurveyAnswer.Add(answer); } } _context.SaveChanges(); return Ok(new { message = "Success!" }); }
public ActionResult AddUserToProject(string uid, [FromBody] ActionWithUsersRequest req) { foreach (string userUid in req.UserUids) { var project = _context.Project.Where(proj => proj.Uid == uid).FirstOrDefault <Project>(); var user = _context.User.Where(u => u.Uid == userUid).FirstOrDefault <User>(); if (user == null) { return(BadRequest(new { message = "Invalid User Uid. [" + userUid + "]" })); } if (project == null) { return(BadRequest(new { message = "Invalid Project." })); } var per = _context.Permissions.Where(p => p.UserId == user.Id && p.ProjId == project.Id).FirstOrDefault <Permissions>(); if (per != null) { return(BadRequest(new { message = "User [" + userUid + "] Already has Permission." })); } var newPer = new Permissions(); newPer.UserId = user.Id; newPer.ProjId = project.Id; _context.Permissions.Add(newPer); } _context.SaveChanges(); return(Ok(new { message = "Success!" })); }