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

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

init commit Elena

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[2aea0fd]1namespace PostgreSqlDotnetCore.Controllers
2{
3 using Microsoft.AspNetCore.Identity;
4 using Microsoft.AspNetCore.Mvc;
5 using PostgreSqlDotnetCore.Data;
6 using PostgreSqlDotnetCore.Models;
7
8 public class BaseController : Controller
9 {
10
11 public ApplicationDbContext db = new ApplicationDbContext();
12 public UserManager<IdentityUser> _userManager;
13
14 public BaseController(UserManager<IdentityUser> userManager)
15 {
16 _userManager = userManager;
17 }
18
19 public async Task<UsersClass?> getCrrentUser()
20 {
21 bool isAuthenticated = User.Identity.IsAuthenticated;
22 UsersClass customerClass = null;
23 if (isAuthenticated)
24 {
25 var user = await _userManager.GetUserAsync(User);
26 customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
27 if (customerClass == null) // if is not admin or manager NO PERMISSION
28 {
29 return null;
30 }
31 return customerClass;
32 }
33 else
34 {
35 return null;
36
37 }
38 }
39
40 public async Task<UsersClass?> checkAuthorizationAsync()
41 {
42 bool isAuthenticated = User.Identity.IsAuthenticated;
43 UsersClass customerClass = null;
44 if (isAuthenticated)
45 {
46 var user = await _userManager.GetUserAsync(User);
47 customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
48 if (customerClass == null || (customerClass.role_id != RoleConstants.Admin && customerClass.role_id != RoleConstants.Manager)) // if is not admin or manager NO PERMISSION
49 {
50 return null;
51 }
52 return customerClass;
53 }
54 else
55 {
56 return null;
57
58 }
59 }
60
61 public async Task<UsersClass?> checkAuthorizationSpecificRoleAsync(int roleId)
62 {
63 bool isAuthenticated = User.Identity.IsAuthenticated;
64 UsersClass customerClass = null;
65 if (isAuthenticated)
66 {
67 var user = await _userManager.GetUserAsync(User);
68 customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
69 if (customerClass == null || (customerClass.role_id != roleId)) // if is a specific role
70 {
71 return null;
72 }
73 return customerClass;
74 }
75 else
76 {
77 return null;
78
79 }
80 }
81 }
82}
Note: See TracBrowser for help on using the repository browser.