source: PostgreSqlDotnetCore/Controllers/HomeController.cs@ 63bd770

main
Last change on this file since 63bd770 was 6782104, checked in by ElenaMoskova <elena.moskova99@…>, 6 weeks ago

fix authorization

implement hiding menu items

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using PostgreSqlDotnetCore.Data;
4using PostgreSqlDotnetCore.Models;
5using System.Diagnostics;
[6782104]6using System.Web.Mvc;
[2aea0fd]7
8namespace PostgreSqlDotnetCore.Controllers
9{
10
[6782104]11 public class HomeController : BaseController
[2aea0fd]12 {
13 private ApplicationDbContext db = new ApplicationDbContext();
14 private UserManager<IdentityUser> _userManager;
15
16 private readonly ILogger<HomeController> _logger;
17
[6782104]18 public HomeController(ILogger<HomeController> logger, UserManager<IdentityUser> userManager) : base(userManager)
[2aea0fd]19 {
20 _logger = logger;
21 _userManager = userManager;
22 }
23
24 public async Task<IActionResult> IndexAsync()
25 {
26 bool isAuthenticated = User.Identity.IsAuthenticated;
27 if (isAuthenticated)
28 {
29 var user = await _userManager.GetUserAsync(User);
30 if (user != null)
31 {
32 UsersClass customerClass = db.CustomerObj.SingleOrDefault(x=> x.email == user.Email);
33 if (customerClass == null)
34 {
35 string[] nameLastName = user.Email.ToString().Split('@');
36 string name = nameLastName[0];
37 string lastName = "-";
38 try
39 {
40 if (nameLastName[0].Contains('.'))
41 {
42 name = nameLastName[0].Split('.')[0];
43 lastName = nameLastName[0].Split('.')[1];
44 }
45 } catch(Exception ex) {
46 }
47 db.CustomerObj.Add(new UsersClass(
48 user.Email,
49 name,
50 lastName,
51 user.PasswordHash != null ? user.PasswordHash : "-",
52 user.PhoneNumber != null ? user.PhoneNumber : user.Email,
53 RoleConstants.Standard,
54 null
55 )
56 );
57 db.SaveChanges();
58 }
[6782104]59 // set if is authenticated
60 ViewBag.isAuthenticated = await getCrrentUser();
[2aea0fd]61 }
62
[6782104]63 } else
64 {
65 ViewBag.isAuthenticated = null;
[2aea0fd]66 }
67 ViewBag.ShowTopBar = true;
[6782104]68
[2aea0fd]69 return View();
70 }
71
[6782104]72 public async Task<IActionResult> PrivacyAsync()
[2aea0fd]73 {
[6782104]74
75 // set if is authenticated
76 ViewBag.isAuthenticated = await getCrrentUser();
[2aea0fd]77 return View();
78 }
[6782104]79 public async Task<IActionResult> ContactAsync()
[2aea0fd]80 {
[6782104]81 // set if is authenticated
82 ViewBag.isAuthenticated = await getCrrentUser();
[2aea0fd]83 return View();
84 }
85
86 [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
87 public IActionResult Error()
88 {
89 return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.