Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. [HttpPost]
  2. [Route("CreateUser/{UserId}/{jobRouterLogin}")]
  3. public HttpResponseMessage AddNewUser(int userId, string jobRouterLogin)
  4. {
  5. if (HttpContext.Current.Request.IsLocal)
  6. {
  7. try
  8. {
  9. using (var optimaDbContext = new OptimaDbContext())
  10. {
  11. var user = optimaDbContext.Employees.FirstOrDefault(n => n.ID == userId);
  12. if (user == null)
  13. {
  14. return new HttpResponseMessage(HttpStatusCode.NotFound);
  15. }
  16. var userDatabase = optimaDbContext.Clients
  17. .FirstOrDefault(n => n.Baza_danych == user.DP_NazwaBazyKlienta).Nazwa_firmy;
  18.  
  19.  
  20. var company = dbContext.Companies.FirstOrDefault(n => n.Name == userDatabase);
  21. if (company == null)
  22. {
  23. company = dbContext.Companies.Add(new Company()
  24. {
  25. Name = userDatabase
  26. });
  27. }
  28.  
  29. string newUserPassword = HashHelper.ComputeSha256("userPassword");
  30.  
  31. dbContext.Users.Add(new User()
  32. {
  33. CompanyId = company.CompanyId,
  34. Password = newUserPassword,
  35. Username = jobRouterLogin,
  36. UserId = userId
  37. });
  38.  
  39. dbContext.SaveChanges();
  40. return new HttpResponseMessage(HttpStatusCode.OK);
  41.  
  42. }
  43. }
  44. catch
  45. {
  46. return new HttpResponseMessage(HttpStatusCode.NotModified);
  47. }
  48. }
  49. else
  50. {
  51. return new HttpResponseMessage(HttpStatusCode.Forbidden);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement