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
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using PostgreSqlDotnetCore.Data;
4using PostgreSqlDotnetCore.Models;
5using System.Diagnostics;
6using System.Web.Mvc;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10
11 public class HomeController : BaseController
12 {
13 private ApplicationDbContext db = new ApplicationDbContext();
14 private UserManager<IdentityUser> _userManager;
15
16 private readonly ILogger<HomeController> _logger;
17
18 public HomeController(ILogger<HomeController> logger, UserManager<IdentityUser> userManager) : base(userManager)
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 }
59 // set if is authenticated
60 ViewBag.isAuthenticated = await getCrrentUser();
61 }
62
63 } else
64 {
65 ViewBag.isAuthenticated = null;
66 }
67 ViewBag.ShowTopBar = true;
68
69 return View();
70 }
71
72 public async Task<IActionResult> PrivacyAsync()
73 {
74
75 // set if is authenticated
76 ViewBag.isAuthenticated = await getCrrentUser();
77 return View();
78 }
79 public async Task<IActionResult> ContactAsync()
80 {
81 // set if is authenticated
82 ViewBag.isAuthenticated = await getCrrentUser();
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.