source: PostgreSqlDotnetCore/Controllers/HomeController.cs@ 118e414

main
Last change on this file since 118e414 was 118e414, checked in by ElenaMoskova <elena.moskova99@…>, 3 months ago

fix access

implement multiple access pages with different roles
optimize present three structure of BlogPost and Answer

  • Property mode set to 100644
File size: 3.9 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();
[118e414]61 // проба на 23.08
62 // no access for standard user
63 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
64
[2aea0fd]65 }
66
[6782104]67 } else
68 {
69 ViewBag.isAuthenticated = null;
[2aea0fd]70 }
71 ViewBag.ShowTopBar = true;
[6782104]72
[2aea0fd]73 return View();
74 }
75
[6782104]76 public async Task<IActionResult> PrivacyAsync()
[2aea0fd]77 {
[6782104]78
79 // set if is authenticated
80 ViewBag.isAuthenticated = await getCrrentUser();
[118e414]81 // no access for standard user
82 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
83
[2aea0fd]84 return View();
85 }
[6782104]86 public async Task<IActionResult> ContactAsync()
[2aea0fd]87 {
[6782104]88 // set if is authenticated
89 ViewBag.isAuthenticated = await getCrrentUser();
[118e414]90 // no access for standard user
91 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
92
[2aea0fd]93 return View();
94 }
95
96 [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
97 public IActionResult Error()
98 {
99 return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.